Sign out
[recipes.git] / backend / src / db.rs
index bae57d0..e38aae7 100644 (file)
@@ -7,7 +7,7 @@ use r2d2::Pool;
 use r2d2_sqlite::SqliteConnectionManager;\r
 use rand::distributions::{Alphanumeric, DistString};\r
 \r
-use crate::consts;\r
+use crate::{consts, user};\r
 use crate::hash::{hash, verify_password};\r
 use crate::model;\r
 use crate::user::*;\r
@@ -67,7 +67,8 @@ pub enum ValidationResult {
 #[derive(Debug)]\r
 pub enum SignInResult {\r
     UserNotFound,\r
-    PasswordsDontMatch,\r
+    WrongPassword,\r
+    AccountNotValidated,\r
     Ok(String, i32), // Returns token and user id.\r
 }\r
 \r
@@ -197,8 +198,8 @@ impl Connection {
 \r
     pub fn get_recipe(&self, id: i32) -> Result<model::Recipe> {\r
         let con = self.pool.get()?;\r
-        con.query_row("SELECT [id], [title] FROM [Recipe] WHERE [id] = ?1", [id], |row| {\r
-            Ok(model::Recipe::new(row.get(0)?, row.get(1)?))\r
+        con.query_row("SELECT [id], [title], [description] FROM [Recipe] WHERE [id] = ?1", [id], |row| {\r
+            Ok(model::Recipe::new(row.get("id")?, row.get("title")?, row.get("description")?))\r
         }).map_err(DBError::from)\r
     }\r
 \r
@@ -213,6 +214,15 @@ impl Connection {
         }).map_err(DBError::from)\r
     }\r
 \r
+    pub fn load_user(&self, user_id: i32) -> Result<User> {\r
+        let con = self.pool.get()?;\r
+        con.query_row("SELECT [email] FROM [User] WHERE [id] = ?1", [user_id], |r| {\r
+            Ok(User {\r
+                email: r.get("email")?,\r
+            })\r
+        }).map_err(DBError::from)\r
+    }\r
+\r
     ///\r
     pub fn sign_up(&self, email: &str, password: &str) -> Result<SignUpResult> {\r
         self.sign_up_with_given_time(email, password, Utc::now())\r
@@ -268,19 +278,21 @@ impl Connection {
         Ok(ValidationResult::Ok(token, user_id))\r
     }\r
 \r
-    pub fn sign_in(&self, password: &str, email: &str, ip: &str, user_agent: &str) -> Result<SignInResult> {\r
+    pub fn sign_in(&self, email: &str, password: &str, ip: &str, user_agent: &str) -> Result<SignInResult> {\r
         let mut con = self.pool.get()?;\r
         let tx = con.transaction()?;\r
-        match tx.query_row("SELECT [id], [password] FROM [User] WHERE [email] = ?1", [email], |r| {\r
-            Ok((r.get::<&str, i32>("id")?, r.get::<&str, String>("password")?))\r
+        match tx.query_row("SELECT [id], [password], [validation_token] FROM [User] WHERE [email] = ?1", [email], |r| {\r
+            Ok((r.get::<&str, i32>("id")?, r.get::<&str, String>("password")?, r.get::<&str, Option<String>>("validation_token")?))\r
         }).optional()? {\r
-            Some((id, stored_password)) => {\r
-                if verify_password(password, &stored_password).map_err(DBError::from_dyn_error)? {\r
+            Some((id, stored_password, validation_token)) => {\r
+                if validation_token.is_some() {\r
+                    Ok(SignInResult::AccountNotValidated)\r
+                } else if verify_password(password, &stored_password).map_err(DBError::from_dyn_error)? {\r
                     let token = Connection::create_login_token(&tx, id, ip, user_agent)?;\r
                     tx.commit()?;\r
                     Ok(SignInResult::Ok(token, id))\r
                 } else {\r
-                    Ok(SignInResult::PasswordsDontMatch)\r
+                    Ok(SignInResult::WrongPassword)\r
                 }\r
             },\r
             None => {\r
@@ -387,6 +399,26 @@ mod tests {
         Ok(())\r
     }\r
 \r
+    #[test]\r
+    fn sign_up_and_sign_in_without_validation() -> Result<()> {\r
+        let connection = Connection::new_in_memory()?;\r
+\r
+        let email = "paul@test.org";\r
+        let password = "12345";\r
+\r
+        match connection.sign_up(email, password)? {\r
+            SignUpResult::UserCreatedWaitingForValidation(_) => (), // Nominal case.\r
+            other => panic!("{:?}", other),\r
+        }\r
+\r
+        match connection.sign_in(email, password, "127.0.0.1", "Mozilla/5.0")? {\r
+            SignInResult::AccountNotValidated => (), // Nominal case.\r
+            other => panic!("{:?}", other),\r
+        }\r
+\r
+        Ok(())\r
+    }\r
+\r
     #[test]\r
     fn sign_up_to_an_unvalidated_already_existing_user() -> Result<()> {\r
         let connection = Connection::new_in_memory()?;\r
@@ -475,7 +507,7 @@ mod tests {
         };\r
 \r
         // Sign in.\r
-        match connection.sign_in(password, email, "127.0.0.1", "Mozilla/5.0")? {\r
+        match connection.sign_in(email, password, "127.0.0.1", "Mozilla/5.0")? {\r
             SignInResult::Ok(_, _) => (), // Nominal case.\r
             other => panic!("{:?}", other),\r
         }\r
@@ -554,7 +586,7 @@ mod tests {
 \r
         // Sign in.\r
         let (authentication_token_2, user_id_2) =\r
-            match connection.sign_in(password, email, "192.168.1.1", "Chrome")? {\r
+            match connection.sign_in(email, password, "192.168.1.1", "Chrome")? {\r
                 SignInResult::Ok(token, user_id) => (token, user_id),\r
                 other => panic!("{:?}", other),\r
             };\r