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

This commit is contained in:
Aravinth Manivannan 2025-01-21 13:50:53 +05:30
parent 1a0c5e7e91
commit 6d6e3da781
Signed by: realaravinth
GPG key ID: F8F50389936984FF
4 changed files with 39 additions and 3 deletions

View file

@ -27,6 +27,12 @@ impl LoginCommand {
mod tests {
use super::*;
impl LoginCommand {
pub fn get_cmd() -> Self {
LoginCommand { success: true }
}
}
#[test]
fn test_cmd() {
let config = argon2_creds::Config::default();

View file

@ -15,3 +15,17 @@ impl LoginEvent {
Self { success }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::identity::application::services::login::command::*;
impl LoginEvent {
pub fn get_event(cmd: &LoginCommand) -> Self {
Self {
success: *cmd.success(),
}
}
}
}

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 LoginUseCase: Send + Sync {
async fn login(

View file

@ -21,6 +21,21 @@ impl LoginUseCase for LoginService {
mod tests {
use super::*;
impl LoginService {
pub fn mock_service(times: Option<usize>, cmd: command::LoginCommand) -> LoginServiceObj {
let mut m = MockLoginUseCase::default();
let res = events::LoginEvent::get_event(&cmd);
if let Some(times) = times {
m.expect_login().times(times).return_const(res);
} else {
m.expect_login().return_const(res);
}
std::sync::Arc::new(m)
}
}
#[actix_rt::test]
async fn test_service() {
let config = argon2_creds::Config::default();
@ -31,9 +46,7 @@ mod tests {
let s = LoginService;
{
let cmd =
command::LoginCommand::new(username.into(), password.into(), &hashed_password)
.unwrap();
let cmd = command::LoginCommand::get_cmd();
let res = s.login(cmd.clone()).await;
assert_eq!(res.success(), cmd.success());
}