|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use anyhow::Result; |
| 4 | +use async_trait::async_trait; |
| 5 | +use strum::Display; |
| 6 | + |
| 7 | +use super::{ |
| 8 | + super::{ |
| 9 | + ports::{hashing_algorithm::HashingAlgorithm, repository::Repository}, |
| 10 | + workshop::Workshop, |
| 11 | + }, |
| 12 | + Case, |
| 13 | +}; |
| 14 | + |
| 15 | +pub(crate) struct AuthenticateCaseInput { |
| 16 | + pub(crate) api_key: String, |
| 17 | + pub(crate) api_secret: String, |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Display)] |
| 21 | +pub(crate) enum AuthenticateCaseOutput { |
| 22 | + NotFound, |
| 23 | + IncorrectSecret, |
| 24 | + Success, |
| 25 | +} |
| 26 | + |
| 27 | +struct AuthenticateCase { |
| 28 | + repository: Arc<dyn Repository>, |
| 29 | + hashing_algorithm: Box<dyn HashingAlgorithm>, |
| 30 | + input: AuthenticateCaseInput, |
| 31 | +} |
| 32 | + |
| 33 | +impl Workshop { |
| 34 | + pub(crate) async fn execute_authenticate_case( |
| 35 | + &self, |
| 36 | + input: AuthenticateCaseInput, |
| 37 | + ) -> Result<AuthenticateCaseOutput> { |
| 38 | + let case = AuthenticateCase { |
| 39 | + repository: Arc::clone(&self.repository), |
| 40 | + hashing_algorithm: self.hashing_algorithm.clone(), |
| 41 | + input, |
| 42 | + }; |
| 43 | + self.run_case(case).await |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +#[async_trait] |
| 48 | +impl Case for AuthenticateCase { |
| 49 | + type Output = AuthenticateCaseOutput; |
| 50 | + |
| 51 | + async fn execute(self) -> Result<Self::Output> { |
| 52 | + let Some(api_secret_value) = self.repository.select_client_api_secret(&self.input.api_key).await? else { |
| 53 | + return Ok(AuthenticateCaseOutput::NotFound); |
| 54 | + }; |
| 55 | + if !self |
| 56 | + .hashing_algorithm |
| 57 | + .verify(&self.input.api_secret, &api_secret_value)? |
| 58 | + { |
| 59 | + return Ok(AuthenticateCaseOutput::IncorrectSecret); |
| 60 | + } |
| 61 | + Ok(AuthenticateCaseOutput::Success) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +#[cfg(test)] |
| 66 | +mod tests { |
| 67 | + use std::sync::Arc; |
| 68 | + |
| 69 | + use anyhow::Result; |
| 70 | + |
| 71 | + use super::{ |
| 72 | + super::{ |
| 73 | + super::ports::{hashing_algorithm::MockHashingAlgorithm, repository::MockRepository}, |
| 74 | + Case, |
| 75 | + }, |
| 76 | + AuthenticateCase, AuthenticateCaseInput, AuthenticateCaseOutput, |
| 77 | + }; |
| 78 | + |
| 79 | + #[tokio::test] |
| 80 | + async fn confirm_successful_authentication() -> Result<()> { |
| 81 | + let mut mock_repository = MockRepository::new(); |
| 82 | + mock_repository |
| 83 | + .expect_select_client_api_secret() |
| 84 | + .times(1) |
| 85 | + .returning(|_| Ok(Some("".to_string()))); |
| 86 | + let mut mock_hashing_algorithm = MockHashingAlgorithm::new(); |
| 87 | + mock_hashing_algorithm |
| 88 | + .expect_verify() |
| 89 | + .times(1) |
| 90 | + .returning(|_, _| Ok(true)); |
| 91 | + let case = AuthenticateCase { |
| 92 | + repository: Arc::new(mock_repository), |
| 93 | + hashing_algorithm: Box::new(mock_hashing_algorithm), |
| 94 | + input: AuthenticateCaseInput { |
| 95 | + api_key: "".to_string(), |
| 96 | + api_secret: "".to_string(), |
| 97 | + }, |
| 98 | + }; |
| 99 | + let output = case.execute().await?; |
| 100 | + assert!(matches!(output, AuthenticateCaseOutput::Success)); |
| 101 | + Ok(()) |
| 102 | + } |
| 103 | +} |
0 commit comments