feat: verify updated email address #23

Merged
realaravinth merged 2 commits from verify-updated-email into master 2024-07-12 18:02:45 +05:30
4 changed files with 231 additions and 0 deletions
Showing only changes of commit 879dc95295 - Show all commits

View file

@ -77,6 +77,7 @@ impl Aggregate for User {
.email(e.email().into()) .email(e.email().into())
.hashed_password(e.hashed_password().into()) .hashed_password(e.hashed_password().into())
.is_admin(e.is_admin().to_owned()) .is_admin(e.is_admin().to_owned())
.email_verified(e.email_verified().to_owned())
.is_verified(e.is_verified().to_owned()) .is_verified(e.is_verified().to_owned())
.deleted(false) .deleted(false)
.build() .build()
@ -89,9 +90,11 @@ impl Aggregate for User {
UserEvent::PasswordUpdated(_) => (), UserEvent::PasswordUpdated(_) => (),
UserEvent::EmailUpdated(e) => { UserEvent::EmailUpdated(e) => {
self.set_email(e.new_email().into()); self.set_email(e.new_email().into());
self.set_email_verified(false);
} }
UserEvent::UserVerified => { UserEvent::UserVerified => {
self.set_is_verified(true); self.set_is_verified(true);
self.set_email_verified(true);
} }
UserEvent::UserPromotedToAdmin(_) => { UserEvent::UserPromotedToAdmin(_) => {
self.set_is_admin(true); self.set_is_admin(true);

View file

@ -0,0 +1,58 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use super::*;
use derive_getters::Getters;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters)]
pub struct VerifyUpdatedEmailCommand {
username: String,
email: String,
}
impl VerifyUpdatedEmailCommand {
pub fn new(
username: String,
email: String,
config: &argon2_creds::Config,
) -> IdentityCommandResult<Self> {
let username = config.username(&username)?;
config.email(&email)?;
Ok(Self { username, email })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cmd() {
let config = argon2_creds::Config::default();
VerifyUpdatedEmailCommand::new(
"realaravinth".into(),
"realaravinth@example.com".into(),
&config,
)
.unwrap();
assert_eq!(
VerifyUpdatedEmailCommand::new("realaravinth".into(), "username".into(), &config,)
.err(),
Some(IdentityCommandError::BadEmail)
);
assert!(matches!(
VerifyUpdatedEmailCommand::new(
"username".into(),
"username@example.com".into(),
&config,
)
.err(),
Some(IdentityCommandError::BadUsername(_))
));
}
}

View file

@ -0,0 +1,18 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
pub mod command;
pub mod service;
use super::errors::*;
#[async_trait::async_trait]
pub trait VerifyUpdatedEmailUseCase: Send + Sync {
async fn send_verification_email(
&self,
cmd: command::VerifyUpdatedEmailCommand,
) -> IdentityResult<()>;
}
pub type VerifyUpdatedEmailServiceObj = std::sync::Arc<dyn VerifyUpdatedEmailUseCase>;

View file

@ -0,0 +1,152 @@
// 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::*, username_exists::*},
mailer::account_validation_link::*,
};
#[derive(Builder)]
pub struct VerifyUpdatedEmailService {
db_email_exists_adapter: EmailExistsOutDBPortObj,
db_username_exists_adapter: UsernameExistsOutDBPortObj,
db_get_verification_secret_adapter: GetVerificationSecretOutDBPortObj,
mailer_account_validation_link_adapter: AccountValidationLinkOutMailerPortObj,
}
#[async_trait::async_trait]
impl VerifyUpdatedEmailUseCase for VerifyUpdatedEmailService {
async fn resend_verification_email(
&self,
cmd: command::VerifyUpdatedEmailCommand,
) -> IdentityResult<()> {
if self
.db_username_exists_adapter
.username_exists(cmd.username())
.await
.unwrap()
{
return Err(IdentityError::DuplicateUsername);
}
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.username())
.await
.unwrap();
self.mailer_account_validation_link_adapter
.account_validation_link(cmd.email(), cmd.username(), &secret)
.await
.unwrap();
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::bdd::*;
use crate::utils::random_string::tests::*;
#[actix_rt::test]
async fn test_service() {
let username = "realaravinth";
let email = format!("{username}@example.com");
let secret = "asdfasdf";
let config = argon2_creds::Config::default();
let cmd =
command::VerifyUpdatedEmailCommand::new(username.into(), email.clone(), &config)
.unwrap();
// happy case
{
let s = VerifyUpdatedEmailServiceBuilder::default()
.db_username_exists_adapter(mock_username_exists_db_port(
IS_CALLED_ONLY_ONCE,
RETURNS_FALSE,
))
.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_db_port(
IS_CALLED_ONLY_ONCE,
))
.build()
.unwrap();
s.resend_verification_email(cmd.clone()).await.unwrap();
}
// username exists
{
let s = VerifyUpdatedEmailServiceBuilder::default()
.db_username_exists_adapter(mock_username_exists_db_port(
IS_CALLED_ONLY_ONCE,
RETURNS_TRUE,
))
.db_email_exists_adapter(mock_email_exists_db_port(IS_NEVER_CALLED, RETURNS_FALSE))
.db_get_verification_secret_adapter(mock_get_verification_secret_db_port(
IS_NEVER_CALLED,
secret.into(),
))
.mailer_account_validation_link_adapter(mock_account_validation_link_db_port(
IS_NEVER_CALLED,
))
.build()
.unwrap();
assert_eq!(
s.resend_verification_email(cmd.clone()).await.err(),
Some(IdentityError::DuplicateUsername)
);
}
// email exists
{
let s = VerifyUpdatedEmailServiceBuilder::default()
.db_username_exists_adapter(mock_username_exists_db_port(
IS_CALLED_ONLY_ONCE,
RETURNS_FALSE,
))
.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_db_port(
IS_NEVER_CALLED,
))
.build()
.unwrap();
assert_eq!(
s.resend_verification_email(cmd.clone()).await.err(),
Some(IdentityError::DuplicateEmail)
);
}
}
}