-
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.
Merge pull request #51 from traP-jp/feat#46-users-integration-test
Feat#46 users integration test
- Loading branch information
Showing
14 changed files
with
431 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ export DB_HOSTNAME="localhost" | |
export DB_PORT="3306" | ||
export DB_DATABASE="world" | ||
export RUST_LOG="debug" | ||
export DATABASE_URL="mariadb://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT/$DB_DATABASE" | ||
|
||
export MAIL_ADDRESS="[email protected]" | ||
export MAIL_PASSWORD="**** **** **** ****" | ||
|
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
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
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 @@ | ||
mod users; |
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,50 @@ | ||
use super::remove_field; | ||
use serde_json::{json, Value}; | ||
|
||
pub fn users_check_by_id(id: i64, resp_json: &mut Value) -> anyhow::Result<()> { | ||
let users_json = match id { | ||
1 => json!({ | ||
"id": "11111111-1111-1111-1111-111111111111", | ||
"displayId": 1, | ||
"name": "test_user_1", | ||
"traqId": null, | ||
"githubId": "test_github_id_1", | ||
"iconUrl": null, | ||
"githubLink": "https://github.com/test_user_1", | ||
"xLink": null, | ||
"selfIntroduction": "test_self_introduction_1", | ||
"role": "commonUser", | ||
}), | ||
2 => json!({ | ||
"id": "22222222-2222-2222-2222-222222222222", | ||
"displayId": 2, | ||
"name": "test_user_2", | ||
"traqId": "test_traq_id_2", | ||
"githubId": null, | ||
"iconUrl": null, | ||
"githubLink": null, | ||
"xLink": "https://x.com/test_user_2", | ||
"selfIntroduction": "", | ||
"role": "traPUser", | ||
}), | ||
3 => json!({ | ||
"id": "33333333-3333-3333-3333-333333333333", | ||
"displayId": 3, | ||
"name": "test_user_3", | ||
"traqId": null, | ||
"githubId": null, | ||
"iconUrl": "https://icon.com/test_user_3", | ||
"githubLink": null, | ||
"xLink": null, | ||
"selfIntroduction": "", | ||
"role": "admin", | ||
}), | ||
_ => return Err(anyhow::anyhow!("Invalid id")), | ||
}; | ||
let ignore_field = vec!["createdAt", "updatedAt"]; | ||
|
||
remove_field(resp_json, &ignore_field); | ||
assert_eq!(resp_json, &users_json); | ||
|
||
Ok(()) | ||
} |
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,31 @@ | ||
use axum::{ | ||
body::Body, | ||
http::{request, Request}, | ||
}; | ||
|
||
pub mod check; | ||
|
||
pub trait RequestBuilderExt { | ||
fn json(self, json: serde_json::Value) -> Request<Body>; | ||
} | ||
|
||
impl RequestBuilderExt for request::Builder { | ||
fn json(self, json: serde_json::Value) -> Request<Body> { | ||
self.header("Content-Type", "application/json") | ||
.body(Body::from(json.to_string())) | ||
.unwrap() | ||
} | ||
} | ||
|
||
pub fn remove_field(json: &mut serde_json::Value, ignore_fields: &Vec<&str>) { | ||
match json { | ||
serde_json::Value::Object(obj) => { | ||
for field in ignore_fields { | ||
obj.remove(*field); | ||
} | ||
} | ||
_ => { | ||
panic!("Invalid JSON format"); | ||
} | ||
} | ||
} |
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,41 @@ | ||
INSERT INTO users | ||
(id, name, role, github_id, github_link, self_introduction, email) | ||
VALUES | ||
( | ||
UNHEX(REPLACE('11111111-1111-1111-1111-111111111111','-','')), | ||
'test_user_1', | ||
0, | ||
"test_github_id_1", | ||
"https://github.com/test_user_1", | ||
"test_self_introduction_1", | ||
"[email protected]" | ||
); | ||
|
||
INSERT INTO users_passwords | ||
(user_id, password) | ||
VALUES | ||
( | ||
UNHEX(REPLACE('11111111-1111-1111-1111-111111111111','-','')), | ||
"test_password_1" | ||
); | ||
|
||
INSERT INTO | ||
users (id, name, role, traq_id, x_link) | ||
VALUES | ||
( | ||
UNHEX(REPLACE('22222222-2222-2222-2222-222222222222','-','')), | ||
'test_user_2', | ||
1, | ||
"test_traq_id_2", | ||
"https://x.com/test_user_2" | ||
); | ||
|
||
INSERT INTO users | ||
(id, name, role, icon_url) | ||
VALUES | ||
( | ||
UNHEX(REPLACE('33333333-3333-3333-3333-333333333333','-','')), | ||
'test_user_3', | ||
2, | ||
"https://icon.com/test_user_3" | ||
); |
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,116 @@ | ||
use std::borrow::BorrowMut; | ||
|
||
use super::common::check::users_check_by_id; | ||
use axum::{ | ||
body::Body, | ||
http::{self, 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 tests = vec![1, 2, 3]; | ||
|
||
for id in tests { | ||
let response = app | ||
.borrow_mut() | ||
.oneshot( | ||
Request::builder() | ||
.method(http::Method::GET) | ||
.uri(format!("/users/{}", id)) | ||
.body(Body::empty())?, | ||
) | ||
.await?; | ||
|
||
assert_eq!(response.status(), 200); | ||
|
||
let mut resp_json: Value = | ||
serde_json::from_slice(&response.into_body().collect().await?.to_bytes())?; | ||
|
||
users_check_by_id(id, &mut resp_json)?; | ||
} | ||
|
||
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() | ||
.method(http::Method::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 tests = vec![1, 2, 3]; | ||
|
||
for id in tests { | ||
let session_id = state | ||
.create_session(state.get_user_by_display_id(id).await?.unwrap()) | ||
.await?; | ||
|
||
let response = app | ||
.borrow_mut() | ||
.oneshot( | ||
Request::builder() | ||
.method(http::Method::GET) | ||
.uri("/users/me") | ||
.header("Cookie", format!("session_id={}", session_id)) | ||
.body(Body::empty())?, | ||
) | ||
.await?; | ||
assert_eq!(response.status(), 200); | ||
|
||
let mut resp_json: Value = | ||
serde_json::from_slice(&response.into_body().collect().await?.to_bytes())?; | ||
|
||
users_check_by_id(id, &mut resp_json)?; | ||
} | ||
|
||
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() | ||
.method(http::Method::GET) | ||
.uri("/users/me") | ||
.body(Body::empty())?, | ||
) | ||
.await?; | ||
assert_eq!(response.status(), 401); | ||
|
||
Ok(()) | ||
} |
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,4 @@ | ||
pub mod common; | ||
|
||
mod get_users; | ||
mod put_users; |
Oops, something went wrong.