diff --git a/README.md b/README.md index c488205..d7e1002 100644 --- a/README.md +++ b/README.md @@ -91,12 +91,12 @@ user_details # Missing backend tests -- [ ] (index) GET / -- [ ] (faq) GET /faq -- [ ] (cal) GET /cal -- [ ] (FileServer: svelte/build) GET / -- [ ] (join) GET /join/ -- [ ] (remove) GET /remove/ +- [x] (index) GET / +- [x] (faq) GET /faq +- [x] (cal) GET /cal +- [x] (FileServer: svelte/build) GET / +- [x] (join) GET /join/ +- [x] (remove) GET /remove/ - [x] (create) POST /cox/trip - [ ] (update) POST /cox/trip/ - [ ] (join) GET /cox/join/ diff --git a/src/tera/misc.rs b/src/tera/misc.rs index 7190750..d449390 100644 --- a/src/tera/misc.rs +++ b/src/tera/misc.rs @@ -11,9 +11,59 @@ async fn faq(user: User) -> Template { #[get("/cal")] async fn cal(db: &State) -> (ContentType, String) { + //TODO: add unit test once proper functionality is there (ContentType::Calendar, PlannedEvent::get_ics_feed(db).await) } pub fn routes() -> Vec { routes![faq, cal] } + +#[cfg(test)] +mod test { + use rocket::{ + http::{ContentType, Status}, + local::asynchronous::Client, + }; + use sqlx::SqlitePool; + + use crate::testdb; + + #[sqlx::test] + fn test_faq() { + 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("/faq"); + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::Ok); + + assert!(response.into_string().await.unwrap().contains("FAQs")); + } + + #[sqlx::test] + fn test_without_login() { + 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("/"); + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::SeeOther); + assert_eq!(response.headers().get("Location").next(), Some("/auth")); + } +} diff --git a/src/tera/mod.rs b/src/tera/mod.rs index 7a4e6bc..fff78e4 100644 --- a/src/tera/mod.rs +++ b/src/tera/mod.rs @@ -123,27 +123,120 @@ pub fn config(rocket: Rocket) -> Rocket { .attach(AdHoc::config::()) } -//#[cfg(test)] -//mod test { -// use crate::testdb; -// -// use super::start; -// use rocket::http::Status; -// use rocket::local::asynchronous::Client; -// use rocket::uri; -// use sqlx::SqlitePool; -// -// #[sqlx::test] -// fn test_not_logged_in() { -// let pool = testdb!(); -// -// let client = Client::tracked(start(pool)) -// .await -// .expect("valid rocket instance"); -// let response = client.get(uri!(super::index)).dispatch().await; -// -// assert_eq!(response.status(), Status::SeeOther); -// let location = response.headers().get("Location").next().unwrap(); -// assert_eq!(location, "/auth"); -// } -//} +#[cfg(test)] +mod test { + use rocket::{ + http::{ContentType, Status}, + local::asynchronous::Client, + }; + use sqlx::SqlitePool; + + use crate::testdb; + + #[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=cox&password=cox"); // Add the form data to the request body; + login.dispatch().await; + + let req = client.get("/"); + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::Ok); + + assert!(response.into_string().await.unwrap().contains("Ausfahrten")); + } + + #[sqlx::test] + fn test_without_login() { + 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("/"); + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::SeeOther); + assert_eq!(response.headers().get("Location").next(), Some("/auth")); + } + + #[sqlx::test] + fn test_join_and_remove() { + 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=rower&password=rower"); // Add the form data to the request body; + login.dispatch().await; + + let req = client.get("/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:successErfolgreich angemeldet!"); + + let req = client.get("/remove/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:successErfolgreich abgemeldet!"); + } + + #[sqlx::test] + fn test_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=rower&password=rower"); // Add the form data to the request body; + login.dispatch().await; + + let req = client.get("/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:errorTrip_details do not exist."); + } +}