feat: identity: register_user command

This commit is contained in:
Aravinth Manivannan 2024-05-17 23:39:48 +05:30
parent 828a53b60d
commit 8c136cb46c
Signed by: realaravinth
GPG key ID: F8F50389936984FF
2 changed files with 72 additions and 28 deletions

View file

@ -2,6 +2,7 @@
// //
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
use super::error::*;
use derive_getters::Getters; use derive_getters::Getters;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -19,20 +20,20 @@ impl RegisterUserCommand {
password: String, password: String,
confirm_password: String, confirm_password: String,
config: &argon2_creds::Config, config: &argon2_creds::Config,
) -> Self { ) -> RegisterUserCommandResult<Self> {
let username = config.username(&username).unwrap(); let username = config.username(&username)?;
config.email(&email).unwrap(); config.email(&email)?;
if password != confirm_password { 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, username,
email, email,
hashed_password, hashed_password,
} })
} }
} }
@ -49,8 +50,22 @@ mod tests {
"asdfasdfasdfasdf".into(), "asdfasdfasdfasdf".into(),
"asdfasdfasdfasdf".into(), "asdfasdfasdfasdf".into(),
&config, &config,
)
.unwrap();
assert_eq!(
RegisterUserCommand::new(
"realaravinth".into(),
"username".into(),
"password".into(),
"password".into(),
&config,
)
.err(),
Some(RegisterUserCommandError::BadEmail)
); );
let result = std::panic::catch_unwind(||
assert!(matches!(
RegisterUserCommand::new( RegisterUserCommand::new(
"username".into(), "username".into(),
"username@example.com".into(), "username@example.com".into(),
@ -58,10 +73,11 @@ mod tests {
"password".into(), "password".into(),
&config, &config,
) )
); // TODO: check for error, and not panic when err handling is implemented .err(),
assert!(result.is_err()); // Blacklist error Some(RegisterUserCommandError::BadUsername(_))
));
let result = std::panic::catch_unwind(|| assert_eq!(
RegisterUserCommand::new( RegisterUserCommand::new(
"realaravinth".into(), "realaravinth".into(),
"realaravinth@example.com".into(), "realaravinth@example.com".into(),
@ -69,8 +85,8 @@ mod tests {
"mismatch_password".into(), "mismatch_password".into(),
&config, &config,
) )
); // TODO: check for error, and not panic when err handling is implemented .err(),
assert!(result.is_err()); Some(RegisterUserCommandError::PasswordsDontMatch)
);
} }
} }

View file

@ -1,3 +1,31 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net> // SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
// //
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
use argon2_creds::CredsError;
use derive_more::Display;
use serde::{Deserialize, Serialize};
pub type RegisterUserCommandResult<V> = Result<V, RegisterUserCommandError>;
#[derive(Debug, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub enum RegisterUserCommandError {
BadUsername(String),
BadEmail,
BadPassowrd(String),
PasswordsDontMatch,
}
impl From<CredsError> 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()),
}
}
}