diff --git a/Cargo.toml b/Cargo.toml index c7b3618..18affc3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] axum = "0.7" +http-body-util = "0.1.0" axum-extra = { version = "0.9", features = [ "typed-header" ] } tokio = { version = "1.0", features = ["full"] } serde = { version = "1.0", features = [ "derive" ] } diff --git a/tests/fixtures/common.sql b/tests/fixtures/common.sql new file mode 100644 index 0000000..ab2da89 --- /dev/null +++ b/tests/fixtures/common.sql @@ -0,0 +1,3 @@ +INSERT INTO users (id, name, role) VALUES (UNHEX(REPLACE('997A6050-D400-ECD0-5E79-E3B84A7079AE','-','')), 'test_user_1', 0); +INSERT INTO users (id, name, role) VALUES (UNHEX(REPLACE('BC51F5BD-FB65-082F-4439-57F540472804','-','')), 'test_user_2', 1); +INSERT INTO users (id, name, role) VALUES (UNHEX(REPLACE('A070E925-3A0B-BC00-D06E-56EA7757F05A','-','')), 'test_user_3', 2); \ No newline at end of file diff --git a/tests/get_user.rs b/tests/get_user.rs new file mode 100644 index 0000000..b7a81c6 --- /dev/null +++ b/tests/get_user.rs @@ -0,0 +1,118 @@ +use std::borrow::BorrowMut; + +use axum::{body::Body, http::Request}; +use http_body_util::BodyExt; +use serde_json::Value; +use tower::ServiceExt; +use trao_judge_backend::{make_router, Repository}; + +#[sqlx::test(fixtures("common"))] +async fn get_user_by_id(pool: sqlx::MySqlPool) -> anyhow::Result<()> { + let state = Repository::create_by_pool(pool).await?; + let mut app = make_router(state); + + let user_case = vec![ + (1, "test_user_1", "commonUser"), + (2, "test_user_2", "traPUser"), + (3, "test_user_3", "admin"), + ]; + for (id, name, role) in user_case { + let response = app + .borrow_mut() + .oneshot( + Request::builder() + .extension("GET") + .uri(format!("/users/{}", id)) + .body(Body::empty())?, + ) + .await?; + + assert_eq!(response.status(), 200); + + let json: Value = + serde_json::from_slice(&response.into_body().collect().await?.to_bytes())?; + assert_eq!(json["name"], name); + assert_eq!(json["role"], role); + } + + Ok(()) +} + +#[sqlx::test(fixtures("common"))] +async fn get_user_by_id_not_found(pool: sqlx::MySqlPool) -> anyhow::Result<()> { + let state = Repository::create_by_pool(pool).await?; + let mut app = make_router(state); + + let not_found_case = vec![0, 4, 10, 1000000]; + for id in not_found_case { + let response = app + .borrow_mut() + .oneshot( + Request::builder() + .extension("GET") + .uri(format!("/users/{}", id)) + .body(Body::empty())?, + ) + .await?; + + assert_eq!(response.status(), 404); + } + + Ok(()) +} + +#[sqlx::test(fixtures("common"))] +async fn get_user_me(pool: sqlx::MySqlPool) -> anyhow::Result<()> { + let state = Repository::create_by_pool(pool).await?; + let mut app = make_router(state.clone()); + + let user_case = vec![ + (1, "test_user_1", "commonUser"), + (2, "test_user_2", "traPUser"), + (3, "test_user_3", "admin"), + ]; + for (id, name, role) in user_case { + let session_id = state + .create_session(state.get_user_by_display_id(id).await?.unwrap()) + .await?; + + let response = app + .borrow_mut() + .oneshot( + Request::builder() + .extension("GET") + .uri("/users/me") + .header("Cookie", format!("session_id={}", session_id)) + .body(Body::empty())?, + ) + .await?; + assert_eq!(response.status(), 200); + + let json: Value = + serde_json::from_slice(&response.into_body().collect().await?.to_bytes())?; + assert_eq!(json["name"], name); + assert_eq!(json["role"], role); + } + + Ok(()) +} + +#[sqlx::test(fixtures("common"))] +async fn get_user_me_unauthorized(pool: sqlx::MySqlPool) -> anyhow::Result<()> { + let state = Repository::create_by_pool(pool).await?; + let mut app = make_router(state.clone()); + + // Test unauthorized case + let response = app + .borrow_mut() + .oneshot( + Request::builder() + .extension("GET") + .uri("/users/me") + .body(Body::empty())?, + ) + .await?; + assert_eq!(response.status(), 401); + + Ok(()) +}