From 4d532e58464d917a24be44a028a6212e7b9da65a Mon Sep 17 00:00:00 2001 From: philipp Date: Fri, 28 Apr 2023 18:18:00 +0200 Subject: [PATCH] add final tests for trip.rs --- README.md | 4 ++-- src/model/trip.rs | 48 ++++++++++++++++++++++++++++++++++++++++++- src/model/usertrip.rs | 1 + 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b067678..9d16c70 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,6 @@ - [] delete flag user administration - [] max_people = 0 -> Rot hervorheben, dass Ausfahrt abgesagt wurde? - [] my trips for cox -- [] remove key from src/rest/admin/rss.rs (line 8); at least before putting code somewhere public - [] add `trip_type` (id, name, desc, question, icon) with a FK to `trip_details` - [] add `always_show` to `planned_trips` (e.g. for wanderfahrten) - [] exactly same time -> deny registration @@ -14,6 +13,7 @@ # Nice to have - [] automatically add regular planned trip - [] User sync w/ nextcloud +- [] remove key from src/rest/admin/rss.rs (line 8); at least before putting code somewhere public # Frontend Process ´cd frontend´ @@ -25,6 +25,6 @@ - [x] model/user.rs - [x] model/tripdetails.rs - [x] model/planned_event.rs -- [.] model/trip.rs +- [x] model/trip.rs - [ ] model/usertrip.rs - [ ] Rest? diff --git a/src/model/trip.rs b/src/model/trip.rs index 8cafea4..faff732 100644 --- a/src/model/trip.rs +++ b/src/model/trip.rs @@ -221,7 +221,7 @@ pub enum CoxHelpError { AlreadyRegisteredAsCox, } -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub enum TripDeleteError { SomebodyAlreadyRegistered, NotYourTrip, @@ -238,8 +238,10 @@ mod test { use crate::{ model::{ planned_event::PlannedEvent, + trip::TripDeleteError, tripdetails::TripDetails, user::{CoxUser, User}, + usertrip::UserTrip, }, testdb, }; @@ -377,4 +379,48 @@ mod test { assert!(Trip::find_by_id(&pool, 1).await.is_none()); } + + #[sqlx::test] + fn test_fail_delete_diff_cox() { + let pool = testdb!(); + + let cox: CoxUser = User::find_by_name(&pool, "cox2".into()) + .await + .unwrap() + .try_into() + .unwrap(); + + let trip = Trip::find_by_id(&pool, 1).await.unwrap(); + + let result = trip + .delete(&pool, &cox) + .await + .expect_err("It should not be possible to delete trips from others"); + let expected = TripDeleteError::NotYourTrip; + + assert_eq!(result, expected); + } + + #[sqlx::test] + fn test_fail_delete_someone_registered() { + let pool = testdb!(); + + let cox: CoxUser = User::find_by_name(&pool, "cox2".into()) + .await + .unwrap() + .try_into() + .unwrap(); + + let trip = Trip::find_by_id(&pool, 1).await.unwrap(); + + UserTrip::create(&pool, 2, 2).await.unwrap(); + + let result = trip + .delete(&pool, &cox) + .await + .expect_err("It should not be possible to delete trips if somebody already registered"); + let expected = TripDeleteError::SomebodyAlreadyRegistered; + + assert_eq!(result, expected); + } } diff --git a/src/model/usertrip.rs b/src/model/usertrip.rs index f786f91..b032962 100644 --- a/src/model/usertrip.rs +++ b/src/model/usertrip.rs @@ -78,6 +78,7 @@ impl UserTrip { } } +#[derive(Debug)] pub enum UserTripError { AlreadyRegistered, AlreadyRegisteredAsCox,