Add unit tests for the services
This commit is contained in:
parent
926ac006e7
commit
3b20d12ba1
9 changed files with 231 additions and 12 deletions
|
|
@ -3,6 +3,10 @@ name = "service_impl"
|
|||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
async-trait = "0.1.80"
|
||||
mockall = "0.12.1"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies.service]
|
||||
|
|
@ -10,3 +14,7 @@ path = "../service"
|
|||
|
||||
[dependencies.dao]
|
||||
path = "../dao"
|
||||
|
||||
[dev-dependencies.tokio]
|
||||
version = "1.37.0"
|
||||
features = ["full"]
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use async_trait::async_trait;
|
||||
|
||||
pub struct HelloServiceImpl<HelloDao, PermissionService>
|
||||
where
|
||||
HelloDao: dao::HelloDao + Sync + Send,
|
||||
|
|
@ -54,6 +56,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<PermissionDao, UserService> service::PermissionService
|
||||
for PermissionServiceImpl<PermissionDao, UserService>
|
||||
where
|
||||
|
|
@ -76,8 +79,109 @@ where
|
|||
|
||||
pub struct UserServiceDev;
|
||||
|
||||
#[async_trait]
|
||||
impl service::UserService for UserServiceDev {
|
||||
async fn current_user(&self) -> Result<Arc<str>, service::ServiceError> {
|
||||
Ok("DEVUSER".into())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use mockall::predicate::eq;
|
||||
use service::{HelloService, MockPermissionService, PermissionService};
|
||||
use tokio;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_hello_successful() {
|
||||
let mut hello_dao = dao::MockHelloDao::new();
|
||||
hello_dao
|
||||
.expect_get_hello()
|
||||
.times(1)
|
||||
.returning(|| Ok("Hello, world!".into()));
|
||||
let mut permission_service = MockPermissionService::new();
|
||||
permission_service
|
||||
.expect_check_permission()
|
||||
.times(1)
|
||||
.returning(|_| Ok(()));
|
||||
|
||||
let hello_service =
|
||||
HelloServiceImpl::new(Arc::new(hello_dao), Arc::new(permission_service));
|
||||
assert_eq!(
|
||||
"Hello, world!",
|
||||
hello_service.hello().await.unwrap().as_ref()
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_hello_no_permission() {
|
||||
let hello_dao = dao::MockHelloDao::new();
|
||||
|
||||
let mut permission_service = MockPermissionService::new();
|
||||
permission_service
|
||||
.expect_check_permission()
|
||||
.times(1)
|
||||
.returning(|_| Err(service::ServiceError::Forbidden));
|
||||
|
||||
let hello_service =
|
||||
HelloServiceImpl::new(Arc::new(hello_dao), Arc::new(permission_service));
|
||||
if let Err(service::ServiceError::Forbidden) = hello_service.hello().await {
|
||||
// All good
|
||||
} else {
|
||||
panic!("Expected forbidden error");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_check_permission() {
|
||||
let mut permission_dao = dao::MockPermissionDao::new();
|
||||
permission_dao
|
||||
.expect_has_privilege()
|
||||
.with(eq("DEVUSER"), eq("hello"))
|
||||
.returning(|_, _| Ok(true));
|
||||
|
||||
let mut user_service = service::MockUserService::new();
|
||||
user_service
|
||||
.expect_current_user()
|
||||
.returning(|| Ok("DEVUSER".into()));
|
||||
|
||||
let permission_service =
|
||||
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
|
||||
let result = permission_service.check_permission("hello").await;
|
||||
result.expect("Expected successful authorization");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_check_permission_denied() {
|
||||
let mut permission_dao = dao::MockPermissionDao::new();
|
||||
permission_dao
|
||||
.expect_has_privilege()
|
||||
.with(eq("DEVUSER"), eq("hello"))
|
||||
.returning(|_, _| Ok(false));
|
||||
|
||||
let mut user_service = service::MockUserService::new();
|
||||
user_service
|
||||
.expect_current_user()
|
||||
.returning(|| Ok("DEVUSER".into()));
|
||||
|
||||
let permission_service =
|
||||
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
|
||||
let result = permission_service.check_permission("hello").await;
|
||||
if let Err(service::ServiceError::Forbidden) = result {
|
||||
// All good
|
||||
} else {
|
||||
panic!("Expected forbidden error");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_user_service_dev() {
|
||||
use service::UserService;
|
||||
let user_service = UserServiceDev;
|
||||
assert_eq!(
|
||||
"DEVUSER",
|
||||
user_service.current_user().await.unwrap().as_ref()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue