From 2f6093fcd12748f23c86a61539bf8e5f3a1eaff4 Mon Sep 17 00:00:00 2001 From: phoenix Date: Tue, 22 Sep 2026 12:32:22 -0400 Subject: [PATCH] Test changes --- src/main.rs | 118 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 97 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2cd1dd8..0244fed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -258,6 +258,20 @@ mod tests { }) } + pub mod test_data { + use serde_json::json; + + pub fn get_updated_password(user_id: &uuid::Uuid) -> serde_json::Value { + json!({ + "username": "somethingsss", + "user_id": user_id, + "current_password": "Raindown!", + "updated_password": "SpeakWithUrChest22!", + "confirmed_password": "SpeakWithUrChest22!" + }) + } + } + pub mod requests { use tower::ServiceExt; // for `call`, `oneshot`, and `ready` @@ -290,6 +304,21 @@ mod tests { app.clone().oneshot(req).await } + + pub async fn update_password( + app: &axum::Router, + user_id: &uuid::Uuid, + ) -> Result { + let payload = super::test_data::get_updated_password(user_id); + let req = axum::http::Request::builder() + .method(axum::http::Method::PATCH) + .uri(crate::callers::endpoints::LOGIN) + .header(axum::http::header::CONTENT_TYPE, "application/json") + .body(axum::body::Body::from(payload.to_string())) + .unwrap(); + + app.clone().oneshot(req).await + } } #[tokio::test] @@ -420,29 +449,10 @@ mod tests { ); assert!(returned_usr.date_created.is_some(), "Date Created is empty"); - /* - let login_payload = json!({ - "username": &usr.username, - "password": &usr.password, - }); - */ - let mut n_usr = callers::login::request::Request::default(); n_usr.username = usr.username.clone(); n_usr.password = usr.password.clone(); - /* - match app - .oneshot( - Request::builder() - .method(axum::http::Method::POST) - .uri(callers::endpoints::LOGIN) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .body(Body::from(login_payload.to_string())) - .unwrap(), - ) - .await - */ match requests::login(&app, &n_usr).await { Ok(resp) => { assert_eq!(StatusCode::OK, resp.status(), "Status is not right"); @@ -607,9 +617,75 @@ mod tests { db::init::migrations(&pool).await; - let _app = init::routes().await.layer(axum::Extension(pool)); + let app = init::routes().await.layer(axum::Extension(pool)); - let _usr = get_test_register_request(); + let usr = get_test_register_request(); + + match requests::register(&app, &usr).await { + Ok(resp) => { + assert_eq!( + resp.status(), + StatusCode::CREATED, + "Message: {:?} {:?}", + resp, + usr.username + ); + let body = axum::body::to_bytes(resp.into_body(), usize::MAX) + .await + .unwrap(); + let parsed_body: callers::register::response::Response = + serde_json::from_slice(&body).unwrap(); + let returned_usr = &parsed_body.data[0]; + + assert_eq!(false, returned_usr.id.is_nil(), "Id is not populated"); + + assert_eq!( + usr.username, returned_usr.username, + "Usernames do not match" + ); + assert!(returned_usr.date_created.is_some(), "Date Created is empty"); + + let mut n_usr = callers::login::request::Request::default(); + n_usr.username = usr.username.clone(); + n_usr.password = usr.password.clone(); + + match requests::login(&app, &n_usr).await { + Ok(resp) => { + assert_eq!(StatusCode::OK, resp.status(), "Status is not right"); + let body = axum::body::to_bytes(resp.into_body(), usize::MAX) + .await + .unwrap(); + let parsed_body: callers::login::response::Response = + serde_json::from_slice(&body).unwrap(); + let login_result = &parsed_body.data[0]; + assert!(!login_result.id.is_nil(), "Id is nil"); + + match requests::update_password(&app, &login_result.id).await { + Ok(resp) => { + assert_eq!(StatusCode::OK, resp.status(), "Status is not right"); + let body = axum::body::to_bytes(resp.into_body(), usize::MAX) + .await + .unwrap(); + let parsed_body: callers::login::response::update_password::Response = + serde_json::from_slice(&body).unwrap(); + let id = parsed_body.data[0]; + + assert_eq!(id, login_result.id, "Ids do not match"); + } + Err(err) => { + assert!(false, "Error: {err:?}"); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err.to_string()); + } + } + } + Err(err) => { + assert!(false, "Error: {err:?}"); + } + } let _ = db_mgr::drop_database(&tm_pool, &db_name).await; }