Update dependencies and implement email service integration
- Refactor app and email modules to include email service - Add tests for user sign-up and mock email service
This commit is contained in:
parent
f31167dd95
commit
3626f8a11b
10 changed files with 291 additions and 151 deletions
37
Cargo.lock
generated
37
Cargo.lock
generated
|
|
@ -166,7 +166,7 @@ dependencies = [
|
||||||
"memchr",
|
"memchr",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_derive",
|
"serde_derive",
|
||||||
"winnow 0.7.7",
|
"winnow 0.7.8",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -226,9 +226,9 @@ checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "axum"
|
name = "axum"
|
||||||
version = "0.8.3"
|
version = "0.8.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "de45108900e1f9b9242f7f2e254aa3e2c029c921c258fe9e6b4217eeebd54288"
|
checksum = "021e862c184ae977658b36c4500f7feac3221ca5da43e3f25bd04ab6c79a29b5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"axum-core",
|
"axum-core",
|
||||||
"axum-macros",
|
"axum-macros",
|
||||||
|
|
@ -1385,9 +1385,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hashbrown"
|
name = "hashbrown"
|
||||||
version = "0.15.2"
|
version = "0.15.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289"
|
checksum = "84b26c544d002229e640969970a2e74021aadf6e2f96372b9c58eff97de08eb3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"allocator-api2",
|
"allocator-api2",
|
||||||
"equivalent",
|
"equivalent",
|
||||||
|
|
@ -1400,7 +1400,7 @@ version = "0.10.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7382cf6263419f2d8df38c55d7da83da5c18aef87fc7a7fc1fb1e344edfe14c1"
|
checksum = "7382cf6263419f2d8df38c55d7da83da5c18aef87fc7a7fc1fb1e344edfe14c1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"hashbrown 0.15.2",
|
"hashbrown 0.15.3",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -1744,7 +1744,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "cea70ddb795996207ad57735b50c5982d8844f38ba9ee5f1aedcfb708a2aa11e"
|
checksum = "cea70ddb795996207ad57735b50c5982d8844f38ba9ee5f1aedcfb708a2aa11e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"equivalent",
|
"equivalent",
|
||||||
"hashbrown 0.15.2",
|
"hashbrown 0.15.3",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -2453,6 +2453,7 @@ dependencies = [
|
||||||
"argon2",
|
"argon2",
|
||||||
"askama",
|
"askama",
|
||||||
"async-compression",
|
"async-compression",
|
||||||
|
"async-trait",
|
||||||
"axum",
|
"axum",
|
||||||
"axum-extra",
|
"axum-extra",
|
||||||
"axum-test",
|
"axum-test",
|
||||||
|
|
@ -2618,9 +2619,9 @@ checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustix"
|
name = "rustix"
|
||||||
version = "1.0.5"
|
version = "1.0.7"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d97817398dd4bb2e6da002002db259209759911da105da92bec29ccb12cf58bf"
|
checksum = "c71e83d6afe7ff64890ec6b71d6a69bb8a610ab78ce364b3352876bb4c801266"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 2.9.0",
|
"bitflags 2.9.0",
|
||||||
"errno",
|
"errno",
|
||||||
|
|
@ -2831,9 +2832,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "sha2"
|
name = "sha2"
|
||||||
version = "0.10.8"
|
version = "0.10.9"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8"
|
checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"cpufeatures",
|
"cpufeatures",
|
||||||
|
|
@ -2973,7 +2974,7 @@ dependencies = [
|
||||||
"futures-intrusive",
|
"futures-intrusive",
|
||||||
"futures-io",
|
"futures-io",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"hashbrown 0.15.2",
|
"hashbrown 0.15.3",
|
||||||
"hashlink",
|
"hashlink",
|
||||||
"indexmap",
|
"indexmap",
|
||||||
"log",
|
"log",
|
||||||
|
|
@ -3241,9 +3242,9 @@ checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "synstructure"
|
name = "synstructure"
|
||||||
version = "0.13.1"
|
version = "0.13.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971"
|
checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
|
|
@ -3884,9 +3885,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "webpki-roots"
|
name = "webpki-roots"
|
||||||
version = "0.26.9"
|
version = "0.26.10"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "29aad86cec885cafd03e8305fd727c418e970a521322c91688414d5b8efba16b"
|
checksum = "37493cadf42a2a939ed404698ded7fb378bf301b5011f973361779a3a74f8c93"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"rustls-pki-types",
|
"rustls-pki-types",
|
||||||
]
|
]
|
||||||
|
|
@ -4150,9 +4151,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winnow"
|
name = "winnow"
|
||||||
version = "0.7.7"
|
version = "0.7.8"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6cb8234a863ea0e8cd7284fcdd4f145233eb00fee02bbdd9861aec44e6477bc5"
|
checksum = "9e27d6ad3dac991091e4d35de9ba2d2d00647c5d0fc26c5496dee55984ae111b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"memchr",
|
"memchr",
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,7 @@ rand_core = { version = "0.9", features = ["std"] }
|
||||||
rand = "0.9"
|
rand = "0.9"
|
||||||
strum = "0.27"
|
strum = "0.27"
|
||||||
strum_macros = "0.27"
|
strum_macros = "0.27"
|
||||||
|
async-trait = "0.1"
|
||||||
|
|
||||||
lettre = { version = "0.11", default-features = false, features = [
|
lettre = { version = "0.11", default-features = false, features = [
|
||||||
"smtp-transport",
|
"smtp-transport",
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use std::net::SocketAddr;
|
use std::{net::SocketAddr, sync::Arc};
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
BoxError, Router, ServiceExt,
|
BoxError, Router, ServiceExt,
|
||||||
|
|
@ -27,6 +27,7 @@ use crate::{
|
||||||
config::Config,
|
config::Config,
|
||||||
consts,
|
consts,
|
||||||
data::{db, model},
|
data::{db, model},
|
||||||
|
email,
|
||||||
log::Log,
|
log::Log,
|
||||||
ron_utils, services,
|
ron_utils, services,
|
||||||
translation::{self, Tr},
|
translation::{self, Tr},
|
||||||
|
|
@ -38,6 +39,7 @@ pub struct AppState {
|
||||||
pub config: Config,
|
pub config: Config,
|
||||||
pub db_connection: db::Connection,
|
pub db_connection: db::Connection,
|
||||||
pub log: Log,
|
pub log: Log,
|
||||||
|
pub email_service: Arc<dyn email::EmailServiceTrait>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromRef<AppState> for Config {
|
impl FromRef<AppState> for Config {
|
||||||
|
|
@ -58,6 +60,12 @@ impl FromRef<AppState> for Log {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromRef<AppState> for Arc<dyn email::EmailServiceTrait> {
|
||||||
|
fn from_ref(app_state: &AppState) -> Arc<dyn email::EmailServiceTrait> {
|
||||||
|
app_state.email_service.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl axum::response::IntoResponse for db::DBError {
|
impl axum::response::IntoResponse for db::DBError {
|
||||||
fn into_response(self) -> Response {
|
fn into_response(self) -> Response {
|
||||||
ron_utils::ron_error(StatusCode::INTERNAL_SERVER_ERROR, &self.to_string()).into_response()
|
ron_utils::ron_error(StatusCode::INTERNAL_SERVER_ERROR, &self.to_string()).into_response()
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use lettre::{
|
use lettre::{
|
||||||
AsyncTransport, Message, Tokio1Executor,
|
AsyncTransport, Message, Tokio1Executor,
|
||||||
transport::smtp::{AsyncSmtpTransport, authentication::Credentials},
|
transport::smtp::{AsyncSmtpTransport, authentication::Credentials},
|
||||||
|
|
@ -18,33 +20,52 @@ pub enum Error {
|
||||||
Email(#[from] lettre::error::Error),
|
Email(#[from] lettre::error::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A function to send an email using the given SMTP address.
|
#[async_trait::async_trait]
|
||||||
/// It may timeout if the SMTP server is not reachable, see [consts::SEND_EMAIL_TIMEOUT].
|
pub trait EmailServiceTrait: Send + Sync {
|
||||||
pub async fn send_email(
|
async fn send_email(&self, email: &str, title: &str, message: &str) -> Result<(), Error>;
|
||||||
email: &str,
|
}
|
||||||
title: &str,
|
|
||||||
message: &str,
|
pub struct EmailService {
|
||||||
smtp_relay_address: &str,
|
smtp_relay_address: String,
|
||||||
smtp_login: &str,
|
smtp_login: String,
|
||||||
smtp_password: &str,
|
smtp_password: String,
|
||||||
) -> Result<(), Error> {
|
}
|
||||||
let email = Message::builder()
|
|
||||||
.message_id(None)
|
impl EmailService {
|
||||||
.from(consts::EMAIL_ADDRESS.parse()?)
|
pub fn create_service(
|
||||||
.to(email.parse()?)
|
smtp_relay_address: &str,
|
||||||
.subject(title)
|
smtp_login: &str,
|
||||||
.body(message.to_string())?;
|
smtp_password: &str,
|
||||||
|
) -> Arc<dyn EmailServiceTrait> {
|
||||||
let credentials = Credentials::new(smtp_login.to_string(), smtp_password.to_string());
|
Arc::new(Self {
|
||||||
|
smtp_relay_address: smtp_relay_address.to_string(),
|
||||||
let mailer = AsyncSmtpTransport::<Tokio1Executor>::relay(smtp_relay_address)?
|
smtp_login: smtp_login.to_string(),
|
||||||
.credentials(credentials)
|
smtp_password: smtp_password.to_string(),
|
||||||
.timeout(Some(consts::SEND_EMAIL_TIMEOUT))
|
})
|
||||||
.build();
|
}
|
||||||
|
}
|
||||||
if let Err(error) = mailer.send(email).await {
|
|
||||||
error!("Error when sending E-mail: {}", &error);
|
#[async_trait::async_trait]
|
||||||
}
|
impl EmailServiceTrait for EmailService {
|
||||||
|
/// A function to send an email using the given SMTP address.
|
||||||
Ok(())
|
/// It may timeout if the SMTP server is not reachable, see [consts::SEND_EMAIL_TIMEOUT].
|
||||||
|
async fn send_email(&self, email: &str, title: &str, message: &str) -> Result<(), Error> {
|
||||||
|
let email = Message::builder()
|
||||||
|
.message_id(None)
|
||||||
|
.from(consts::EMAIL_ADDRESS.parse()?)
|
||||||
|
.to(email.parse()?)
|
||||||
|
.subject(title)
|
||||||
|
.body(message.to_string())?;
|
||||||
|
|
||||||
|
let credentials = Credentials::new(self.smtp_login.clone(), self.smtp_password.clone());
|
||||||
|
|
||||||
|
let mailer = AsyncSmtpTransport::<Tokio1Executor>::relay(&self.smtp_relay_address)?
|
||||||
|
.credentials(credentials)
|
||||||
|
.timeout(Some(consts::SEND_EMAIL_TIMEOUT))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
mailer.send(email).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,9 @@ pub mod app;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod consts;
|
pub mod consts;
|
||||||
pub mod data;
|
pub mod data;
|
||||||
|
pub mod email;
|
||||||
pub mod log;
|
pub mod log;
|
||||||
|
|
||||||
mod email;
|
|
||||||
mod hash;
|
mod hash;
|
||||||
mod html_templates;
|
mod html_templates;
|
||||||
mod ron_extractor;
|
mod ron_extractor;
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ use tracing::{error, info};
|
||||||
use recipes::{
|
use recipes::{
|
||||||
app, config, consts,
|
app, config, consts,
|
||||||
data::{backup, db},
|
data::{backup, db},
|
||||||
|
email,
|
||||||
log::Log,
|
log::Log,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -17,13 +18,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let config = config::load();
|
let config = config::load();
|
||||||
let log = Log::new_to_directory(&config.logs_directory);
|
let log = Log::new_to_directory(&config.logs_directory);
|
||||||
|
|
||||||
info!("Configuration: {:?}", config);
|
|
||||||
|
|
||||||
if !process_args(&config.database_directory).await {
|
if !process_args(&config.database_directory).await {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
info!("Starting Recipes as web server...");
|
info!("Starting Recipes as web server...");
|
||||||
|
info!("Configuration: {:?}", config);
|
||||||
|
|
||||||
let db_connection = db::Connection::new(&config.database_directory)
|
let db_connection = db::Connection::new(&config.database_directory)
|
||||||
.await
|
.await
|
||||||
|
|
@ -42,9 +42,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let port = config.port;
|
let port = config.port;
|
||||||
|
|
||||||
let state = app::AppState {
|
let state = app::AppState {
|
||||||
config,
|
|
||||||
db_connection,
|
db_connection,
|
||||||
log,
|
log,
|
||||||
|
email_service: email::EmailService::create_service(
|
||||||
|
&config.smtp_relay_address,
|
||||||
|
&config.smtp_login,
|
||||||
|
&config.smtp_password,
|
||||||
|
),
|
||||||
|
config,
|
||||||
};
|
};
|
||||||
|
|
||||||
let make_service = app::make_service(state);
|
let make_service = app::make_service(state);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{collections::HashMap, net::SocketAddr};
|
use std::{collections::HashMap, net::SocketAddr, sync::Arc};
|
||||||
|
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
use axum::{
|
use axum::{
|
||||||
|
|
@ -16,12 +16,10 @@ use axum_extra::extract::{
|
||||||
use chrono::Duration;
|
use chrono::Duration;
|
||||||
use lettre::Address;
|
use lettre::Address;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use strum_macros::Display;
|
|
||||||
use tracing::{error, warn};
|
use tracing::{error, warn};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
app::{AppState, Context, Result},
|
app::{AppState, Context, Result},
|
||||||
config::Config,
|
|
||||||
consts,
|
consts,
|
||||||
data::db,
|
data::db,
|
||||||
email,
|
email,
|
||||||
|
|
@ -66,21 +64,32 @@ pub struct SignUpFormData {
|
||||||
password_2: String,
|
password_2: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Display)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
enum SignUpError {
|
enum SignUpError {
|
||||||
|
#[error("Invalid email")]
|
||||||
InvalidEmail,
|
InvalidEmail,
|
||||||
|
|
||||||
|
#[error("Password not equal")]
|
||||||
PasswordsNotEqual,
|
PasswordsNotEqual,
|
||||||
|
|
||||||
|
#[error("Invalid password")]
|
||||||
InvalidPassword,
|
InvalidPassword,
|
||||||
|
|
||||||
|
#[error("User already exists")]
|
||||||
UserAlreadyExists,
|
UserAlreadyExists,
|
||||||
DatabaseError,
|
|
||||||
UnableSendEmail,
|
#[error("Database error: {0}")]
|
||||||
|
DatabaseError(db::DBError),
|
||||||
|
|
||||||
|
#[error("Unable to send email: {0}")]
|
||||||
|
UnableToSendEmail(email::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[debug_handler(state = AppState)]
|
#[debug_handler(state = AppState)]
|
||||||
pub async fn sign_up_post(
|
pub async fn sign_up_post(
|
||||||
Host(host): Host,
|
Host(host): Host,
|
||||||
State(connection): State<db::Connection>,
|
State(connection): State<db::Connection>,
|
||||||
State(config): State<Config>,
|
State(email_service): State<Arc<dyn email::EmailServiceTrait>>,
|
||||||
Extension(context): Extension<Context>,
|
Extension(context): Extension<Context>,
|
||||||
Form(form_data): Form<SignUpFormData>,
|
Form(form_data): Form<SignUpFormData>,
|
||||||
) -> Result<Response> {
|
) -> Result<Response> {
|
||||||
|
|
@ -89,8 +98,8 @@ pub async fn sign_up_post(
|
||||||
form_data: &SignUpFormData,
|
form_data: &SignUpFormData,
|
||||||
context: Context,
|
context: Context,
|
||||||
) -> Result<Response> {
|
) -> Result<Response> {
|
||||||
warn!(
|
error!(
|
||||||
"Unable to sign up with email {}: {}",
|
"Error during sign up (email={}): {}",
|
||||||
form_data.email, error
|
form_data.email, error
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -112,8 +121,9 @@ pub async fn sign_up_post(
|
||||||
},
|
},
|
||||||
message: match error {
|
message: match error {
|
||||||
SignUpError::UserAlreadyExists => context.tr.t(Sentence::EmailAlreadyTaken),
|
SignUpError::UserAlreadyExists => context.tr.t(Sentence::EmailAlreadyTaken),
|
||||||
SignUpError::DatabaseError => context.tr.t(Sentence::DatabaseError),
|
// The error is not shown to the user (it is logged above).
|
||||||
SignUpError::UnableSendEmail => context.tr.t(Sentence::UnableToSendEmail),
|
SignUpError::DatabaseError(_) => context.tr.t(Sentence::DatabaseError),
|
||||||
|
SignUpError::UnableToSendEmail(_) => context.tr.t(Sentence::UnableToSendEmail),
|
||||||
_ => "",
|
_ => "",
|
||||||
},
|
},
|
||||||
context,
|
context,
|
||||||
|
|
@ -159,31 +169,31 @@ pub async fn sign_up_post(
|
||||||
Ok(db::user::SignUpResult::UserCreatedWaitingForValidation(token)) => {
|
Ok(db::user::SignUpResult::UserCreatedWaitingForValidation(token)) => {
|
||||||
let url = utils::get_url_from_host(&host);
|
let url = utils::get_url_from_host(&host);
|
||||||
let email = form_data.email.clone();
|
let email = form_data.email.clone();
|
||||||
match email::send_email(
|
match email_service
|
||||||
&email,
|
.send_email(
|
||||||
context.tr.t(Sentence::SignUpEmailTitle),
|
&email,
|
||||||
&context.tr.tp(
|
context.tr.t(Sentence::SignUpEmailTitle),
|
||||||
Sentence::SignUpFollowEmailLink,
|
&context.tr.tp(
|
||||||
&[Box::new(format!(
|
Sentence::SignUpFollowEmailLink,
|
||||||
"{}/validation?{}={}",
|
&[Box::new(format!(
|
||||||
url, VALIDATION_TOKEN_KEY, token
|
"{}/validation?{}={}",
|
||||||
))],
|
url, VALIDATION_TOKEN_KEY, token
|
||||||
),
|
))],
|
||||||
&config.smtp_relay_address,
|
),
|
||||||
&config.smtp_login,
|
)
|
||||||
&config.smtp_password,
|
.await
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
{
|
||||||
Ok(()) => Ok(Html(
|
Ok(()) => Ok(Html(
|
||||||
MessageTemplate::new(context.tr.t(Sentence::SignUpEmailSent), context)
|
MessageTemplate::new(context.tr.t(Sentence::SignUpEmailSent), context)
|
||||||
.render()?,
|
.render()?,
|
||||||
)
|
)
|
||||||
.into_response()),
|
.into_response()),
|
||||||
Err(_) => error_response(SignUpError::UnableSendEmail, &form_data, context),
|
Err(error) => {
|
||||||
|
error_response(SignUpError::UnableToSendEmail(error), &form_data, context)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(_) => error_response(SignUpError::DatabaseError, &form_data, context),
|
Err(error) => error_response(SignUpError::DatabaseError(error), &form_data, context),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -415,19 +425,29 @@ pub struct AskResetPasswordForm {
|
||||||
email: String,
|
email: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
enum AskResetPasswordError {
|
enum AskResetPasswordError {
|
||||||
|
#[error("Invalid email")]
|
||||||
InvalidEmail,
|
InvalidEmail,
|
||||||
|
|
||||||
|
#[error("Email already reset")]
|
||||||
EmailAlreadyReset,
|
EmailAlreadyReset,
|
||||||
|
|
||||||
|
#[error("Email unknown")]
|
||||||
EmailUnknown,
|
EmailUnknown,
|
||||||
UnableSendEmail,
|
|
||||||
DatabaseError,
|
#[error("Database Error: {0}")]
|
||||||
|
DatabaseError(db::DBError),
|
||||||
|
|
||||||
|
#[error("Unable to send email: {0}")]
|
||||||
|
UnableSendEmail(email::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[debug_handler(state = AppState)]
|
#[debug_handler(state = AppState)]
|
||||||
pub async fn ask_reset_password_post(
|
pub async fn ask_reset_password_post(
|
||||||
Host(host): Host,
|
Host(host): Host,
|
||||||
State(connection): State<db::Connection>,
|
State(connection): State<db::Connection>,
|
||||||
State(config): State<Config>,
|
State(email_service): State<Arc<dyn email::EmailServiceTrait>>,
|
||||||
Extension(context): Extension<Context>,
|
Extension(context): Extension<Context>,
|
||||||
Form(form_data): Form<AskResetPasswordForm>,
|
Form(form_data): Form<AskResetPasswordForm>,
|
||||||
) -> Result<Response> {
|
) -> Result<Response> {
|
||||||
|
|
@ -436,6 +456,11 @@ pub async fn ask_reset_password_post(
|
||||||
email: &str,
|
email: &str,
|
||||||
context: Context,
|
context: Context,
|
||||||
) -> Result<Response> {
|
) -> Result<Response> {
|
||||||
|
error!(
|
||||||
|
"Error when asking password reset (email={}): {}",
|
||||||
|
email, error
|
||||||
|
);
|
||||||
|
|
||||||
Ok(Html(
|
Ok(Html(
|
||||||
AskResetPasswordTemplate {
|
AskResetPasswordTemplate {
|
||||||
email,
|
email,
|
||||||
|
|
@ -448,10 +473,12 @@ pub async fn ask_reset_password_post(
|
||||||
context.tr.t(Sentence::AskResetEmailAlreadyResetError)
|
context.tr.t(Sentence::AskResetEmailAlreadyResetError)
|
||||||
}
|
}
|
||||||
AskResetPasswordError::EmailUnknown => context.tr.t(Sentence::EmailUnknown),
|
AskResetPasswordError::EmailUnknown => context.tr.t(Sentence::EmailUnknown),
|
||||||
AskResetPasswordError::UnableSendEmail => {
|
AskResetPasswordError::UnableSendEmail(_) => {
|
||||||
context.tr.t(Sentence::UnableToSendResetEmail)
|
context.tr.t(Sentence::UnableToSendResetEmail)
|
||||||
}
|
}
|
||||||
AskResetPasswordError::DatabaseError => context.tr.t(Sentence::DatabaseError),
|
AskResetPasswordError::DatabaseError(_) => {
|
||||||
|
context.tr.t(Sentence::DatabaseError)
|
||||||
|
}
|
||||||
_ => "",
|
_ => "",
|
||||||
},
|
},
|
||||||
context,
|
context,
|
||||||
|
|
@ -489,45 +516,37 @@ pub async fn ask_reset_password_post(
|
||||||
),
|
),
|
||||||
Ok(db::user::GetTokenResetPasswordResult::Ok(token)) => {
|
Ok(db::user::GetTokenResetPasswordResult::Ok(token)) => {
|
||||||
let url = utils::get_url_from_host(&host);
|
let url = utils::get_url_from_host(&host);
|
||||||
match email::send_email(
|
match email_service
|
||||||
&form_data.email,
|
.send_email(
|
||||||
context.tr.t(Sentence::AskResetEmailTitle),
|
&form_data.email,
|
||||||
&context.tr.tp(
|
context.tr.t(Sentence::AskResetEmailTitle),
|
||||||
Sentence::AskResetFollowEmailLink,
|
&context.tr.tp(
|
||||||
&[Box::new(format!(
|
Sentence::AskResetFollowEmailLink,
|
||||||
"{}/reset_password?reset_token={}",
|
&[Box::new(format!(
|
||||||
url, token
|
"{}/reset_password?reset_token={}",
|
||||||
))],
|
url, token
|
||||||
),
|
))],
|
||||||
&config.smtp_relay_address,
|
),
|
||||||
&config.smtp_login,
|
)
|
||||||
&config.smtp_password,
|
.await
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
{
|
||||||
Ok(()) => Ok(Html(
|
Ok(()) => Ok(Html(
|
||||||
MessageTemplate::new(context.tr.t(Sentence::AskResetEmailSent), context)
|
MessageTemplate::new(context.tr.t(Sentence::AskResetEmailSent), context)
|
||||||
.render()?,
|
.render()?,
|
||||||
)
|
)
|
||||||
.into_response()),
|
.into_response()),
|
||||||
Err(error) => {
|
Err(error) => error_response(
|
||||||
error!("Email validation error: {}", error);
|
AskResetPasswordError::UnableSendEmail(error),
|
||||||
error_response(
|
&form_data.email,
|
||||||
AskResetPasswordError::UnableSendEmail,
|
context,
|
||||||
&form_data.email,
|
),
|
||||||
context,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(error) => {
|
Err(error) => error_response(
|
||||||
error!("{}", error);
|
AskResetPasswordError::DatabaseError(error),
|
||||||
error_response(
|
&form_data.email,
|
||||||
AskResetPasswordError::DatabaseError,
|
context,
|
||||||
&form_data.email,
|
),
|
||||||
context,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -578,12 +597,19 @@ pub struct ResetPasswordForm {
|
||||||
reset_token: String,
|
reset_token: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Display)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
enum ResetPasswordError {
|
enum ResetPasswordError {
|
||||||
|
#[error("Password not equal")]
|
||||||
PasswordsNotEqual,
|
PasswordsNotEqual,
|
||||||
|
|
||||||
|
#[error("Invalid password")]
|
||||||
InvalidPassword,
|
InvalidPassword,
|
||||||
|
|
||||||
|
#[error("Token expired")]
|
||||||
TokenExpired,
|
TokenExpired,
|
||||||
DatabaseError,
|
|
||||||
|
#[error("Database error: {0}")]
|
||||||
|
DatabaseError(db::DBError),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[debug_handler]
|
#[debug_handler]
|
||||||
|
|
@ -597,8 +623,8 @@ pub async fn reset_password_post(
|
||||||
form_data: &ResetPasswordForm,
|
form_data: &ResetPasswordForm,
|
||||||
context: Context,
|
context: Context,
|
||||||
) -> Result<Response> {
|
) -> Result<Response> {
|
||||||
warn!(
|
error!(
|
||||||
"Email: {}: {}",
|
"Error during password reset (email={}): {}",
|
||||||
if let Some(ref user) = context.user {
|
if let Some(ref user) = context.user {
|
||||||
&user.email
|
&user.email
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -624,7 +650,7 @@ pub async fn reset_password_post(
|
||||||
ResetPasswordError::TokenExpired => {
|
ResetPasswordError::TokenExpired => {
|
||||||
context.tr.t(Sentence::AskResetTokenExpired)
|
context.tr.t(Sentence::AskResetTokenExpired)
|
||||||
}
|
}
|
||||||
ResetPasswordError::DatabaseError => context.tr.t(Sentence::DatabaseError),
|
ResetPasswordError::DatabaseError(_) => context.tr.t(Sentence::DatabaseError),
|
||||||
_ => "",
|
_ => "",
|
||||||
},
|
},
|
||||||
context,
|
context,
|
||||||
|
|
@ -659,7 +685,11 @@ pub async fn reset_password_post(
|
||||||
Ok(db::user::ResetPasswordResult::ResetTokenExpired) => {
|
Ok(db::user::ResetPasswordResult::ResetTokenExpired) => {
|
||||||
error_response(ResetPasswordError::TokenExpired, &form_data, context)
|
error_response(ResetPasswordError::TokenExpired, &form_data, context)
|
||||||
}
|
}
|
||||||
Err(_) => error_response(ResetPasswordError::DatabaseError, &form_data, context),
|
Err(error) => error_response(
|
||||||
|
ResetPasswordError::DatabaseError(error),
|
||||||
|
&form_data,
|
||||||
|
context,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -697,21 +727,32 @@ pub struct EditUserForm {
|
||||||
password_2: String,
|
password_2: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Display)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
enum ProfileUpdateError {
|
enum ProfileUpdateError {
|
||||||
|
#[error("Invalid email")]
|
||||||
InvalidEmail,
|
InvalidEmail,
|
||||||
|
|
||||||
|
#[error("Email already taken")]
|
||||||
EmailAlreadyTaken,
|
EmailAlreadyTaken,
|
||||||
|
|
||||||
|
#[error("Password not equal")]
|
||||||
PasswordsNotEqual,
|
PasswordsNotEqual,
|
||||||
|
|
||||||
|
#[error("Invalid password")]
|
||||||
InvalidPassword,
|
InvalidPassword,
|
||||||
DatabaseError,
|
|
||||||
UnableSendEmail,
|
#[error("Database error: {0}")]
|
||||||
|
DatabaseError(db::DBError),
|
||||||
|
|
||||||
|
#[error("Unable to send email: {0}")]
|
||||||
|
UnableToSendEmail(email::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[debug_handler(state = AppState)]
|
#[debug_handler(state = AppState)]
|
||||||
pub async fn edit_user_post(
|
pub async fn edit_user_post(
|
||||||
Host(host): Host,
|
Host(host): Host,
|
||||||
State(connection): State<db::Connection>,
|
State(connection): State<db::Connection>,
|
||||||
State(config): State<Config>,
|
State(email_service): State<Arc<dyn email::EmailServiceTrait>>,
|
||||||
Extension(context): Extension<Context>,
|
Extension(context): Extension<Context>,
|
||||||
Form(form_data): Form<EditUserForm>,
|
Form(form_data): Form<EditUserForm>,
|
||||||
) -> Result<Response> {
|
) -> Result<Response> {
|
||||||
|
|
@ -721,8 +762,8 @@ pub async fn edit_user_post(
|
||||||
form_data: &EditUserForm,
|
form_data: &EditUserForm,
|
||||||
context: Context,
|
context: Context,
|
||||||
) -> Result<Response> {
|
) -> Result<Response> {
|
||||||
warn!(
|
error!(
|
||||||
"Email: {}: {}",
|
"Error during edit user (email={}): {}",
|
||||||
if let Some(ref user) = context.user {
|
if let Some(ref user) = context.user {
|
||||||
&user.email
|
&user.email
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -754,8 +795,10 @@ pub async fn edit_user_post(
|
||||||
_ => "",
|
_ => "",
|
||||||
},
|
},
|
||||||
message: match error {
|
message: match error {
|
||||||
ProfileUpdateError::DatabaseError => context.tr.t(Sentence::DatabaseError),
|
ProfileUpdateError::DatabaseError(_) => {
|
||||||
ProfileUpdateError::UnableSendEmail => {
|
context.tr.t(Sentence::DatabaseError)
|
||||||
|
}
|
||||||
|
ProfileUpdateError::UnableToSendEmail(_) => {
|
||||||
context.tr.t(Sentence::UnableToSendEmail)
|
context.tr.t(Sentence::UnableToSendEmail)
|
||||||
}
|
}
|
||||||
_ => "",
|
_ => "",
|
||||||
|
|
@ -805,29 +848,26 @@ pub async fn edit_user_post(
|
||||||
Ok(db::user::UpdateUserResult::UserUpdatedWaitingForRevalidation(token)) => {
|
Ok(db::user::UpdateUserResult::UserUpdatedWaitingForRevalidation(token)) => {
|
||||||
let url = utils::get_url_from_host(&host);
|
let url = utils::get_url_from_host(&host);
|
||||||
let email = form_data.email.clone();
|
let email = form_data.email.clone();
|
||||||
match email::send_email(
|
match email_service
|
||||||
&email,
|
.send_email(
|
||||||
context.tr.t(Sentence::ProfileFollowEmailTitle),
|
&email,
|
||||||
&context.tr.tp(
|
context.tr.t(Sentence::ProfileFollowEmailTitle),
|
||||||
Sentence::ProfileFollowEmailLink,
|
&context.tr.tp(
|
||||||
&[Box::new(format!(
|
Sentence::ProfileFollowEmailLink,
|
||||||
"{}/revalidation?{}={}",
|
&[Box::new(format!(
|
||||||
url, VALIDATION_TOKEN_KEY, token
|
"{}/revalidation?{}={}",
|
||||||
))],
|
url, VALIDATION_TOKEN_KEY, token
|
||||||
),
|
))],
|
||||||
&config.smtp_relay_address,
|
),
|
||||||
&config.smtp_login,
|
)
|
||||||
&config.smtp_password,
|
.await
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
{
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
message = context.tr.t(Sentence::ProfileEmailSent);
|
message = context.tr.t(Sentence::ProfileEmailSent);
|
||||||
}
|
}
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
error!("Email validation error: {}", error);
|
|
||||||
return error_response(
|
return error_response(
|
||||||
ProfileUpdateError::UnableSendEmail,
|
ProfileUpdateError::UnableToSendEmail(error),
|
||||||
&form_data,
|
&form_data,
|
||||||
context,
|
context,
|
||||||
);
|
);
|
||||||
|
|
@ -837,8 +877,12 @@ pub async fn edit_user_post(
|
||||||
Ok(db::user::UpdateUserResult::Ok) => {
|
Ok(db::user::UpdateUserResult::Ok) => {
|
||||||
message = context.tr.t(Sentence::ProfileSaved);
|
message = context.tr.t(Sentence::ProfileSaved);
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(error) => {
|
||||||
return error_response(ProfileUpdateError::DatabaseError, &form_data, context);
|
return error_response(
|
||||||
|
ProfileUpdateError::DatabaseError(error),
|
||||||
|
&form_data,
|
||||||
|
context,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -914,7 +958,7 @@ pub async fn email_revalidation(
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
error @ db::user::ValidationResult::ValidationExpired => {
|
error @ db::user::ValidationResult::ValidationExpired => {
|
||||||
warn!("Token: {}: {}", token, error);
|
error!("Token: {}: {}", token, error);
|
||||||
Ok((
|
Ok((
|
||||||
jar,
|
jar,
|
||||||
Html(
|
Html(
|
||||||
|
|
@ -927,7 +971,7 @@ pub async fn email_revalidation(
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
error @ db::user::ValidationResult::UnknownUser => {
|
error @ db::user::ValidationResult::UnknownUser => {
|
||||||
warn!("Email: {}: {}", token, error);
|
error!("(email={}): {}", token, error);
|
||||||
Ok((
|
Ok((
|
||||||
jar,
|
jar,
|
||||||
Html(
|
Html(
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ use cookie::Cookie;
|
||||||
use scraper::{ElementRef, Html, Selector};
|
use scraper::{ElementRef, Html, Selector};
|
||||||
|
|
||||||
use recipes::app;
|
use recipes::app;
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
|
|
@ -92,3 +93,36 @@ async fn user_edit() -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Debug)]
|
||||||
|
pub struct SignUpFormData {
|
||||||
|
email: String,
|
||||||
|
password_1: String,
|
||||||
|
password_2: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn sign_up() -> Result<(), Box<dyn Error>> {
|
||||||
|
// Arrange.
|
||||||
|
let state = utils::common_state().await?;
|
||||||
|
let server = TestServer::new(app::make_service(state))?;
|
||||||
|
|
||||||
|
// Act.
|
||||||
|
let response = server
|
||||||
|
.post("/signup")
|
||||||
|
.form(&SignUpFormData {
|
||||||
|
email: "president@spaceball.planet".into(),
|
||||||
|
password_1: "12345678".into(),
|
||||||
|
password_2: "12345678".into(),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
// Assert.
|
||||||
|
response.assert_status_ok();
|
||||||
|
|
||||||
|
let document = Html::parse_document(&response.text());
|
||||||
|
assert_eq!(document.errors.len(), 0);
|
||||||
|
dbg!(response);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
|
||||||
23
backend/tests/utils/mock_email.rs
Normal file
23
backend/tests/utils/mock_email.rs
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use recipes::email;
|
||||||
|
|
||||||
|
pub struct MockEmailService;
|
||||||
|
|
||||||
|
impl MockEmailService {
|
||||||
|
pub fn create_service() -> Arc<dyn email::EmailServiceTrait> {
|
||||||
|
Arc::new(Self {})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl email::EmailServiceTrait for MockEmailService {
|
||||||
|
async fn send_email(
|
||||||
|
&self,
|
||||||
|
_email: &str,
|
||||||
|
_title: &str,
|
||||||
|
_message: &str,
|
||||||
|
) -> Result<(), email::Error> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,6 +2,8 @@ use std::error::Error;
|
||||||
|
|
||||||
use recipes::{app, config, data::db, log};
|
use recipes::{app, config, data::db, log};
|
||||||
|
|
||||||
|
mod mock_email;
|
||||||
|
|
||||||
pub async fn common_state() -> Result<app::AppState, Box<dyn Error>> {
|
pub async fn common_state() -> Result<app::AppState, Box<dyn Error>> {
|
||||||
let db_connection = db::Connection::new_in_memory().await?;
|
let db_connection = db::Connection::new_in_memory().await?;
|
||||||
let config = config::Config::default();
|
let config = config::Config::default();
|
||||||
|
|
@ -10,6 +12,7 @@ pub async fn common_state() -> Result<app::AppState, Box<dyn Error>> {
|
||||||
config,
|
config,
|
||||||
db_connection,
|
db_connection,
|
||||||
log,
|
log,
|
||||||
|
email_service: mock_email::MockEmailService::create_service(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue