From 8c136cb46ccbaacacddbe0a6a49b049e09d34f93 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Fri, 17 May 2024 23:39:48 +0530 Subject: [PATCH] feat: identity: register_user command --- .../services/register_user/command.rs | 72 +++++++++++-------- .../services/register_user/error.rs | 28 ++++++++ 2 files changed, 72 insertions(+), 28 deletions(-) diff --git a/src/identity/application/services/register_user/command.rs b/src/identity/application/services/register_user/command.rs index f4af68e..89db19b 100644 --- a/src/identity/application/services/register_user/command.rs +++ b/src/identity/application/services/register_user/command.rs @@ -2,6 +2,7 @@ // // SPDX-License-Identifier: AGPL-3.0-or-later +use super::error::*; use derive_getters::Getters; use serde::{Deserialize, Serialize}; @@ -19,20 +20,20 @@ impl RegisterUserCommand { password: String, confirm_password: String, config: &argon2_creds::Config, - ) -> Self { - let username = config.username(&username).unwrap(); - config.email(&email).unwrap(); + ) -> RegisterUserCommandResult { + let username = config.username(&username)?; + config.email(&email)?; if password != confirm_password { - todo!("Passwords Don't match"); + return Err(RegisterUserCommandError::PasswordsDontMatch); } - let hashed_password: String = config.password(&password).unwrap(); + let hashed_password: String = config.password(&password)?; - Self { + Ok(Self { username, email, hashed_password, - } + }) } } @@ -49,28 +50,43 @@ mod tests { "asdfasdfasdfasdf".into(), "asdfasdfasdfasdf".into(), &config, - ); - let result = std::panic::catch_unwind(|| - RegisterUserCommand::new( - "username".into(), - "username@example.com".into(), - "password".into(), - "password".into(), - &config, ) - ); // TODO: check for error, and not panic when err handling is implemented - assert!(result.is_err()); // Blacklist error + .unwrap(); - let result = std::panic::catch_unwind(|| - RegisterUserCommand::new( - "realaravinth".into(), - "realaravinth@example.com".into(), - "password".into(), - "mismatch_password".into(), - &config, - ) - ); // TODO: check for error, and not panic when err handling is implemented - assert!(result.is_err()); - + assert_eq!( + RegisterUserCommand::new( + "realaravinth".into(), + "username".into(), + "password".into(), + "password".into(), + &config, + ) + .err(), + Some(RegisterUserCommandError::BadEmail) + ); + + assert!(matches!( + RegisterUserCommand::new( + "username".into(), + "username@example.com".into(), + "password".into(), + "password".into(), + &config, + ) + .err(), + Some(RegisterUserCommandError::BadUsername(_)) + )); + + assert_eq!( + RegisterUserCommand::new( + "realaravinth".into(), + "realaravinth@example.com".into(), + "password".into(), + "mismatch_password".into(), + &config, + ) + .err(), + Some(RegisterUserCommandError::PasswordsDontMatch) + ); } } diff --git a/src/identity/application/services/register_user/error.rs b/src/identity/application/services/register_user/error.rs index 56f60de..db091a9 100644 --- a/src/identity/application/services/register_user/error.rs +++ b/src/identity/application/services/register_user/error.rs @@ -1,3 +1,31 @@ // SPDX-FileCopyrightText: 2024 Aravinth Manivannan // // SPDX-License-Identifier: AGPL-3.0-or-later + +use argon2_creds::CredsError; +use derive_more::Display; +use serde::{Deserialize, Serialize}; + +pub type RegisterUserCommandResult = Result; + +#[derive(Debug, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] +pub enum RegisterUserCommandError { + BadUsername(String), + BadEmail, + BadPassowrd(String), + PasswordsDontMatch, +} + +impl From for RegisterUserCommandError { + fn from(v: CredsError) -> Self { + match v { + CredsError::ProfainityError => Self::BadUsername(v.to_string()), + CredsError::UsernameCaseMappedError => Self::BadUsername(v.to_string()), + CredsError::BlacklistError => Self::BadUsername(v.to_string()), + CredsError::NotAnEmail => Self::BadEmail, + CredsError::PasswordTooShort => Self::BadPassowrd(v.to_string()), + CredsError::PasswordTooLong => Self::BadPassowrd(v.to_string()), + CredsError::Argon2Error(e) => Self::BadUsername(e.to_string()), + } + } +}