-use std::{fmt::Display, fs::{self, File}, path::Path, io::Read};\r
+use std::{fmt, fs::{self, File}, path::Path, io::Read};\r
\r
use itertools::Itertools;\r
use chrono::{prelude::*, Duration};\r
Other(String),\r
}\r
\r
+impl fmt::Display for DBError {\r
+ fn fmt(&self, f: &mut fmt::Formatter) -> std::result::Result<(), fmt::Error> {\r
+ write!(f, "{:?}", self)\r
+ }\r
+}\r
+\r
+impl std::error::Error for DBError { }\r
+\r
impl From<rusqlite::Error> for DBError {\r
fn from(error: rusqlite::Error) -> Self {\r
DBError::SqliteError(error)\r
Self::create_connection(SqliteConnectionManager::file(file))\r
}\r
\r
- fn create_connection(manager: SqliteConnectionManager) -> Result<Connection> {;\r
+ fn create_connection(manager: SqliteConnectionManager) -> Result<Connection> {\r
let pool = r2d2::Pool::new(manager).unwrap();\r
let connection = Connection { pool };\r
connection.create_or_update()?;\r
}\r
\r
///\r
- pub fn sign_up(&self, password: &str, email: &str) -> Result<SignUpResult> {\r
- self.sign_up_with_given_time(password, email, Utc::now())\r
+ pub fn sign_up(&self, email: &str, password: &str) -> Result<SignUpResult> {\r
+ self.sign_up_with_given_time(email, password, Utc::now())\r
}\r
\r
- fn sign_up_with_given_time(&self, password: &str, email: &str, datetime: DateTime<Utc>) -> Result<SignUpResult> {\r
+ fn sign_up_with_given_time(&self, email: &str, password: &str, datetime: DateTime<Utc>) -> Result<SignUpResult> {\r
let mut con = self.pool.get()?;\r
let tx = con.transaction()?;\r
let token =\r
}\r
\r
/// Execute a given SQL file.\r
- pub fn execute_file<P: AsRef<Path> + Display>(&self, file: P) -> Result<()> {\r
+ pub fn execute_file<P: AsRef<Path> + fmt::Display>(&self, file: P) -> Result<()> {\r
let con = self.pool.get()?;\r
let sql = load_sql_file(file)?;\r
con.execute_batch(&sql).map_err(DBError::from)\r
}\r
}\r
\r
-fn load_sql_file<P: AsRef<Path> + Display>(sql_file: P) -> Result<String> {\r
+fn load_sql_file<P: AsRef<Path> + fmt::Display>(sql_file: P) -> Result<String> {\r
let mut file = File::open(&sql_file).map_err(|err| DBError::Other(format!("Cannot open SQL file ({}): {}", &sql_file, err.to_string())))?;\r
let mut sql = String::new();\r
file.read_to_string(&mut sql).map_err(|err| DBError::Other(format!("Cannot read SQL file ({}) : {}", &sql_file, err.to_string())))?;\r
#[test]\r
fn sign_up() -> Result<()> {\r
let connection = Connection::new_in_memory()?;\r
- match connection.sign_up("12345", "paul@test.org")? {\r
+ match connection.sign_up("paul@test.org", "12345")? {\r
SignUpResult::UserCreatedWaitingForValidation(_) => (), // Nominal case.\r
other => panic!("{:?}", other),\r
}\r
0,\r
NULL\r
);", [])?;\r
- match connection.sign_up("12345", "paul@test.org")? {\r
+ match connection.sign_up("paul@test.org", "12345")? {\r
SignUpResult::UserAlreadyExists => (), // Nominal case.\r
other => panic!("{:?}", other),\r
}\r
0,\r
:token\r
);", named_params! { ":token": token })?;\r
- match connection.sign_up("12345", "paul@test.org")? {\r
+ match connection.sign_up("paul@test.org", "12345")? {\r
SignUpResult::UserCreatedWaitingForValidation(_) => (), // Nominal case.\r
other => panic!("{:?}", other),\r
}\r
fn sign_up_then_send_validation_at_time() -> Result<()> {\r
let connection = Connection::new_in_memory()?;\r
let validation_token =\r
- match connection.sign_up("12345", "paul@test.org")? {\r
+ match connection.sign_up("paul@test.org", "12345")? {\r
SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case.\r
other => panic!("{:?}", other),\r
};\r
fn sign_up_then_send_validation_too_late() -> Result<()> {\r
let connection = Connection::new_in_memory()?;\r
let validation_token =\r
- match connection.sign_up_with_given_time("12345", "paul@test.org", Utc::now() - Duration::days(1))? {\r
+ match connection.sign_up_with_given_time("paul@test.org", "12345", Utc::now() - Duration::days(1))? {\r
SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case.\r
other => panic!("{:?}", other),\r
};\r
fn sign_up_then_send_validation_with_bad_token() -> Result<()> {\r
let connection = Connection::new_in_memory()?;\r
let _validation_token =\r
- match connection.sign_up("12345", "paul@test.org")? {\r
+ match connection.sign_up("paul@test.org", "12345")? {\r
SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case.\r
other => panic!("{:?}", other),\r
};\r
fn sign_up_then_send_validation_then_sign_in() -> Result<()> {\r
let connection = Connection::new_in_memory()?;\r
\r
- let password = "12345";\r
let email = "paul@test.org";\r
+ let password = "12345";\r
\r
// Sign up.\r
let validation_token =\r
- match connection.sign_up(password, email)? {\r
+ match connection.sign_up(email, password)? {\r
SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case.\r
other => panic!("{:?}", other),\r
};\r
fn sign_up_then_send_validation_then_authentication() -> Result<()> {\r
let connection = Connection::new_in_memory()?;\r
\r
- let password = "12345";\r
let email = "paul@test.org";\r
+ let password = "12345";\r
\r
// Sign up.\r
let validation_token =\r
- match connection.sign_up(password, email)? {\r
+ match connection.sign_up(email, password)? {\r
SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case.\r
other => panic!("{:?}", other),\r
};\r
fn sign_up_then_send_validation_then_sign_out_then_sign_in() -> Result<()> {\r
let connection = Connection::new_in_memory()?;\r
\r
- let password = "12345";\r
let email = "paul@test.org";\r
+ let password = "12345";\r
\r
// Sign up.\r
let validation_token =\r
- match connection.sign_up(password, email)? {\r
+ match connection.sign_up(email, password)? {\r
SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case.\r
other => panic!("{:?}", other),\r
};\r