forked from Ruderverein-Donau-Linz/rowt
move todos to gitlab; write tests for /cox/remove/<id>; Closes #9
This commit is contained in:
@ -154,7 +154,7 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i
|
||||
.await
|
||||
.unwrap(); //Okay, as trip can only be created with proper DB backing
|
||||
let Some(trip_details_id) = trip_details.id else {
|
||||
return Err(TripUpdateError::TripDetailsDoesNotExist); //TODO: Remove?
|
||||
return Err(TripUpdateError::TripDetailsDoesNotExist); //TODO: Remove?
|
||||
};
|
||||
|
||||
sqlx::query!(
|
||||
@ -176,7 +176,7 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i
|
||||
db: &SqlitePool,
|
||||
cox: &CoxUser,
|
||||
planned_event: &PlannedEvent,
|
||||
) {
|
||||
) -> bool {
|
||||
sqlx::query!(
|
||||
"DELETE FROM trip WHERE cox_id = ? AND planned_event_id = ?",
|
||||
cox.id,
|
||||
@ -184,7 +184,9 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap(); //TODO: handle case where cox is not registered
|
||||
.unwrap()
|
||||
.rows_affected()
|
||||
> 0
|
||||
}
|
||||
|
||||
pub(crate) async fn delete(
|
||||
|
121
src/tera/cox.rs
121
src/tera/cox.rs
@ -129,18 +129,20 @@ async fn remove_trip(db: &State<SqlitePool>, trip_id: i64, cox: CoxUser) -> Flas
|
||||
#[get("/remove/<planned_event_id>")]
|
||||
async fn remove(db: &State<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Flash<Redirect> {
|
||||
if let Some(planned_event) = PlannedEvent::find_by_id(db, planned_event_id).await {
|
||||
Trip::delete_by_planned_event(db, &cox, &planned_event).await;
|
||||
if Trip::delete_by_planned_event(db, &cox, &planned_event).await {
|
||||
Log::create(
|
||||
db,
|
||||
format!(
|
||||
"Cox {} deleted registration for planned_event.id={}",
|
||||
cox.name, planned_event_id
|
||||
),
|
||||
)
|
||||
.await;
|
||||
|
||||
Log::create(
|
||||
db,
|
||||
format!(
|
||||
"Cox {} deleted registration for planned_event.id={}",
|
||||
cox.name, planned_event_id
|
||||
),
|
||||
)
|
||||
.await;
|
||||
|
||||
Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!")
|
||||
Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!")
|
||||
} else {
|
||||
Flash::error(Redirect::to("/"), "Steuermann hilft nicht aus...")
|
||||
}
|
||||
} else {
|
||||
Flash::error(Redirect::to("/"), "Planned_event does not exist.")
|
||||
}
|
||||
@ -425,4 +427,101 @@ mod test {
|
||||
|
||||
assert_eq!(flash_cookie.value(), "5:errorEvent gibt's nicht");
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_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=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;
|
||||
|
||||
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;
|
||||
|
||||
let req = client.get("/cox/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_remove_wrong_id() {
|
||||
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/remove/999");
|
||||
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:errorPlanned_event does not exist.");
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_remove_cox_not_participating() {
|
||||
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/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(), "5:errorSteuermann hilft nicht aus...");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user