use rocket::{ catch, catchers, fairing::AdHoc, fs::FileServer, get, request::FlashMessage, response::{Flash, Redirect}, routes, Build, Rocket, State, }; use rocket_dyn_templates::{tera::Context, Template}; use serde::Deserialize; use sqlx::SqlitePool; use crate::model::{ log::Log, tripdetails::TripDetails, triptype::TripType, user::User, usertrip::{UserTrip, UserTripError}, }; mod admin; mod auth; mod cox; mod log; mod misc; mod stat; #[get("/")] async fn index(db: &State, user: User, flash: Option>) -> Template { let mut context = Context::new(); if user.is_cox || user.is_admin { let triptypes = TripType::all(db).await; context.insert("trip_types", &triptypes); } let days = user.get_days(db).await; if let Some(msg) = flash { context.insert("flash", &msg.into_inner()); } context.insert("loggedin_user", &user); context.insert("days", &days); Template::render("index", context.into_json()) } #[get("/join/")] async fn join(db: &State, trip_details_id: i64, user: User) -> Flash { let Some(trip_details) = TripDetails::find_by_id(db, trip_details_id).await else { return Flash::error(Redirect::to("/"), "Trip_details do not exist.") }; match UserTrip::create(db, &user, &trip_details).await { Ok(_) => { Log::create( db, format!( "User {} registered for trip_details.id={}", user.name, trip_details_id ), ) .await; Flash::success(Redirect::to("/"), "Erfolgreich angemeldet!") } Err(UserTripError::EventAlreadyFull) => { Flash::error(Redirect::to("/"), "Event bereits ausgebucht!") } Err(UserTripError::AlreadyRegistered) => { Flash::error(Redirect::to("/"), "Du nimmst bereits teil!") } Err(UserTripError::AlreadyRegisteredAsCox) => { Flash::error(Redirect::to("/"), "Du hilfst bereits als Steuerperson aus!") } Err(UserTripError::CantRegisterAtOwnEvent) => Flash::error( Redirect::to("/"), "Du kannst bei einer selbst ausgeschriebenen Fahrt nicht mitrudern ;)", ), Err(UserTripError::GuestNotAllowedForThisEvent) => Flash::error( Redirect::to("/"), "Bei dieser Ausfahrt können leider keine Gäste mitfahren.", ), } } #[get("/remove/")] async fn remove(db: &State, trip_details_id: i64, user: User) -> Flash { let Some(trip_details) = TripDetails::find_by_id(db, trip_details_id).await else { return Flash::error(Redirect::to("/"), "TripDetailsId does not exist"); }; UserTrip::delete(db, &user, &trip_details).await; Log::create( db, format!( "User {} unregistered for trip_details.id={}", user.name, trip_details_id ), ) .await; Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!") } #[catch(401)] //unauthorized fn unauthorized_error() -> Redirect { Redirect::to("/auth") } #[derive(Deserialize)] #[serde(crate = "rocket::serde")] pub struct Config { rss_key: String, } pub fn config(rocket: Rocket) -> Rocket { rocket .mount("/", routes![index, join, remove]) .mount("/auth", auth::routes()) .mount("/log", log::routes()) .mount("/stat", stat::routes()) .mount("/cox", cox::routes()) .mount("/admin", admin::routes()) .mount("/", misc::routes()) .mount("/public", FileServer::from("static/")) .register("/", catchers![unauthorized_error]) .attach(Template::fairing()) .attach(AdHoc::config::()) } #[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."); } }