From c9f17b936f08efa3cfbfac4aee9e3285d48287f7 Mon Sep 17 00:00:00 2001 From: phoenix Date: Fri, 4 Apr 2025 18:59:46 -0400 Subject: [PATCH] Added test for register endpoint with transactions --- src/main.rs | 137 ++++++++++++++++++++++++++++++++++--------- src/models/common.rs | 2 +- 2 files changed, 110 insertions(+), 29 deletions(-) diff --git a/src/main.rs b/src/main.rs index 64534e0..63a0e79 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,3 @@ -use axum::{ - Router, - routing::{get, post}, -}; - use icarus_auth::callers; use icarus_auth::config; @@ -11,7 +6,7 @@ async fn main() { // initialize tracing tracing_subscriber::fmt::init(); - let app = app().await; + let app = init::app().await; // run our app with hyper, listening globally on port 3000 let url = config::get_full(); @@ -19,30 +14,46 @@ async fn main() { axum::serve(listener, app).await.unwrap(); } -async fn routes() -> Router { - // build our application with a route - Router::new() - .route(callers::endpoints::DBTEST, get(callers::common::db_ping)) - .route(callers::endpoints::ROOT, get(callers::common::root)) - .route( - callers::endpoints::REGISTER, - post(callers::register::register_user), - ) +mod db { + pub async fn migrations(pool: &sqlx::PgPool) { + // Run migrations using the sqlx::migrate! macro + // Assumes your migrations are in a ./migrations folder relative to Cargo.toml + sqlx::migrate!("./migrations") + .run(pool) + .await + .expect("Failed to run migrations on testcontainer DB"); + } } -async fn app() -> Router { - let pool = icarus_auth::db_pool::create_pool() - .await - .expect("Failed to create pool"); +mod init { + use axum::{ + Router, + routing::{get, post}, + }; - // Run migrations using the sqlx::migrate! macro - // Assumes your migrations are in a ./migrations folder relative to Cargo.toml - sqlx::migrate!("./migrations") - .run(&pool) - .await - .expect("Failed to run migrations on testcontainer DB"); + use crate::callers; + use crate::db; - routes().await.layer(axum::Extension(pool)) + pub async fn routes() -> Router { + // build our application with a route + Router::new() + .route(callers::endpoints::DBTEST, get(callers::common::db_ping)) + .route(callers::endpoints::ROOT, get(callers::common::root)) + .route( + callers::endpoints::REGISTER, + post(callers::register::register_user), + ) + } + + pub async fn app() -> Router { + let pool = icarus_auth::db_pool::create_pool() + .await + .expect("Failed to create pool"); + + db::migrations(&pool).await; + + routes().await.layer(axum::Extension(pool)) + } } #[cfg(test)] @@ -53,11 +64,13 @@ mod tests { http::{Request, StatusCode}, }; use http_body_util::BodyExt; + // use reqwest; + use serde_json::json; use tower::ServiceExt; // for `call`, `oneshot`, and `ready` #[tokio::test] - async fn hello_world() { - let app = app().await; + async fn test_hello_world() { + let app = init::app().await; // `Router` implements `tower::Service>` so we can // call it like any tower service, no need to run an HTTP server. @@ -76,4 +89,72 @@ mod tests { let body = response.into_body().collect().await.unwrap().to_bytes(); assert_eq!(&body[..], b"Hello, World!"); } + + #[tokio::test] + async fn test_register_user() { + let pool = icarus_auth::db_pool::create_pool() + .await + .expect("Failed to create pool"); + db::migrations(&pool).await; + + let mut tx = pool.begin().await.unwrap(); + let app = init::routes().await.layer(axum::Extension(pool)); + + let usr = icarus_auth::models::common::CreateUser { + username: String::from("somethingsss"), + password: String::from("Raindown!"), + }; + + /* + let client = reqwest::Client::new(); + let url = icarus_auth::callers::endpoints::REGISTER; + let response = app.oneshot( + println!("Errr") + ) + .await.unwrap(); + */ + + /* + match client.post(url).json(&usr).send().await { + Ok(response) => {} + Err(err) => { + assert!(false, "Error: {:?}", err.to_string()); + } + } + */ + + let payload = json!({ + "username": usr.username, + "password": usr.password, + }); + + assert!(true, "Info: {:?}", payload); + println!("Info: {:?}", payload); + + let response = app + .oneshot( + Request::builder() + .method(axum::http::Method::POST) + .uri(callers::endpoints::REGISTER) + .header(axum::http::header::CONTENT_TYPE, "application/json") + // .body(Body::new(axum::Json(usr))) + // .body(Body::empty()) + .body(Body::from(payload.to_string())) + // .body(Body::from("{\"username\": \"sssss\",\"theanswer\"}")) + .unwrap(), + ) + .await; + // .unwrap(); + + match response { + Ok(resp) => { + assert_eq!(resp.status(), StatusCode::CREATED, "Message: {:?}", resp); + } + Err(err) => { + assert!(false, "Error: {:?}", err.to_string()); + } + }; + + tx.rollback().await.unwrap(); + } } diff --git a/src/models/common.rs b/src/models/common.rs index 8bf5ab7..7b978d5 100644 --- a/src/models/common.rs +++ b/src/models/common.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -#[derive(Deserialize)] +#[derive(Deserialize, Serialize)] pub struct CreateUser { pub username: String, pub password: String,