From 4c9b75b2188594846e7d3f140d4d15ee6b177893 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Tue, 21 Jan 2025 14:24:18 +0530 Subject: [PATCH] feat: t mock update_email service and test init utils for cmd and event --- .../services/update_email/command.rs | 23 +++++++++++++ .../services/update_email/events.rs | 15 ++++++++ .../application/services/update_email/mod.rs | 3 ++ .../services/update_email/service.rs | 34 +++++++++++-------- 4 files changed, 60 insertions(+), 15 deletions(-) diff --git a/src/identity/application/services/update_email/command.rs b/src/identity/application/services/update_email/command.rs index e5a911e..7cb97c2 100644 --- a/src/identity/application/services/update_email/command.rs +++ b/src/identity/application/services/update_email/command.rs @@ -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() + } + } } diff --git a/src/identity/application/services/update_email/events.rs b/src/identity/application/services/update_email/events.rs index 26a4f7c..8177db5 100644 --- a/src/identity/application/services/update_email/events.rs +++ b/src/identity/application/services/update_email/events.rs @@ -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(), + } + } + } +} diff --git a/src/identity/application/services/update_email/mod.rs b/src/identity/application/services/update_email/mod.rs index 12dde84..3718cb0 100644 --- a/src/identity/application/services/update_email/mod.rs +++ b/src/identity/application/services/update_email/mod.rs @@ -1,6 +1,8 @@ // SPDX-FileCopyrightText: 2024 Aravinth Manivannan // // 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( diff --git a/src/identity/application/services/update_email/service.rs b/src/identity/application/services/update_email/service.rs index 6ee0d02..9e6514b 100644 --- a/src/identity/application/services/update_email/service.rs +++ b/src/identity/application/services/update_email/service.rs @@ -68,23 +68,27 @@ mod tests { use crate::tests::bdd::*; use crate::utils::uuid::tests::UUID; + impl UpdateEmailService { + pub fn mock_service( + times: Option, + 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 {