complete tests for logbook

This commit is contained in:
2023-10-01 15:53:45 +02:00
parent 6d0501d3b0
commit 02e1546f0e
3 changed files with 282 additions and 30 deletions

View File

@ -20,7 +20,7 @@ use crate::model::{
LogbookUpdateError,
},
logtype::LogType,
user::{AdminUser, User, UserWithWaterStatus},
user::{User, UserWithWaterStatus},
};
pub struct KioskCookie(String);
@ -139,10 +139,11 @@ async fn kiosk(
Template::render("kiosk", context.into_json())
}
async fn create_logbook(db: &SqlitePool, data: Form<LogToAdd>) -> Flash<Redirect> {
async fn create_logbook(db: &SqlitePool, data: Form<LogToAdd>, user: &User) -> Flash<Redirect> {
match Logbook::create(
db,
data.into_inner()
data.into_inner(),
user
)
.await
{
@ -158,17 +159,14 @@ async fn create_logbook(db: &SqlitePool, data: Form<LogToAdd>) -> Flash<Redirect
Err(LogbookCreateError::ArrivalSetButNoDistance) => Flash::error(Redirect::to("/log"), format!("Distanz notwendig, wenn Ankunftszeit angegeben wurde")),
Err(LogbookCreateError::ArrivalSetButNoDestination) => Flash::error(Redirect::to("/log"), format!("Ziel notwendig, wenn Ankunftszeit angegeben wurde")),
Err(LogbookCreateError::ArrivalNotAfterDeparture) => Flash::error(Redirect::to("/log"), format!("Ankunftszeit kann nicht vor der Abfahrtszeit sein")),
Err(LogbookCreateError::UserNotAllowedToUseBoat) => Flash::error(Redirect::to("/log"), format!("Schiffsführer darf dieses Boot nicht verwenden")),
}
}
#[post("/", data = "<data>", rank = 2)]
async fn create(
db: &State<SqlitePool>,
data: Form<LogToAdd>,
_adminuser: AdminUser,
) -> Flash<Redirect> {
create_logbook(db, data).await
async fn create(db: &State<SqlitePool>, data: Form<LogToAdd>, user: User) -> Flash<Redirect> {
create_logbook(db, data, &user).await
}
#[post("/", data = "<data>")]
@ -177,7 +175,8 @@ async fn create_kiosk(
data: Form<LogToAdd>,
_kiosk: KioskCookie,
) -> Flash<Redirect> {
create_logbook(db, data).await
let creator = User::find_by_id(db, data.shipmaster as i32).await.unwrap();
create_logbook(db, data, &creator).await
}
async fn home_logbook(
@ -498,6 +497,11 @@ mod test {
let rocket = rocket::build().manage(db.clone());
let rocket = crate::tera::config(rocket);
sqlx::query("DELETE FROM logbook;")
.execute(&db)
.await
.unwrap();
let mut client = Client::tracked(rocket).await.unwrap();
let req = client.get("/log/kiosk/ekrv2019/Linz");
let _ = req.dispatch().await;
@ -517,11 +521,81 @@ mod test {
&db,
&mut client,
"second_private_boat_from_rower".into(),
"admin".into(),
"rower".into(),
)
.await;
}
#[sqlx::test]
fn test_shipowner_can_allow_others_to_drive() {
let db = testdb!();
let rocket = rocket::build().manage(db.clone());
let rocket = crate::tera::config(rocket);
sqlx::query("DELETE FROM logbook;")
.execute(&db)
.await
.unwrap();
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;
// Owner can start trip:
let boat_id = Boat::find_by_name(&db, "private_boat_from_rower".into())
.await
.unwrap()
.id;
let shipmaster_id = User::find_by_name(&db, "rower2".into()).await.unwrap().id;
let req = client.post("/log").header(ContentType::Form).body(format!(
"boat_id={boat_id}&shipmaster={shipmaster_id}&departure=1199-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"
);
// Shipmaster can end it
let log_id = Logbook::highest_id(&db).await;
let login = client
.post("/auth")
.header(ContentType::Form) // Set the content type to form
.body("name=rower2&password=rower"); // Add the form data to the request body;
login.dispatch().await;
let req = client
.post(format!("/log/{log_id}"))
.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");
}
#[sqlx::test]
fn test_normal_user_sees_appropriate_boats() {
let db = testdb!();
@ -529,7 +603,7 @@ mod test {
let rocket = rocket::build().manage(db.clone());
let rocket = crate::tera::config(rocket);
let client = Client::tracked(rocket).await.unwrap();
let mut client = Client::tracked(rocket).await.unwrap();
let login = client
.post("/auth")
.header(ContentType::Form) // Set the content type to form
@ -541,16 +615,176 @@ mod test {
let text = response.into_string().await.unwrap();
sqlx::query("DELETE FROM logbook;")
.execute(&db)
.await
.unwrap();
//Sees all 1x
assert!(text.contains("Haichenbach"));
can_start_and_end_trip(&db, &mut client, "Haichenbach".into(), "rower".into()).await;
assert!(text.contains("private_boat_from_rower"));
can_start_and_end_trip(
&db,
&mut client,
"private_boat_from_rower".into(),
"rower".into(),
)
.await;
assert!(text.contains("second_private_boat_from_rower"));
can_start_and_end_trip(
&db,
&mut client,
"second_private_boat_from_rower".into(),
"rower".into(),
)
.await;
//Don't see anything else
assert!(!text.contains("Joe"));
cant_start_trip(
&db,
&mut client,
"Joe".into(),
"rower".into(),
"Schiffsführer darf dieses Boot nicht verwenden".into(),
)
.await;
assert!(!text.contains("Kaputtes Boot :-("));
cant_start_trip(
&db,
&mut client,
"Kaputtes Boot :-(".into(),
"rower".into(),
"Schiffsführer darf dieses Boot nicht verwenden".into(),
)
.await;
assert!(!text.contains("Sehr kaputtes Boot :-(("));
cant_start_trip(
&db,
&mut client,
"Sehr kaputtes Boot :-((".into(),
"rower".into(),
"Boot gesperrt".into(),
)
.await;
assert!(!text.contains("Ottensheim Boot"));
cant_start_trip(
&db,
&mut client,
"Ottensheim Boot".into(),
"rower".into(),
"Schiffsführer darf dieses Boot nicht verwenden".into(),
)
.await;
}
#[sqlx::test]
fn test_cox_sees_appropriate_boats() {
let db = testdb!();
let rocket = rocket::build().manage(db.clone());
let rocket = crate::tera::config(rocket);
let mut 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;
sqlx::query("DELETE FROM logbook;")
.execute(&db)
.await
.unwrap();
let req = client.get("/log");
let response = req.dispatch().await;
let text = response.into_string().await.unwrap();
//Sees all 1x
assert!(text.contains("Haichenbach"));
can_start_and_end_trip(&db, &mut client, "Haichenbach".into(), "cox".into()).await;
assert!(text.contains("Joe"));
can_start_and_end_trip(&db, &mut client, "Joe".into(), "cox".into()).await;
assert!(text.contains("Kaputtes Boot :-("));
can_start_and_end_trip(&db, &mut client, "Kaputtes Boot :-(".into(), "cox".into()).await;
assert!(text.contains("Sehr kaputtes Boot :-(("));
cant_start_trip(
&db,
&mut client,
"Sehr kaputtes Boot :-((".into(),
"cox".into(),
"Boot gesperrt".into(),
)
.await;
assert!(text.contains("Ottensheim Boot"));
can_start_and_end_trip(&db, &mut client, "Ottensheim Boot".into(), "cox".into()).await;
//Can't use private boats
assert!(!text.contains("private_boat_from_rower"));
cant_start_trip(
&db,
&mut client,
"private_boat_from_rower".into(),
"cox".into(),
"Schiffsführer darf dieses Boot nicht verwenden".into(),
)
.await;
assert!(!text.contains("second_private_boat_from_rower"));
cant_start_trip(
&db,
&mut client,
"second_private_boat_from_rower".into(),
"cox".into(),
"Schiffsführer darf dieses Boot nicht verwenden".into(),
)
.await;
}
#[sqlx::test]
fn test_cant_end_trip_other_user() {
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=rower2&password=rower"); // Add the form data to the request body;
login.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(),
"5:errorLogbook with ID 1 could not be updated!"
);
}
async fn can_start_and_end_trip(
@ -559,7 +793,6 @@ mod test {
boat_name: String,
shipmaster_name: String,
) {
println!("{boat_name}");
let boat_id = Boat::find_by_name(db, boat_name).await.unwrap().id;
let shipmaster_id = User::find_by_name(db, &shipmaster_name).await.unwrap().id;
@ -576,7 +809,6 @@ mod test {
.get("_flash")
.expect("Expected flash cookie");
println!("{shipmaster_id}");
assert_eq!(
flash_cookie.value(),
"7:successAusfahrt erfolgreich hinzugefügt"
@ -599,14 +831,6 @@ mod test {
.expect("Expected flash cookie");
assert_eq!(flash_cookie.value(), "7:successSuccessfully updated log");
//TODO: Remove the following query?
//sqlx::query(&format!(
// "DELETE FROM logbook WHERE boat_id={boat_id} AND shipmaster={shipmaster_id}"
//))
//.execute(db)
//.await
//.unwrap();
}
async fn cant_start_trip(