diff --git a/src/model/planned_event.rs b/src/model/planned_event.rs index 51cd58e..d411744 100644 --- a/src/model/planned_event.rs +++ b/src/model/planned_event.rs @@ -10,7 +10,7 @@ use sqlx::{FromRow, SqlitePool}; use super::{tripdetails::TripDetails, triptype::TripType, user::User}; -#[derive(Serialize, Clone, FromRow)] +#[derive(Serialize, Clone, FromRow, Debug, PartialEq)] pub struct PlannedEvent { pub id: i64, pub name: String, diff --git a/src/tera/admin/planned_event.rs b/src/tera/admin/planned_event.rs index 54e9604..bbd7684 100644 --- a/src/tera/admin/planned_event.rs +++ b/src/tera/admin/planned_event.rs @@ -87,3 +87,81 @@ async fn delete(db: &State, id: i64, _admin: AdminUser) -> Flash Vec { routes![create, delete, update] } + +#[cfg(test)] +mod test { + use rocket::{ + http::{ContentType, Status}, + local::asynchronous::Client, + }; + use sqlx::SqlitePool; + + use super::*; + use crate::testdb; + + #[sqlx::test] + fn test_delete() { + let db = testdb!(); + + let _ = PlannedEvent::find_by_id(&db, 1).await.unwrap(); + + let rocket = rocket::build().manage(db.clone()); + let rocket = crate::tera::config(rocket); + + let client = Client::tracked(rocket).await.unwrap(); + let login = client + .post("/auth") + .header(ContentType::Form) // Set the content type to form + .body("name=admin&password=admin"); // Add the form data to the request body; + login.dispatch().await; + + let req = client.get("/admin/planned-event/1/delete"); + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::SeeOther); + assert_eq!(response.headers().get("Location").next(), Some("/")); + + let flash_cookie = response + .cookies() + .get("_flash") + .expect("Expected flash cookie"); + + assert_eq!( + flash_cookie.value(), + "7:successSuccessfully deleted the event" + ); + + let event = PlannedEvent::find_by_id(&db, 1).await; + assert_eq!(event, None); + } + + #[sqlx::test] + fn test_delete_invalid_id() { + let db = testdb!(); + + let rocket = rocket::build().manage(db.clone()); + let rocket = crate::tera::config(rocket); + + let client = Client::tracked(rocket).await.unwrap(); + let login = client + .post("/auth") + .header(ContentType::Form) // Set the content type to form + .body("name=admin&password=admin"); // Add the form data to the request body; + login.dispatch().await; + + let req = client.get("/admin/planned-event/1337/delete"); + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::SeeOther); + assert_eq!(response.headers().get("Location").next(), Some("/")); + + let flash_cookie = response + .cookies() + .get("_flash") + .expect("Expected flash cookie"); + + assert_eq!(flash_cookie.value(), "5:errorPlannedEvent does not exist"); + + let _ = PlannedEvent::find_by_id(&db, 1).await.unwrap(); + } +}