diff --git a/src/callers/login.rs b/src/callers/login.rs index 45dca0e..2926593 100644 --- a/src/callers/login.rs +++ b/src/callers/login.rs @@ -45,7 +45,8 @@ pub mod response { #[derive(Default, Deserialize, Serialize, utoipa::ToSchema)] pub struct ServiceUserLoginResponse { pub message: String, - pub data: Vec, + // TODO: Change this to login result + pub data: Vec, } pub async fn extract( @@ -208,7 +209,10 @@ pub async fn user_login( pub async fn service_user_login( axum::Extension(pool): axum::Extension, axum::Json(payload): axum::Json, -) -> (axum::http::StatusCode, axum::Json) { +) -> ( + axum::http::StatusCode, + axum::Json, +) { if payload.username.is_empty() || payload.passphrase.is_empty() { let reason = if payload.username.is_empty() { String::from("Username not provided") @@ -218,7 +222,7 @@ pub async fn service_user_login( ( axum::http::StatusCode::BAD_REQUEST, - axum::Json(response::LoginResponse { + axum::Json(response::ServiceUserLoginResponse { message: reason, data: Vec::new(), }), @@ -230,7 +234,7 @@ pub async fn service_user_login( println!("User does not exists"); ( axum::http::StatusCode::INTERNAL_SERVER_ERROR, - axum::Json(response::LoginResponse { + axum::Json(response::ServiceUserLoginResponse { message: String::from("Unable to login"), data: Vec::new(), }), @@ -244,7 +248,7 @@ pub async fn service_user_login( eprintln!("Error: {err:?}"); return ( axum::http::StatusCode::INTERNAL_SERVER_ERROR, - axum::Json(response::LoginResponse { + axum::Json(response::ServiceUserLoginResponse { message: String::from("Unable to login"), data: Vec::new(), }), @@ -278,7 +282,7 @@ pub async fn service_user_login( ( axum::http::StatusCode::OK, - axum::Json(response::LoginResponse { + axum::Json(response::ServiceUserLoginResponse { message: String::from( super::messages::SUCCESSFUL_MESSAGE, ), @@ -297,7 +301,7 @@ pub async fn service_user_login( eprintln!("Invalid token"); ( axum::http::StatusCode::INTERNAL_SERVER_ERROR, - axum::Json(response::LoginResponse { + axum::Json(response::ServiceUserLoginResponse { message: String::from("Invalid attempt"), data: Vec::new(), }), @@ -307,7 +311,7 @@ pub async fn service_user_login( eprintln!("No match"); ( axum::http::StatusCode::INTERNAL_SERVER_ERROR, - axum::Json(response::LoginResponse { + axum::Json(response::ServiceUserLoginResponse { message: String::from("Invalid attempt"), data: Vec::new(), }), @@ -316,7 +320,7 @@ pub async fn service_user_login( } Err(err) => ( axum::http::StatusCode::INTERNAL_SERVER_ERROR, - axum::Json(response::LoginResponse { + axum::Json(response::ServiceUserLoginResponse { message: err.to_string(), data: Vec::new(), }), @@ -326,7 +330,7 @@ pub async fn service_user_login( } Err(err) => ( axum::http::StatusCode::INTERNAL_SERVER_ERROR, - axum::Json(response::LoginResponse { + axum::Json(response::ServiceUserLoginResponse { message: err.to_string(), data: Vec::new(), }), diff --git a/tests/tests.rs b/tests/tests.rs index e490996..83b5b96 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -221,13 +221,16 @@ async fn login_user( response.status() ))) } else { - match axum::body::to_bytes(response.into_body(), usize::MAX).await { - Ok(body) => { - let parsed_body: callers::login::response::LoginResponse = - serde_json::from_slice(&body).unwrap(); - Ok(parsed_body.data[0].clone()) + match convert_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(std::io::Error::other(err.to_string())), + Err(err) => Err(err), } } } @@ -246,8 +249,19 @@ async fn register_service_user( response.status() ))) } else { - match convert_response::(response).await { - Ok(service_user) => Ok(service_user), + match convert_response::( + 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), } } @@ -256,7 +270,7 @@ async fn register_service_user( } } -async fn _login_service_user( +async fn login_service_user( app: &axum::Router, username: &str, passphrase: &str, @@ -269,8 +283,19 @@ async fn _login_service_user( response.status() ))) } else { - match convert_response::(response).await { - Ok(service_user) => Ok(service_user), + match convert_response::( + 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), } } @@ -445,7 +470,6 @@ async fn test_login_service_user() { user.id.is_nil(), "The service user id should not be nil" ); - /* match login_service_user(&app, TEST_SERVICE_USERNAME, TEST_SERVICE_PASSPHRASE).await { Ok(login_result) => { assert_eq!( @@ -458,7 +482,6 @@ async fn test_login_service_user() { assert!(false, "Error: {err:?}"); } } - */ } Err(err) => { assert!(false, "Error: {err:?}");