test: assignment deletion

This commit is contained in:
Max Hohlfeld 2025-04-08 10:27:20 +02:00
parent 6ee3ca9e89
commit ab4ff439f4
4 changed files with 138 additions and 4 deletions

View File

@ -62,3 +62,120 @@ pub async fn delete(
Ok(template.to_response()?) Ok(template.to_response()?)
} }
#[cfg(test)]
mod tests {
use actix_http::StatusCode;
use brass_macros::db_test;
use chrono::{NaiveDate, NaiveTime};
use fake::{faker::chrono::en::Date, Fake, Faker};
use sqlx::PgPool;
use crate::{
models::{
Assignment, AssignmentChangeset, Availability, AvailabilityChangeset, AvailabilityTime,
Event, EventChangeset, Function, Location, Role, User,
},
utils::test_helper::{test_delete, DbTestContext, RequestConfig},
};
async fn arrange(pool: &PgPool) -> anyhow::Result<()> {
Location::create(pool, "Location", 1).await?;
User::create(pool, Faker.fake()).await?;
let date: NaiveDate = Date().fake();
let start = NaiveTime::from_hms_opt(10, 0, 0).unwrap();
let end = NaiveTime::from_hms_opt(15, 30, 0).unwrap();
let new_event = EventChangeset::create_for_test(date.clone(), start.clone(), end.clone());
Event::create(pool, new_event).await?;
let new_availability = AvailabilityChangeset {
time: AvailabilityTime::WholeDay,
comment: None,
};
Availability::create(pool, 1, date, new_availability).await?;
let new_assignment = AssignmentChangeset {
function: Function::Posten,
time: (start, end),
};
Assignment::create(pool, 1, 1, new_assignment).await?;
Ok(())
}
#[db_test]
async fn works_if_assignment_exists_and_actor_is_area_manager(context: &DbTestContext) {
arrange(&context.db_pool).await.unwrap();
let app = context.app().await;
let config = RequestConfig {
uri: "/assignments/delete?availabillity=1&event=1".to_string(),
role: Role::AreaManager,
function: vec![Function::Posten],
user_area: 1,
};
let response = test_delete(&context.db_pool, app, &config).await;
assert_eq!(StatusCode::OK, response.status());
assert_eq!(
0,
Assignment::read_all_by_event(&context.db_pool, 1)
.await
.unwrap()
.len()
)
}
#[db_test]
async fn doesnt_work_when_actor_is_staff(context: &DbTestContext) {
arrange(&context.db_pool).await.unwrap();
let app = context.app().await;
let config = RequestConfig {
uri: "/assignments/delete?availabillity=1&event=1".to_string(),
role: Role::Staff,
function: vec![Function::Posten],
user_area: 1,
};
let response = test_delete(&context.db_pool, app, &config).await;
assert_eq!(StatusCode::UNAUTHORIZED, response.status());
}
#[db_test]
async fn doesnt_work_when_event_does_not_exist(context: &DbTestContext) {
let app = context.app().await;
let config = RequestConfig {
uri: "/assignments/delete?availabillity=1&event=1".to_string(),
role: Role::AreaManager,
function: vec![Function::Posten],
user_area: 1,
};
let response = test_delete(&context.db_pool, app, &config).await;
assert_eq!(StatusCode::NOT_FOUND, response.status());
}
#[db_test]
async fn doesnt_work_when_assignment_does_not_exist(context: &DbTestContext) {
arrange(&context.db_pool).await.unwrap();
let app = context.app().await;
let config = RequestConfig {
uri: "/assignments/delete?availabillity=2&event=1".to_string(),
role: Role::AreaManager,
function: vec![Function::Posten],
user_area: 1,
};
let response = test_delete(&context.db_pool, app, &config).await;
assert_eq!(StatusCode::NOT_FOUND, response.status());
}
}

View File

@ -1,5 +1,7 @@
use chrono::NaiveDate; use chrono::NaiveDate;
use chrono::NaiveTime; use chrono::NaiveTime;
#[cfg(test)]
use fake::{Faker, Fake};
use garde::Validate; use garde::Validate;
use super::start_time_lies_before_end_time; use super::start_time_lies_before_end_time;
@ -28,6 +30,25 @@ pub struct EventChangeset {
pub note: Option<String>, pub note: Option<String>,
} }
#[cfg(test)]
impl EventChangeset {
pub fn create_for_test(date: NaiveDate, start: NaiveTime, end: NaiveTime) -> EventChangeset {
let changeset = EventChangeset {
date,
time: (start, end),
name: Faker.fake(),
location_id: 1,
voluntary_wachhabender: true,
voluntary_fuehrungsassistent: true,
amount_of_posten: 5,
clothing: "Tuchuniform".to_string(),
note: None,
};
changeset
}
}
pub struct EventContext { pub struct EventContext {
pub date_in_db: NaiveDate, pub date_in_db: NaiveDate,
pub common_min_max_available_time: (NaiveTime, NaiveTime), pub common_min_max_available_time: (NaiveTime, NaiveTime),

View File

@ -85,6 +85,4 @@
</div> </div>
</section> </section>
<script>
</script>
{% endblock %} {% endblock %}

View File

@ -73,6 +73,4 @@
</div> </div>
</section> </section>
<script>
</script>
{% endblock %} {% endblock %}