add test for cancel-event-notification
This commit is contained in:
@ -7,7 +7,7 @@ use sqlx::{FromRow, Sqlite, SqlitePool, Transaction};
|
||||
|
||||
use super::{role::Role, user::User};
|
||||
|
||||
#[derive(FromRow, Debug, Serialize, Deserialize)]
|
||||
#[derive(FromRow, Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct Notification {
|
||||
pub id: i64,
|
||||
pub user_id: i64,
|
||||
@ -153,7 +153,7 @@ ORDER BY read_at DESC, created_at DESC;
|
||||
}
|
||||
}
|
||||
// Cox read notification about cancelled event
|
||||
let re = Regex::new(r"^remove_trip_by_planned_event:(\d+)$").unwrap();
|
||||
let re = Regex::new(r"^remove_trip_by_event:(\d+)$").unwrap();
|
||||
if let Some(caps) = re.captures(action) {
|
||||
if let Some(matched) = caps.get(1) {
|
||||
if let Ok(number) = matched.as_str().parse::<i32>() {
|
||||
@ -180,3 +180,114 @@ ORDER BY read_at DESC, created_at DESC;
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{
|
||||
model::{
|
||||
event::{Event, EventUpdate, Registration},
|
||||
notification::Notification,
|
||||
trip::Trip,
|
||||
tripdetails::{TripDetails, TripDetailsToAdd},
|
||||
user::{CoxUser, User},
|
||||
usertrip::UserTrip,
|
||||
},
|
||||
testdb,
|
||||
};
|
||||
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
#[sqlx::test]
|
||||
fn event_canceled() {
|
||||
let pool = testdb!();
|
||||
|
||||
// Create event
|
||||
let add_tripdetails = TripDetailsToAdd {
|
||||
planned_starting_time: "10:00",
|
||||
max_people: 4,
|
||||
day: "1970-02-01".into(),
|
||||
notes: None,
|
||||
trip_type: None,
|
||||
allow_guests: false,
|
||||
always_show: false,
|
||||
};
|
||||
let tripdetails_id = TripDetails::create(&pool, add_tripdetails).await;
|
||||
let trip_details = TripDetails::find_by_id(&pool, tripdetails_id)
|
||||
.await
|
||||
.unwrap();
|
||||
Event::create(&pool, "new-event".into(), 2, &trip_details).await;
|
||||
let event = Event::find_by_trip_details(&pool, trip_details.id)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// Rower + Cox joins
|
||||
let rower = User::find_by_name(&pool, "rower").await.unwrap();
|
||||
UserTrip::create(&pool, &rower, &trip_details, None)
|
||||
.await
|
||||
.unwrap();
|
||||
let cox = CoxUser::new(&pool, User::find_by_name(&pool, "cox").await.unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
Trip::new_join(&pool, &cox, &event).await.unwrap();
|
||||
|
||||
// Cancel Event
|
||||
let cancel_update = EventUpdate {
|
||||
name: &event.name,
|
||||
planned_amount_cox: event.planned_amount_cox as i32,
|
||||
max_people: 0,
|
||||
notes: event.notes.as_deref(),
|
||||
always_show: event.always_show,
|
||||
is_locked: event.is_locked,
|
||||
};
|
||||
event.update(&pool, &cancel_update).await;
|
||||
|
||||
// Rower received notification
|
||||
let notifications = Notification::for_user(&pool, &rower).await;
|
||||
let rower_notification = notifications[0].clone();
|
||||
assert_eq!(rower_notification.category, "Absage Ausfahrt");
|
||||
assert_eq!(
|
||||
rower_notification.action_after_reading.as_deref(),
|
||||
Some("remove_user_trip_with_trip_details_id:3")
|
||||
);
|
||||
|
||||
// Cox received notification
|
||||
let notifications = Notification::for_user(&pool, &cox.user).await;
|
||||
let cox_notification = notifications[0].clone();
|
||||
assert_eq!(cox_notification.category, "Absage Ausfahrt");
|
||||
assert_eq!(
|
||||
cox_notification.action_after_reading.as_deref(),
|
||||
Some("remove_trip_by_event:2")
|
||||
);
|
||||
|
||||
// Notification removed if cancellation is cancelled
|
||||
let update = EventUpdate {
|
||||
name: &event.name,
|
||||
planned_amount_cox: event.planned_amount_cox as i32,
|
||||
max_people: 3,
|
||||
notes: event.notes.as_deref(),
|
||||
always_show: event.always_show,
|
||||
is_locked: event.is_locked,
|
||||
};
|
||||
event.update(&pool, &update).await;
|
||||
assert!(Notification::for_user(&pool, &rower).await.is_empty());
|
||||
assert!(Notification::for_user(&pool, &cox.user).await.is_empty());
|
||||
|
||||
// Cancel event again
|
||||
event.update(&pool, &cancel_update).await;
|
||||
|
||||
// Rower is removed if notification is accepted
|
||||
assert!(event.is_rower_registered(&pool, &rower).await);
|
||||
rower_notification.mark_read(&pool).await;
|
||||
assert!(!event.is_rower_registered(&pool, &rower).await);
|
||||
|
||||
// Cox is removed if notification is accepted
|
||||
let registration = Registration::all_cox(&pool, event.id).await;
|
||||
assert_eq!(registration.len(), 1);
|
||||
assert_eq!(registration[0].name, "cox");
|
||||
|
||||
cox_notification.mark_read(&pool).await;
|
||||
|
||||
let registration = Registration::all_cox(&pool, event.id).await;
|
||||
assert!(registration.is_empty());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user