feat: t mock update_email service and test init utils for cmd and event
This commit is contained in:
parent
9d1be6a6e1
commit
4c9b75b218
4 changed files with 60 additions and 15 deletions
|
@ -93,4 +93,27 @@ mod tests {
|
||||||
Some(IdentityCommandError::WrongPassword)
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,3 +15,18 @@ impl EmailUpdatedEvent {
|
||||||
Self { new_email }
|
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(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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 UpdateEmailUseCase: Send + Sync {
|
pub trait UpdateEmailUseCase: Send + Sync {
|
||||||
async fn update_email(
|
async fn update_email(
|
||||||
|
|
|
@ -68,23 +68,27 @@ mod tests {
|
||||||
use crate::tests::bdd::*;
|
use crate::tests::bdd::*;
|
||||||
use crate::utils::uuid::tests::UUID;
|
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]
|
#[actix_rt::test]
|
||||||
async fn test_service() {
|
async fn test_service() {
|
||||||
let user_id = UUID;
|
let cmd = command::UpdateEmailCommand::get_cmd();
|
||||||
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();
|
|
||||||
|
|
||||||
// happy case
|
// happy case
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue