119 lines
3.6 KiB
Rust
119 lines
3.6 KiB
Rust
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
|
//
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
use derive_builder::Builder;
|
|
|
|
use super::*;
|
|
use crate::identity::application::port::output::{
|
|
db::{email_exists::*, get_verification_secret::*},
|
|
mailer::account_validation_link::*,
|
|
};
|
|
|
|
#[derive(Builder)]
|
|
pub struct ResendVerificationEmailService {
|
|
db_email_exists_adapter: EmailExistsOutDBPortObj,
|
|
db_get_verification_secret_adapter: GetVerificationSecretOutDBPortObj,
|
|
mailer_account_validation_link_adapter: AccountValidationLinkOutMailerPortObj,
|
|
}
|
|
|
|
#[async_trait::async_trait]
|
|
impl ResendVerificationEmailUseCase for ResendVerificationEmailService {
|
|
async fn resend_verification_email(
|
|
&self,
|
|
cmd: command::ResendVerificationEmailCommand,
|
|
) -> IdentityResult<()> {
|
|
if self
|
|
.db_email_exists_adapter
|
|
.email_exists(cmd.email())
|
|
.await
|
|
.unwrap()
|
|
{
|
|
return Err(IdentityError::DuplicateEmail);
|
|
}
|
|
|
|
let secret = self
|
|
.db_get_verification_secret_adapter
|
|
.get_verification_secret(cmd.user_id())
|
|
.await
|
|
.unwrap();
|
|
|
|
self.mailer_account_validation_link_adapter
|
|
.account_validation_link(cmd.email(), cmd.first_name(), &secret)
|
|
.await
|
|
.unwrap();
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
use crate::tests::bdd::*;
|
|
use crate::utils::uuid::tests::UUID;
|
|
|
|
#[actix_rt::test]
|
|
async fn test_service() {
|
|
let user_id = UUID;
|
|
let email = "john@example.com".to_string();
|
|
let secret = "asdfasdf";
|
|
let config = argon2_creds::Config::default();
|
|
let cmd = command::ResendVerificationEmailCommand::new(
|
|
UUID,
|
|
"john".into(),
|
|
email.clone(),
|
|
&config,
|
|
)
|
|
.unwrap();
|
|
|
|
let s = ResendVerificationEmailServiceBuilder::default()
|
|
.db_get_verification_secret_adapter(mock_get_verification_secret_db_port(
|
|
IS_CALLED_ONLY_ONCE,
|
|
secret.into(),
|
|
))
|
|
.db_email_exists_adapter(mock_email_exists_db_port(
|
|
IS_CALLED_ONLY_ONCE,
|
|
RETURNS_FALSE,
|
|
))
|
|
.mailer_account_validation_link_adapter(mock_account_validation_link_mailer_port(
|
|
IS_CALLED_ONLY_ONCE,
|
|
))
|
|
.build()
|
|
.unwrap();
|
|
|
|
s.resend_verification_email(cmd.clone()).await.unwrap();
|
|
}
|
|
#[actix_rt::test]
|
|
async fn test_service_email_exists() {
|
|
let user_id = UUID;
|
|
let email = "john@example.com".to_string();
|
|
let secret = "asdfasdf";
|
|
let config = argon2_creds::Config::default();
|
|
let cmd = command::ResendVerificationEmailCommand::new(
|
|
UUID,
|
|
"john".into(),
|
|
email.clone(),
|
|
&config,
|
|
)
|
|
.unwrap();
|
|
|
|
let s = ResendVerificationEmailServiceBuilder::default()
|
|
.db_get_verification_secret_adapter(mock_get_verification_secret_db_port(
|
|
IS_NEVER_CALLED,
|
|
secret.into(),
|
|
))
|
|
.db_email_exists_adapter(mock_email_exists_db_port(IS_CALLED_ONLY_ONCE, RETURNS_TRUE))
|
|
.mailer_account_validation_link_adapter(mock_account_validation_link_mailer_port(
|
|
IS_NEVER_CALLED,
|
|
))
|
|
.build()
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
s.resend_verification_email(cmd.clone()).await.err(),
|
|
Some(IdentityError::DuplicateEmail)
|
|
);
|
|
}
|
|
}
|