feat: mock set_user_admin service and test init utils for cmd and event
This commit is contained in:
parent
5bdc11e232
commit
f73ff9b36e
4 changed files with 59 additions and 30 deletions
|
@ -34,21 +34,7 @@ mod tests {
|
||||||
async fn test_cmd() {
|
async fn test_cmd() {
|
||||||
let username = "realaravinth";
|
let username = "realaravinth";
|
||||||
|
|
||||||
SetAdminCommand::new(
|
SetAdminCommand::get_cmd();
|
||||||
UserBuilder::default()
|
|
||||||
.first_name(username.into())
|
|
||||||
.last_name(username.into())
|
|
||||||
.email(username.into())
|
|
||||||
.hashed_password(username.into())
|
|
||||||
.is_verified(true)
|
|
||||||
.email_verified(false)
|
|
||||||
.is_admin(true)
|
|
||||||
.deleted(false)
|
|
||||||
.user_id(UUID)
|
|
||||||
.build()
|
|
||||||
.unwrap(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
SetAdminCommand::new(
|
SetAdminCommand::new(
|
||||||
|
@ -69,4 +55,25 @@ mod tests {
|
||||||
Some(IdentityCommandError::PermissionDenied)
|
Some(IdentityCommandError::PermissionDenied)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl SetAdminCommand {
|
||||||
|
pub fn get_cmd() -> Self {
|
||||||
|
let u = User::default();
|
||||||
|
Self::new(
|
||||||
|
UserBuilder::default()
|
||||||
|
.first_name(u.first_name().clone())
|
||||||
|
.last_name(u.last_name().clone())
|
||||||
|
.email(u.email().clone())
|
||||||
|
.hashed_password(u.hashed_password().clone())
|
||||||
|
.is_verified(true)
|
||||||
|
.email_verified(false)
|
||||||
|
.is_admin(true)
|
||||||
|
.deleted(false)
|
||||||
|
.user_id(*u.user_id())
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,3 +17,18 @@ impl UserPromotedToAdminEvent {
|
||||||
Self { promoted_by_user }
|
Self { promoted_by_user }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use crate::identity::application::services::set_user_admin::command::SetAdminCommand;
|
||||||
|
|
||||||
|
impl UserPromotedToAdminEvent {
|
||||||
|
pub fn get_event(cmd: &SetAdminCommand) -> Self {
|
||||||
|
Self {
|
||||||
|
promoted_by_user: cmd.promoted_by_user().clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
use mockall::predicate::*;
|
||||||
|
use mockall::*;
|
||||||
|
|
||||||
pub mod command;
|
pub mod command;
|
||||||
pub mod events;
|
pub mod events;
|
||||||
|
@ -8,6 +10,7 @@ pub mod service;
|
||||||
|
|
||||||
use super::errors::*;
|
use super::errors::*;
|
||||||
|
|
||||||
|
#[automock]
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
pub trait SetUserAdminUseCase: Send + Sync {
|
pub trait SetUserAdminUseCase: Send + Sync {
|
||||||
async fn set_user_admin(
|
async fn set_user_admin(
|
||||||
|
|
|
@ -26,26 +26,30 @@ mod tests {
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_service() {
|
async fn test_service() {
|
||||||
let username = "realaravinth";
|
|
||||||
|
|
||||||
let s = SetUserAdminService;
|
let s = SetUserAdminService;
|
||||||
let u = UserBuilder::default()
|
|
||||||
.first_name(username.into())
|
|
||||||
.last_name(username.into())
|
|
||||||
.email(username.into())
|
|
||||||
.hashed_password(username.into())
|
|
||||||
.is_verified(true)
|
|
||||||
.email_verified(false)
|
|
||||||
.is_admin(true)
|
|
||||||
.deleted(false)
|
|
||||||
.user_id(UUID)
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let cmd = command::SetAdminCommand::new(u).unwrap();
|
let cmd = command::SetAdminCommand::get_cmd();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
s.set_user_admin(cmd.clone()).await.promoted_by_user(),
|
s.set_user_admin(cmd.clone()).await.promoted_by_user(),
|
||||||
cmd.promoted_by_user()
|
cmd.promoted_by_user()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl SetUserAdminService {
|
||||||
|
pub fn mock_service(
|
||||||
|
times: Option<usize>,
|
||||||
|
cmd: command::SetAdminCommand,
|
||||||
|
) -> SetUserAdminServiceObj {
|
||||||
|
let mut m = MockSetUserAdminUseCase::default();
|
||||||
|
let res = events::UserPromotedToAdminEvent::get_event(&cmd);
|
||||||
|
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_set_user_admin().times(times).return_const(res);
|
||||||
|
} else {
|
||||||
|
m.expect_set_user_admin().return_const(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::sync::Arc::new(m)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue