feat: mock set_user_admin service and test init utils for cmd and event

This commit is contained in:
Aravinth Manivannan 2025-01-21 14:57:39 +05:30
parent 5bdc11e232
commit f73ff9b36e
Signed by: realaravinth
GPG key ID: F8F50389936984FF
4 changed files with 59 additions and 30 deletions

View file

@ -34,21 +34,7 @@ mod tests {
async fn test_cmd() {
let username = "realaravinth";
SetAdminCommand::new(
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();
SetAdminCommand::get_cmd();
assert_eq!(
SetAdminCommand::new(
@ -69,4 +55,25 @@ mod tests {
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()
}
}
}

View file

@ -17,3 +17,18 @@ impl UserPromotedToAdminEvent {
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(),
}
}
}
}

View file

@ -1,6 +1,8 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use mockall::predicate::*;
use mockall::*;
pub mod command;
pub mod events;
@ -8,6 +10,7 @@ pub mod service;
use super::errors::*;
#[automock]
#[async_trait::async_trait]
pub trait SetUserAdminUseCase: Send + Sync {
async fn set_user_admin(

View file

@ -26,26 +26,30 @@ mod tests {
#[actix_rt::test]
async fn test_service() {
let username = "realaravinth";
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!(
s.set_user_admin(cmd.clone()).await.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)
}
}
}