diff --git a/tests/tests.rs b/tests/tests.rs index 6cdc91e..6b8b1dc 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -187,131 +187,139 @@ mod util { } } -async fn register_user( - app: &axum::Router, -) -> Result { - 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::(response) +mod flow { + use super::callers; + use super::requests; + use super::util; + + pub async fn register_user( + app: &axum::Router, + ) -> Result { + 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::(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())), + } + } + + pub async fn login_user( + app: &axum::Router, + username: &str, + password: &str, + ) -> Result { + 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::( + 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")) + { + 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_user( - app: &axum::Router, - username: &str, - password: &str, -) -> Result { - 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::(response) + pub async fn register_service_user( + app: &axum::Router, + ) -> Result { + 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 user = response.data[0].clone(); - Ok(user) - } else { - Err(std::io::Error::other("No data returned")) + { + 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())), } - Err(err) => Err(std::io::Error::other(err.to_string())), } -} -async fn register_service_user( - app: &axum::Router, -) -> Result { - 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")) + pub async fn login_service_user( + app: &axum::Router, + username: &str, + passphrase: &str, + ) -> Result { + 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::( + 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(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 { - 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::( - 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())), } } @@ -335,7 +343,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, @@ -375,8 +383,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, @@ -474,14 +482,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,