feat: t mock update_email service and test init utils for cmd and event

This commit is contained in:
Aravinth Manivannan 2025-01-21 14:24:18 +05:30
parent 9d1be6a6e1
commit 4c9b75b218
Signed by: realaravinth
GPG key ID: F8F50389936984FF
4 changed files with 60 additions and 15 deletions

View file

@ -93,4 +93,27 @@ mod tests {
Some(IdentityCommandError::WrongPassword)
);
}
// command
impl UpdateEmailCommand {
pub fn get_cmd() -> Self {
let config = argon2_creds::Config::default();
let password = "adsfasdfasd";
let first_name = "john";
let user_id = UUID;
let new_email = "newemail@example.com".to_string();
let hashed_password = config.password(password).unwrap();
UpdateEmailCommand::new(
new_email.clone(),
user_id,
first_name.into(),
password.into(),
&hashed_password,
&config,
)
.unwrap()
}
}
}

View file

@ -15,3 +15,18 @@ impl EmailUpdatedEvent {
Self { new_email }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::identity::application::services::update_email::command::UpdateEmailCommand;
impl EmailUpdatedEvent {
pub fn get_event(cmd: &UpdateEmailCommand) -> Self {
Self {
new_email: cmd.new_email().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 UpdateEmailUseCase: Send + Sync {
async fn update_email(

View file

@ -68,23 +68,27 @@ mod tests {
use crate::tests::bdd::*;
use crate::utils::uuid::tests::UUID;
impl UpdateEmailService {
pub fn mock_service(
times: Option<usize>,
cmd: command::UpdateEmailCommand,
) -> UpdateEmailServiceObj {
let mut m = MockUpdateEmailUseCase::default();
let res = events::EmailUpdatedEvent::get_event(&cmd);
if let Some(times) = times {
m.expect_update_email().times(times).return_const(Ok(res));
} else {
m.expect_update_email().return_const(Ok(res));
}
std::sync::Arc::new(m)
}
}
#[actix_rt::test]
async fn test_service() {
let user_id = UUID;
let new_email = "john@example.com".to_string();
let password = "password";
let config = argon2_creds::Config::default();
let hashed_password = config.password(password).unwrap();
let cmd = command::UpdateEmailCommand::new(
new_email.clone(),
user_id,
"john".into(),
password.into(),
&hashed_password,
&config,
)
.unwrap();
let cmd = command::UpdateEmailCommand::get_cmd();
// happy case
{