Refresh endpoint (#7)
Rust Build / Rustfmt (push) Successful in 1m0s
Rust Build / Check (push) Successful in 1m10s
Rust Build / Clippy (push) Successful in 1m4s
Rust Build / Test Suite (push) Successful in 2m25s
Rust Build / build (push) Successful in 1m41s
Rust Build / Rustfmt (pull_request) Successful in 29s
Rust Build / Test Suite (pull_request) Successful in 1m1s
Rust Build / Check (pull_request) Successful in 1m45s
Rust Build / Clippy (pull_request) Successful in 1m48s
Rust Build / build (pull_request) Successful in 3m11s
Rust Build / Rustfmt (push) Successful in 1m0s
Rust Build / Check (push) Successful in 1m10s
Rust Build / Clippy (push) Successful in 1m4s
Rust Build / Test Suite (push) Successful in 2m25s
Rust Build / build (push) Successful in 1m41s
Rust Build / Rustfmt (pull_request) Successful in 29s
Rust Build / Test Suite (pull_request) Successful in 1m1s
Rust Build / Check (pull_request) Successful in 1m45s
Rust Build / Clippy (pull_request) Successful in 1m48s
Rust Build / build (pull_request) Successful in 3m11s
Reviewed-on: phoenix/textsender-auth#7
This commit was merged in pull request #7.
This commit is contained in:
+289
-116
@@ -134,6 +134,7 @@ pub mod requests {
|
||||
app.clone().oneshot(req).await
|
||||
}
|
||||
|
||||
/// Function to call service user login endpoint
|
||||
pub async fn login_service_user(
|
||||
app: &axum::Router,
|
||||
username: &str,
|
||||
@@ -152,6 +153,24 @@ pub mod requests {
|
||||
|
||||
app.clone().oneshot(req).await
|
||||
}
|
||||
|
||||
/// Function to call token refresh endpoint
|
||||
pub async fn refresh_token(
|
||||
app: &axum::Router,
|
||||
access_token: &str,
|
||||
) -> Result<axum::response::Response, std::convert::Infallible> {
|
||||
let payload = serde_json::json!({
|
||||
"access_token": access_token,
|
||||
});
|
||||
let req = axum::http::Request::builder()
|
||||
.method(axum::http::Method::POST)
|
||||
.uri(super::callers::endpoints::REFRESH_TOKEN)
|
||||
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
||||
.body(axum::body::Body::from(payload.to_string()))
|
||||
.unwrap();
|
||||
|
||||
app.clone().oneshot(req).await
|
||||
}
|
||||
}
|
||||
|
||||
/// Test user firstname
|
||||
@@ -170,140 +189,194 @@ const TEST_SERVICE_USERNAME: &str = "swoon";
|
||||
/// Test service passphrase
|
||||
const TEST_SERVICE_PASSPHRASE: &str = "4n5cf349tfy34w857ty39wq45nfdq23";
|
||||
|
||||
async fn convert_response<T>(response: axum::response::Response) -> Result<T, std::io::Error>
|
||||
where
|
||||
T: serde::de::DeserializeOwned,
|
||||
{
|
||||
match axum::body::to_bytes(response.into_body(), usize::MAX).await {
|
||||
Ok(body) => {
|
||||
let resp: T = serde_json::from_slice(&body).unwrap();
|
||||
Ok(resp)
|
||||
mod util {
|
||||
pub async fn convert_response<T>(
|
||||
response: axum::response::Response,
|
||||
) -> Result<T, std::io::Error>
|
||||
where
|
||||
T: serde::de::DeserializeOwned,
|
||||
{
|
||||
match axum::body::to_bytes(response.into_body(), usize::MAX).await {
|
||||
Ok(body) => {
|
||||
let resp: T = match serde_json::from_slice(&body) {
|
||||
Ok(val) => val,
|
||||
Err(err) => {
|
||||
return Err(std::io::Error::other(err.to_string()));
|
||||
}
|
||||
};
|
||||
Ok(resp)
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
async fn register_user(
|
||||
app: &axum::Router,
|
||||
) -> Result<textsender_models::user::User, std::io::Error> {
|
||||
match requests::register_user(&app).await {
|
||||
Ok(response) => {
|
||||
if axum::http::StatusCode::CREATED != response.status() {
|
||||
Err(std::io::Error::other(format!(
|
||||
"Status code is off {:?}",
|
||||
response.status()
|
||||
)))
|
||||
} else {
|
||||
match convert_response::<callers::register::response::Response>(response).await {
|
||||
Ok(response) => {
|
||||
if response.data.len() > 0 {
|
||||
let user = response.data[0].clone();
|
||||
Ok(user)
|
||||
} else {
|
||||
Err(std::io::Error::other("No data returned"))
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
mod flow {
|
||||
use super::callers;
|
||||
use super::requests;
|
||||
use super::util;
|
||||
|
||||
async fn login_user(
|
||||
app: &axum::Router,
|
||||
username: &str,
|
||||
password: &str,
|
||||
) -> Result<textsender_models::token::LoginResult, std::io::Error> {
|
||||
match requests::login_user(&app, username, password).await {
|
||||
Ok(response) => {
|
||||
if axum::http::StatusCode::OK != response.status() {
|
||||
Err(std::io::Error::other(format!(
|
||||
"Status code is off {:?}",
|
||||
response.status()
|
||||
)))
|
||||
} else {
|
||||
match convert_response::<callers::login::response::LoginResponse>(response).await {
|
||||
Ok(response) => {
|
||||
if response.data.len() > 0 {
|
||||
let user = response.data[0].clone();
|
||||
Ok(user)
|
||||
} else {
|
||||
Err(std::io::Error::other("No data returned"))
|
||||
pub async fn register_user(
|
||||
app: &axum::Router,
|
||||
) -> Result<textsender_models::user::User, std::io::Error> {
|
||||
match requests::register_user(&app).await {
|
||||
Ok(response) => {
|
||||
if axum::http::StatusCode::CREATED != response.status() {
|
||||
Err(std::io::Error::other(format!(
|
||||
"Status code is off {:?}",
|
||||
response.status()
|
||||
)))
|
||||
} else {
|
||||
match util::convert_response::<callers::register::response::Response>(response)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
if response.data.len() > 0 {
|
||||
let user = response.data[0].clone();
|
||||
Ok(user)
|
||||
} else {
|
||||
Err(std::io::Error::other("No data returned"))
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
async fn register_service_user(
|
||||
app: &axum::Router,
|
||||
) -> Result<textsender_models::user::ServiceUser, std::io::Error> {
|
||||
match requests::register_service_user(&app).await {
|
||||
Ok(response) => {
|
||||
if axum::http::StatusCode::CREATED != response.status() {
|
||||
Err(std::io::Error::other(format!(
|
||||
"Status code is off {:?}",
|
||||
response.status()
|
||||
)))
|
||||
} else {
|
||||
match convert_response::<callers::register::response::RegisterServiceUserResponse>(
|
||||
response,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
if response.data.len() > 0 {
|
||||
let service_user = response.data[0].clone();
|
||||
Ok(service_user)
|
||||
} else {
|
||||
Err(std::io::Error::other("No data returned"))
|
||||
pub async fn login_user(
|
||||
app: &axum::Router,
|
||||
username: &str,
|
||||
password: &str,
|
||||
) -> Result<textsender_models::token::LoginResult, std::io::Error> {
|
||||
match requests::login_user(&app, username, password).await {
|
||||
Ok(response) => {
|
||||
if axum::http::StatusCode::OK != response.status() {
|
||||
Err(std::io::Error::other(format!(
|
||||
"Status code is off {:?}",
|
||||
response.status()
|
||||
)))
|
||||
} else {
|
||||
match util::convert_response::<callers::login::response::LoginResponse>(
|
||||
response,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
if response.data.len() > 0 {
|
||||
let user = response.data[0].clone();
|
||||
Ok(user)
|
||||
} else {
|
||||
Err(std::io::Error::other("No data returned"))
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
async fn login_service_user(
|
||||
app: &axum::Router,
|
||||
username: &str,
|
||||
passphrase: &str,
|
||||
) -> Result<textsender_models::token::LoginResult, std::io::Error> {
|
||||
match requests::login_service_user(&app, username, passphrase).await {
|
||||
Ok(response) => {
|
||||
if axum::http::StatusCode::OK != response.status() {
|
||||
Err(std::io::Error::other(format!(
|
||||
"Status code is off {:?}",
|
||||
response.status()
|
||||
)))
|
||||
} else {
|
||||
match convert_response::<callers::login::response::ServiceUserLoginResponse>(
|
||||
response,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
if response.data.len() > 0 {
|
||||
let login_result = response.data[0].clone();
|
||||
Ok(login_result)
|
||||
} else {
|
||||
Err(std::io::Error::other("No data returned"))
|
||||
pub async fn register_service_user(
|
||||
app: &axum::Router,
|
||||
) -> Result<textsender_models::user::ServiceUser, std::io::Error> {
|
||||
match requests::register_service_user(&app).await {
|
||||
Ok(response) => {
|
||||
if axum::http::StatusCode::CREATED != response.status() {
|
||||
Err(std::io::Error::other(format!(
|
||||
"Status code is off {:?}",
|
||||
response.status()
|
||||
)))
|
||||
} else {
|
||||
match util::convert_response::<
|
||||
callers::register::response::RegisterServiceUserResponse,
|
||||
>(response)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
if response.data.len() > 0 {
|
||||
let service_user = response.data[0].clone();
|
||||
Ok(service_user)
|
||||
} else {
|
||||
Err(std::io::Error::other("No data returned"))
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn login_service_user(
|
||||
app: &axum::Router,
|
||||
username: &str,
|
||||
passphrase: &str,
|
||||
) -> Result<textsender_models::token::LoginResult, std::io::Error> {
|
||||
match requests::login_service_user(&app, username, passphrase).await {
|
||||
Ok(response) => {
|
||||
if axum::http::StatusCode::OK != response.status() {
|
||||
Err(std::io::Error::other(format!(
|
||||
"Status code is off {:?}",
|
||||
response.status()
|
||||
)))
|
||||
} else {
|
||||
match util::convert_response::<callers::login::response::ServiceUserLoginResponse>(
|
||||
response,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
if response.data.len() > 0 {
|
||||
let login_result = response.data[0].clone();
|
||||
Ok(login_result)
|
||||
} else {
|
||||
Err(std::io::Error::other("No data returned"))
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn refresh_token(
|
||||
app: &axum::Router,
|
||||
access_token: &str,
|
||||
) -> Result<textsender_models::token::LoginResult, std::io::Error> {
|
||||
match requests::refresh_token(app, access_token).await {
|
||||
Ok(response) => {
|
||||
if axum::http::StatusCode::OK != response.status() {
|
||||
Err(std::io::Error::other(format!(
|
||||
"Status code is off {:?}",
|
||||
response.status()
|
||||
)))
|
||||
} else {
|
||||
match util::convert_response::<callers::login::response::RefreshTokenResponse>(
|
||||
response,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
if response.data.len() > 0 {
|
||||
let login_result = response.data[0].clone();
|
||||
Ok(login_result)
|
||||
} else {
|
||||
Err(std::io::Error::other("No data returned"))
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -327,7 +400,7 @@ async fn test_register_user() {
|
||||
|
||||
let app = init::routes().await.layer(axum::Extension(pool));
|
||||
|
||||
match register_user(&app).await {
|
||||
match flow::register_user(&app).await {
|
||||
Ok(returned_user) => {
|
||||
assert_eq!(
|
||||
TEST_USERNAME, returned_user.username,
|
||||
@@ -367,8 +440,8 @@ async fn test_login_user() {
|
||||
|
||||
let app = init::routes().await.layer(axum::Extension(pool));
|
||||
|
||||
match register_user(&app).await {
|
||||
Ok(user) => match login_user(&app, &user.username, TEST_PASSWORD).await {
|
||||
match flow::register_user(&app).await {
|
||||
Ok(user) => match flow::login_user(&app, &user.username, TEST_PASSWORD).await {
|
||||
Ok(login_result) => {
|
||||
assert_eq!(
|
||||
false,
|
||||
@@ -415,7 +488,7 @@ async fn test_register_service_user() {
|
||||
|
||||
match requests::register_service_user(&app).await {
|
||||
Ok(response) => {
|
||||
match convert_response::<callers::register::response::RegisterServiceUserResponse>(
|
||||
match util::convert_response::<callers::register::response::RegisterServiceUserResponse>(
|
||||
response,
|
||||
)
|
||||
.await
|
||||
@@ -466,14 +539,16 @@ async fn test_login_service_user() {
|
||||
|
||||
let app = init::routes().await.layer(axum::Extension(pool));
|
||||
|
||||
match register_service_user(&app).await {
|
||||
match flow::register_service_user(&app).await {
|
||||
Ok(user) => {
|
||||
assert_eq!(
|
||||
false,
|
||||
user.id.is_nil(),
|
||||
"The service user id should not be nil"
|
||||
);
|
||||
match login_service_user(&app, TEST_SERVICE_USERNAME, TEST_SERVICE_PASSPHRASE).await {
|
||||
match flow::login_service_user(&app, TEST_SERVICE_USERNAME, TEST_SERVICE_PASSPHRASE)
|
||||
.await
|
||||
{
|
||||
Ok(login_result) => {
|
||||
assert_eq!(
|
||||
false,
|
||||
@@ -498,3 +573,101 @@ async fn test_login_service_user() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_refresh_token() {
|
||||
let tm_pool = db_mgr::get_pool().await.unwrap();
|
||||
let db_name = db_mgr::generate_db_name().await;
|
||||
|
||||
match db_mgr::create_database(&tm_pool, &db_name).await {
|
||||
Ok(_) => {
|
||||
println!("Success");
|
||||
}
|
||||
Err(e) => {
|
||||
assert!(false, "Error: {:?}", e.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
|
||||
|
||||
db::init::migrations(&pool).await;
|
||||
|
||||
let app = init::routes().await.layer(axum::Extension(pool));
|
||||
|
||||
match flow::register_user(&app).await {
|
||||
Ok(user) => match flow::login_user(&app, &user.username, TEST_PASSWORD).await {
|
||||
Ok(login_result) => {
|
||||
assert_eq!(
|
||||
false,
|
||||
login_result.access_token.is_empty(),
|
||||
"Access token is empty when it should not be"
|
||||
);
|
||||
match flow::refresh_token(&app, &login_result.access_token).await {
|
||||
Ok(refresh_login_result) => {
|
||||
assert_eq!(
|
||||
false,
|
||||
refresh_login_result.access_token.is_empty(),
|
||||
"Refreshed access token should not be empty"
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
|
||||
match flow::register_service_user(&app).await {
|
||||
Ok(user) => {
|
||||
assert_eq!(
|
||||
false,
|
||||
user.id.is_nil(),
|
||||
"The service user id should not be nil"
|
||||
);
|
||||
match flow::login_service_user(&app, TEST_SERVICE_USERNAME, TEST_SERVICE_PASSPHRASE)
|
||||
.await
|
||||
{
|
||||
Ok(login_result) => {
|
||||
assert_eq!(
|
||||
false,
|
||||
login_result.access_token.is_empty(),
|
||||
"Access token is empty when it should not be"
|
||||
);
|
||||
|
||||
match flow::refresh_token(&app, &login_result.access_token).await {
|
||||
Ok(refresh_login_result) => {
|
||||
assert_eq!(
|
||||
false,
|
||||
refresh_login_result.access_token.is_empty(),
|
||||
"Refreshed access token should not be empty"
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
|
||||
match db_mgr::drop_database(&tm_pool, &db_name).await {
|
||||
Ok(()) => {}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user