Skip to content

Commit

Permalink
add get_user test
Browse files Browse the repository at this point in the history
  • Loading branch information
PonponJuice committed Oct 31, 2024
1 parent 467111e commit 9411655
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ] }
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/common.sql
Original file line number Diff line number Diff line change
@@ -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);
118 changes: 118 additions & 0 deletions tests/get_user.rs
Original file line number Diff line number Diff line change
@@ -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(())
}

0 comments on commit 9411655

Please sign in to comment.