Skip to content

Commit ef3ef2a

Browse files
tests: commonize test request wrappers
1 parent 20d2994 commit ef3ef2a

5 files changed

Lines changed: 64 additions & 51 deletions

File tree

tests/test_web/mod.rs

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,41 @@ pub struct TestResponse {
1111
pub headers: Vec<Header<'static>>,
1212
}
1313

14-
pub async fn test_route(
14+
pub async fn test_request(
15+
method: &str,
1516
path: &'static str,
17+
json: Option<Value>,
1618
expected_status: Status,
19+
client: Option<&Client>,
1720
) -> TestResponse {
18-
let rocket = web::rocket();
19-
let client = Client::tracked(rocket)
20-
.await
21-
.expect("Failed to launch web server");
21+
let client = match client {
22+
Some(c) => c.to_owned(),
23+
None => {
24+
let rocket = web::rocket();
25+
&Client::tracked(rocket)
26+
.await
27+
.expect("Failed to launch web server")
28+
}
29+
};
2230

23-
let response = client.get(path).dispatch().await;
24-
create_test_response(response, expected_status).await
25-
}
31+
let request = match method.to_lowercase().as_str() {
32+
"get" => client.get(path),
33+
"post" => client.post(path),
34+
"put" => client.put(path),
35+
"delete" => client.delete(path),
36+
"patch" => client.patch(path),
37+
_ => panic!("Unsupported HTTP method: {}", method),
38+
};
2639

27-
pub async fn test_post_json(
28-
client: &Client,
29-
path: &'static str,
30-
json: Value,
31-
expected_status: Status,
32-
) -> TestResponse {
33-
let response = client
34-
.post(path)
35-
.header(ContentType::JSON)
36-
.body(json.to_string())
37-
.dispatch()
38-
.await;
40+
let request = if let Some(json_value) = json {
41+
request
42+
.header(ContentType::JSON)
43+
.body(json_value.to_string())
44+
} else {
45+
request
46+
};
3947

48+
let response = request.dispatch().await;
4049
create_test_response(response, expected_status).await
4150
}
4251

@@ -63,15 +72,15 @@ async fn create_test_response(
6372

6473
#[rocket::async_test]
6574
async fn test_swagger_ui_route() {
66-
test_route("/swagger-ui/", Status::SeeOther).await;
75+
test_request("get", "/swagger-ui/", None, Status::SeeOther, None).await;
6776
}
6877

6978
#[rocket::async_test]
7079
async fn test_rapidoc_route() {
71-
test_route("/rapidoc/", Status::SeeOther).await;
80+
test_request("get", "/rapidoc/", None, Status::SeeOther, None).await;
7281
}
7382

7483
#[rocket::async_test]
7584
async fn test_non_existent_route() {
76-
test_route("/non-existent", Status::NotFound).await;
85+
test_request("get", "/non-existent", None, Status::NotFound, None).await;
7786
}

tests/test_web/routes/auth.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use serde_json::json;
44
use serial_test::serial;
55

66
use crate::fixtures;
7-
use crate::test_web::test_post_json;
8-
use crate::test_web::test_route;
7+
use crate::test_web::test_request;
98

109
#[rstest]
1110
#[serial(db)]
@@ -32,27 +31,29 @@ async fn test_login(
3231
let client = &db.client;
3332

3433
// Create test user
35-
test_post_json(
36-
client,
34+
test_request(
35+
"post",
3736
"/create_user",
38-
json!({
37+
Some(json!({
3938
"username": username_0,
4039
"password": password_0,
4140
"admin": false
42-
}),
41+
})),
4342
Status::Ok,
43+
Some(client),
4444
)
4545
.await;
4646

4747
// Test login
48-
let response = test_post_json(
49-
client,
48+
let response = test_request(
49+
"post",
5050
"/login",
51-
json!({
51+
Some(json!({
5252
"username": username_1,
5353
"password": password_1
54-
}),
54+
})),
5555
expected_status,
56+
Some(client),
5657
)
5758
.await;
5859

@@ -63,5 +64,5 @@ async fn test_login(
6364

6465
#[rocket::async_test]
6566
async fn test_logout_route() {
66-
test_route("/logout", Status::Ok).await;
67+
test_request("get", "/logout", None, Status::Ok, None).await;
6768
}

tests/test_web/routes/common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use crate::test_web::test_route;
1+
use crate::test_web::test_request;
22

33
use rocket::http::Status;
44

55
#[rocket::async_test]
66
async fn test_root_route() {
7-
let response = test_route("/", Status::Ok).await;
7+
let response = test_request("get", "/", None, Status::Ok, None).await;
88

99
assert_eq!(response.body, "Welcome to Koko!");
1010
}

tests/test_web/routes/dependencies.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use crate::test_web::test_route;
1+
use crate::test_web::test_request;
22

33
use rocket::http::Status;
44
use rocket::serde::json::{serde_json, Value};
55

66
#[rocket::async_test]
77
async fn test_get_dependencies_route() {
8-
let response = test_route("/dependencies", Status::Ok).await;
8+
let response = test_request("get", "/dependencies", None, Status::Ok, None).await;
99

1010
// ensure response is a json list of dictionaries, and each dictionary has the keys name, version, and license
1111
let body = response.body;

tests/test_web/routes/user.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use serde_json::json;
44
use serial_test::serial;
55

66
use crate::fixtures;
7-
use crate::test_web::test_post_json;
7+
use crate::test_web::test_request;
88

99
#[rstest]
1010
#[serial(db)]
@@ -23,16 +23,17 @@ async fn test_create_first_user(
2323
let db = db_future.await;
2424
let client = &db.client;
2525

26-
let response = test_post_json(
27-
client,
26+
let response = test_request(
27+
"post",
2828
"/create_user",
29-
json!({
29+
Some(json!({
3030
"username": username,
3131
"password": password,
3232
"pin": pin,
3333
"admin": admin
34-
}),
34+
})),
3535
expected_status,
36+
Some(client),
3637
)
3738
.await;
3839

@@ -56,28 +57,30 @@ async fn test_create_subsequent_user_requires_auth(
5657
let client = &db.client;
5758

5859
// Create first user
59-
test_post_json(
60-
client,
60+
test_request(
61+
"post",
6162
"/create_user",
62-
json!({
63+
Some(json!({
6364
"username": "user1",
6465
"password": "password123",
6566
"admin": true
66-
}),
67+
})),
6768
Status::Ok,
69+
Some(client),
6870
)
6971
.await;
7072

7173
// Try to create second user without auth
72-
test_post_json(
73-
client,
74+
test_request(
75+
"post",
7476
"/create_user",
75-
json!({
77+
Some(json!({
7678
"username": username,
7779
"password": password,
7880
"admin": admin
79-
}),
81+
})),
8082
expected_status,
83+
Some(client),
8184
)
8285
.await;
8386
}

0 commit comments

Comments
 (0)