From 33132697f2b2088208119f3f3aa7641ecaf978d1 Mon Sep 17 00:00:00 2001 From: Max Hohlfeld Date: Sun, 20 Jul 2025 21:54:28 +0200 Subject: [PATCH] test: split test request and creation of test login user into separate functions --- web/src/endpoints/area/delete.rs | 18 ++++++--- web/src/endpoints/area/get_edit.rs | 11 +++-- web/src/endpoints/area/get_new.rs | 9 +++-- web/src/endpoints/area/post_edit.rs | 11 +++-- web/src/endpoints/area/post_new.rs | 8 ++-- web/src/endpoints/assignment/delete.rs | 16 +++++--- web/src/endpoints/assignment/post_new.rs | 31 +++++++++----- .../endpoints/availability/get_overview.rs | 6 ++- web/src/endpoints/clothing/delete.rs | 29 +++++++------- web/src/endpoints/clothing/get_edit.rs | 12 ++++-- web/src/endpoints/clothing/get_overview.rs | 11 +++-- web/src/endpoints/clothing/get_read.rs | 16 ++++---- web/src/endpoints/events/get_edit.rs | 7 ++-- web/src/endpoints/location/post_new.rs | 10 +++-- web/src/endpoints/user/get_changepassword.rs | 6 ++- web/src/endpoints/user/post_edit.rs | 12 ++++-- web/src/endpoints/user/put_lock.rs | 34 +++++++--------- .../user/put_receive_notifications.rs | 14 +++---- web/src/endpoints/vehicle/delete.rs | 30 ++++++-------- web/src/endpoints/vehicle/get_edit.rs | 32 ++++++--------- web/src/endpoints/vehicle/get_new.rs | 32 ++++++--------- web/src/endpoints/vehicle/get_overview.rs | 16 ++++---- web/src/endpoints/vehicle/post_edit.rs | 11 +++-- web/src/endpoints/vehicle/post_new.rs | 8 ++-- web/src/utils/test_helper/mod.rs | 9 ++++- web/src/utils/test_helper/test_requests.rs | 40 +++++++------------ 26 files changed, 233 insertions(+), 206 deletions(-) diff --git a/web/src/endpoints/area/delete.rs b/web/src/endpoints/area/delete.rs index 70e50724..64d054db 100644 --- a/web/src/endpoints/area/delete.rs +++ b/web/src/endpoints/area/delete.rs @@ -25,7 +25,9 @@ pub async fn delete( #[cfg(test)] mod tests { - use crate::utils::test_helper::{test_delete, DbTestContext, RequestConfig, StatusCode}; + use crate::utils::test_helper::{ + create_test_login_user, test_delete, DbTestContext, RequestConfig, StatusCode, + }; use brass_db::models::{Area, Function, Location, Role}; use brass_macros::db_test; @@ -46,7 +48,8 @@ mod tests { function: vec![Function::Posten], user_area: 1, }; - let response = test_delete(&context.db_pool, app, &config).await; + create_test_login_user(&context.db_pool, &config).await; + let response = test_delete(app, &config).await; assert_eq!(StatusCode::OK, response.status()); assert!(Area::read_by_id(&context.db_pool, 2) @@ -58,8 +61,9 @@ mod tests { #[db_test] async fn returns_unauthorized_when_user_is_not_admin(context: &DbTestContext) { let app = context.app().await; - let response = - test_delete(&context.db_pool, app, &RequestConfig::new("/area/delete/1")).await; + let config = RequestConfig::new("/area/delete/1"); + create_test_login_user(&context.db_pool, &config).await; + let response = test_delete(app, &config).await; assert_eq!(StatusCode::UNAUTHORIZED, response.status()); } @@ -73,7 +77,8 @@ mod tests { function: vec![Function::Posten], user_area: 1, }; - let response = test_delete(&context.db_pool, app, &config).await; + create_test_login_user(&context.db_pool, &config).await; + let response = test_delete(app, &config).await; assert_eq!(StatusCode::NOT_FOUND, response.status()); } @@ -95,7 +100,8 @@ mod tests { function: vec![Function::Posten], user_area: 1, }; - let response = test_delete(&context.db_pool, app, &config).await; + create_test_login_user(&context.db_pool, &config).await; + let response = test_delete(app, &config).await; assert_eq!(StatusCode::OK, response.status()); assert!(Area::read_by_id(&context.db_pool, 2) diff --git a/web/src/endpoints/area/get_edit.rs b/web/src/endpoints/area/get_edit.rs index a25c66c2..f631c5ef 100644 --- a/web/src/endpoints/area/get_edit.rs +++ b/web/src/endpoints/area/get_edit.rs @@ -36,7 +36,7 @@ mod tests { use brass_macros::db_test; use crate::utils::test_helper::{ - assert_snapshot, read_body, test_get, DbTestContext, RequestConfig, + assert_snapshot, create_test_login_user, read_body, test_get, DbTestContext, RequestConfig, }; #[db_test] @@ -44,7 +44,8 @@ mod tests { let app = context.app().await; let config = RequestConfig::new("/area/edit/1").with_role(Role::Admin); - let response = test_get(&context.db_pool, app, &config).await; + create_test_login_user(&context.db_pool, &config).await; + let response = test_get(app, &config).await; assert_eq!(StatusCode::OK, response.status()); @@ -57,7 +58,8 @@ mod tests { let app = context.app().await; let config = RequestConfig::new("/area/edit/1").with_role(Role::AreaManager); - let response = test_get(&context.db_pool, app, &config).await; + create_test_login_user(&context.db_pool, &config).await; + let response = test_get(app, &config).await; assert_eq!(StatusCode::UNAUTHORIZED, response.status()); } @@ -67,7 +69,8 @@ mod tests { let app = context.app().await; let config = RequestConfig::new("/area/edit/2").with_role(Role::Admin); - let response = test_get(&context.db_pool, app, &config).await; + create_test_login_user(&context.db_pool, &config).await; + let response = test_get(app, &config).await; assert_eq!(StatusCode::NOT_FOUND, response.status()); } diff --git a/web/src/endpoints/area/get_new.rs b/web/src/endpoints/area/get_new.rs index 4842364a..d525befb 100644 --- a/web/src/endpoints/area/get_new.rs +++ b/web/src/endpoints/area/get_new.rs @@ -22,7 +22,8 @@ async fn get(user: web::ReqData) -> Result(&context.db_pool, app, &config, None).await; + let response = test_post::<_, _, String>(app, &config, None).await; let (status, body) = response.into_status_and_body().await; assert_eq!(StatusCode::OK, status, "{body}"); @@ -159,8 +160,9 @@ mod tests { let config = RequestConfig::new("/assignments/new?availability=1&function=1&event=1") .with_role(Role::Admin); + create_test_login_user(&context.db_pool, &config).await; - let response = test_post::<_, _, String>(&context.db_pool, app, &config, None).await; + let response = test_post::<_, _, String>(app, &config, None).await; assert_eq!(StatusCode::UNPROCESSABLE_ENTITY, response.status()); } @@ -176,8 +178,9 @@ mod tests { let config = RequestConfig::new("/assignments/new?availability=1&function=1&event=1") .with_role(Role::Admin); + create_test_login_user(&context.db_pool, &config).await; - let response = test_post::<_, _, String>(&context.db_pool, app, &config, None).await; + let response = test_post::<_, _, String>(app, &config, None).await; assert_eq!(StatusCode::NOT_FOUND, response.status()); } @@ -199,8 +202,9 @@ mod tests { let config = RequestConfig::new("/assignments/new?availability=1&function=1&event=1") .with_role(Role::AreaManager) .with_user_area(2); + create_test_login_user(&context.db_pool, &config).await; - let response = test_post::<_, _, String>(&context.db_pool, app, &config, None).await; + let response = test_post::<_, _, String>(app, &config, None).await; assert_eq!(StatusCode::UNPROCESSABLE_ENTITY, response.status()); } @@ -224,8 +228,9 @@ mod tests { let config = RequestConfig::new("/assignments/new?availability=1&function=1&event=1") .with_role(Role::Admin); + create_test_login_user(&context.db_pool, &config).await; - let response = test_post::<_, _, String>(&context.db_pool, app, &config, None).await; + let response = test_post::<_, _, String>(app, &config, None).await; assert_eq!(StatusCode::UNPROCESSABLE_ENTITY, response.status()); } @@ -245,8 +250,9 @@ mod tests { let config = RequestConfig::new("/assignments/new?availability=1&function=1&event=1") .with_role(Role::Admin); + create_test_login_user(&context.db_pool, &config).await; - let response = test_post::<_, _, String>(&context.db_pool, app, &config, None).await; + let response = test_post::<_, _, String>(app, &config, None).await; assert_eq!(StatusCode::UNPROCESSABLE_ENTITY, response.status()); } @@ -276,8 +282,9 @@ mod tests { let config = RequestConfig::new("/assignments/new?availability=1&function=1&event=2") .with_role(Role::Admin); + create_test_login_user(&context.db_pool, &config).await; - let response = test_post::<_, _, String>(&context.db_pool, app, &config, None).await; + let response = test_post::<_, _, String>(app, &config, None).await; assert_eq!(StatusCode::UNPROCESSABLE_ENTITY, response.status()); } @@ -295,8 +302,9 @@ mod tests { let config = RequestConfig::new("/assignments/new?availability=1&function=5&event=1") .with_role(Role::Admin); + create_test_login_user(&context.db_pool, &config).await; - let response = test_post::<_, _, String>(&context.db_pool, app, &config, None).await; + let response = test_post::<_, _, String>(app, &config, None).await; assert_eq!(StatusCode::UNPROCESSABLE_ENTITY, response.status()); } @@ -357,8 +365,9 @@ mod tests { let config = RequestConfig::new("/assignments/new?availability=2&function=5&event=1") .with_role(Role::Admin); + create_test_login_user(&context.db_pool, &config).await; - let response = test_post::<_, _, String>(&context.db_pool, app, &config, None).await; + let response = test_post::<_, _, String>(app, &config, None).await; assert_eq!(StatusCode::UNPROCESSABLE_ENTITY, response.status()); } diff --git a/web/src/endpoints/availability/get_overview.rs b/web/src/endpoints/availability/get_overview.rs index c928a7d6..765935cd 100644 --- a/web/src/endpoints/availability/get_overview.rs +++ b/web/src/endpoints/availability/get_overview.rs @@ -75,7 +75,8 @@ mod tests { use brass_macros::db_test; use crate::utils::test_helper::{ - assert_snapshot, test_get, DbTestContext, RequestConfig, ServiceResponseExt, + assert_snapshot, create_test_login_user, test_get, DbTestContext, RequestConfig, + ServiceResponseExt, }; #[db_test] @@ -83,7 +84,8 @@ mod tests { let app = context.app().await; let config = RequestConfig::new("/"); - let response = test_get(&context.db_pool, app, &config).await; + create_test_login_user(&context.db_pool, &config).await; + let response = test_get(app, &config).await; let (status, body) = response.into_status_and_body().await; diff --git a/web/src/endpoints/clothing/delete.rs b/web/src/endpoints/clothing/delete.rs index 97b4a911..3e32b012 100644 --- a/web/src/endpoints/clothing/delete.rs +++ b/web/src/endpoints/clothing/delete.rs @@ -25,7 +25,9 @@ pub async fn delete( #[cfg(test)] mod tests { - use crate::utils::test_helper::{test_delete, DbTestContext, RequestConfig, StatusCode}; + use crate::utils::test_helper::{ + create_test_login_user, test_delete, DbTestContext, RequestConfig, StatusCode, + }; use brass_db::models::{Clothing, Role}; use brass_macros::db_test; @@ -39,8 +41,9 @@ mod tests { assert_eq!(2, Clothing::read_all(&context.db_pool).await.unwrap().len()); let config = RequestConfig::new("/clothing/1").with_role(Role::Admin); + create_test_login_user(&context.db_pool, &config).await; - let response = test_delete(&context.db_pool, app, &config).await; + let response = test_delete(app, &config).await; assert_eq!(StatusCode::OK, response.status()); assert_eq!(1, Clothing::read_all(&context.db_pool).await.unwrap().len()); @@ -50,7 +53,9 @@ mod tests { #[db_test] async fn returns_unauthorized_when_user_is_user(context: &DbTestContext) { let app = context.app().await; - let response = test_delete(&context.db_pool, app, &RequestConfig::new("/clothing/1")).await; + let config = RequestConfig::new("/clothing/1"); + create_test_login_user(&context.db_pool, &config).await; + let response = test_delete(app, &config).await; assert_eq!(StatusCode::UNAUTHORIZED, response.status()); } @@ -58,12 +63,9 @@ mod tests { #[db_test] async fn returns_unauthorized_when_user_is_area_manager(context: &DbTestContext) { let app = context.app().await; - let response = test_delete( - &context.db_pool, - app, - &RequestConfig::new("/clothing/1").with_role(Role::AreaManager), - ) - .await; + let config = RequestConfig::new("/clothing/1").with_role(Role::AreaManager); + create_test_login_user(&context.db_pool, &config).await; + let response = test_delete(app, &config).await; assert_eq!(StatusCode::UNAUTHORIZED, response.status()); } @@ -71,12 +73,9 @@ mod tests { #[db_test] async fn returns_not_found_when_clothing_does_not_exist(context: &DbTestContext) { let app = context.app().await; - let response = test_delete( - &context.db_pool, - app, - &RequestConfig::new("/clothing/100").with_role(Role::Admin), - ) - .await; + let config = RequestConfig::new("/clothing/100").with_role(Role::Admin); + create_test_login_user(&context.db_pool, &config).await; + let response = test_delete(app, &config).await; assert_eq!(StatusCode::NOT_FOUND, response.status()); } diff --git a/web/src/endpoints/clothing/get_edit.rs b/web/src/endpoints/clothing/get_edit.rs index eadc83dd..4942e9f5 100644 --- a/web/src/endpoints/clothing/get_edit.rs +++ b/web/src/endpoints/clothing/get_edit.rs @@ -32,7 +32,8 @@ pub async fn get( #[cfg(test)] mod tests { use crate::utils::test_helper::{ - assert_snapshot, read_body, test_get, DbTestContext, RequestConfig, StatusCode, + assert_snapshot, create_test_login_user, read_body, test_get, DbTestContext, RequestConfig, + StatusCode, }; use brass_db::models::{Clothing, Role}; use brass_macros::db_test; @@ -46,8 +47,9 @@ mod tests { let app = context.app().await; let config = RequestConfig::new("/clothing/edit/1"); + create_test_login_user(&context.db_pool, &config).await; - let response = test_get(&context.db_pool, &app, &config).await; + let response = test_get(&app, &config).await; assert_eq!(StatusCode::UNAUTHORIZED, response.status()); } @@ -60,8 +62,9 @@ mod tests { let app = context.app().await; let config = RequestConfig::new("/clothing/edit/1").with_role(Role::AreaManager); + create_test_login_user(&context.db_pool, &config).await; - let response = test_get(&context.db_pool, &app, &config).await; + let response = test_get(&app, &config).await; assert_eq!(StatusCode::UNAUTHORIZED, response.status()); } @@ -73,8 +76,9 @@ mod tests { .unwrap(); let config = RequestConfig::new("/clothing/edit/1").with_role(Role::Admin); + create_test_login_user(&context.db_pool, &config).await; - let response = test_get(&context.db_pool, &app, &config).await; + let response = test_get(&app, &config).await; assert_eq!(StatusCode::OK, response.status()); let body = read_body(response).await; diff --git a/web/src/endpoints/clothing/get_overview.rs b/web/src/endpoints/clothing/get_overview.rs index a98c8b87..86f19628 100644 --- a/web/src/endpoints/clothing/get_overview.rs +++ b/web/src/endpoints/clothing/get_overview.rs @@ -39,7 +39,7 @@ pub async fn get( #[cfg(test)] mod tests { use crate::utils::test_helper::{ - assert_snapshot, read_body, test_get, DbTestContext, RequestConfig, StatusCode, + assert_snapshot, create_test_login_user, read_body, test_get, DbTestContext, RequestConfig, StatusCode }; use brass_db::models::{Clothing, Role}; use brass_macros::db_test; @@ -49,8 +49,9 @@ mod tests { let app = context.app().await; let config = RequestConfig::new("/clothing"); + create_test_login_user(&context.db_pool, &config).await; - let response = test_get(&context.db_pool, &app, &config).await; + let response = test_get(&app, &config).await; assert_eq!(StatusCode::UNAUTHORIZED, response.status()); } @@ -59,8 +60,9 @@ mod tests { let app = context.app().await; let config = RequestConfig::new("/clothing").with_role(Role::AreaManager); + create_test_login_user(&context.db_pool, &config).await; - let response = test_get(&context.db_pool, &app, &config).await; + let response = test_get(&app, &config).await; assert_eq!(StatusCode::UNAUTHORIZED, response.status()); } @@ -72,8 +74,9 @@ mod tests { .unwrap(); let config = RequestConfig::new("/clothing").with_role(Role::Admin); + create_test_login_user(&context.db_pool, &config).await; - let response = test_get(&context.db_pool, &app, &config).await; + let response = test_get(&app, &config).await; assert_eq!(StatusCode::OK, response.status()); let body = read_body(response).await; diff --git a/web/src/endpoints/clothing/get_read.rs b/web/src/endpoints/clothing/get_read.rs index 596963c0..7f08fe0c 100644 --- a/web/src/endpoints/clothing/get_read.rs +++ b/web/src/endpoints/clothing/get_read.rs @@ -29,7 +29,8 @@ pub async fn get( #[cfg(test)] mod tests { use crate::utils::test_helper::{ - assert_snapshot, read_body, test_get, DbTestContext, RequestConfig, StatusCode, + assert_snapshot, create_test_login_user, test_get, DbTestContext, RequestConfig, + ServiceResponseExt, StatusCode, }; use brass_db::models::{Clothing, Role}; use brass_macros::db_test; @@ -43,8 +44,9 @@ mod tests { let app = context.app().await; let config = RequestConfig::new("/clothing/1"); + create_test_login_user(&context.db_pool, &config).await; - let response = test_get(&context.db_pool, &app, &config).await; + let response = test_get(&app, &config).await; assert_eq!(StatusCode::UNAUTHORIZED, response.status()); } @@ -57,8 +59,9 @@ mod tests { let app = context.app().await; let config = RequestConfig::new("/clothing/1").with_role(Role::AreaManager); + create_test_login_user(&context.db_pool, &config).await; - let response = test_get(&context.db_pool, &app, &config).await; + let response = test_get(&app, &config).await; assert_eq!(StatusCode::UNAUTHORIZED, response.status()); } @@ -70,11 +73,10 @@ mod tests { .unwrap(); let config = RequestConfig::new("/clothing/1").with_role(Role::Admin); + create_test_login_user(&context.db_pool, &config).await; - let response = test_get(&context.db_pool, &app, &config).await; - assert_eq!(StatusCode::OK, response.status()); - - let body = read_body(response).await; + let (status, body) = test_get(&app, &config).await.into_status_and_body().await; + assert_eq!(StatusCode::OK, status); assert_snapshot!(body); } } diff --git a/web/src/endpoints/events/get_edit.rs b/web/src/endpoints/events/get_edit.rs index c7d45a34..1bda15e7 100644 --- a/web/src/endpoints/events/get_edit.rs +++ b/web/src/endpoints/events/get_edit.rs @@ -69,8 +69,8 @@ mod tests { use chrono::NaiveDateTime; use crate::utils::test_helper::{ - assert_snapshot, test_get, DbTestContext, NaiveDateTimeExt, RequestConfig, - ServiceResponseExt, StatusCode, + assert_snapshot, create_test_login_user, test_get, DbTestContext, NaiveDateTimeExt, + RequestConfig, ServiceResponseExt, StatusCode, }; #[db_test] @@ -96,7 +96,8 @@ mod tests { Event::create(&context.db_pool, changeset).await.unwrap(); let config = RequestConfig::new("/events/1/edit").with_role(Role::Admin); - let response = test_get(&context.db_pool, context.app().await, &config).await; + create_test_login_user(&context.db_pool, &config).await; + let response = test_get(context.app().await, &config).await; let (status, body) = response.into_status_and_body().await; assert_eq!(StatusCode::OK, status); diff --git a/web/src/endpoints/location/post_new.rs b/web/src/endpoints/location/post_new.rs index f9b74f0c..8a9748d1 100644 --- a/web/src/endpoints/location/post_new.rs +++ b/web/src/endpoints/location/post_new.rs @@ -32,7 +32,9 @@ pub async fn post( mod tests { use crate::{ endpoints::location::LocationForm, - utils::test_helper::{test_post, DbTestContext, RequestConfig, StatusCode}, + utils::test_helper::{ + create_test_login_user, test_post, DbTestContext, RequestConfig, StatusCode, + }, }; use brass_db::models::{Function, Location, Role}; use brass_macros::db_test; @@ -46,13 +48,14 @@ mod tests { function: vec![Function::Posten], user_area: 1, }; + create_test_login_user(&context.db_pool, &config).await; let form = LocationForm { name: "Hauptbahnhof".to_string(), area: Some(1), }; - let response = test_post(&context.db_pool, app, &config, Some(form)).await; + let response = test_post(app, &config, Some(form)).await; assert_eq!(StatusCode::FOUND, response.status()); assert_eq!( @@ -74,13 +77,14 @@ mod tests { function: vec![Function::Posten], user_area: 1, }; + create_test_login_user(&context.db_pool, &config).await; let form = LocationForm { name: "Hauptbahnhof".to_string(), area: None, }; - let response = test_post(&context.db_pool, app, &config, Some(form)).await; + let response = test_post(app, &config, Some(form)).await; assert_eq!(StatusCode::FOUND, response.status()); assert_eq!( diff --git a/web/src/endpoints/user/get_changepassword.rs b/web/src/endpoints/user/get_changepassword.rs index 7322d665..65dee5b6 100644 --- a/web/src/endpoints/user/get_changepassword.rs +++ b/web/src/endpoints/user/get_changepassword.rs @@ -22,14 +22,16 @@ mod tests { use brass_macros::db_test; use crate::utils::test_helper::{ - assert_snapshot, test_get, DbTestContext, RequestConfig, ServiceResponseExt, + assert_snapshot, create_test_login_user, test_get, DbTestContext, RequestConfig, + ServiceResponseExt, }; #[db_test] async fn produces_template_fine(context: &DbTestContext) { let config = RequestConfig::new("/users/changepassword"); + create_test_login_user(&context.db_pool, &config).await; - let response = test_get(&context.db_pool, context.app().await, &config).await; + let response = test_get(context.app().await, &config).await; let (status, body) = response.into_status_and_body().await; assert_eq!(StatusCode::OK, status); diff --git a/web/src/endpoints/user/post_edit.rs b/web/src/endpoints/user/post_edit.rs index 16b31edd..8d2cbf50 100644 --- a/web/src/endpoints/user/post_edit.rs +++ b/web/src/endpoints/user/post_edit.rs @@ -103,6 +103,7 @@ mod tests { function: vec![Function::Posten], user_area: 1, }; + create_test_login_user(&context.db_pool, &config).await; let new_name: String = Name().fake(); let new_mail: String = SafeEmail().fake(); @@ -117,7 +118,7 @@ mod tests { area: Some(2), }; - let response = test_post(&context.db_pool, app, &config, Some(form)).await; + let response = test_post(app, &config, Some(form)).await; assert_eq!(StatusCode::FOUND, response.status()); let updated_user = User::read_by_id(&context.db_pool, 1) @@ -141,6 +142,7 @@ mod tests { function: vec![Function::Posten], user_area: 1, }; + create_test_login_user(&context.db_pool, &config).await; let form = NewOrEditUserForm { name: "".to_string(), @@ -152,7 +154,7 @@ mod tests { area: Some(1), }; - let response = test_post(&context.db_pool, app, &config, Some(form)).await; + let response = test_post(app, &config, Some(form)).await; assert_eq!(StatusCode::BAD_REQUEST, response.status()); } @@ -162,6 +164,7 @@ mod tests { let app = context.app().await; let config = RequestConfig::new("/users/edit/1").with_role(Role::Admin); + create_test_login_user(&context.db_pool, &config).await; let new_name: String = Name().fake(); let new_mail: String = String::from("NONLowercaseEMAIL@example.com"); @@ -176,7 +179,7 @@ mod tests { area: Some(1), }; - let response = test_post(&context.db_pool, app, &config, Some(form)).await; + let response = test_post(app, &config, Some(form)).await; let (status, body) = response.into_status_and_body().await; debug!(body); @@ -198,6 +201,7 @@ mod tests { let app = context.app().await; let config = RequestConfig::new("/users/edit/1").with_role(Role::Admin); + create_test_login_user(&context.db_pool, &config).await; let second_user = User::read_by_id(&context.db_pool, 2) .await @@ -217,7 +221,7 @@ mod tests { area: Some(2), }; - let response = test_post(&context.db_pool, app, &config, Some(form)).await; + let response = test_post(app, &config, Some(form)).await; assert_eq!(StatusCode::UNPROCESSABLE_ENTITY, response.status()); } } diff --git a/web/src/endpoints/user/put_lock.rs b/web/src/endpoints/user/put_lock.rs index 88096280..ba7786f1 100644 --- a/web/src/endpoints/user/put_lock.rs +++ b/web/src/endpoints/user/put_lock.rs @@ -83,7 +83,8 @@ async fn handle_lock_state_for_user( #[cfg(test)] mod tests { use crate::utils::test_helper::{ - assert_snapshot, read_body, test_put, DbTestContext, RequestConfig, StatusCode, + assert_snapshot, create_test_login_user, read_body, test_put, DbTestContext, RequestConfig, + StatusCode, }; use brass_db::models::{Area, Function, Role, User}; use brass_macros::db_test; @@ -94,28 +95,18 @@ mod tests { let app = context.app().await; User::create(&context.db_pool, &Faker.fake()).await.unwrap(); - let lock_config = RequestConfig { - uri: "/users/1/lock".to_string(), - role: Role::Admin, - function: vec![Function::Posten], - user_area: 1, - }; - let lock_response = - test_put::<_, _, String>(&context.db_pool, &app, &lock_config, None).await; + let lock_config = RequestConfig::new("/users/1/lock").with_role(Role::Admin); + create_test_login_user(&context.db_pool, &lock_config).await; + + let lock_response = test_put::<_, _, String>(&app, &lock_config, None).await; assert_eq!(StatusCode::OK, lock_response.status()); let lock_body = read_body(lock_response).await; assert_snapshot!(lock_body); - let unlock_config = RequestConfig { - uri: "/users/1/unlock".to_string(), - role: Role::Admin, - function: vec![Function::Posten], - user_area: 1, - }; - let unlock_response = - test_put::<_, _, String>(&context.db_pool, &app, &unlock_config, None).await; + let unlock_config = RequestConfig::new("/users/1/unlock").with_role(Role::Admin); + let unlock_response = test_put::<_, _, String>(&app, &unlock_config, None).await; assert_eq!(StatusCode::OK, unlock_response.status()); @@ -135,8 +126,9 @@ mod tests { function: vec![Function::Posten], user_area: 2, }; + create_test_login_user(&context.db_pool, &config).await; - let response = test_put::<_, _, String>(&context.db_pool, &app, &config, None).await; + let response = test_put::<_, _, String>(&app, &config, None).await; assert_eq!(StatusCode::UNAUTHORIZED, response.status()) } @@ -151,8 +143,9 @@ mod tests { function: vec![Function::Posten], user_area: 1, }; + create_test_login_user(&context.db_pool, &config).await; - let response = test_put::<_, _, String>(&context.db_pool, &app, &config, None).await; + let response = test_put::<_, _, String>(&app, &config, None).await; assert_eq!(StatusCode::BAD_REQUEST, response.status()) } @@ -167,8 +160,9 @@ mod tests { function: vec![Function::Posten], user_area: 1, }; + create_test_login_user(&context.db_pool, &config).await; - let response = test_put::<_, _, String>(&context.db_pool, &app, &config, None).await; + let response = test_put::<_, _, String>(&app, &config, None).await; assert_eq!(StatusCode::NOT_FOUND, response.status()) } diff --git a/web/src/endpoints/user/put_receive_notifications.rs b/web/src/endpoints/user/put_receive_notifications.rs index 7c2853e9..9c694188 100644 --- a/web/src/endpoints/user/put_receive_notifications.rs +++ b/web/src/endpoints/user/put_receive_notifications.rs @@ -75,7 +75,8 @@ async fn handle_subscription_to_notifications( #[cfg(test)] mod tests { use crate::utils::test_helper::{ - assert_snapshot, read_body, test_put, DbTestContext, RequestConfig, StatusCode, + assert_snapshot, create_test_login_user, read_body, test_put, DbTestContext, RequestConfig, + StatusCode, }; use brass_macros::db_test; @@ -84,8 +85,8 @@ mod tests { let app = context.app().await; let unsubscribe_config = RequestConfig::new("/users/1/unsubscribeNotifications"); - let unsubscribe_response = - test_put::<_, _, String>(&context.db_pool, &app, &unsubscribe_config, None).await; + create_test_login_user(&context.db_pool, &unsubscribe_config).await; + let unsubscribe_response = test_put::<_, _, String>(&app, &unsubscribe_config, None).await; assert_eq!(StatusCode::OK, unsubscribe_response.status()); @@ -93,8 +94,7 @@ mod tests { assert_snapshot!(unsubscribe_body); let subscribe_config = RequestConfig::new("/users/1/subscribeNotifications"); - let subscribe_response = - test_put::<_, _, String>(&context.db_pool, &app, &subscribe_config, None).await; + let subscribe_response = test_put::<_, _, String>(&app, &subscribe_config, None).await; assert_eq!(StatusCode::OK, subscribe_response.status()); @@ -107,8 +107,8 @@ mod tests { let app = context.app().await; let unsubscribe_config = RequestConfig::new("/users/3/unsubscribeNotifications"); - let unsubscribe_response = - test_put::<_, _, String>(&context.db_pool, &app, &unsubscribe_config, None).await; + create_test_login_user(&context.db_pool, &unsubscribe_config).await; + let unsubscribe_response = test_put::<_, _, String>(&app, &unsubscribe_config, None).await; assert_eq!(StatusCode::UNAUTHORIZED, unsubscribe_response.status()); } diff --git a/web/src/endpoints/vehicle/delete.rs b/web/src/endpoints/vehicle/delete.rs index ca0a3c48..6f1a29f0 100644 --- a/web/src/endpoints/vehicle/delete.rs +++ b/web/src/endpoints/vehicle/delete.rs @@ -25,8 +25,10 @@ pub async fn delete( #[cfg(test)] mod tests { - use crate::utils::test_helper::{test_delete, DbTestContext, RequestConfig, StatusCode}; - use brass_db::models::{Function, Role, Vehicle}; + use crate::utils::test_helper::{ + create_test_login_user, test_delete, DbTestContext, RequestConfig, StatusCode, + }; + use brass_db::models::{Role, Vehicle}; use brass_macros::db_test; #[db_test] @@ -47,13 +49,9 @@ mod tests { assert!(Vehicle::read(&context.db_pool, 1).await.unwrap().is_some()); let app = context.app().await; - let config = RequestConfig { - uri: "/vehicles/1".to_string(), - role, - function: vec![Function::Posten], - user_area: 1, - }; - let response = test_delete(&context.db_pool, app, &config).await; + let config = RequestConfig::new("/vehicles/1").with_role(role); + create_test_login_user(&context.db_pool, &config).await; + let response = test_delete(app, &config).await; assert_eq!(StatusCode::OK, response.status()); assert!(Vehicle::read(&context.db_pool, 1).await.unwrap().is_none()); @@ -62,7 +60,9 @@ mod tests { #[db_test] async fn returns_unauthorized_when_user_is_staff(context: &DbTestContext) { let app = context.app().await; - let response = test_delete(&context.db_pool, app, &RequestConfig::new("/vehicles/1")).await; + let config = RequestConfig::new("/vehicles/1"); + create_test_login_user(&context.db_pool, &config).await; + let response = test_delete(app, &config).await; assert_eq!(StatusCode::UNAUTHORIZED, response.status()); } @@ -70,13 +70,9 @@ mod tests { #[db_test] async fn returns_not_found_when_vehicle_does_not_exist(context: &DbTestContext) { let app = context.app().await; - let config = RequestConfig { - uri: "/vehicles/1".to_string(), - role: Role::Admin, - function: vec![Function::Posten], - user_area: 1, - }; - let response = test_delete(&context.db_pool, app, &config).await; + let config = RequestConfig::new("/vehicles/1").with_role(Role::Admin); + create_test_login_user(&context.db_pool, &config).await; + let response = test_delete(app, &config).await; assert_eq!(StatusCode::NOT_FOUND, response.status()); } diff --git a/web/src/endpoints/vehicle/get_edit.rs b/web/src/endpoints/vehicle/get_edit.rs index d6be086e..318ed84e 100644 --- a/web/src/endpoints/vehicle/get_edit.rs +++ b/web/src/endpoints/vehicle/get_edit.rs @@ -32,9 +32,10 @@ pub async fn get( #[cfg(test)] mod tests { use crate::utils::test_helper::{ - assert_snapshot, read_body, test_get, DbTestContext, RequestConfig, StatusCode, + assert_snapshot, create_test_login_user, test_get, DbTestContext, RequestConfig, + ServiceResponseExt, StatusCode, }; - use brass_db::models::{Role, Function, Vehicle}; + use brass_db::models::{Role, Vehicle}; use brass_macros::db_test; #[db_test] @@ -46,8 +47,9 @@ mod tests { let app = context.app().await; let config = RequestConfig::new("/vehicles/1"); + create_test_login_user(&context.db_pool, &config).await; - let response = test_get(&context.db_pool, &app, &config).await; + let response = test_get(&app, &config).await; assert_eq!(StatusCode::UNAUTHORIZED, response.status()); } @@ -59,14 +61,10 @@ mod tests { let app = context.app().await; - let config = RequestConfig { - uri: "/vehicles/1".to_string(), - role: Role::AreaManager, - function: vec![Function::Posten], - user_area: 1, - }; + let config = RequestConfig::new("/vehicles/1").with_role(Role::AreaManager); + create_test_login_user(&context.db_pool, &config).await; - let response = test_get(&context.db_pool, &app, &config).await; + let response = test_get(&app, &config).await; assert_eq!(StatusCode::OK, response.status()); } @@ -77,17 +75,11 @@ mod tests { .await .unwrap(); - let config = RequestConfig { - uri: "/vehicles/1".to_string(), - role: Role::Admin, - function: vec![Function::Posten], - user_area: 1, - }; + let config = RequestConfig::new("/vehicles/1").with_role(Role::Admin); + create_test_login_user(&context.db_pool, &config).await; - let response = test_get(&context.db_pool, &app, &config).await; - assert_eq!(StatusCode::OK, response.status()); - - let body = read_body(response).await; + let (status, body) = test_get(&app, &config).await.into_status_and_body().await; + assert_eq!(StatusCode::OK, status); assert_snapshot!(body); } } diff --git a/web/src/endpoints/vehicle/get_new.rs b/web/src/endpoints/vehicle/get_new.rs index 44353a1e..37964ea2 100644 --- a/web/src/endpoints/vehicle/get_new.rs +++ b/web/src/endpoints/vehicle/get_new.rs @@ -23,9 +23,10 @@ pub async fn get(user: web::ReqData) -> Result (StatusCode, String); } -impl ServiceResponseExt for ServiceResponse where B: MessageBody { +impl ServiceResponseExt for ServiceResponse +where + B: MessageBody, +{ async fn into_status_and_body(self) -> (StatusCode, String) { let status = self.status(); let response = String::from_utf8(test::read_body(self).await.to_vec()).unwrap(); diff --git a/web/src/utils/test_helper/test_requests.rs b/web/src/utils/test_helper/test_requests.rs index e85bf346..6e8f8aee 100644 --- a/web/src/utils/test_helper/test_requests.rs +++ b/web/src/utils/test_helper/test_requests.rs @@ -9,7 +9,7 @@ use actix_web::{ }; use brass_db::models::{Function, Role, User}; use serde::Serialize; -use sqlx::{Pool, Postgres}; +use sqlx::PgPool; pub struct RequestConfig { pub uri: String, @@ -48,15 +48,7 @@ impl RequestConfig { } } -async fn create_user_and_get_login_cookie<'a, T, R>( - pool: &Pool, - app: &T, - config: &RequestConfig, -) -> Cookie<'a> -where - T: Service, Error = Error>, - R: MessageBody + 'a, -{ +pub async fn create_test_login_user(pool: &PgPool, config: &RequestConfig) { const HASH: &str = "$argon2id$v=19$m=19456,t=2,p=1$IPiLaPCFZOK69MA1a6GUzw$ZZinpbkP7pXhN7g7dGkh87kGTeuFd/2er1U+y+4IKWo"; const SALT: &str = "IPiLaPCFZOK69MA1a6GUzw"; @@ -72,7 +64,13 @@ where ) .await .unwrap(); +} +async fn perform_login_and_get_cookie<'a, T, R>(app: &T) -> Cookie<'a> +where + T: Service, Error = Error>, + R: MessageBody + 'a, +{ let login_form = LoginForm { email: "abc".to_string(), password: "abc".to_string(), @@ -95,16 +93,12 @@ where String::from_utf8(test::read_body(response).await.to_vec()).unwrap() } -pub async fn test_get( - pool: &Pool, - app: T, - config: &RequestConfig, -) -> ServiceResponse +pub async fn test_get(app: T, config: &RequestConfig) -> ServiceResponse where T: Service, Error = Error>, R: MessageBody, { - let cookie = create_user_and_get_login_cookie(pool, &app, &config).await; + let cookie = perform_login_and_get_cookie(&app).await; let get_request = test::TestRequest::get() .uri(&config.uri) @@ -115,7 +109,6 @@ where } pub async fn test_post( - pool: &Pool, app: T, config: &RequestConfig, form: Option, @@ -125,7 +118,7 @@ where R: MessageBody, F: Serialize, { - let cookie = create_user_and_get_login_cookie(pool, &app, config).await; + let cookie = perform_login_and_get_cookie(&app).await; let post_request = test::TestRequest::post() .uri(&config.uri) @@ -137,7 +130,6 @@ where } pub async fn test_put( - pool: &Pool, app: &T, config: &RequestConfig, form: Option, @@ -147,7 +139,7 @@ where R: MessageBody, F: Serialize, { - let cookie = create_user_and_get_login_cookie(pool, app, config).await; + let cookie = perform_login_and_get_cookie(&app).await; let put_request = test::TestRequest::put() .uri(&config.uri) @@ -158,16 +150,12 @@ where test::call_service(app, put_request).await } -pub async fn test_delete( - pool: &Pool, - app: T, - config: &RequestConfig, -) -> ServiceResponse +pub async fn test_delete(app: T, config: &RequestConfig) -> ServiceResponse where T: Service, Error = Error>, R: MessageBody, { - let cookie = create_user_and_get_login_cookie(pool, &app, config).await; + let cookie = perform_login_and_get_cookie(&app).await; let delete_request = test::TestRequest::delete() .uri(&config.uri)