From 5d49ecee5c18947e4261f1f26fb7161c21fce954 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sat, 18 May 2024 00:09:47 +0530 Subject: [PATCH] feat: identity: delete_user service --- .../services/delete_user/command.rs | 44 +++++++++++++++++++ .../application/services/delete_user/error.rs | 1 - .../services/delete_user/events.rs | 3 ++ .../application/services/delete_user/mod.rs | 21 +++++++-- .../services/delete_user/service.rs | 37 ++++++++++++++++ 5 files changed, 102 insertions(+), 4 deletions(-) delete mode 100644 src/identity/application/services/delete_user/error.rs create mode 100644 src/identity/application/services/delete_user/events.rs diff --git a/src/identity/application/services/delete_user/command.rs b/src/identity/application/services/delete_user/command.rs index 8b13789..2002859 100644 --- a/src/identity/application/services/delete_user/command.rs +++ b/src/identity/application/services/delete_user/command.rs @@ -1 +1,45 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later +use super::*; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)] +pub struct DeleteUserCommand; + +impl DeleteUserCommand { + pub fn new( + _username: String, + supplied_password: String, + actual_password_hash: &str, + ) -> IdentityCommandResult { + if !argon2_creds::Config::verify(actual_password_hash, &supplied_password).unwrap() { + return Err(IdentityCommandError::WrongPassword); + } + Ok(Self) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_cmd() { + let config = argon2_creds::Config::default(); + let password = "adsfasdfasd"; + let hashed_password = config.password(password).unwrap(); + DeleteUserCommand::new("realaravinth".into(), password.into(), &hashed_password); + + assert_eq!( + DeleteUserCommand::new( + "realaravinth".into(), + "wrongpassword".into(), + &hashed_password, + ) + .err(), + Some(IdentityCommandError::WrongPassword) + ); + } +} diff --git a/src/identity/application/services/delete_user/error.rs b/src/identity/application/services/delete_user/error.rs deleted file mode 100644 index 8b13789..0000000 --- a/src/identity/application/services/delete_user/error.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/identity/application/services/delete_user/events.rs b/src/identity/application/services/delete_user/events.rs new file mode 100644 index 0000000..56f60de --- /dev/null +++ b/src/identity/application/services/delete_user/events.rs @@ -0,0 +1,3 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/src/identity/application/services/delete_user/mod.rs b/src/identity/application/services/delete_user/mod.rs index 0d0d0f7..60e57ac 100644 --- a/src/identity/application/services/delete_user/mod.rs +++ b/src/identity/application/services/delete_user/mod.rs @@ -1,3 +1,18 @@ -mod command; -mod error; -mod service; +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +pub mod command; +pub mod events; +pub mod service; + +use super::errors::*; + +#[async_trait::async_trait] +pub trait DeleteUserUseCase { + async fn delete_user( + &self, + cmd: command::DeleteUserCommand, + //) -> errors::ProcessAuthorizationServiceResult; + ); +} diff --git a/src/identity/application/services/delete_user/service.rs b/src/identity/application/services/delete_user/service.rs index 8b13789..620cafe 100644 --- a/src/identity/application/services/delete_user/service.rs +++ b/src/identity/application/services/delete_user/service.rs @@ -1 +1,38 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later +use super::*; + +struct DeleteUserService; + +#[async_trait::async_trait] +impl DeleteUserUseCase for DeleteUserService { + async fn delete_user( + &self, + _cmd: command::DeleteUserCommand, + //) -> errors::ProcessAuthorizationServiceResult; + ) { + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[actix_rt::test] + async fn test_service() { + let config = argon2_creds::Config::default(); + let username = "realaravinth"; + let password = "password"; + let hashed_password = config.password(password).unwrap(); + + let s = DeleteUserService; + + let cmd = + command::DeleteUserCommand::new(username.into(), password.into(), &hashed_password) + .unwrap(); + + s.delete_user(cmd).await; + } +}