add tests for tera/admin/planned_event

Closes #24
This commit is contained in:
philipp 2023-07-31 21:15:28 +02:00
parent 96af3c3b17
commit 8fa8e17c5a
2 changed files with 79 additions and 1 deletions

View File

@ -10,7 +10,7 @@ use sqlx::{FromRow, SqlitePool};
use super::{tripdetails::TripDetails, triptype::TripType, user::User};
#[derive(Serialize, Clone, FromRow)]
#[derive(Serialize, Clone, FromRow, Debug, PartialEq)]
pub struct PlannedEvent {
pub id: i64,
pub name: String,

View File

@ -87,3 +87,81 @@ async fn delete(db: &State<SqlitePool>, id: i64, _admin: AdminUser) -> Flash<Red
pub fn routes() -> Vec<Route> {
routes![create, delete, update]
}
#[cfg(test)]
mod test {
use rocket::{
http::{ContentType, Status},
local::asynchronous::Client,
};
use sqlx::SqlitePool;
use super::*;
use crate::testdb;
#[sqlx::test]
fn test_delete() {
let db = testdb!();
let _ = PlannedEvent::find_by_id(&db, 1).await.unwrap();
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("/admin/planned-event/1/delete");
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:successSuccessfully deleted the event"
);
let event = PlannedEvent::find_by_id(&db, 1).await;
assert_eq!(event, None);
}
#[sqlx::test]
fn test_delete_invalid_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=admin&password=admin"); // Add the form data to the request body;
login.dispatch().await;
let req = client.get("/admin/planned-event/1337/delete");
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:errorPlannedEvent does not exist");
let _ = PlannedEvent::find_by_id(&db, 1).await.unwrap();
}
}