63 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			63 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| use async_trait::async_trait;
 | |
| use json;
 | |
| 
 | |
| use crate::utils::extract_json_value;
 | |
| 
 | |
| #[async_trait]
 | |
| pub trait Store {
 | |
|     async fn is_auth(&mut self, data: &json::JsonValue) -> Option<Credentials>;
 | |
| }
 | |
| 
 | |
| #[derive(Default, Debug)]
 | |
| pub struct Credentials {
 | |
|     pub email: String,
 | |
|     pub password: String,
 | |
| }
 | |
| 
 | |
| impl Credentials {
 | |
|     pub fn new(email: String, password: String) -> Self {
 | |
|         Credentials { email, password }
 | |
|     }
 | |
| 
 | |
|     pub fn is_empty(&self) -> bool {
 | |
|         self.email == "" || self.password == ""
 | |
|     }
 | |
| }
 | |
| 
 | |
| impl From<&json::JsonValue> for Credentials {
 | |
|     fn from(data: &json::JsonValue) -> Self {
 | |
|         let mut credentials = Credentials::default();
 | |
|         match data {
 | |
|             json::JsonValue::Object(ref d) => {
 | |
|                 credentials.email = extract_json_value(&d, "email").unwrap_or("".to_string());
 | |
|                 credentials.password = extract_json_value(&d, "password").unwrap_or("".to_string());
 | |
|             }
 | |
|             _ => return credentials,
 | |
|         }
 | |
|         credentials
 | |
|     }
 | |
| }
 | |
| 
 | |
| #[test]
 | |
| fn test_credentials() {
 | |
|     struct Expect {
 | |
|         data: json::JsonValue,
 | |
|         is_empty: bool,
 | |
|     }
 | |
|     let test_cases: [Expect; 2] = [
 | |
|         Expect {
 | |
|             data: json::parse(r#"{"access_token":"AAAAAAAAAAAA.BBBBBBBBBB.CCCCCCCCCC","refresh_token": "DDDDDDDDDDD.EEEEEEEEEEE.FFFFF"}"#).unwrap(),
 | |
|             is_empty: true
 | |
|         },
 | |
|         Expect {
 | |
|             data: json::parse(r#"{"email":"toto@toto.fr","password": "tata"}"#).unwrap(),
 | |
|             is_empty: false
 | |
|         }
 | |
|     ];
 | |
| 
 | |
|     for t in test_cases {
 | |
|         let credentials = Credentials::from(&t.data);
 | |
|         assert_eq!(t.is_empty, credentials.is_empty())
 | |
|     }
 | |
| }
 |