-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from EleisonC/sign-up-success-route-impl
[sprint3][task2]ft-impl sign up route success route
- Loading branch information
Showing
10 changed files
with
218 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use std::sync::Arc; | ||
use tokio::sync::RwLock; | ||
|
||
use crate::services::hashmap_user_store::HashmapUserStore; | ||
|
||
pub type UserStoreType = Arc<RwLock<HashmapUserStore>>; | ||
|
||
#[derive(Clone)] | ||
pub struct AppState { | ||
pub user_store: UserStoreType, | ||
} | ||
|
||
impl AppState { | ||
pub fn new(user_store: UserStoreType) -> Self { | ||
Self { | ||
user_store | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod user; | ||
|
||
pub use user::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#[derive(Clone, Debug, PartialEq)] | ||
pub struct User { | ||
pub email: String, | ||
pub password: String, | ||
pub requires2fa: bool | ||
} | ||
|
||
impl User { | ||
pub fn new(email: String, password: String, requires2fa: bool) -> Self { | ||
User { | ||
email, | ||
password, | ||
requires2fa | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use std::collections::HashMap; | ||
use crate::domain::User; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub enum UserStoreError { | ||
UserAlreadyExists, | ||
UserNotFound, | ||
InvalidCredentials, | ||
UnexpectedError, | ||
} | ||
|
||
#[derive(Default, Debug)] | ||
pub struct HashmapUserStore { | ||
users: HashMap<String, User> | ||
} | ||
|
||
impl HashmapUserStore { | ||
pub fn add_user(&mut self, user: User) -> Result<(), UserStoreError> { | ||
let current_user_email = user.email.clone(); | ||
|
||
if self.users.contains_key(¤t_user_email) { | ||
return Err(UserStoreError::UserAlreadyExists) | ||
} | ||
|
||
self.users.insert(current_user_email, user); | ||
Ok(()) | ||
} | ||
|
||
pub fn get_user(&self, email: &str) -> Result<User, UserStoreError> { | ||
if let Some(user) = self.users.get(email) { | ||
return Ok(user.clone()) | ||
} else { | ||
Err(UserStoreError::UserNotFound) | ||
} | ||
} | ||
|
||
pub fn validate_user(&self, email: &str, password: &str) -> Result<(), UserStoreError> { | ||
if let Some(user) = self.users.get(email) { | ||
if user.password == password { | ||
return Ok(()) | ||
} | ||
return Err(UserStoreError::InvalidCredentials) | ||
} else { | ||
return Err(UserStoreError::UserNotFound) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn test_add_user() { | ||
let mut store = HashmapUserStore::default(); | ||
let user = User::new( | ||
"[email protected]".to_string(), | ||
"password".to_string(), | ||
false | ||
); | ||
|
||
let result = store.add_user(user.clone()); | ||
assert_eq!(result, Ok(())); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_get_user() { | ||
let mut store = HashmapUserStore::default(); | ||
let user = User::new( | ||
"[email protected]".to_string(), | ||
"password".to_string(), | ||
false | ||
); | ||
store.add_user(user.clone()).unwrap(); | ||
|
||
let result = store.get_user(&user.email); | ||
match result { | ||
Ok(u) => assert_eq!(u, user), | ||
Err(e) => panic!("Expected Ok, got Err: {:?}", e), | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_validate_user() { | ||
let mut store = HashmapUserStore::default(); | ||
let user = User::new( | ||
"[email protected]".to_string(), | ||
"password".to_string(), | ||
true | ||
); | ||
|
||
store.add_user(user.clone()).unwrap(); | ||
|
||
let result = store.validate_user(&user.email, &user.password); | ||
assert_eq!(result, Ok(())); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod hashmap_user_store; | ||
|
||
|
||
pub use hashmap_user_store::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters