diff --git a/tests/tests.rs b/tests/tests.rs index 7708911..618ae12 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -85,6 +85,25 @@ pub mod requests { "firstname": String::from(super::TEST_FIRSTNAME), "lastname": String::from(super::TEST_LASTNAME), }); + let req = axum::http::Request::builder() + .method(axum::http::Method::POST) + .uri(super::callers::endpoints::REGISTER) + .header(axum::http::header::CONTENT_TYPE, "application/json") + .body(axum::body::Body::from(payload.to_string())) + .unwrap(); + + app.clone().oneshot(req).await + } + + pub async fn login_user( + app: &axum::Router, + username: &str, + password: &str, + ) -> Result { + let payload = serde_json::json!({ + "username": username, + "password": password, + }); let req = axum::http::Request::builder() .method(axum::http::Method::POST) .uri(super::callers::endpoints::LOGIN) @@ -109,6 +128,52 @@ const TEST_USERNAME: &str = "BillyBob01"; const TEST_PASSWORD: &str = "923ndcry392qryudx328qrdy328r"; const TEST_PHONE_NUMBER: &str = "+10123456789"; +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("")) + } else { + match axum::body::to_bytes(response.into_body(), usize::MAX).await { + Ok(body) => { + let parsed_body: callers::register::response::Response = + serde_json::from_slice(&body).unwrap(); + Ok(parsed_body.data[0].clone()) + } + 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("")) + } 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()) + } + Err(err) => Err(std::io::Error::other(err.to_string())), + } + } + } + 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(); @@ -176,6 +241,24 @@ async fn test_login_user() { } } + 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_user(&app).await { + Ok(user) => match login_user(&app, &user.username, TEST_PASSWORD).await { + Ok(_login_result) => {} + 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) => {