test: split test request and creation of test login user into separate functions

This commit is contained in:
Max Hohlfeld 2025-07-20 21:54:28 +02:00
parent 50edbc9171
commit 33132697f2
26 changed files with 233 additions and 206 deletions

View File

@ -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)

View File

@ -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());
}

View File

@ -22,7 +22,8 @@ async fn get(user: web::ReqData<User>) -> Result<impl Responder, ApplicationErro
#[cfg(test)]
mod tests {
use crate::utils::test_helper::{
assert_snapshot, test_get, DbTestContext, RequestConfig, ServiceResponseExt, StatusCode,
assert_snapshot, create_test_login_user, test_get, DbTestContext, RequestConfig,
ServiceResponseExt, StatusCode,
};
use brass_db::models::Role;
use brass_macros::db_test;
@ -31,7 +32,8 @@ mod tests {
async fn produces_template_when_user_is_admin(context: &DbTestContext) {
let config = RequestConfig::new("/area/new").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);
@ -41,7 +43,8 @@ mod tests {
#[db_test]
async fn returns_unauthorized_when_user_is_not_admin(context: &DbTestContext) {
let config = RequestConfig::new("/area/new").with_role(Role::AreaManager);
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;
assert_eq!(StatusCode::UNAUTHORIZED, response.status());
}

View File

@ -37,7 +37,7 @@ mod tests {
use crate::{
endpoints::area::AreaForm,
utils::test_helper::{test_post, DbTestContext, RequestConfig},
utils::test_helper::{create_test_login_user, test_post, DbTestContext, RequestConfig},
};
#[db_test]
@ -50,12 +50,13 @@ mod tests {
function: vec![Function::Posten],
user_area: 1,
};
create_test_login_user(&context.db_pool, &config).await;
let request = AreaForm {
name: "Neuer Name".to_string(),
};
let response = test_post(&context.db_pool, app, &config, Some(request)).await;
let response = test_post(app, &config, Some(request)).await;
assert_eq!(StatusCode::FOUND, response.status());
@ -77,12 +78,13 @@ mod tests {
function: vec![Function::Posten],
user_area: 1,
};
create_test_login_user(&context.db_pool, &config).await;
let request = AreaForm {
name: "Neuer Name".to_string(),
};
let response = test_post(&context.db_pool, app, &config, Some(request)).await;
let response = test_post(app, &config, Some(request)).await;
assert_eq!(StatusCode::UNAUTHORIZED, response.status());
}
@ -97,12 +99,13 @@ mod tests {
function: vec![Function::Posten],
user_area: 1,
};
create_test_login_user(&context.db_pool, &config).await;
let request = AreaForm {
name: "Neuer Name".to_string(),
};
let response = test_post(&context.db_pool, app, &config, Some(request)).await;
let response = test_post(app, &config, Some(request)).await;
assert_eq!(StatusCode::NOT_FOUND, response.status());
}

View File

@ -30,7 +30,7 @@ mod tests {
use crate::{
endpoints::area::AreaForm,
utils::test_helper::{test_post, DbTestContext, RequestConfig},
utils::test_helper::{create_test_login_user, test_post, DbTestContext, RequestConfig},
};
#[db_test]
@ -43,12 +43,13 @@ mod tests {
function: vec![Function::Posten],
user_area: 1,
};
create_test_login_user(&context.db_pool, &config).await;
let request = AreaForm {
name: "Neuer Name".to_string(),
};
let response = test_post(&context.db_pool, app, &config, Some(request)).await;
let response = test_post(app, &config, Some(request)).await;
assert_eq!(StatusCode::FOUND, response.status());
@ -70,12 +71,13 @@ mod tests {
function: vec![Function::Posten],
user_area: 1,
};
create_test_login_user(&context.db_pool, &config).await;
let request = AreaForm {
name: "Neuer Name".to_string(),
};
let response = test_post(&context.db_pool, app, &config, Some(request)).await;
let response = test_post(app, &config, Some(request)).await;
assert_eq!(StatusCode::UNAUTHORIZED, response.status());
}

View File

@ -71,7 +71,9 @@ mod tests {
use fake::{faker::chrono::en::Date, Fake, Faker};
use sqlx::PgPool;
use crate::utils::test_helper::{test_delete, DbTestContext, RequestConfig};
use crate::utils::test_helper::{
create_test_login_user, test_delete, DbTestContext, RequestConfig,
};
use brass_db::models::{
Assignment, AssignmentChangeset, Availability, AvailabilityChangeset, Event,
EventChangeset, Function, Location, Role, User,
@ -118,8 +120,9 @@ mod tests {
function: vec![Function::Posten],
user_area: 1,
};
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!(
@ -142,8 +145,9 @@ mod tests {
function: vec![Function::Posten],
user_area: 1,
};
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::UNAUTHORIZED, response.status());
}
@ -157,8 +161,9 @@ mod tests {
function: vec![Function::Posten],
user_area: 1,
};
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::NOT_FOUND, response.status());
}
@ -174,8 +179,9 @@ mod tests {
function: vec![Function::Posten],
user_area: 1,
};
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::NOT_FOUND, response.status());
}

View File

@ -91,8 +91,8 @@ mod tests {
use sqlx::PgPool;
use crate::utils::test_helper::{
assert_snapshot, test_post, DbTestContext, NaiveDateTimeExt, RequestConfig,
ServiceResponseExt,
assert_snapshot, create_test_login_user, test_post, DbTestContext, NaiveDateTimeExt,
RequestConfig, ServiceResponseExt,
};
async fn arrange(pool: &PgPool) {
@ -139,8 +139,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;
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());
}

View File

@ -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;

View File

@ -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());
}

View File

@ -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;

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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!(

View File

@ -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);

View File

@ -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());
}
}

View File

@ -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())
}

View File

@ -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());
}

View File

@ -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());
}

View File

@ -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);
}
}

View File

@ -23,9 +23,10 @@ pub async fn get(user: web::ReqData<User>) -> Result<impl Responder, Application
#[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::{Function, Role};
use brass_db::models::Role;
use brass_macros::db_test;
#[db_test]
@ -33,8 +34,9 @@ mod tests {
let app = context.app().await;
let config = RequestConfig::new("/vehicles/new");
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());
}
@ -42,14 +44,10 @@ mod tests {
async fn area_manager_can_edit(context: &DbTestContext) {
let app = context.app().await;
let config = RequestConfig {
uri: "/vehicles/new".to_string(),
role: Role::AreaManager,
function: vec![Function::Posten],
user_area: 1,
};
let config = RequestConfig::new("/vehicles/new").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());
}
@ -57,17 +55,11 @@ mod tests {
async fn produces_template_fine_when_user_is_admin(context: &DbTestContext) {
let app = context.app().await;
let config = RequestConfig {
uri: "/vehicles/new".to_string(),
role: Role::Admin,
function: vec![Function::Posten],
user_area: 1,
};
let config = RequestConfig::new("/vehicles/new").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);
}
}

View File

@ -39,7 +39,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::{Role, Vehicle};
use brass_macros::db_test;
@ -49,8 +50,9 @@ mod tests {
let app = context.app().await;
let config = RequestConfig::new("/vehicles");
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 +61,9 @@ mod tests {
let app = context.app().await;
let config = RequestConfig::new("/vehicles").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());
}
@ -72,11 +75,10 @@ mod tests {
.unwrap();
let config = RequestConfig::new("/vehicles").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);
}
}

View File

@ -46,7 +46,7 @@ mod tests {
use crate::{
endpoints::vehicle::VehicleForm,
utils::test_helper::{test_post, DbTestContext, RequestConfig},
utils::test_helper::{create_test_login_user, test_post, DbTestContext, RequestConfig},
};
#[db_test]
@ -67,13 +67,14 @@ mod tests {
let app = context.app().await;
let config = RequestConfig::new("/vehicles/1").with_role(role);
create_test_login_user(&context.db_pool, &config).await;
let request = VehicleForm {
station: "FF Leipzig Ost".to_string(),
radio_call_name: "11.49.2".to_string(),
};
let response = test_post(&context.db_pool, app, &config, Some(request)).await;
let response = test_post(app, &config, Some(request)).await;
assert_eq!(StatusCode::FOUND, response.status());
@ -87,13 +88,14 @@ mod tests {
let app = context.app().await;
let config = RequestConfig::new("/vehicles/1");
create_test_login_user(&context.db_pool, &config).await;
let request = VehicleForm {
station: "FF Leipzig Ost".to_string(),
radio_call_name: "11.49.2".to_string(),
};
let response = test_post(&context.db_pool, app, &config, Some(request)).await;
let response = test_post(app, &config, Some(request)).await;
assert_eq!(StatusCode::UNAUTHORIZED, response.status());
}
@ -103,13 +105,14 @@ mod tests {
let app = context.app().await;
let config = RequestConfig::new("/vehicles/1").with_role(Role::Admin);
create_test_login_user(&context.db_pool, &config).await;
let request = VehicleForm {
station: "FF Leipzig Ost".to_string(),
radio_call_name: "11.49.2".to_string(),
};
let response = test_post(&context.db_pool, app, &config, Some(request)).await;
let response = test_post(app, &config, Some(request)).await;
assert_eq!(StatusCode::NOT_FOUND, response.status());
}

View File

@ -30,7 +30,7 @@ mod tests {
use crate::{
endpoints::vehicle::VehicleForm,
utils::test_helper::{test_post, DbTestContext, RequestConfig},
utils::test_helper::{create_test_login_user, test_post, DbTestContext, RequestConfig},
};
#[db_test]
@ -47,13 +47,14 @@ mod tests {
let app = context.app().await;
let config = RequestConfig::new("/vehicles/new").with_role(role);
create_test_login_user(&context.db_pool, &config).await;
let request = VehicleForm {
station: "FF Leipzig Ost".to_string(),
radio_call_name: "11.49.1".to_string(),
};
let response = test_post(&context.db_pool, app, &config, Some(request)).await;
let response = test_post(app, &config, Some(request)).await;
assert_eq!(StatusCode::FOUND, response.status());
@ -67,13 +68,14 @@ mod tests {
let app = context.app().await;
let config = RequestConfig::new("/vehicles/new");
create_test_login_user(&context.db_pool, &config).await;
let request = VehicleForm {
station: "FF Leipzig Ost".to_string(),
radio_call_name: "11.49.2".to_string(),
};
let response = test_post(&context.db_pool, app, &config, Some(request)).await;
let response = test_post(app, &config, Some(request)).await;
assert_eq!(StatusCode::UNAUTHORIZED, response.status());
}

View File

@ -6,7 +6,9 @@ use actix_web::test;
use chrono::{NaiveDate, NaiveDateTime};
pub use test_context::{setup, teardown, DbTestContext};
pub use test_requests::RequestConfig;
pub use test_requests::{read_body, test_delete, test_get, test_post, test_put};
pub use test_requests::{
create_test_login_user, read_body, test_delete, test_get, test_post, test_put,
};
pub use actix_http::StatusCode;
@ -60,7 +62,10 @@ pub trait ServiceResponseExt {
async fn into_status_and_body(self) -> (StatusCode, String);
}
impl<B> ServiceResponseExt for ServiceResponse<B> where B: MessageBody {
impl<B> ServiceResponseExt for ServiceResponse<B>
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();

View File

@ -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<Postgres>,
app: &T,
config: &RequestConfig,
) -> Cookie<'a>
where
T: Service<Request, Response = ServiceResponse<R>, 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<Request, Response = ServiceResponse<R>, 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<T, R>(
pool: &Pool<Postgres>,
app: T,
config: &RequestConfig,
) -> ServiceResponse<R>
pub async fn test_get<T, R>(app: T, config: &RequestConfig) -> ServiceResponse<R>
where
T: Service<Request, Response = ServiceResponse<R>, 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<T, R, F>(
pool: &Pool<Postgres>,
app: T,
config: &RequestConfig,
form: Option<F>,
@ -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<T, R, F>(
pool: &Pool<Postgres>,
app: &T,
config: &RequestConfig,
form: Option<F>,
@ -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<T, R>(
pool: &Pool<Postgres>,
app: T,
config: &RequestConfig,
) -> ServiceResponse<R>
pub async fn test_delete<T, R>(app: T, config: &RequestConfig) -> ServiceResponse<R>
where
T: Service<Request, Response = ServiceResponse<R>, 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)