feat: register user service
This commit is contained in:
parent
ddb6034924
commit
b6304193f0
9 changed files with 164 additions and 6 deletions
|
@ -1 +0,0 @@
|
|||
|
|
@ -1 +0,0 @@
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
mod command;
|
||||
mod service;
|
||||
mod error;
|
|
@ -1 +0,0 @@
|
|||
|
76
src/identity/application/services/register_user/command.rs
Normal file
76
src/identity/application/services/register_user/command.rs
Normal 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());
|
||||
|
||||
}
|
||||
}
|
3
src/identity/application/services/register_user/error.rs
Normal file
3
src/identity/application/services/register_user/error.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
11
src/identity/application/services/register_user/events.rs
Normal file
11
src/identity/application/services/register_user/events.rs
Normal 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,
|
||||
}
|
17
src/identity/application/services/register_user/mod.rs
Normal file
17
src/identity/application/services/register_user/mod.rs
Normal 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;
|
||||
}
|
57
src/identity/application/services/register_user/service.rs
Normal file
57
src/identity/application/services/register_user/service.rs
Normal 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())
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue