From 3259582aab639b7e90ec63bcc35241c79d693447 Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 23 Jul 2023 14:21:27 +0200 Subject: [PATCH] create new tests for /cox/trip/ --- README.md | 2 +- src/model/trip.rs | 4 +- src/tera/cox.rs | 114 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2a16a77..0e33590 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ user_details - [x] (join) GET /join/ - [x] (remove) GET /remove/ - [x] (create) POST /cox/trip -- [ ] (update) POST /cox/trip/ +- [x] (update) POST /cox/trip/ - [ ] (join) GET /cox/join/ - [ ] (remove) GET /cox/remove/ - [ ] (remove_trip) GET /cox/remove/trip/ diff --git a/src/model/trip.rs b/src/model/trip.rs index 339798a..9df89f8 100644 --- a/src/model/trip.rs +++ b/src/model/trip.rs @@ -16,9 +16,9 @@ pub struct Trip { cox_name: String, trip_details_id: Option, planned_starting_time: String, - max_people: i64, + pub max_people: i64, day: String, - notes: Option, + pub notes: Option, pub allow_guests: bool, trip_type_id: Option, } diff --git a/src/tera/cox.rs b/src/tera/cox.rs index bbc6ed0..fb2237f 100644 --- a/src/tera/cox.rs +++ b/src/tera/cox.rs @@ -218,4 +218,118 @@ mod test { .len() ); } + + #[sqlx::test] + fn test_trip_update_succ() { + let db = testdb!(); + + let trip = &Trip::get_for_day(&db, NaiveDate::from_ymd_opt(1970, 01, 02).unwrap()).await[0]; + assert_eq!(1, trip.trip.max_people); + assert_eq!( + "trip_details for trip from cox", + &trip.trip.notes.clone().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=cox&password=cox"); // Add the form data to the request body; + login.dispatch().await; + + let req = client + .post("/cox/trip/1") + .header(ContentType::Form) + .body("notes=my-new-notes&max_people=12"); + 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:successAusfahrt erfolgreich aktualisiert." + ); + + let trip = &Trip::get_for_day(&db, NaiveDate::from_ymd_opt(1970, 01, 02).unwrap()).await[0]; + assert_eq!(12, trip.trip.max_people); + assert_eq!("my-new-notes", &trip.trip.notes.clone().unwrap()); + } + + #[sqlx::test] + fn test_trip_update_wrong_event() { + 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=cox&password=cox"); // Add the form data to the request body; + login.dispatch().await; + + let req = client + .post("/cox/trip/9999") + .header(ContentType::Form) + .body("notes=my-new-notes&max_people=12"); + 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:errorAusfahrt gibt's nicht"); + } + + #[sqlx::test] + fn test_trip_update_wrong_cox() { + let db = testdb!(); + + let trip = &Trip::get_for_day(&db, NaiveDate::from_ymd_opt(1970, 01, 02).unwrap()).await[0]; + assert_eq!(1, trip.trip.max_people); + assert_eq!( + "trip_details for trip from cox", + &trip.trip.notes.clone().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=cox2&password=cox"); // Add the form data to the request body; + login.dispatch().await; + + let req = client + .post("/cox/trip/1") + .header(ContentType::Form) + .body("notes=my-new-notes&max_people=12"); + 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:errorNicht deine Ausfahrt!"); + } }