From 3730940aedf34bfe7a376b74dc93c002bafa9d67 Mon Sep 17 00:00:00 2001 From: philipp Date: Mon, 31 Jul 2023 20:09:03 +0200 Subject: [PATCH] add tests for tera/log.rs Closes #34 --- src/model/logbook.rs | 4 +- src/tera/admin/boat.rs | 3 +- src/tera/log.rs | 162 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 164 insertions(+), 5 deletions(-) diff --git a/src/model/logbook.rs b/src/model/logbook.rs index b3048b1..9781476 100644 --- a/src/model/logbook.rs +++ b/src/model/logbook.rs @@ -347,9 +347,9 @@ mod test { fn test_on_water() { let pool = testdb!(); let logbook = Logbook::find_by_id(&pool, 1).await.unwrap(); - let logbookWithDetails = Logbook::on_water(&pool).await; + let logbook_with_details = Logbook::on_water(&pool).await; - assert_eq!(logbookWithDetails[0].logbook, logbook); + assert_eq!(logbook_with_details[0].logbook, logbook); } #[sqlx::test] diff --git a/src/tera/admin/boat.rs b/src/tera/admin/boat.rs index 447f28f..d57cf19 100644 --- a/src/tera/admin/boat.rs +++ b/src/tera/admin/boat.rs @@ -86,7 +86,6 @@ pub fn routes() -> Vec { #[cfg(test)] mod test { - use chrono::NaiveDate; use rocket::{ http::{ContentType, Status}, local::asynchronous::Client, @@ -94,7 +93,7 @@ mod test { use sqlx::SqlitePool; use crate::tera::admin::boat::Boat; - use crate::{model::trip::Trip, testdb}; + use crate::testdb; #[sqlx::test] fn test_boat_index() { diff --git a/src/tera/log.rs b/src/tera/log.rs index 1eacdff..1b1bbef 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -220,4 +220,164 @@ pub fn routes() -> Vec { } #[cfg(test)] -mod test {} +mod test { + use rocket::http::ContentType; + use rocket::{http::Status, local::asynchronous::Client}; + use sqlx::SqlitePool; + + use crate::testdb; + + #[sqlx::test] + fn test_kiosk_cookie() { + let db = testdb!(); + + let rocket = rocket::build().manage(db.clone()); + let rocket = crate::tera::config(rocket); + + let client = Client::tracked(rocket).await.unwrap(); + let req = client.get("/log"); + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::SeeOther); + assert_eq!(response.headers().get("Location").next(), Some("/auth")); + + let req = client.get("/log/kiosk/ekrv2019"); + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::SeeOther); + assert_eq!(response.headers().get("Location").next(), Some("/log")); + + let req = client.get("/log"); + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::Ok); + let text = response.into_string().await.unwrap(); + assert!(text.contains("Logbuch")); + assert!(text.contains("Neue Ausfahrt")); + } + + #[sqlx::test] + fn test_index() { + 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("/log"); + let response = req.dispatch().await; + + let text = response.into_string().await.unwrap(); + assert!(text.contains("Logbuch")); + assert!(text.contains("Neue Ausfahrt")); + } + + #[sqlx::test] + fn test_show() { + 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("/log/show"); + let response = req.dispatch().await; + + let text = response.into_string().await.unwrap(); + assert!(text.contains("Logbuch")); + assert!(text.contains("Bootsname: Joe")); + } + + #[sqlx::test] + fn test_show_kiosk() { + let db = testdb!(); + + let rocket = rocket::build().manage(db.clone()); + let rocket = crate::tera::config(rocket); + + let client = Client::tracked(rocket).await.unwrap(); + let req = client.get("/log/kiosk/ekrv2019"); + let response = req.dispatch().await; + + let req = client.get("/log/show"); + let response = req.dispatch().await; + + let text = response.into_string().await.unwrap(); + assert!(text.contains("Logbuch")); + assert!(text.contains("Bootsname: Joe")); + } + + #[sqlx::test] + fn test_create() { + 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 + .post("/log") + .header(ContentType::Form) + .body("boat_id=1&shipmaster=4&departure=2199-12-31T10:00"); + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::SeeOther); + assert_eq!(response.headers().get("Location").next(), Some("/log")); + + let flash_cookie = response + .cookies() + .get("_flash") + .expect("Expected flash cookie"); + + assert_eq!( + flash_cookie.value(), + "7:successAusfahrt erfolgreich hinzugefĆ¼gt" + ); + } + + #[sqlx::test] + fn test_home_kiosk() { + let db = testdb!(); + + let rocket = rocket::build().manage(db.clone()); + let rocket = crate::tera::config(rocket); + + let client = Client::tracked(rocket).await.unwrap(); + let req = client.get("/log/kiosk/ekrv2019"); + let response = req.dispatch().await; + + let req = client + .post("/log/1") + .header(ContentType::Form) + .body("destination=Ottensheim&distance_in_km=25"); + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::SeeOther); + assert_eq!(response.headers().get("Location").next(), Some("/log")); + + let flash_cookie = response + .cookies() + .get("_flash") + .expect("Expected flash cookie"); + + assert_eq!(flash_cookie.value(), "7:successSuccessfully updated log"); + } +}