From 458612fb28055919de1add0f2d902f18cbeb2b9a Mon Sep 17 00:00:00 2001 From: phoenix Date: Fri, 12 Jun 2026 10:41:53 -0400 Subject: [PATCH] Added test --- tests/tests.rs | 110 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/tests/tests.rs b/tests/tests.rs index 6b8b1dc..ee7005e 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -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 { + 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 @@ -321,6 +340,39 @@ mod flow { Err(err) => Err(std::io::Error::other(err.to_string())), } } + + pub async fn refresh_token( + app: &axum::Router, + access_token: &str, + ) -> Result { + 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::( + 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())), + } + } } #[tokio::test] @@ -516,3 +568,61 @@ 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 db_mgr::drop_database(&tm_pool, &db_name).await { + Ok(()) => {} + Err(err) => { + assert!(false, "Error: {err:?}"); + } + } +}