feat: register user service

This commit is contained in:
Aravinth Manivannan 2024-05-16 19:49:42 +05:30
parent ddb6034924
commit b6304193f0
Signed by: realaravinth
GPG key ID: F8F50389936984FF
9 changed files with 164 additions and 6 deletions

View file

@ -1,3 +0,0 @@
mod command;
mod service;
mod error;

View file

@ -0,0 +1,76 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use derive_getters::Getters;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters)]
pub struct RegisterUserCommand {
username: String,
email: String,
hashed_password: String,
}
impl RegisterUserCommand {
pub fn new(
username: String,
email: String,
password: String,
confirm_password: String,
config: &argon2_creds::Config,
) -> Self {
let username = config.username(&username).unwrap();
config.email(&email).unwrap();
if password != confirm_password {
todo!("Passwords Don't match");
}
let hashed_password: String = config.password(&password).unwrap();
Self {
username,
email,
hashed_password,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cmd() {
let config = argon2_creds::Config::default();
RegisterUserCommand::new(
"realaravinth".into(),
"realaravinth@example.com".into(),
"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
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());
}
}

View file

@ -0,0 +1,3 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

View file

@ -0,0 +1,11 @@
use derive_getters::Getters;
use derive_builder::Builder;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Builder, Serialize, Deserialize, Getters, Eq, PartialEq, Ord, PartialOrd)]
pub struct UserRegisteredEvent {
username: String,
email: String,
hashed_password: String,
is_verified: bool,
}

View file

@ -0,0 +1,17 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
pub mod command;
pub mod error;
pub mod events;
pub mod service;
#[async_trait::async_trait]
pub trait RegisterUserUseCase {
async fn register_user(
&self,
cmd: command::RegisterUserCommand,
//) -> errors::ProcessAuthorizationServiceResult<String>;
) -> events::UserRegisteredEvent;
}

View file

@ -0,0 +1,57 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
// - Hash password
// - Add user record
// - Send verification email
// - If UID == 0; set admin
use super::*;
struct RegisterUserService;
#[async_trait::async_trait]
impl RegisterUserUseCase for RegisterUserService {
async fn register_user(
&self,
cmd: command::RegisterUserCommand,
//) -> errors::ProcessAuthorizationServiceResult<String>;
) -> events::UserRegisteredEvent {
events::UserRegisteredEventBuilder::default()
.username(cmd.username().into())
.email(cmd.email().into())
.hashed_password(cmd.hashed_password().into())
.is_verified(false)
.build().unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[actix_rt::test]
async fn test_service() {
let username = "realaravinth";
let email = format!("{username}@example.com");
let password = "password";
let config = argon2_creds::Config::default();
let cmd = command::RegisterUserCommand::new(
username.into(),
email.clone(),
password.into(),
password.into(),
&config,
);
let s = RegisterUserService;
let res = s.register_user(cmd.clone()).await;
assert_eq!(res.username(), cmd.username());
assert_eq!(res.email(), cmd.email());
assert!(argon2_creds::Config::verify(res.hashed_password(), password).unwrap())
}
}