forked from Ruderverein-Donau-Linz/rowt
finish tests
This commit is contained in:
parent
4d532e5846
commit
0ff64c5c9e
10
README.md
10
README.md
@ -14,17 +14,9 @@
|
||||
- [] 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
|
||||
- [] Rocket tests for /rest
|
||||
|
||||
# Frontend Process
|
||||
´cd frontend´
|
||||
´npm install´
|
||||
´npm run (watch/build)´
|
||||
|
||||
|
||||
# Backend tests
|
||||
- [x] model/user.rs
|
||||
- [x] model/tripdetails.rs
|
||||
- [x] model/planned_event.rs
|
||||
- [x] model/trip.rs
|
||||
- [ ] model/usertrip.rs
|
||||
- [ ] Rest?
|
||||
|
@ -4,8 +4,9 @@ INSERT INTO "user" (name, is_cox, is_admin, is_guest, pw) VALUES('guest', false,
|
||||
INSERT INTO "user" (name, is_cox, is_admin, is_guest, pw) VALUES('cox', true, false, false, '$argon2id$v=19$m=19456,t=2,p=1$dS/X5/sPEKTj4Rzs/CuvzQ$lnWzHx3DdqS9GQyWYel82kIotZuK2wk9EyfhPFtjNzs');
|
||||
INSERT INTO "user" (name) VALUES('new');
|
||||
INSERT INTO "user" (name, is_cox, is_admin, is_guest, pw) VALUES('cox2', true, false, false, '$argon2id$v=19$m=19456,t=2,p=1$dS/X5/sPEKTj4Rzs/CuvzQ$lnWzHx3DdqS9GQyWYel82kIotZuK2wk9EyfhPFtjNzs');
|
||||
INSERT INTO "user" (name, is_cox, is_admin, is_guest, pw) VALUES('rower2', false, false, false, '$argon2id$v=19$m=19456,t=2,p=1$dS/X5/sPEKTj4Rzs/CuvzQ$jWKzDmI0jqT2dqINFt6/1NjVF4Dx15n07PL1ZMBmFsY');
|
||||
|
||||
INSERT INTO "trip_details" (planned_starting_time, max_people, day, notes) VALUES('10:00', 1, '1970-01-01', 'trip_details for a planned event');
|
||||
INSERT INTO "trip_details" (planned_starting_time, max_people, day, notes) VALUES('10:00', 2, '1970-01-01', 'trip_details for a planned event');
|
||||
INSERT INTO "planned_event" (name, planned_amount_cox, trip_details_id) VALUES('test-planned-event', 2, 1);
|
||||
|
||||
INSERT INTO "trip_details" (planned_starting_time, max_people, day, notes) VALUES('11:00', 1, '1970-01-02', 'trip_details for trip from cox');
|
||||
|
@ -31,6 +31,7 @@ pub struct PlannedEventWithUser {
|
||||
pub struct Registration {
|
||||
pub name: String,
|
||||
pub registered_at: String,
|
||||
pub is_guest: bool,
|
||||
}
|
||||
|
||||
impl PlannedEvent {
|
||||
@ -81,7 +82,11 @@ WHERE day=?",
|
||||
sqlx::query_as!(
|
||||
Registration,
|
||||
"
|
||||
SELECT (SELECT name FROM user WHERE cox_id = id) as name, (SELECT created_at FROM user WHERE cox_id = id) as registered_at FROM trip WHERE planned_event_id = ?
|
||||
SELECT
|
||||
(SELECT name FROM user WHERE cox_id = id) as name,
|
||||
(SELECT created_at FROM user WHERE cox_id = id) as registered_at,
|
||||
(SELECT is_guest FROM user WHERE cox_id = id) as is_guest
|
||||
FROM trip WHERE planned_event_id = ?
|
||||
",
|
||||
self.id
|
||||
)
|
||||
@ -96,7 +101,8 @@ SELECT (SELECT name FROM user WHERE cox_id = id) as name, (SELECT created_at FRO
|
||||
"
|
||||
SELECT
|
||||
(SELECT name FROM user WHERE user_trip.user_id = user.id) as name,
|
||||
(SELECT created_at FROM user WHERE user_trip.user_id = user.id) as registered_at
|
||||
(SELECT created_at FROM user WHERE user_trip.user_id = user.id) as registered_at,
|
||||
(SELECT is_guest FROM user WHERE user_trip.user_id = user.id) as is_guest
|
||||
FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_event WHERE id = ?)
|
||||
",
|
||||
self.id
|
||||
|
@ -124,7 +124,8 @@ WHERE day=?
|
||||
"
|
||||
SELECT
|
||||
(SELECT name FROM user WHERE user_trip.user_id = user.id) as name,
|
||||
(SELECT created_at FROM user WHERE user_trip.user_id = user.id) as registered_at
|
||||
(SELECT created_at FROM user WHERE user_trip.user_id = user.id) as registered_at,
|
||||
(SELECT is_guest FROM user WHERE user_trip.user_id = user.id) as is_guest
|
||||
FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE id = ?)",
|
||||
self.id
|
||||
)
|
||||
@ -405,7 +406,7 @@ mod test {
|
||||
fn test_fail_delete_someone_registered() {
|
||||
let pool = testdb!();
|
||||
|
||||
let cox: CoxUser = User::find_by_name(&pool, "cox2".into())
|
||||
let cox: CoxUser = User::find_by_name(&pool, "cox".into())
|
||||
.await
|
||||
.unwrap()
|
||||
.try_into()
|
||||
@ -413,7 +414,12 @@ mod test {
|
||||
|
||||
let trip = Trip::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
UserTrip::create(&pool, 2, 2).await.unwrap();
|
||||
let trip_details = TripDetails::find_by_id(&pool, trip.trip_details_id.unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
let user = User::find_by_name(&pool, "rower".into()).await.unwrap();
|
||||
|
||||
UserTrip::create(&pool, &user, &trip_details).await.unwrap();
|
||||
|
||||
let result = trip
|
||||
.delete(&pool, &cox)
|
||||
|
@ -1,19 +1,15 @@
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use super::tripdetails::TripDetails;
|
||||
use super::{tripdetails::TripDetails, user::User};
|
||||
|
||||
pub struct UserTrip {}
|
||||
|
||||
impl UserTrip {
|
||||
pub async fn create(
|
||||
db: &SqlitePool,
|
||||
user_id: i64,
|
||||
trip_details_id: i64,
|
||||
user: &User,
|
||||
trip_details: &TripDetails,
|
||||
) -> Result<(), UserTripError> {
|
||||
let trip_details = TripDetails::find_by_id(db, trip_details_id)
|
||||
.await
|
||||
.ok_or(UserTripError::TripDetailsNotFound)?;
|
||||
|
||||
if trip_details.is_full(db).await {
|
||||
return Err(UserTripError::EventAlreadyFull);
|
||||
}
|
||||
@ -24,14 +20,14 @@ impl UserTrip {
|
||||
FROM trip
|
||||
WHERE trip_details_id = ?
|
||||
AND cox_id = ?",
|
||||
trip_details_id,
|
||||
user_id
|
||||
trip_details.id,
|
||||
user.id
|
||||
)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
.unwrap();
|
||||
if is_cox.amount > 0 {
|
||||
return Err(UserTripError::AlreadyRegisteredAsCox);
|
||||
return Err(UserTripError::CantRegisterAtOwnEvent);
|
||||
}
|
||||
|
||||
//check if cox if planned_event
|
||||
@ -42,8 +38,8 @@ impl UserTrip {
|
||||
SELECT id FROM planned_event WHERE trip_details_id = ?
|
||||
)
|
||||
AND cox_id = ?",
|
||||
trip_details_id,
|
||||
user_id
|
||||
trip_details.id,
|
||||
user.id
|
||||
)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
@ -54,8 +50,8 @@ impl UserTrip {
|
||||
|
||||
match sqlx::query!(
|
||||
"INSERT INTO user_trip (user_id, trip_details_id) VALUES(?, ?)",
|
||||
user_id,
|
||||
trip_details_id
|
||||
user.id,
|
||||
trip_details.id
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
@ -65,12 +61,12 @@ impl UserTrip {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete(db: &SqlitePool, user_id: i64, trip_details_id: i64) {
|
||||
pub async fn delete(db: &SqlitePool, user: &User, trip_details: &TripDetails) {
|
||||
//TODO: Check if > 2 hrs to event
|
||||
let _ = sqlx::query!(
|
||||
"DELETE FROM user_trip WHERE user_id = ? AND trip_details_id = ?",
|
||||
user_id,
|
||||
trip_details_id
|
||||
user.id,
|
||||
trip_details.id
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
@ -78,25 +74,110 @@ impl UserTrip {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum UserTripError {
|
||||
AlreadyRegistered,
|
||||
AlreadyRegisteredAsCox,
|
||||
EventAlreadyFull,
|
||||
TripDetailsNotFound,
|
||||
CantRegisterAtOwnEvent,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
//use crate::testdb;
|
||||
use crate::{
|
||||
model::{
|
||||
planned_event::PlannedEvent, trip::Trip, tripdetails::TripDetails, user::CoxUser,
|
||||
usertrip::UserTripError,
|
||||
},
|
||||
testdb,
|
||||
};
|
||||
|
||||
//use super::User;
|
||||
//use sqlx::SqlitePool;
|
||||
use super::{User, UserTrip};
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
//#[sqlx::test]
|
||||
//fn test_find_correct_id() {
|
||||
// let pool = testdb!();
|
||||
// let user = User::find_by_id(&pool, 1).await.unwrap();
|
||||
// assert_eq!(user.id, 1);
|
||||
//}
|
||||
#[sqlx::test]
|
||||
fn test_succ_create() {
|
||||
let pool = testdb!();
|
||||
|
||||
let user = User::find_by_name(&pool, "rower".into()).await.unwrap();
|
||||
|
||||
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
UserTrip::create(&pool, &user, &trip_details).await.unwrap();
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_fail_create_full() {
|
||||
let pool = testdb!();
|
||||
|
||||
let user = User::find_by_name(&pool, "rower".into()).await.unwrap();
|
||||
let user2 = User::find_by_name(&pool, "cox".into()).await.unwrap();
|
||||
let user3 = User::find_by_name(&pool, "rower2".into()).await.unwrap();
|
||||
|
||||
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
UserTrip::create(&pool, &user, &trip_details).await.unwrap();
|
||||
UserTrip::create(&pool, &user2, &trip_details)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let result = UserTrip::create(&pool, &user3, &trip_details)
|
||||
.await
|
||||
.expect_err("Expect registration to fail because trip is already full");
|
||||
|
||||
assert_eq!(result, UserTripError::EventAlreadyFull);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_fail_create_already_registered() {
|
||||
let pool = testdb!();
|
||||
|
||||
let user = User::find_by_name(&pool, "cox".into()).await.unwrap();
|
||||
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
UserTrip::create(&pool, &user, &trip_details).await.unwrap();
|
||||
|
||||
let result = UserTrip::create(&pool, &user, &trip_details)
|
||||
.await
|
||||
.expect_err("Expect registration to fail because user is same as responsible cox");
|
||||
|
||||
assert_eq!(result, UserTripError::AlreadyRegistered);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_fail_create_is_cox_own_trip() {
|
||||
let pool = testdb!();
|
||||
|
||||
let user = User::find_by_name(&pool, "cox".into()).await.unwrap();
|
||||
|
||||
let trip_details = TripDetails::find_by_id(&pool, 2).await.unwrap();
|
||||
|
||||
let result = UserTrip::create(&pool, &user, &trip_details)
|
||||
.await
|
||||
.expect_err("Expect registration to fail because user is same as responsible cox");
|
||||
|
||||
assert_eq!(result, UserTripError::CantRegisterAtOwnEvent);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_fail_create_is_cox_planned_event() {
|
||||
let pool = testdb!();
|
||||
|
||||
let cox: CoxUser = User::find_by_name(&pool, "cox".into())
|
||||
.await
|
||||
.unwrap()
|
||||
.try_into()
|
||||
.unwrap();
|
||||
|
||||
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
||||
let planned_event = PlannedEvent::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
Trip::new_join(&pool, &cox, &planned_event).await.unwrap();
|
||||
|
||||
let result = UserTrip::create(&pool, &cox, &trip_details)
|
||||
.await
|
||||
.expect_err("Expect registration to fail because user is already registered as cox");
|
||||
|
||||
assert_eq!(result, UserTripError::AlreadyRegisteredAsCox);
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ use sqlx::SqlitePool;
|
||||
|
||||
use crate::model::{
|
||||
log::Log,
|
||||
tripdetails::TripDetails,
|
||||
user::User,
|
||||
usertrip::{UserTrip, UserTripError},
|
||||
Day,
|
||||
@ -51,7 +52,11 @@ async fn index(db: &State<SqlitePool>, user: User, flash: Option<FlashMessage<'_
|
||||
|
||||
#[get("/join/<trip_details_id>")]
|
||||
async fn join(db: &State<SqlitePool>, trip_details_id: i64, user: User) -> Flash<Redirect> {
|
||||
match UserTrip::create(db, user.id, trip_details_id).await {
|
||||
let trip_details = match TripDetails::find_by_id(db, trip_details_id).await {
|
||||
Some(trip_details) => trip_details,
|
||||
None => return Flash::error(Redirect::to("/"), "Trip_details do not exist."),
|
||||
};
|
||||
match UserTrip::create(db, &user, &trip_details).await {
|
||||
Ok(_) => {
|
||||
Log::create(
|
||||
db,
|
||||
@ -72,15 +77,23 @@ async fn join(db: &State<SqlitePool>, trip_details_id: i64, user: User) -> Flash
|
||||
Err(UserTripError::AlreadyRegisteredAsCox) => {
|
||||
Flash::error(Redirect::to("/"), "Du hilfst bereits als Steuerperson aus!")
|
||||
}
|
||||
Err(UserTripError::TripDetailsNotFound) => {
|
||||
Flash::error(Redirect::to("/"), "Trip_details do not exist.")
|
||||
}
|
||||
Err(UserTripError::CantRegisterAtOwnEvent) => Flash::error(
|
||||
Redirect::to("/"),
|
||||
"Du kannst bei einer selbst ausgeschriebenen Fahrt nicht mitrudern ;)",
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/remove/<trip_details_id>")]
|
||||
async fn remove(db: &State<SqlitePool>, trip_details_id: i64, user: User) -> Flash<Redirect> {
|
||||
UserTrip::delete(db, user.id, trip_details_id).await;
|
||||
let trip_details = match TripDetails::find_by_id(db, trip_details_id).await {
|
||||
Some(trip_details) => trip_details,
|
||||
None => {
|
||||
return Flash::error(Redirect::to("/"), "TripDetailsId does not exist");
|
||||
}
|
||||
};
|
||||
|
||||
UserTrip::delete(db, &user, &trip_details).await;
|
||||
|
||||
Log::create(
|
||||
db,
|
||||
|
Loading…
x
Reference in New Issue
Block a user