Wrap Context with Autentication enum

Context should contain information which is required to get
the information if the service call is authenticated.  Context
could be the username for example.  But services call other
services internally and for this, authentication must not be
checked.  In this case, they can now pass Authentication::Full
which always successfully authenticates.
This commit is contained in:
Simon Goller 2024-05-09 14:58:19 +02:00
parent bf94ec33de
commit b0000c0117
18 changed files with 252 additions and 217 deletions

View file

@ -70,7 +70,7 @@ pub async fn get_all_bookings<RestState: RestStateDef>(rest_state: State<RestSta
(async {
let bookings: Arc<[BookingTO]> = rest_state
.booking_service()
.get_all(())
.get_all(().into())
.await?
.iter()
.map(BookingTO::from)
@ -90,7 +90,7 @@ pub async fn get_booking<RestState: RestStateDef>(
) -> Response {
error_handler(
(async {
let booking = rest_state.booking_service().get(booking_id, ()).await?;
let booking = rest_state.booking_service().get(booking_id, ().into()).await?;
Ok(Response::builder()
.status(200)
.body(Body::new(
@ -110,7 +110,7 @@ pub async fn create_booking<RestState: RestStateDef>(
(async {
let booking = rest_state
.booking_service()
.create(&Booking::from(&booking), ())
.create(&Booking::from(&booking), ().into())
.await?;
Ok(Response::builder()
.status(200)
@ -129,7 +129,7 @@ pub async fn delete_booking<RestState: RestStateDef>(
) -> Response {
error_handler(
(async {
rest_state.booking_service().delete(booking_id, ()).await?;
rest_state.booking_service().delete(booking_id, ().into()).await?;
Ok(Response::builder().status(200).body(Body::empty()).unwrap())
})
.await,

View file

@ -87,7 +87,7 @@ pub async fn add_user<RestState: RestStateDef>(
(async {
rest_state
.permission_service()
.create_user(user.name.as_str(), ())
.create_user(user.name.as_str(), ().into())
.await?;
Ok(Response::builder()
.status(201)
@ -107,7 +107,7 @@ pub async fn remove_user<RestState: RestStateDef>(
(async {
rest_state
.permission_service()
.delete_user(&user, ())
.delete_user(&user, ().into())
.await?;
Ok(Response::builder()
.status(200)
@ -126,7 +126,7 @@ pub async fn add_role<RestState: RestStateDef>(
(async {
rest_state
.permission_service()
.create_role(role.name.as_str(), ())
.create_role(role.name.as_str(), ().into())
.await?;
Ok(Response::builder()
.status(200)
@ -145,7 +145,7 @@ pub async fn delete_role<RestState: RestStateDef>(
(async {
rest_state
.permission_service()
.delete_role(role.as_str(), ())
.delete_role(role.as_str(), ().into())
.await?;
Ok(Response::builder()
.status(200)
@ -164,7 +164,7 @@ pub async fn add_user_role<RestState: RestStateDef>(
(async {
rest_state
.permission_service()
.add_user_role(user_role.user.as_str(), user_role.role.as_str(), ())
.add_user_role(user_role.user.as_str(), user_role.role.as_str(), ().into())
.await?;
Ok(Response::builder()
.status(201)
@ -183,7 +183,7 @@ pub async fn remove_user_role<RestState: RestStateDef>(
(async {
rest_state
.permission_service()
.delete_user_role(user_role.user.as_str(), user_role.role.as_str(), ())
.delete_user_role(user_role.user.as_str(), user_role.role.as_str(), ().into())
.await?;
Ok(Response::builder()
.status(200)
@ -205,7 +205,7 @@ pub async fn add_role_privilege<RestState: RestStateDef>(
.add_role_privilege(
role_privilege.role.as_str(),
role_privilege.privilege.as_str(),
(),
().into(),
)
.await?;
Ok(Response::builder()
@ -228,7 +228,7 @@ pub async fn remove_role_privilege<RestState: RestStateDef>(
.delete_role_privilege(
role_privilege.role.as_str(),
role_privilege.privilege.as_str(),
(),
().into(),
)
.await?;
Ok(Response::builder()
@ -245,7 +245,7 @@ pub async fn get_all_users<RestState: RestStateDef>(rest_state: State<RestState>
(async {
let users: Arc<[UserTO]> = rest_state
.permission_service()
.get_all_users(())
.get_all_users(().into())
.await?
.iter()
.map(UserTO::from)
@ -264,7 +264,7 @@ pub async fn get_all_roles<RestState: RestStateDef>(rest_state: State<RestState>
(async {
let roles: Arc<[RoleTO]> = rest_state
.permission_service()
.get_all_roles(())
.get_all_roles(().into())
.await?
.iter()
.map(RoleTO::from)
@ -283,7 +283,7 @@ pub async fn get_all_privileges<RestState: RestStateDef>(rest_state: State<RestS
(async {
let privileges: Arc<[PrivilegeTO]> = rest_state
.permission_service()
.get_all_privileges(())
.get_all_privileges(().into())
.await?
.iter()
.map(PrivilegeTO::from)

View file

@ -67,7 +67,7 @@ pub async fn get_all_sales_persons<RestState: RestStateDef>(
(async {
let sales_persons: Arc<[SalesPersonTO]> = rest_state
.sales_person_service()
.get_all(())
.get_all(().into())
.await?
.iter()
.map(SalesPersonTO::from)
@ -90,7 +90,7 @@ pub async fn get_sales_person<RestState: RestStateDef>(
let sales_person = SalesPersonTO::from(
&rest_state
.sales_person_service()
.get(sales_person_id, ())
.get(sales_person_id, ().into())
.await?,
);
Ok(Response::builder()
@ -111,7 +111,7 @@ pub async fn create_sales_person<RestState: RestStateDef>(
let sales_person = SalesPersonTO::from(
&rest_state
.sales_person_service()
.create(&(&sales_person).into(), ())
.create(&(&sales_person).into(), ().into())
.await?,
);
Ok(Response::builder()
@ -135,7 +135,7 @@ pub async fn update_sales_person<RestState: RestStateDef>(
}
rest_state
.sales_person_service()
.update(&(&sales_person).into(), ())
.update(&(&sales_person).into(), ().into())
.await?;
Ok(Response::builder()
.status(200)
@ -154,7 +154,7 @@ pub async fn delete_sales_person<RestState: RestStateDef>(
(async {
rest_state
.sales_person_service()
.delete(sales_person_id, ())
.delete(sales_person_id, ().into())
.await?;
Ok(Response::builder().status(204).body(Body::empty()).unwrap())
})
@ -171,7 +171,7 @@ pub async fn get_sales_person_user<RestState: RestStateDef>(
(async {
let user = rest_state
.sales_person_service()
.get_assigned_user(sales_person_id, ())
.get_assigned_user(sales_person_id, ().into())
.await?;
Ok(Response::builder()
.status(200)
@ -191,7 +191,7 @@ pub async fn set_sales_person_user<RestState: RestStateDef>(
(async {
rest_state
.sales_person_service()
.set_user(sales_person_id, user.into(), ())
.set_user(sales_person_id, user.into(), ().into())
.await?;
Ok(Response::builder().status(204).body(Body::empty()).unwrap())
})
@ -207,7 +207,7 @@ pub async fn delete_sales_person_user<RestState: RestStateDef>(
(async {
rest_state
.sales_person_service()
.set_user(sales_person_id, None, ())
.set_user(sales_person_id, None, ().into())
.await?;
Ok(Response::builder().status(204).body(Body::empty()).unwrap())
})

View file

@ -107,7 +107,7 @@ pub async fn get_all_slots<RestState: RestStateDef>(rest_state: State<RestState>
(async {
let slots: Arc<[SlotTO]> = rest_state
.slot_service()
.get_slots(())
.get_slots(().into())
.await?
.iter()
.map(SlotTO::from)
@ -127,7 +127,7 @@ pub async fn get_slot<RestState: RestStateDef>(
) -> Response {
error_handler(
(async {
let slot = SlotTO::from(&rest_state.slot_service().get_slot(&slot_id, ()).await?);
let slot = SlotTO::from(&rest_state.slot_service().get_slot(&slot_id, ().into()).await?);
Ok(Response::builder()
.status(200)
.body(Body::new(serde_json::to_string(&slot).unwrap()))
@ -146,7 +146,7 @@ pub async fn create_slot<RestState: RestStateDef>(
let slot = SlotTO::from(
&rest_state
.slot_service()
.create_slot(&(&slot).into(), ())
.create_slot(&(&slot).into(), ().into())
.await?,
);
Ok(Response::builder()
@ -170,7 +170,7 @@ pub async fn update_slot<RestState: RestStateDef>(
}
rest_state
.slot_service()
.update_slot(&(&slot).into(), ())
.update_slot(&(&slot).into(), ().into())
.await?;
Ok(Response::builder()
.status(200)

View file

@ -1,10 +1,12 @@
use std::sync::Arc;
use std::fmt::Debug;
use async_trait::async_trait;
use time::PrimitiveDateTime;
use uuid::Uuid;
use crate::ServiceError;
use crate::permission::Authentication;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Booking {
@ -51,14 +53,14 @@ impl TryFrom<&Booking> for dao::booking::BookingEntity {
#[async_trait]
pub trait BookingService {
type Context: Clone + Send + Sync;
type Context: Clone + PartialEq + Eq + Debug + Send + Sync;
async fn get_all(&self, context: Self::Context) -> Result<Arc<[Booking]>, ServiceError>;
async fn get(&self, id: Uuid, context: Self::Context) -> Result<Booking, ServiceError>;
async fn get_all(&self, context: Authentication<Self::Context>) -> Result<Arc<[Booking]>, ServiceError>;
async fn get(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<Booking, ServiceError>;
async fn create(
&self,
booking: &Booking,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<Booking, ServiceError>;
async fn delete(&self, id: Uuid, context: Self::Context) -> Result<(), ServiceError>;
async fn delete(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<(), ServiceError>;
}

View file

@ -1,4 +1,5 @@
use std::sync::Arc;
use std::fmt::Debug;
use async_trait::async_trait;
use mockall::automock;
@ -41,63 +42,74 @@ impl From<&dao::PrivilegeEntity> for Privilege {
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Authentication<Context: Clone + PartialEq + Eq + Send + Sync + Debug + 'static>{
Full,
Context(Context),
}
impl<Context: Clone + Debug + PartialEq + Eq + Send + Sync + 'static> From<Context> for Authentication<Context> {
fn from(context: Context) -> Self {
Self::Context(context)
}
}
#[automock(type Context=();)]
#[async_trait]
pub trait PermissionService {
type Context: Clone + Send + Sync + 'static;
type Context: Clone + PartialEq + Eq + Debug + Send + Sync + 'static;
async fn check_permission(
&self,
privilege: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
async fn create_user(&self, user: &str, context: Self::Context) -> Result<(), ServiceError>;
async fn user_exists(&self, user: &str, context: Self::Context) -> Result<bool, ServiceError>;
async fn delete_user(&self, user: &str, context: Self::Context) -> Result<(), ServiceError>;
async fn get_all_users(&self, context: Self::Context) -> Result<Arc<[User]>, ServiceError>;
async fn create_user(&self, user: &str, context: Authentication<Self::Context>) -> Result<(), ServiceError>;
async fn user_exists(&self, user: &str, context: Authentication<Self::Context>) -> Result<bool, ServiceError>;
async fn delete_user(&self, user: &str, context: Authentication<Self::Context>) -> Result<(), ServiceError>;
async fn get_all_users(&self, context: Authentication<Self::Context>) -> Result<Arc<[User]>, ServiceError>;
async fn create_role(&self, role: &str, context: Self::Context) -> Result<(), ServiceError>;
async fn delete_role(&self, role: &str, context: Self::Context) -> Result<(), ServiceError>;
async fn get_all_roles(&self, context: Self::Context) -> Result<Arc<[Role]>, ServiceError>;
async fn create_role(&self, role: &str, context: Authentication<Self::Context>) -> Result<(), ServiceError>;
async fn delete_role(&self, role: &str, context: Authentication<Self::Context>) -> Result<(), ServiceError>;
async fn get_all_roles(&self, context: Authentication<Self::Context>) -> Result<Arc<[Role]>, ServiceError>;
async fn create_privilege(
&self,
privilege: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
async fn delete_privilege(
&self,
privilege: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
async fn get_all_privileges(
&self,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<Arc<[Privilege]>, ServiceError>;
async fn add_user_role(
&self,
user: &str,
role: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
async fn add_role_privilege(
&self,
role: &str,
privilege: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
async fn delete_role_privilege(
&self,
role: &str,
privilege: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
async fn delete_user_role(
&self,
user: &str,
role: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
}

View file

@ -1,10 +1,12 @@
use std::sync::Arc;
use std::fmt::Debug;
use async_trait::async_trait;
use mockall::automock;
use uuid::Uuid;
use crate::ServiceError;
use crate::permission::Authentication;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SalesPerson {
@ -40,31 +42,31 @@ impl From<&SalesPerson> for dao::sales_person::SalesPersonEntity {
#[automock(type Context=();)]
#[async_trait]
pub trait SalesPersonService {
type Context: Clone + Send + Sync + 'static;
type Context: Clone + Debug + PartialEq + Eq + Send + Sync + 'static;
async fn get_all(&self, context: Self::Context) -> Result<Arc<[SalesPerson]>, ServiceError>;
async fn get(&self, id: Uuid, context: Self::Context) -> Result<SalesPerson, ServiceError>;
async fn exists(&self, id: Uuid, context: Self::Context) -> Result<bool, ServiceError>;
async fn get_all(&self, context: Authentication<Self::Context>) -> Result<Arc<[SalesPerson]>, ServiceError>;
async fn get(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<SalesPerson, ServiceError>;
async fn exists(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<bool, ServiceError>;
async fn create(
&self,
item: &SalesPerson,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<SalesPerson, ServiceError>;
async fn update(
&self,
item: &SalesPerson,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<SalesPerson, ServiceError>;
async fn delete(&self, id: Uuid, context: Self::Context) -> Result<(), ServiceError>;
async fn delete(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<(), ServiceError>;
async fn get_assigned_user(
&self,
sales_person_id: Uuid,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<Option<Arc<str>>, ServiceError>;
async fn set_user(
&self,
sales_person_id: Uuid,
user_id: Option<Arc<str>>,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
}

View file

@ -2,8 +2,10 @@ use async_trait::async_trait;
use mockall::automock;
use std::sync::Arc;
use uuid::Uuid;
use std::fmt::Debug;
use crate::ServiceError;
use crate::permission::Authentication;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum DayOfWeek {
@ -85,12 +87,12 @@ impl From<&Slot> for dao::slot::SlotEntity {
#[automock(type Context=();)]
#[async_trait]
pub trait SlotService {
type Context: Clone + Send + Sync + 'static;
type Context: Clone + Debug + PartialEq + Eq + Send + Sync + 'static;
async fn get_slots(&self, context: Self::Context) -> Result<Arc<[Slot]>, ServiceError>;
async fn get_slot(&self, id: &Uuid, context: Self::Context) -> Result<Slot, ServiceError>;
async fn exists(&self, id: Uuid, context: Self::Context) -> Result<bool, ServiceError>;
async fn create_slot(&self, slot: &Slot, context: Self::Context) -> Result<Slot, ServiceError>;
async fn delete_slot(&self, id: &Uuid, context: Self::Context) -> Result<(), ServiceError>;
async fn update_slot(&self, slot: &Slot, context: Self::Context) -> Result<(), ServiceError>;
async fn get_slots(&self, context: Authentication<Self::Context>) -> Result<Arc<[Slot]>, ServiceError>;
async fn get_slot(&self, id: &Uuid, context: Authentication<Self::Context>) -> Result<Slot, ServiceError>;
async fn exists(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<bool, ServiceError>;
async fn create_slot(&self, slot: &Slot, context: Authentication<Self::Context>) -> Result<Slot, ServiceError>;
async fn delete_slot(&self, id: &Uuid, context: Authentication<Self::Context>) -> Result<(), ServiceError>;
async fn update_slot(&self, slot: &Slot, context: Authentication<Self::Context>) -> Result<(), ServiceError>;
}

View file

@ -1,4 +1,5 @@
use std::sync::Arc;
use std::fmt::Debug;
use async_trait::async_trait;
use mockall::automock;
@ -8,7 +9,7 @@ use crate::ServiceError;
#[automock(type Context=();)]
#[async_trait]
pub trait UserService {
type Context: Clone + Send + Sync + 'static;
type Context: Clone + Debug + PartialEq + Eq + Send + Sync + 'static;
async fn current_user(&self, context: Self::Context) -> Result<Arc<str>, ServiceError>;
}

View file

@ -2,6 +2,7 @@ use async_trait::async_trait;
use service::{
booking::{Booking, BookingService},
ServiceError, ValidationFailureItem,
permission::Authentication,
};
use std::sync::Arc;
use uuid::Uuid;
@ -89,7 +90,7 @@ where
{
type Context = PermissionService::Context;
async fn get_all(&self, context: Self::Context) -> Result<Arc<[Booking]>, ServiceError> {
async fn get_all(&self, context: Authentication<Self::Context>) -> Result<Arc<[Booking]>, ServiceError> {
self.permission_service
.check_permission("hr", context)
.await?;
@ -102,7 +103,7 @@ where
.collect())
}
async fn get(&self, id: Uuid, context: Self::Context) -> Result<Booking, ServiceError> {
async fn get(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<Booking, ServiceError> {
self.permission_service
.check_permission("hr", context)
.await?;
@ -117,7 +118,7 @@ where
async fn create(
&self,
booking: &Booking,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<Booking, ServiceError> {
self.permission_service
.check_permission("hr", context.clone())
@ -201,7 +202,7 @@ where
Ok(new_booking)
}
async fn delete(&self, id: Uuid, context: Self::Context) -> Result<(), ServiceError> {
async fn delete(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<(), ServiceError> {
self.permission_service
.check_permission("hr", context)
.await?;

View file

@ -2,6 +2,7 @@ use std::sync::Arc;
use async_trait::async_trait;
use service::ServiceError;
use service::permission::Authentication;
pub struct PermissionServiceImpl<PermissionDao, UserService>
where
@ -38,24 +39,29 @@ where
async fn check_permission(
&self,
privilege: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), service::ServiceError> {
let current_user = self.user_service.current_user(context).await?;
if self
.permission_dao
.has_privilege(current_user.as_ref(), privilege)
.await?
{
Ok(())
} else {
Err(service::ServiceError::Forbidden)
match context {
Authentication::Full => Ok(()),
Authentication::Context(context) => {
let current_user = self.user_service.current_user(context).await?;
if self
.permission_dao
.has_privilege(current_user.as_ref(), privilege)
.await?
{
Ok(())
} else {
Err(service::ServiceError::Forbidden)
}
}
}
}
async fn create_user(
&self,
user: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), service::ServiceError> {
self.check_permission("admin", context).await?;
self.permission_dao
@ -69,21 +75,21 @@ where
async fn delete_user(
&self,
user: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), service::ServiceError> {
self.check_permission("admin", context).await?;
self.permission_dao.delete_user(user).await?;
Ok(())
}
async fn user_exists(&self, user: &str, context: Self::Context) -> Result<bool, ServiceError> {
async fn user_exists(&self, user: &str, context: Authentication<Self::Context>) -> Result<bool, ServiceError> {
self.check_permission("hr", context).await?;
Ok(self.permission_dao.find_user(user).await.map(|x| x.is_some())?)
}
async fn get_all_users(
&self,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<Arc<[service::User]>, service::ServiceError> {
self.check_permission("admin", context).await?;
Ok(self
@ -98,7 +104,7 @@ where
async fn create_role(
&self,
role: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), service::ServiceError> {
self.check_permission("admin", context).await?;
self.permission_dao
@ -112,7 +118,7 @@ where
async fn delete_role(
&self,
role: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), service::ServiceError> {
self.check_permission("admin", context).await?;
self.permission_dao.delete_role(role).await?;
@ -120,7 +126,7 @@ where
}
async fn get_all_roles(
&self,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<Arc<[service::Role]>, service::ServiceError> {
self.check_permission("admin", context).await?;
Ok(self
@ -135,7 +141,7 @@ where
async fn create_privilege(
&self,
privilege: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), service::ServiceError> {
self.check_permission("admin", context).await?;
self.permission_dao
@ -152,7 +158,7 @@ where
async fn delete_privilege(
&self,
privilege: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), service::ServiceError> {
self.check_permission("admin", context).await?;
self.permission_dao.delete_privilege(privilege).await?;
@ -160,7 +166,7 @@ where
}
async fn get_all_privileges(
&self,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<Arc<[service::Privilege]>, service::ServiceError> {
self.check_permission("admin", context).await?;
Ok(self
@ -176,7 +182,7 @@ where
&self,
user: &str,
role: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), service::ServiceError> {
self.check_permission("admin", context).await?;
self.permission_dao
@ -188,7 +194,7 @@ where
&self,
role: &str,
privilege: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), service::ServiceError> {
self.check_permission("admin", context).await?;
self.permission_dao
@ -200,7 +206,7 @@ where
&self,
role: &str,
privilege: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), service::ServiceError> {
self.check_permission("admin", context).await?;
self.permission_dao
@ -212,7 +218,7 @@ where
&self,
user: &str,
role: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), service::ServiceError> {
self.check_permission("admin", context).await?;
self.permission_dao.delete_user_role(user, role).await?;

View file

@ -2,7 +2,7 @@ use std::sync::Arc;
use async_trait::async_trait;
use dao::sales_person::SalesPersonEntity;
use service::{sales_person::SalesPerson, ServiceError, ValidationFailureItem};
use service::{permission::Authentication, sales_person::SalesPerson, ServiceError, ValidationFailureItem};
use uuid::Uuid;
pub struct SalesPersonServiceImpl<SalesPersonDao, PermissionService, ClockService, UuidService>
@ -56,7 +56,7 @@ where
async fn get_all(
&self,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<Arc<[service::sales_person::SalesPerson]>, service::ServiceError> {
self.permission_service
.check_permission("hr", context)
@ -73,7 +73,7 @@ where
async fn get(
&self,
id: Uuid,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<service::sales_person::SalesPerson, service::ServiceError> {
self.permission_service
.check_permission("hr", context)
@ -86,7 +86,7 @@ where
.ok_or(ServiceError::EntityNotFound(id))
}
async fn exists(&self, id: Uuid, _context: Self::Context) -> Result<bool, ServiceError> {
async fn exists(&self, id: Uuid, _context: Authentication<Self::Context>) -> Result<bool, ServiceError> {
Ok(self
.sales_person_dao
.find_by_id(id)
@ -97,7 +97,7 @@ where
async fn create(
&self,
sales_person: &SalesPerson,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<SalesPerson, service::ServiceError> {
self.permission_service
.check_permission("hr", context)
@ -127,7 +127,7 @@ where
async fn update(
&self,
sales_person: &SalesPerson,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<SalesPerson, ServiceError> {
self.permission_service
.check_permission("hr", context)
@ -172,7 +172,7 @@ where
Ok(sales_person)
}
async fn delete(&self, id: Uuid, context: Self::Context) -> Result<(), ServiceError> {
async fn delete(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<(), ServiceError> {
self.permission_service
.check_permission("hr", context)
.await?;
@ -192,7 +192,7 @@ where
async fn get_assigned_user(
&self,
sales_person_id: Uuid,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<Option<Arc<str>>, ServiceError> {
self.permission_service
.check_permission("hr", context)
@ -204,7 +204,7 @@ where
&self,
sales_person_id: Uuid,
user_id: Option<Arc<str>>,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError> {
self.permission_service
.check_permission("hr", context)

View file

@ -1,7 +1,7 @@
use std::sync::Arc;
use async_trait::async_trait;
use service::{slot::Slot, ServiceError, ValidationFailureItem};
use service::{permission::Authentication, slot::Slot, ServiceError, ValidationFailureItem};
use tokio::join;
use uuid::Uuid;
@ -60,7 +60,7 @@ where
{
type Context = PermissionService::Context;
async fn get_slots(&self, context: Self::Context) -> Result<Arc<[Slot]>, ServiceError> {
async fn get_slots(&self, context: Authentication<Self::Context>) -> Result<Arc<[Slot]>, ServiceError> {
let (hr_permission, sales_permission) = join!(
self.permission_service
.check_permission("hr", context.clone()),
@ -76,7 +76,7 @@ where
.map(Slot::from)
.collect())
}
async fn get_slot(&self, id: &Uuid, context: Self::Context) -> Result<Slot, ServiceError> {
async fn get_slot(&self, id: &Uuid, context: Authentication<Self::Context>) -> Result<Slot, ServiceError> {
let (hr_permission, sales_permission) = join!(
self.permission_service
.check_permission("hr", context.clone()),
@ -92,11 +92,11 @@ where
Ok(slot)
}
async fn exists(&self, id: Uuid, _context: Self::Context) -> Result<bool, ServiceError> {
async fn exists(&self, id: Uuid, _context: Authentication<Self::Context>) -> Result<bool, ServiceError> {
Ok(self.slot_dao.get_slot(&id).await.map(|s| s.is_some())?)
}
async fn create_slot(&self, slot: &Slot, context: Self::Context) -> Result<Slot, ServiceError> {
async fn create_slot(&self, slot: &Slot, context: Authentication<Self::Context>) -> Result<Slot, ServiceError> {
self.permission_service
.check_permission("hr", context.clone())
.await?;
@ -137,7 +137,7 @@ where
Ok(slot)
}
async fn delete_slot(&self, id: &Uuid, context: Self::Context) -> Result<(), ServiceError> {
async fn delete_slot(&self, id: &Uuid, context: Authentication<Self::Context>) -> Result<(), ServiceError> {
self.permission_service
.check_permission("hr", context)
.await?;
@ -152,7 +152,7 @@ where
.await?;
Ok(())
}
async fn update_slot(&self, slot: &Slot, context: Self::Context) -> Result<(), ServiceError> {
async fn update_slot(&self, slot: &Slot, context: Authentication<Self::Context>) -> Result<(), ServiceError> {
self.permission_service
.check_permission("hr", context)
.await?;

View file

@ -2,15 +2,14 @@ use crate::test::error_test::*;
use dao::booking::{BookingEntity, MockBookingDao};
use mockall::predicate::eq;
use service::{
booking::Booking, clock::MockClockService, sales_person::MockSalesPersonService,
slot::MockSlotService, uuid_service::MockUuidService, MockPermissionService,
ValidationFailureItem,
booking::Booking, clock::MockClockService, sales_person::MockSalesPersonService, slot::MockSlotService, uuid_service::MockUuidService, MockPermissionService, ValidationFailureItem
};
use time::{Date, Month, PrimitiveDateTime, Time};
use uuid::{uuid, Uuid};
use crate::booking::BookingServiceImpl;
use service::booking::BookingService;
use super::error_test::NoneTypeExt;
pub fn default_id() -> Uuid {
uuid!("CEA260A0-112B-4970-936C-F7E529955BD0")
@ -99,7 +98,7 @@ pub fn build_dependencies(permission: bool, role: &'static str) -> BookingServic
let mut permission_service = MockPermissionService::new();
permission_service
.expect_check_permission()
.with(eq(role), eq(()))
.with(eq(role), eq(().auth()))
.returning(move |_, _| {
if permission {
Ok(())
@ -156,7 +155,7 @@ async fn test_get_all() {
.into())
});
let service = deps.build_service();
let result = service.get_all(()).await;
let result = service.get_all(().auth()).await;
assert!(result.is_ok());
let result = result.unwrap();
assert_eq!(result.len(), 2);
@ -174,7 +173,7 @@ async fn test_get_all() {
async fn test_get_all_no_permission() {
let deps = build_dependencies(false, "hr");
let service = deps.build_service();
let result = service.get_all(()).await;
let result = service.get_all(().auth()).await;
test_forbidden(&result);
}
@ -186,7 +185,7 @@ async fn test_get() {
.with(eq(default_id()))
.returning(|_| Ok(Some(default_booking_entity())));
let service = deps.build_service();
let result = service.get(default_id(), ()).await;
let result = service.get(default_id(), ().auth()).await;
assert!(result.is_ok());
assert_eq!(result.unwrap(), default_booking());
}
@ -199,7 +198,7 @@ async fn test_get_not_found() {
.with(eq(default_id()))
.returning(|_| Ok(None));
let service = deps.build_service();
let result = service.get(default_id(), ()).await;
let result = service.get(default_id(), ().auth()).await;
test_not_found(&result, &default_id());
}
@ -207,7 +206,7 @@ async fn test_get_not_found() {
async fn test_get_no_permission() {
let deps = build_dependencies(false, "hr");
let service = deps.build_service();
let result = service.get(default_id(), ()).await;
let result = service.get(default_id(), ().auth()).await;
test_forbidden(&result);
}
@ -241,7 +240,7 @@ async fn test_create() {
created: None,
..default_booking()
},
(),
().auth(),
)
.await;
assert!(result.is_ok());
@ -265,7 +264,7 @@ async fn test_create_no_permission() {
version: Uuid::nil(),
..default_booking()
},
(),
().auth(),
)
.await;
test_forbidden(&result);
@ -281,7 +280,7 @@ async fn test_create_with_id() {
version: Uuid::nil(),
..default_booking()
},
(),
().auth(),
)
.await;
test_zero_id_error(&result);
@ -297,7 +296,7 @@ async fn test_create_with_version() {
id: Uuid::nil(),
..default_booking()
},
(),
().auth(),
)
.await;
test_zero_version_error(&result);
@ -314,7 +313,7 @@ async fn test_create_with_created_fail() {
version: Uuid::nil(),
..default_booking()
},
(),
().auth(),
)
.await;
test_validation_error(
@ -330,7 +329,7 @@ async fn test_create_sales_person_does_not_exist() {
deps.sales_person_service.checkpoint();
deps.sales_person_service
.expect_exists()
.with(eq(default_sales_person_id()), eq(()))
.with(eq(default_sales_person_id()), eq(().auth()))
.returning(|_, _| Ok(false));
let service = deps.build_service();
let result = service
@ -341,7 +340,7 @@ async fn test_create_sales_person_does_not_exist() {
created: None,
..default_booking()
},
(),
().auth(),
)
.await;
dbg!(&result);
@ -369,7 +368,7 @@ async fn test_create_booking_data_already_exists() {
created: None,
..default_booking()
},
(),
().auth(),
)
.await;
test_validation_error(
@ -386,7 +385,7 @@ async fn test_create_slot_does_not_exist() {
deps.slot_service.checkpoint();
deps.slot_service
.expect_exists()
.with(eq(default_slot_id()), eq(()))
.with(eq(default_slot_id()), eq(().auth()))
.returning(|_, _| Ok(false));
let service = deps.build_service();
let result = service
@ -397,7 +396,7 @@ async fn test_create_slot_does_not_exist() {
created: None,
..default_booking()
},
(),
().auth(),
)
.await;
test_validation_error(
@ -411,7 +410,7 @@ async fn test_create_slot_does_not_exist() {
async fn test_delete_no_permission() {
let deps = build_dependencies(false, "hr");
let service = deps.build_service();
let result = service.delete(default_id(), ()).await;
let result = service.delete(default_id(), ().auth()).await;
test_forbidden(&result);
}
@ -423,7 +422,7 @@ async fn test_delete_not_found() {
.with(eq(default_id()))
.returning(|_| Ok(None));
let service = deps.build_service();
let result = service.delete(default_id(), ()).await;
let result = service.delete(default_id(), ().auth()).await;
test_not_found(&result, &default_id());
}
@ -450,7 +449,7 @@ async fn test_delete() {
.with(eq("booking-version"))
.returning(|_| alternate_version());
let service = deps.build_service();
let result = service.delete(default_id(), ()).await;
let result = service.delete(default_id(), ().auth()).await;
assert!(result.is_ok());
assert_eq!(result.unwrap(), ());
}

View file

@ -1,4 +1,4 @@
use service::ValidationFailureItem;
use service::{permission::Authentication, ValidationFailureItem};
use time::{Date, Month, PrimitiveDateTime, Time};
use uuid::Uuid;
@ -114,3 +114,13 @@ pub fn generate_default_datetime() -> PrimitiveDateTime {
Time::from_hms(23, 42, 0).unwrap(),
)
}
pub trait NoneTypeExt {
fn auth(&self) -> Authentication<()>;
}
impl NoneTypeExt for () {
fn auth(&self) -> Authentication<()> {
Authentication::Context(())
}
}

View file

@ -30,7 +30,7 @@ async fn test_check_permission() {
let permission_service =
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
let result = permission_service.check_permission("hello", ()).await;
let result = permission_service.check_permission("hello", ().auth()).await;
result.expect("Expected successful authorization");
}
@ -40,7 +40,7 @@ async fn test_check_permission_denied() {
let permission_service =
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
let result = permission_service.check_permission("hello", ()).await;
let result = permission_service.check_permission("hello", ().auth()).await;
test_forbidden(&result);
}
@ -71,7 +71,7 @@ async fn test_create_user() {
let permission_service =
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
permission_service
.create_user("testuser", ())
.create_user("testuser", ().auth())
.await
.expect("Extected successful user creation");
}
@ -81,7 +81,7 @@ async fn test_create_user_without_permission() {
let (permission_dao, user_service) = generate_dependencies_mocks_permission(false, "admin");
let permission_service =
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
test_forbidden(&permission_service.create_user("testuser", ()).await);
test_forbidden(&permission_service.create_user("testuser", ().auth()).await);
}
#[tokio::test]
@ -97,7 +97,7 @@ async fn test_delete_user() {
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
permission_service
.delete_user("testuser", ())
.delete_user("testuser", ().auth())
.await
.expect("Expected successful delete");
}
@ -106,7 +106,7 @@ async fn test_delete_user_without_permission() {
let (permission_dao, user_service) = generate_dependencies_mocks_permission(false, "admin");
let permission_service =
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
test_forbidden(&permission_service.delete_user("testuser", ()).await);
test_forbidden(&permission_service.delete_user("testuser", ().auth()).await);
}
#[tokio::test]
@ -126,7 +126,7 @@ async fn test_create_role() {
let permission_service =
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
permission_service
.create_role("testrole", ())
.create_role("testrole", ().auth())
.await
.expect("Extected successful role creation");
}
@ -136,7 +136,7 @@ async fn test_create_role_without_permission() {
let (permission_dao, user_service) = generate_dependencies_mocks_permission(false, "admin");
let permission_service =
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
test_forbidden(&permission_service.create_role("testrole", ()).await);
test_forbidden(&permission_service.create_role("testrole", ().auth()).await);
}
#[tokio::test]
@ -152,7 +152,7 @@ async fn test_delete_role() {
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
permission_service
.delete_role("testrole", ())
.delete_role("testrole", ().auth())
.await
.expect("Expected successful delete");
}
@ -162,7 +162,7 @@ async fn test_delete_role_without_permission() {
let (permission_dao, user_service) = generate_dependencies_mocks_permission(false, "admin");
let permission_service =
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
test_forbidden(&permission_service.delete_role("testrole", ()).await);
test_forbidden(&permission_service.delete_role("testrole", ().auth()).await);
}
#[tokio::test]
@ -183,7 +183,7 @@ async fn test_create_privilege() {
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
permission_service
.create_privilege("testprivilege", ())
.create_privilege("testprivilege", ().auth())
.await
.expect("Extected successful privilege creation");
}
@ -194,7 +194,7 @@ async fn test_create_privilege_without_permission() {
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
test_forbidden(
&permission_service
.create_privilege("testprivilege", ())
.create_privilege("testprivilege", ().auth())
.await,
);
}
@ -212,7 +212,7 @@ async fn test_delete_privilege() {
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
permission_service
.delete_privilege("testprivilege", ())
.delete_privilege("testprivilege", ().auth())
.await
.expect("Expected successful delete");
}
@ -224,7 +224,7 @@ async fn test_delete_privilege_without_permission() {
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
test_forbidden(
&permission_service
.delete_privilege("testprivilege", ())
.delete_privilege("testprivilege", ().auth())
.await,
);
}
@ -242,7 +242,7 @@ async fn test_add_user_role() {
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
permission_service
.add_user_role("testuser", "testrole", ())
.add_user_role("testuser", "testrole", ().auth())
.await
.expect("Extected successful user role creation");
}
@ -254,7 +254,7 @@ async fn test_add_user_role_without_permission() {
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
test_forbidden(
&permission_service
.add_user_role("testuser", "testrole", ())
.add_user_role("testuser", "testrole", ().auth())
.await,
);
}
@ -276,7 +276,7 @@ async fn test_add_role_privilege() {
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
permission_service
.add_role_privilege("testrole", "testprivilege", ())
.add_role_privilege("testrole", "testprivilege", ().auth())
.await
.expect("Extected successful role privilege creation");
}
@ -288,7 +288,7 @@ async fn test_add_role_privilege_without_permission() {
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
test_forbidden(
&permission_service
.add_role_privilege("testrole", "testprivilege", ())
.add_role_privilege("testrole", "testprivilege", ().auth())
.await,
);
}
@ -306,7 +306,7 @@ async fn test_delete_role_privilege() {
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
permission_service
.delete_role_privilege("testrole", "testprivilege", ())
.delete_role_privilege("testrole", "testprivilege", ().auth())
.await
.expect("Extected successful role privilege deletion");
}
@ -318,7 +318,7 @@ async fn test_delete_role_privilege_without_permission() {
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
test_forbidden(
&permission_service
.delete_role_privilege("testrole", "testprivilege", ())
.delete_role_privilege("testrole", "testprivilege", ().auth())
.await,
);
}
@ -336,7 +336,7 @@ async fn test_delete_user_role() {
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
permission_service
.delete_user_role("testuser", "testrole", ())
.delete_user_role("testuser", "testrole", ().auth())
.await
.expect("Extected successful user role deletion");
}
@ -348,7 +348,7 @@ async fn test_delete_user_role_without_permission() {
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
test_forbidden(
&permission_service
.delete_user_role("testuser", "testrole", ())
.delete_user_role("testuser", "testrole", ().auth())
.await,
);
}
@ -371,7 +371,7 @@ async fn test_all_roles() {
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
let all_roles = permission_service
.get_all_roles(())
.get_all_roles(().auth())
.await
.expect("Expected roles successfully");
assert_eq!(all_roles.len(), 2);
@ -384,7 +384,7 @@ async fn test_all_roles_without_permission() {
let (permission_dao, user_service) = generate_dependencies_mocks_permission(false, "admin");
let permission_service =
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
test_forbidden(&permission_service.get_all_roles(()).await);
test_forbidden(&permission_service.get_all_roles(().auth()).await);
}
#[tokio::test]
@ -405,7 +405,7 @@ async fn test_all_users() {
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
let all_users = permission_service
.get_all_users(())
.get_all_users(().auth())
.await
.expect("Expected users successfully");
@ -419,7 +419,7 @@ async fn test_all_users_without_permission() {
let (permission_dao, user_service) = generate_dependencies_mocks_permission(false, "admin");
let permission_service =
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
test_forbidden(&permission_service.get_all_users(()).await);
test_forbidden(&permission_service.get_all_users(().auth()).await);
}
#[tokio::test]
@ -443,7 +443,7 @@ async fn test_all_privileges() {
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
let all_privileges = permission_service
.get_all_privileges(())
.get_all_privileges(().auth())
.await
.expect("Expected privileges successfully");
@ -457,5 +457,5 @@ async fn test_all_privileges_without_permission() {
let (permission_dao, user_service) = generate_dependencies_mocks_permission(false, "admin");
let permission_service =
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
test_forbidden(&permission_service.get_all_privileges(()).await);
test_forbidden(&permission_service.get_all_privileges(().auth()).await);
}

View file

@ -42,7 +42,7 @@ pub fn build_dependencies(permission: bool, role: &'static str) -> SalesPersonSe
let mut permission_service = MockPermissionService::new();
permission_service
.expect_check_permission()
.with(eq(role), eq(()))
.with(eq(role), eq(().auth()))
.returning(move |_, _| {
if permission {
Ok(())
@ -125,7 +125,7 @@ async fn test_get_all() {
.into())
});
let sales_person_service = dependencies.build_service();
let result = sales_person_service.get_all(()).await.unwrap();
let result = sales_person_service.get_all(().auth()).await.unwrap();
assert_eq!(2, result.len());
assert_eq!(default_sales_person(), result[0]);
assert_eq!(
@ -142,7 +142,7 @@ async fn test_get_all() {
async fn test_get_all_no_permission() {
let dependencies = build_dependencies(false, "hr");
let sales_person_service = dependencies.build_service();
let result = sales_person_service.get_all(()).await;
let result = sales_person_service.get_all(().auth()).await;
test_forbidden(&result);
}
@ -156,7 +156,7 @@ async fn test_get() {
.times(1)
.returning(|_| Ok(Some(default_sales_person_entity())));
let sales_person_service = dependencies.build_service();
let result = sales_person_service.get(default_id(), ()).await;
let result = sales_person_service.get(default_id(), ().auth()).await;
assert_eq!(default_sales_person(), result.unwrap());
}
@ -164,7 +164,7 @@ async fn test_get() {
async fn test_get_no_permission() {
let dependencies = build_dependencies(false, "hr");
let sales_person_service = dependencies.build_service();
let result = sales_person_service.get(default_id(), ()).await;
let result = sales_person_service.get(default_id(), ().auth()).await;
test_forbidden(&result);
}
@ -178,7 +178,7 @@ async fn test_get_not_found() {
.times(1)
.returning(|_| Ok(None));
let sales_person_service = dependencies.build_service();
let result = sales_person_service.get(default_id(), ()).await;
let result = sales_person_service.get(default_id(), ().auth()).await;
test_not_found(&result, &default_id());
}
@ -214,7 +214,7 @@ async fn test_create() {
version: Uuid::nil(),
..default_sales_person()
},
(),
().auth(),
)
.await
.unwrap();
@ -232,7 +232,7 @@ async fn test_create_no_permission() {
version: Uuid::nil(),
..default_sales_person()
},
(),
().auth(),
)
.await;
test_forbidden(&result);
@ -259,7 +259,7 @@ async fn test_create_validation() {
version: Uuid::nil(),
..default_sales_person()
},
(),
().auth(),
)
.await;
test_zero_id_error(&result);
@ -270,7 +270,7 @@ async fn test_create_validation() {
id: Uuid::nil(),
..default_sales_person()
},
(),
().auth(),
)
.await;
test_zero_version_error(&result);
@ -286,7 +286,7 @@ async fn test_update_no_permission() {
name: "Jane Doe".into(),
..default_sales_person()
},
(),
().auth(),
)
.await;
test_forbidden(&result);
@ -307,7 +307,7 @@ async fn test_update_not_found() {
name: "Jane Doe".into(),
..default_sales_person()
},
(),
().auth(),
)
.await;
test_not_found(&result, &default_id());
@ -328,7 +328,7 @@ async fn test_update_conflicts() {
version: alternate_version(),
..default_sales_person()
},
(),
().auth(),
)
.await;
test_conflicts(
@ -357,7 +357,7 @@ async fn test_update_deleted_no_allowed() {
)),
..default_sales_person()
},
(),
().auth(),
)
.await;
test_validation_error(
@ -399,7 +399,7 @@ async fn test_update_inactive() {
inactive: true,
..default_sales_person()
},
(),
().auth(),
)
.await
.unwrap();
@ -445,7 +445,7 @@ async fn test_update_name() {
name: "Jane Doe".into(),
..default_sales_person()
},
(),
().auth(),
)
.await
.unwrap();
@ -488,7 +488,7 @@ async fn test_delete() {
.with(eq("sales-person-version"))
.returning(|_| alternate_version());
let sales_person_service = dependencies.build_service();
let result = sales_person_service.delete(default_id(), ()).await;
let result = sales_person_service.delete(default_id(), ().auth()).await;
assert!(result.is_ok());
}
@ -501,7 +501,7 @@ async fn test_delete_no_permission() {
.with(eq(default_id()))
.returning(|_| Ok(Some(default_sales_person_entity())));
let sales_person_service = dependencies.build_service();
let result = sales_person_service.delete(default_id(), ()).await;
let result = sales_person_service.delete(default_id(), ().auth()).await;
test_forbidden(&result);
}
@ -514,7 +514,7 @@ async fn test_delete_not_found() {
.with(eq(default_id()))
.returning(|_| Ok(None));
let sales_person_service = dependencies.build_service();
let result = sales_person_service.delete(default_id(), ()).await;
let result = sales_person_service.delete(default_id(), ().auth()).await;
test_not_found(&result, &default_id());
}
@ -527,7 +527,7 @@ async fn test_exists() {
.with(eq(default_id()))
.returning(|_| Ok(Some(default_sales_person_entity())));
let sales_person_service = dependencies.build_service();
let result = sales_person_service.exists(default_id(), ()).await.unwrap();
let result = sales_person_service.exists(default_id(), ().auth()).await.unwrap();
assert!(result);
let mut dependencies = build_dependencies(true, "hr");
@ -537,6 +537,6 @@ async fn test_exists() {
.expect_find_by_id()
.with(eq(default_id()))
.returning(|_| Ok(None));
let result = sales_person_service.exists(default_id(), ()).await.unwrap();
let result = sales_person_service.exists(default_id(), ().auth()).await.unwrap();
assert_eq!(false, !result);
}

View file

@ -72,7 +72,7 @@ pub fn build_dependencies(permission: bool, role: &'static str) -> SlotServiceDe
let mut permission_service = MockPermissionService::new();
permission_service
.expect_check_permission()
.with(eq(role), eq(()))
.with(eq(role), eq(().auth()))
.returning(move |_, _| {
if permission {
Ok(())
@ -122,7 +122,7 @@ async fn test_get_slots() {
let slot_service = dependencies.build_service();
let result = slot_service.get_slots(()).await;
let result = slot_service.get_slots(().auth()).await;
assert!(result.is_ok());
let result = result.unwrap();
@ -146,7 +146,7 @@ async fn test_get_slots_sales_role() {
.expect_get_slots()
.returning(|| Ok(Arc::new([])));
let slot_service = dependencies.build_service();
let result = slot_service.get_slots(()).await;
let result = slot_service.get_slots(().auth()).await;
assert!(result.is_ok());
}
@ -158,7 +158,7 @@ async fn test_get_slots_no_permission() {
.expect_get_slots()
.returning(|| Ok(Arc::new([])));
let slot_service = dependencies.build_service();
let result = slot_service.get_slots(()).await;
let result = slot_service.get_slots(().auth()).await;
test_forbidden(&result);
}
@ -172,7 +172,7 @@ async fn test_get_slot() {
.times(1)
.returning(|_| Ok(Some(generate_default_slot_entity())));
let slot_service = dependencies.build_service();
let result = slot_service.get_slot(&default_id(), ()).await;
let result = slot_service.get_slot(&default_id(), ().auth()).await;
assert!(result.is_ok());
let result = result.unwrap();
assert_eq!(result, generate_default_slot());
@ -188,7 +188,7 @@ async fn test_get_slot_sales_role() {
.times(1)
.returning(|_| Ok(Some(generate_default_slot_entity())));
let slot_service = dependencies.build_service();
let result = slot_service.get_slot(&default_id(), ()).await;
let result = slot_service.get_slot(&default_id(), ().auth()).await;
assert!(result.is_ok());
}
@ -202,7 +202,7 @@ async fn test_get_slot_not_found() {
.times(1)
.returning(|_| Ok(None));
let slot_service = dependencies.build_service();
let result = slot_service.get_slot(&default_id(), ()).await;
let result = slot_service.get_slot(&default_id(), ().auth()).await;
test_not_found(&result, &default_id());
}
@ -210,7 +210,7 @@ async fn test_get_slot_not_found() {
async fn test_get_slot_no_permission() {
let dependencies = build_dependencies(false, "hr");
let slot_service = dependencies.build_service();
let result = slot_service.get_slot(&default_id(), ()).await;
let result = slot_service.get_slot(&default_id(), ().auth()).await;
test_forbidden(&result);
}
@ -246,7 +246,7 @@ async fn test_create_slot() {
version: Uuid::nil(),
..generate_default_slot()
},
(),
().auth(),
)
.await;
assert!(result.is_ok());
@ -257,7 +257,7 @@ async fn test_create_slot() {
async fn test_create_slot_no_permission() {
let dependencies = build_dependencies(false, "hr");
let slot_service = dependencies.build_service();
let result = slot_service.create_slot(&generate_default_slot(), ()).await;
let result = slot_service.create_slot(&generate_default_slot(), ().auth()).await;
test_forbidden(&result);
}
@ -281,7 +281,7 @@ async fn test_create_slot_non_zero_id() {
version: Uuid::nil(),
..generate_default_slot()
},
(),
().auth(),
)
.await;
test_zero_id_error(&result);
@ -307,7 +307,7 @@ async fn test_create_slot_non_zero_version() {
id: Uuid::nil(),
..generate_default_slot()
},
(),
().auth(),
)
.await;
test_zero_version_error(&result);
@ -360,7 +360,7 @@ async fn test_create_slot_intersects() {
to: Time::from_hms(12, 0, 0).unwrap(),
..generate_default_slot()
},
(),
().auth(),
)
.await;
assert!(result.is_ok());
@ -375,7 +375,7 @@ async fn test_create_slot_intersects() {
to: Time::from_hms(11, 0, 0).unwrap(),
..generate_default_slot()
},
(),
().auth(),
)
.await;
test_overlapping_time_range_error(&result);
@ -390,7 +390,7 @@ async fn test_create_slot_intersects() {
to: Time::from_hms(11, 30, 0).unwrap(),
..generate_default_slot()
},
(),
().auth(),
)
.await;
test_overlapping_time_range_error(&result);
@ -405,7 +405,7 @@ async fn test_create_slot_intersects() {
to: Time::from_hms(12, 30, 0).unwrap(),
..generate_default_slot()
},
(),
().auth(),
)
.await;
test_overlapping_time_range_error(&result);
@ -420,7 +420,7 @@ async fn test_create_slot_intersects() {
to: Time::from_hms(10, 45, 0).unwrap(),
..generate_default_slot()
},
(),
().auth(),
)
.await;
test_overlapping_time_range_error(&result);
@ -435,7 +435,7 @@ async fn test_create_slot_intersects() {
to: Time::from_hms(11, 0, 0).unwrap(),
..generate_default_slot()
},
(),
().auth(),
)
.await;
test_overlapping_time_range_error(&result);
@ -450,7 +450,7 @@ async fn test_create_slot_intersects() {
day_of_week: DayOfWeek::Tuesday.into(),
..generate_default_slot()
},
(),
().auth(),
)
.await;
assert!(result.is_ok());
@ -478,7 +478,7 @@ async fn test_create_slot_time_order() {
to: Time::from_hms(11, 00, 00).unwrap(),
..generate_default_slot()
},
(),
().auth(),
)
.await;
test_time_order_wrong(&result);
@ -506,7 +506,7 @@ async fn test_create_slot_date_order() {
valid_to: Some(Date::from_calendar_date(2022, Month::January, 1).unwrap()),
..generate_default_slot()
},
(),
().auth(),
)
.await;
test_date_order_wrong(&result);
@ -538,7 +538,7 @@ async fn test_delete_slot() {
.returning(|_, _| Ok(()));
let slot_service = dependencies.build_service();
let result = slot_service.delete_slot(&default_id(), ()).await;
let result = slot_service.delete_slot(&default_id(), ().auth()).await;
assert!(result.is_ok());
}
@ -546,7 +546,7 @@ async fn test_delete_slot() {
async fn test_delete_slot_no_permission() {
let dependencies = build_dependencies(false, "hr");
let slot_service = dependencies.build_service();
let result = slot_service.delete_slot(&default_id(), ()).await;
let result = slot_service.delete_slot(&default_id(), ().auth()).await;
test_forbidden(&result);
}
@ -560,7 +560,7 @@ async fn test_delete_slot_not_found() {
.times(1)
.returning(|_| Ok(None));
let slot_service = dependencies.build_service();
let result = slot_service.delete_slot(&default_id(), ()).await;
let result = slot_service.delete_slot(&default_id(), ().auth()).await;
test_not_found(&result, &default_id());
}
@ -568,7 +568,7 @@ async fn test_delete_slot_not_found() {
async fn test_update_slot_no_permission() {
let dependencies = build_dependencies(false, "hr");
let slot_service = dependencies.build_service();
let result = slot_service.update_slot(&generate_default_slot(), ()).await;
let result = slot_service.update_slot(&generate_default_slot(), ().auth()).await;
test_forbidden(&result);
}
@ -582,7 +582,7 @@ async fn test_update_slot_not_found() {
.times(1)
.returning(|_| Ok(None));
let slot_service = dependencies.build_service();
let result = slot_service.update_slot(&generate_default_slot(), ()).await;
let result = slot_service.update_slot(&generate_default_slot(), ().auth()).await;
test_not_found(&result, &default_id());
}
@ -601,7 +601,7 @@ async fn test_update_slot_version_mismatch() {
version: uuid!("86DE856C-D176-4F1F-A4FE-0D9844C02C04"),
..generate_default_slot()
},
(),
().auth(),
)
.await;
test_conflicts(
@ -651,7 +651,7 @@ async fn test_update_slot_valid_to() {
),
..generate_default_slot()
},
(),
().auth(),
)
.await;
dbg!(&result);
@ -676,7 +676,7 @@ async fn test_update_slot_valid_to_before_valid_from() {
),
..generate_default_slot()
},
(),
().auth(),
)
.await;
test_date_order_wrong(&result);
@ -722,7 +722,7 @@ async fn test_update_slot_deleted() {
)),
..generate_default_slot()
},
(),
().auth(),
)
.await;
assert!(result.is_ok());
@ -743,7 +743,7 @@ async fn test_update_slot_day_of_week_forbidden() {
day_of_week: service::slot::DayOfWeek::Friday,
..generate_default_slot()
},
(),
().auth(),
)
.await;
test_validation_error(
@ -777,7 +777,7 @@ async fn test_update_to_forbidden_when_not_none() {
),
..generate_default_slot()
},
(),
().auth(),
)
.await;
test_validation_error(
@ -802,7 +802,7 @@ async fn test_update_from_forbidden() {
from: time::Time::from_hms(14, 0, 0).unwrap(),
..generate_default_slot()
},
(),
().auth(),
)
.await;
test_validation_error(
@ -827,7 +827,7 @@ async fn test_update_to_forbidden() {
to: time::Time::from_hms(14, 0, 0).unwrap(),
..generate_default_slot()
},
(),
().auth(),
)
.await;
test_validation_error(
@ -853,7 +853,7 @@ async fn test_update_valid_from_forbidden() {
.unwrap(),
..generate_default_slot()
},
(),
().auth(),
)
.await;
test_validation_error(
@ -880,7 +880,7 @@ async fn test_update_valid_multiple_forbidden_changes() {
from: time::Time::from_hms(14, 0, 0).unwrap(),
..generate_default_slot()
},
(),
().auth(),
)
.await;
test_validation_error(