From a6d70e3155e1425c2b66fa4aefd6c8d19148ac32 Mon Sep 17 00:00:00 2001 From: phoenix Date: Thu, 11 Jun 2026 17:17:17 -0400 Subject: [PATCH] Added test --- tests/tests.rs | 108 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/tests/tests.rs b/tests/tests.rs index 4753c91..dd0c2fe 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -133,6 +133,22 @@ pub mod requests { app.clone().oneshot(req).await } + + + pub async fn login_service_user(app: &axum::Router, username: &String, passphrase: &String) -> Result { + let payload = serde_json::json!({ + "username": username, + "passphrase": passphrase, + }); + let req = axum::http::Request::builder() + .method(axum::http::Method::POST) + .uri(super::callers::endpoints::LOGIN_SERVICE_USER) + .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 @@ -216,6 +232,52 @@ async fn login_user( } } +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 convert_response::(response).await { + Ok(service_user) => { + Ok(service_user) + } + Err(err) => { + Err(err) + } + } + } + } + Err(err) => Err(std::io::Error::other(err.to_string())), + } +} + +async fn login_service_user(app: &axum::Router, username: &String, passphrase: &String) -> 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 convert_response::(response).await { + Ok(service_user) => { + Ok(service_user) + } + Err(err) => { + Err(err) + } + } + } + } + Err(err) => Err(std::io::Error::other(err.to_string())), + } +} + #[tokio::test] async fn test_register_user() { let tm_pool = db_mgr::get_pool().await.unwrap(); @@ -354,3 +416,49 @@ async fn test_register_service_user() { } } } + +#[tokio::test] +async fn test_login_service_user() { + 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 register_service_user(&app).await { + Ok(user) => match login_service_user(&app, &user.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" + ); + } + 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:?}"); + } + } +}