feat: command and event init utils with service mocking
This commit is contained in:
parent
c75029dd72
commit
9fe9799545
4 changed files with 73 additions and 30 deletions
|
@ -48,9 +48,31 @@ impl UnvalidatedRegisterUserCommand {
|
|||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
pub mod tests {
|
||||
use super::*;
|
||||
|
||||
pub const PASSWORD: &str = "sadfasdfasdf";
|
||||
|
||||
impl RegisterUserCommand {
|
||||
pub fn get_command() -> Self {
|
||||
let config = argon2_creds::Config::default();
|
||||
let first_name = "John";
|
||||
let last_name = "Doe";
|
||||
let email = "john@example.com";
|
||||
|
||||
UnvalidatedRegisterUserCommandBuilder::default()
|
||||
.first_name(first_name.into())
|
||||
.last_name(last_name.into())
|
||||
.email(email.into())
|
||||
.password(PASSWORD.into())
|
||||
.confirm_password(PASSWORD.into())
|
||||
.build()
|
||||
.unwrap()
|
||||
.validate(&config)
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cmd() {
|
||||
let config = argon2_creds::Config::default();
|
||||
|
|
|
@ -20,3 +20,25 @@ pub struct UserRegisteredEvent {
|
|||
is_admin: bool,
|
||||
email_verified: bool,
|
||||
}
|
||||
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::{
|
||||
identity::application::services::register_user::command::*, utils::uuid::tests::*,
|
||||
};
|
||||
|
||||
impl UserRegisteredEvent {
|
||||
pub fn get_event(cmd: &RegisterUserCommand) -> Self {
|
||||
Self {
|
||||
first_name: cmd.first_name().clone(),
|
||||
last_name: cmd.last_name().clone(),
|
||||
user_id: UUID,
|
||||
email: cmd.email().clone(),
|
||||
hashed_password: cmd.hashed_password().clone(),
|
||||
is_verified: false,
|
||||
is_admin: false,
|
||||
email_verified: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 RegisterUserUseCase: Send + Sync {
|
||||
async fn register_user(
|
||||
|
|
|
@ -94,22 +94,29 @@ mod tests {
|
|||
use crate::utils::random_string::tests::*;
|
||||
use crate::utils::uuid::tests::*;
|
||||
|
||||
impl RegisterUserService {
|
||||
pub fn mock_service(
|
||||
times: Option<usize>,
|
||||
cmd: command::RegisterUserCommand,
|
||||
) -> RegisterUserServiceObj {
|
||||
let res = events::UserRegisteredEvent::get_event(&cmd);
|
||||
let mut m = MockRegisterUserUseCase::default();
|
||||
|
||||
if let Some(times) = times {
|
||||
m.expect_register_user()
|
||||
.times(times)
|
||||
.return_const(Ok(res.clone()));
|
||||
} else {
|
||||
m.expect_register_user().return_const(Ok(res.clone()));
|
||||
}
|
||||
|
||||
std::sync::Arc::new(m)
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_service() {
|
||||
let username = "realaravinth";
|
||||
let email = format!("{username}@example.com");
|
||||
let password = "password";
|
||||
let config = argon2_creds::Config::default();
|
||||
let cmd = command::UnvalidatedRegisterUserCommandBuilder::default()
|
||||
.first_name(username.into())
|
||||
.last_name(username.into())
|
||||
.email(email)
|
||||
.password(password.into())
|
||||
.confirm_password(password.into())
|
||||
.build()
|
||||
.unwrap()
|
||||
.validate(&config)
|
||||
.unwrap();
|
||||
let cmd = command::RegisterUserCommand::get_command();
|
||||
|
||||
let s = RegisterUserServiceBuilder::default()
|
||||
.db_user_id_exists_adapter(mock_user_id_exists_db_port(
|
||||
|
@ -140,24 +147,13 @@ mod tests {
|
|||
assert_eq!(res.user_id(), &UUID);
|
||||
assert_eq!(res.email(), cmd.email());
|
||||
assert!(!res.is_admin());
|
||||
assert!(argon2_creds::Config::verify(res.hashed_password(), password).unwrap())
|
||||
assert!(
|
||||
argon2_creds::Config::verify(res.hashed_password(), command::tests::PASSWORD).unwrap()
|
||||
)
|
||||
}
|
||||
#[actix_rt::test]
|
||||
async fn test_service_email_exists() {
|
||||
let username = "realaravinth";
|
||||
let email = format!("{username}@example.com");
|
||||
let password = "password";
|
||||
let config = argon2_creds::Config::default();
|
||||
let cmd = command::UnvalidatedRegisterUserCommandBuilder::default()
|
||||
.first_name(username.into())
|
||||
.last_name(username.into())
|
||||
.email(email)
|
||||
.password(password.into())
|
||||
.confirm_password(password.into())
|
||||
.build()
|
||||
.unwrap()
|
||||
.validate(&config)
|
||||
.unwrap();
|
||||
let cmd = command::RegisterUserCommand::get_command();
|
||||
|
||||
let s = RegisterUserServiceBuilder::default()
|
||||
.db_user_id_exists_adapter(mock_user_id_exists_db_port(
|
||||
|
|
Loading…
Reference in a new issue