Skip to content

Commit

Permalink
use uri macro properly instead of hardcoding the uris in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Timtam committed Mar 3, 2024
1 parent e9bada6 commit 852d16c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
38 changes: 19 additions & 19 deletions server/src/routes/games.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ pub async fn join_game(
#[cfg(test)]
mod tests {
use crate::{
responses::GameResponse, routes::users::tests::create_test_users, test::mocked_client,
responses::GameResponse,
routes::users::{self as user_routes, tests::create_test_users},
test::mocked_client,
users::UserLoginPayload,
};
use rocket::http::{ContentType, Status};
Expand All @@ -105,7 +107,7 @@ mod tests {
create_test_users(&client).await;

let response = client
.post(uri!("/users/login"))
.post(uri!(user_routes::user_login))
.header(ContentType::JSON)
.body(
serde_json::to_string(&UserLoginPayload {
Expand All @@ -118,7 +120,7 @@ mod tests {
.await;

let game = client
.post(uri!("/games"))
.post(uri!(super::create_game))
.private_cookie(response.cookies().get_private("login").unwrap())
.dispatch()
.await;
Expand All @@ -134,7 +136,7 @@ mod tests {
create_test_users(&client).await;

let response = client
.post(uri!("/users/login"))
.post(uri!(user_routes::user_login))
.header(ContentType::JSON)
.body(
serde_json::to_string(&UserLoginPayload {
Expand All @@ -147,12 +149,12 @@ mod tests {
.await;

let game1 = client
.post(uri!("/games"))
.post(uri!(super::create_game))
.private_cookie(response.cookies().get_private("login").unwrap())
.dispatch()
.await;
let game2 = client
.post(uri!("/games"))
.post(uri!(super::create_game))
.private_cookie(response.cookies().get_private("login").unwrap())
.dispatch()
.await;
Expand All @@ -170,7 +172,7 @@ mod tests {
create_test_users(&client).await;

let response = client
.post(uri!("/users/login"))
.post(uri!(user_routes::user_login))
.header(ContentType::JSON)
.body(
serde_json::to_string(&UserLoginPayload {
Expand All @@ -183,13 +185,13 @@ mod tests {
.await;

let game = client
.post(uri!("/games"))
.post(uri!(super::create_game))
.private_cookie(response.cookies().get_private("login").unwrap())
.dispatch()
.await;

let response = client
.post(uri!("/users/login"))
.post(uri!(user_routes::user_login))
.header(ContentType::JSON)
.body(
serde_json::to_string(&UserLoginPayload {
Expand All @@ -203,10 +205,9 @@ mod tests {

assert_eq!(
client
.patch(format!(
"/games/{}/join",
game.into_json::<GameResponse>().await.unwrap().id
))
.patch(uri!(super::join_game(
game_id = game.into_json::<GameResponse>().await.unwrap().id
)))
.private_cookie(response.cookies().get_private("login").unwrap())
.dispatch()
.await
Expand All @@ -222,7 +223,7 @@ mod tests {
create_test_users(&client).await;

let response = client
.post(uri!("/users/login"))
.post(uri!(user_routes::user_login))
.header(ContentType::JSON)
.body(
serde_json::to_string(&UserLoginPayload {
Expand All @@ -235,17 +236,16 @@ mod tests {
.await;

let game = client
.post(uri!("/games"))
.post(uri!(super::create_game))
.private_cookie(response.cookies().get_private("login").unwrap())
.dispatch()
.await;

assert_eq!(
client
.patch(format!(
"/games/{}/join",
game.into_json::<GameResponse>().await.unwrap().id
))
.patch(uri!(super::join_game(
game_id = game.into_json::<GameResponse>().await.unwrap().id
)))
.private_cookie(response.cookies().get_private("login").unwrap())
.dispatch()
.await
Expand Down
40 changes: 25 additions & 15 deletions server/src/routes/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ pub mod tests {

pub async fn create_test_users(client: &Client) {
client
.post(uri!("/users/signup"))
.post(uri!(super::user_signup))
.header(ContentType::JSON)
.body(
serde_json::to_string(&UserLoginPayload {
Expand All @@ -227,7 +227,7 @@ pub mod tests {
.dispatch()
.await;
client
.post(uri!("/users/signup"))
.post(uri!(super::user_signup))
.header(ContentType::JSON)
.body(
serde_json::to_string(&UserLoginPayload {
Expand All @@ -244,7 +244,7 @@ pub mod tests {
async fn can_create_user() {
let client = mocked_client().await;
let response = client
.post(uri!("/users/signup"))
.post(uri!(super::user_signup))
.header(ContentType::JSON)
.body(
serde_json::to_string(&UserLoginPayload {
Expand Down Expand Up @@ -273,7 +273,7 @@ pub mod tests {
create_test_users(&client).await;

client
.post(uri!("/users/login"))
.post(uri!(super::user_login))
.header(ContentType::JSON)
.body(
serde_json::to_string(&UserLoginPayload {
Expand All @@ -285,7 +285,7 @@ pub mod tests {
.dispatch()
.await;
client
.post(uri!("/users/login"))
.post(uri!(super::user_login))
.header(ContentType::JSON)
.body(
serde_json::to_string(&UserLoginPayload {
Expand All @@ -298,7 +298,7 @@ pub mod tests {
.await;

let users = client
.get(uri!("/users"))
.get(uri!(super::get_all_users))
.dispatch()
.await
.into_json::<UsersResponse>()
Expand All @@ -319,7 +319,7 @@ pub mod tests {

create_test_users(&client).await;

let response = client.get(uri!("/users")).dispatch().await;
let response = client.get(uri!(super::get_all_users)).dispatch().await;

assert_eq!(response.status(), Status::Ok);
assert_eq!(
Expand All @@ -336,7 +336,10 @@ pub mod tests {
#[sqlx::test]
async fn cause_error_when_retrieving_invalid_user() {
let client = mocked_client().await;
let response = client.get(uri!("/users/1")).dispatch().await;
let response = client
.get(uri!(super::get_user(user_id = 1)))
.dispatch()
.await;
assert_eq!(response.status(), Status::NotFound);
}

Expand All @@ -346,7 +349,10 @@ pub mod tests {

create_test_users(&client).await;

let response = client.get(uri!("/users/1")).dispatch().await;
let response = client
.get(uri!(super::get_user(user_id = 1)))
.dispatch()
.await;

assert_eq!(response.status(), Status::Ok);

Expand All @@ -362,7 +368,7 @@ pub mod tests {
create_test_users(&client).await;

let response = client
.post(uri!("/users/login"))
.post(uri!(super::user_login))
.header(ContentType::JSON)
.body(
serde_json::to_string(&UserLoginPayload {
Expand All @@ -382,7 +388,7 @@ pub mod tests {
let client = mocked_client().await;

let response = client
.post(uri!("/users/login"))
.post(uri!(super::user_login))
.header(ContentType::JSON)
.body(
serde_json::to_string(&UserLoginPayload {
Expand All @@ -404,7 +410,7 @@ pub mod tests {
create_test_users(&client).await;

let response = client
.post(uri!("/users/login"))
.post(uri!(super::user_login))
.header(ContentType::JSON)
.body(
serde_json::to_string(&UserLoginPayload {
Expand All @@ -418,7 +424,7 @@ pub mod tests {

assert_eq!(
client
.post(uri!("/users/logout"))
.post(uri!(super::user_logout))
.private_cookie(response.cookies().get_private("login").unwrap())
.dispatch()
.await
Expand All @@ -432,7 +438,11 @@ pub mod tests {
let client = mocked_client().await;

assert_eq!(
client.post(uri!("/users/logout")).dispatch().await.status(),
client
.post(uri!(super::user_logout))
.dispatch()
.await
.status(),
Status::Unauthorized
);
}
Expand All @@ -443,7 +453,7 @@ pub mod tests {

assert_eq!(
client
.post(uri!("/users/logout"))
.post(uri!(super::user_logout))
.private_cookie(Cookie::new(
"login",
"this is totally not the expected payload"
Expand Down

0 comments on commit 852d16c

Please sign in to comment.