-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
467111e
commit 9411655
Showing
3 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |