From c43feb183448ae38782c65b2fae3ce5e192d867a Mon Sep 17 00:00:00 2001 From: philipp Date: Mon, 24 Jul 2023 09:40:28 +0200 Subject: [PATCH] add test --- README.md | 2 +- src/tera/cox.rs | 103 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index be65ef9..585df13 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ user_details - [x] (remove) GET /remove/ - [x] (create) POST /cox/trip - [x] (update) POST /cox/trip/ -- [ ] (join) GET /cox/join/ +- [x] (join) GET /cox/join/ - [ ] (remove) GET /cox/remove/ - [ ] (remove_trip) GET /cox/remove/trip/ - [ ] (index) GET /auth/ diff --git a/src/tera/cox.rs b/src/tera/cox.rs index 96f1772..8d2a54f 100644 --- a/src/tera/cox.rs +++ b/src/tera/cox.rs @@ -345,4 +345,107 @@ mod test { assert_eq!(flash_cookie.value(), "5:errorNicht deine Ausfahrt!"); } + + #[sqlx::test] + fn test_trip_join() { + 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.get("/cox/join/1"); + 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:successDanke für's helfen!"); + + let req = client.get("/cox/join/1"); + 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:errorDu hilfst bereits aus!"); + } + + #[sqlx::test] + fn test_trip_join_already_rower() { + 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.get("/join/1"); + let response = req.dispatch().await; + + let req = client.get("/cox/join/1"); + 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:errorDu hast dich bereits als Ruderer angemeldet!" + ); + } + + #[sqlx::test] + fn test_trip_join_invalid_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.get("/cox/join/9999"); + 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:errorEvent gibt's nicht"); + } }