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

@ -78,6 +78,18 @@ impl Boat {
.ok()
}
pub async fn shipmaster_allowed(&self, user: &User) -> bool {
if let Some(owner_id) = self.owner {
return owner_id == user.id;
}
if self.amount_seats == 1 {
return true;
}
user.is_cox
}
pub async fn is_locked(&self, db: &SqlitePool) -> bool {
sqlx::query!("SELECT * FROM boat_damage WHERE boat_id=? AND lock_boat=true AND user_id_verified is null", self.id).fetch_optional(db).await.unwrap().is_some()
}
@ -136,7 +148,7 @@ ORDER BY amount_seats DESC
if user.is_admin {
return Self::all(db).await;
}
let mut boats;
let boats;
if user.is_cox {
boats = sqlx::query_as!(
Boat,

View File

@ -41,7 +41,7 @@ pub struct LogToAdd {
pub rowers: Vec<i64>,
}
#[derive(FromForm)]
#[derive(FromForm, Debug)]
pub struct LogToFinalize {
pub destination: String,
pub distance_in_km: i64,
@ -75,6 +75,7 @@ pub enum LogbookDeleteError {
#[derive(Debug, PartialEq)]
pub enum LogbookCreateError {
ArrivalSetButNoDestination,
UserNotAllowedToUseBoat,
ArrivalSetButNoDistance,
BoatAlreadyOnWater,
BoatLocked,
@ -170,7 +171,11 @@ ORDER BY departure DESC
ret
}
pub async fn create(db: &SqlitePool, log: LogToAdd) -> Result<(), LogbookCreateError> {
pub async fn create(
db: &SqlitePool,
log: LogToAdd,
created_by_user: &User,
) -> Result<(), LogbookCreateError> {
let Some(boat) = Boat::find_by_id(db, log.boat_id).await else {
return Err(LogbookCreateError::BoatNotFound);
};
@ -183,10 +188,9 @@ ORDER BY departure DESC
return Err(LogbookCreateError::BoatAlreadyOnWater);
}
if (User::find_by_id(db, log.shipmaster as i32).await.unwrap())
.on_water(db)
.await
{
let shipmaster = User::find_by_id(db, log.shipmaster as i32).await.unwrap();
if shipmaster.on_water(db).await {
return Err(LogbookCreateError::ShipmasterAlreadyOnWater);
}
@ -223,6 +227,10 @@ ORDER BY departure DESC
}
}
if !boat.shipmaster_allowed(created_by_user).await {
return Err(LogbookCreateError::UserNotAllowedToUseBoat);
}
//let departure = format!("{}+02:00", &log.departure);
let mut tx = db.begin().await.unwrap();
@ -431,7 +439,7 @@ mod test {
&pool,
LogToAdd {
boat_id: 3,
shipmaster: 5,
shipmaster: 4,
shipmaster_only_steering: false,
departure: "2128-05-20T12:00".into(),
arrival: None,
@ -441,6 +449,7 @@ mod test {
logtype: None,
rowers: Vec::new(),
},
&User::find_by_id(&pool, 4).await.unwrap(),
)
.await
.unwrap()
@ -464,6 +473,7 @@ mod test {
logtype: None,
rowers: Vec::new(),
},
&User::find_by_id(&pool, 4).await.unwrap(),
)
.await;
@ -488,6 +498,7 @@ mod test {
logtype: None,
rowers: Vec::new(),
},
&User::find_by_id(&pool, 4).await.unwrap(),
)
.await;
@ -512,6 +523,7 @@ mod test {
logtype: None,
rowers: Vec::new(),
},
&User::find_by_id(&pool, 5).await.unwrap(),
)
.await;
@ -536,6 +548,7 @@ mod test {
logtype: None,
rowers: Vec::new(),
},
&User::find_by_id(&pool, 5).await.unwrap(),
)
.await;
@ -560,6 +573,7 @@ mod test {
logtype: None,
rowers: Vec::new(),
},
&User::find_by_id(&pool, 2).await.unwrap(),
)
.await;
@ -584,6 +598,7 @@ mod test {
logtype: None,
rowers: vec![5],
},
&User::find_by_id(&pool, 5).await.unwrap(),
)
.await;
@ -608,6 +623,7 @@ mod test {
logtype: None,
rowers: vec![1],
},
&User::find_by_id(&pool, 5).await.unwrap(),
)
.await;