Sign up form and other stuff

This commit is contained in:
Greg Burri 2022-11-26 12:26:05 +01:00
parent b1ffd1a04a
commit 45d4867cb3
18 changed files with 817 additions and 107 deletions

View file

@ -1,4 +1,4 @@
use std::{fmt::Display, fs::{self, File}, path::Path, io::Read};
use std::{fmt, fs::{self, File}, path::Path, io::Read};
use itertools::Itertools;
use chrono::{prelude::*, Duration};
@ -22,6 +22,14 @@ pub enum DBError {
Other(String),
}
impl fmt::Display for DBError {
fn fmt(&self, f: &mut fmt::Formatter) -> std::result::Result<(), fmt::Error> {
write!(f, "{:?}", self)
}
}
impl std::error::Error for DBError { }
impl From<rusqlite::Error> for DBError {
fn from(error: rusqlite::Error) -> Self {
DBError::SqliteError(error)
@ -95,7 +103,7 @@ impl Connection {
Self::create_connection(SqliteConnectionManager::file(file))
}
fn create_connection(manager: SqliteConnectionManager) -> Result<Connection> {;
fn create_connection(manager: SqliteConnectionManager) -> Result<Connection> {
let pool = r2d2::Pool::new(manager).unwrap();
let connection = Connection { pool };
connection.create_or_update()?;
@ -206,11 +214,11 @@ impl Connection {
}
///
pub fn sign_up(&self, password: &str, email: &str) -> Result<SignUpResult> {
self.sign_up_with_given_time(password, email, Utc::now())
pub fn sign_up(&self, email: &str, password: &str) -> Result<SignUpResult> {
self.sign_up_with_given_time(email, password, Utc::now())
}
fn sign_up_with_given_time(&self, password: &str, email: &str, datetime: DateTime<Utc>) -> Result<SignUpResult> {
fn sign_up_with_given_time(&self, email: &str, password: &str, datetime: DateTime<Utc>) -> Result<SignUpResult> {
let mut con = self.pool.get()?;
let tx = con.transaction()?;
let token =
@ -313,7 +321,7 @@ impl Connection {
}
/// Execute a given SQL file.
pub fn execute_file<P: AsRef<Path> + Display>(&self, file: P) -> Result<()> {
pub fn execute_file<P: AsRef<Path> + fmt::Display>(&self, file: P) -> Result<()> {
let con = self.pool.get()?;
let sql = load_sql_file(file)?;
con.execute_batch(&sql).map_err(DBError::from)
@ -334,7 +342,7 @@ impl Connection {
}
}
fn load_sql_file<P: AsRef<Path> + Display>(sql_file: P) -> Result<String> {
fn load_sql_file<P: AsRef<Path> + fmt::Display>(sql_file: P) -> Result<String> {
let mut file = File::open(&sql_file).map_err(|err| DBError::Other(format!("Cannot open SQL file ({}): {}", &sql_file, err.to_string())))?;
let mut sql = String::new();
file.read_to_string(&mut sql).map_err(|err| DBError::Other(format!("Cannot read SQL file ({}) : {}", &sql_file, err.to_string())))?;
@ -352,7 +360,7 @@ mod tests {
#[test]
fn sign_up() -> Result<()> {
let connection = Connection::new_in_memory()?;
match connection.sign_up("12345", "paul@test.org")? {
match connection.sign_up("paul@test.org", "12345")? {
SignUpResult::UserCreatedWaitingForValidation(_) => (), // Nominal case.
other => panic!("{:?}", other),
}
@ -372,7 +380,7 @@ mod tests {
0,
NULL
);", [])?;
match connection.sign_up("12345", "paul@test.org")? {
match connection.sign_up("paul@test.org", "12345")? {
SignUpResult::UserAlreadyExists => (), // Nominal case.
other => panic!("{:?}", other),
}
@ -393,7 +401,7 @@ mod tests {
0,
:token
);", named_params! { ":token": token })?;
match connection.sign_up("12345", "paul@test.org")? {
match connection.sign_up("paul@test.org", "12345")? {
SignUpResult::UserCreatedWaitingForValidation(_) => (), // Nominal case.
other => panic!("{:?}", other),
}
@ -404,7 +412,7 @@ mod tests {
fn sign_up_then_send_validation_at_time() -> Result<()> {
let connection = Connection::new_in_memory()?;
let validation_token =
match connection.sign_up("12345", "paul@test.org")? {
match connection.sign_up("paul@test.org", "12345")? {
SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case.
other => panic!("{:?}", other),
};
@ -419,7 +427,7 @@ mod tests {
fn sign_up_then_send_validation_too_late() -> Result<()> {
let connection = Connection::new_in_memory()?;
let validation_token =
match connection.sign_up_with_given_time("12345", "paul@test.org", Utc::now() - Duration::days(1))? {
match connection.sign_up_with_given_time("paul@test.org", "12345", Utc::now() - Duration::days(1))? {
SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case.
other => panic!("{:?}", other),
};
@ -434,7 +442,7 @@ mod tests {
fn sign_up_then_send_validation_with_bad_token() -> Result<()> {
let connection = Connection::new_in_memory()?;
let _validation_token =
match connection.sign_up("12345", "paul@test.org")? {
match connection.sign_up("paul@test.org", "12345")? {
SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case.
other => panic!("{:?}", other),
};
@ -450,12 +458,12 @@ mod tests {
fn sign_up_then_send_validation_then_sign_in() -> Result<()> {
let connection = Connection::new_in_memory()?;
let password = "12345";
let email = "paul@test.org";
let password = "12345";
// Sign up.
let validation_token =
match connection.sign_up(password, email)? {
match connection.sign_up(email, password)? {
SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case.
other => panic!("{:?}", other),
};
@ -479,12 +487,12 @@ mod tests {
fn sign_up_then_send_validation_then_authentication() -> Result<()> {
let connection = Connection::new_in_memory()?;
let password = "12345";
let email = "paul@test.org";
let password = "12345";
// Sign up.
let validation_token =
match connection.sign_up(password, email)? {
match connection.sign_up(email, password)? {
SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case.
other => panic!("{:?}", other),
};
@ -519,12 +527,12 @@ mod tests {
fn sign_up_then_send_validation_then_sign_out_then_sign_in() -> Result<()> {
let connection = Connection::new_in_memory()?;
let password = "12345";
let email = "paul@test.org";
let password = "12345";
// Sign up.
let validation_token =
match connection.sign_up(password, email)? {
match connection.sign_up(email, password)? {
SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case.
other => panic!("{:?}", other),
};