From 3626f8a11b78b1ba224f79a3fc0b66d9253ae1cb Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Fri, 2 May 2025 00:57:32 +0200 Subject: [PATCH] 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 --- Cargo.lock | 37 ++-- backend/Cargo.toml | 1 + backend/src/app.rs | 10 +- backend/src/email.rs | 79 +++++--- backend/src/lib.rs | 2 +- backend/src/main.rs | 11 +- backend/src/services/user.rs | 242 +++++++++++++---------- backend/tests/http.rs | 34 ++++ backend/tests/utils/mock_email.rs | 23 +++ backend/tests/{utils.rs => utils/mod.rs} | 3 + 10 files changed, 291 insertions(+), 151 deletions(-) create mode 100644 backend/tests/utils/mock_email.rs rename backend/tests/{utils.rs => utils/mod.rs} (95%) diff --git a/Cargo.lock b/Cargo.lock index 15d84cd..703d5ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -166,7 +166,7 @@ dependencies = [ "memchr", "serde", "serde_derive", - "winnow 0.7.7", + "winnow 0.7.8", ] [[package]] @@ -226,9 +226,9 @@ checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" [[package]] name = "axum" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de45108900e1f9b9242f7f2e254aa3e2c029c921c258fe9e6b4217eeebd54288" +checksum = "021e862c184ae977658b36c4500f7feac3221ca5da43e3f25bd04ab6c79a29b5" dependencies = [ "axum-core", "axum-macros", @@ -1385,9 +1385,9 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.15.2" +version = "0.15.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" +checksum = "84b26c544d002229e640969970a2e74021aadf6e2f96372b9c58eff97de08eb3" dependencies = [ "allocator-api2", "equivalent", @@ -1400,7 +1400,7 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7382cf6263419f2d8df38c55d7da83da5c18aef87fc7a7fc1fb1e344edfe14c1" dependencies = [ - "hashbrown 0.15.2", + "hashbrown 0.15.3", ] [[package]] @@ -1744,7 +1744,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cea70ddb795996207ad57735b50c5982d8844f38ba9ee5f1aedcfb708a2aa11e" dependencies = [ "equivalent", - "hashbrown 0.15.2", + "hashbrown 0.15.3", ] [[package]] @@ -2453,6 +2453,7 @@ dependencies = [ "argon2", "askama", "async-compression", + "async-trait", "axum", "axum-extra", "axum-test", @@ -2618,9 +2619,9 @@ checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" [[package]] name = "rustix" -version = "1.0.5" +version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d97817398dd4bb2e6da002002db259209759911da105da92bec29ccb12cf58bf" +checksum = "c71e83d6afe7ff64890ec6b71d6a69bb8a610ab78ce364b3352876bb4c801266" dependencies = [ "bitflags 2.9.0", "errno", @@ -2831,9 +2832,9 @@ dependencies = [ [[package]] name = "sha2" -version = "0.10.8" +version = "0.10.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" dependencies = [ "cfg-if", "cpufeatures", @@ -2973,7 +2974,7 @@ dependencies = [ "futures-intrusive", "futures-io", "futures-util", - "hashbrown 0.15.2", + "hashbrown 0.15.3", "hashlink", "indexmap", "log", @@ -3241,9 +3242,9 @@ checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" [[package]] name = "synstructure" -version = "0.13.1" +version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" dependencies = [ "proc-macro2", "quote", @@ -3884,9 +3885,9 @@ dependencies = [ [[package]] name = "webpki-roots" -version = "0.26.9" +version = "0.26.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29aad86cec885cafd03e8305fd727c418e970a521322c91688414d5b8efba16b" +checksum = "37493cadf42a2a939ed404698ded7fb378bf301b5011f973361779a3a74f8c93" dependencies = [ "rustls-pki-types", ] @@ -4150,9 +4151,9 @@ dependencies = [ [[package]] name = "winnow" -version = "0.7.7" +version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6cb8234a863ea0e8cd7284fcdd4f145233eb00fee02bbdd9861aec44e6477bc5" +checksum = "9e27d6ad3dac991091e4d35de9ba2d2d00647c5d0fc26c5496dee55984ae111b" dependencies = [ "memchr", ] diff --git a/backend/Cargo.toml b/backend/Cargo.toml index ae59e93..7486cf3 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -38,6 +38,7 @@ rand_core = { version = "0.9", features = ["std"] } rand = "0.9" strum = "0.27" strum_macros = "0.27" +async-trait = "0.1" lettre = { version = "0.11", default-features = false, features = [ "smtp-transport", diff --git a/backend/src/app.rs b/backend/src/app.rs index 01898df..4057bbb 100644 --- a/backend/src/app.rs +++ b/backend/src/app.rs @@ -1,4 +1,4 @@ -use std::net::SocketAddr; +use std::{net::SocketAddr, sync::Arc}; use axum::{ BoxError, Router, ServiceExt, @@ -27,6 +27,7 @@ use crate::{ config::Config, consts, data::{db, model}, + email, log::Log, ron_utils, services, translation::{self, Tr}, @@ -38,6 +39,7 @@ pub struct AppState { pub config: Config, pub db_connection: db::Connection, pub log: Log, + pub email_service: Arc, } impl FromRef for Config { @@ -58,6 +60,12 @@ impl FromRef for Log { } } +impl FromRef for Arc { + fn from_ref(app_state: &AppState) -> Arc { + app_state.email_service.clone() + } +} + impl axum::response::IntoResponse for db::DBError { fn into_response(self) -> Response { ron_utils::ron_error(StatusCode::INTERNAL_SERVER_ERROR, &self.to_string()).into_response() diff --git a/backend/src/email.rs b/backend/src/email.rs index a59c5bb..250a0d8 100644 --- a/backend/src/email.rs +++ b/backend/src/email.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use lettre::{ AsyncTransport, Message, Tokio1Executor, transport::smtp::{AsyncSmtpTransport, authentication::Credentials}, @@ -18,33 +20,52 @@ pub enum Error { Email(#[from] lettre::error::Error), } -/// A function to send an email using the given SMTP address. -/// It may timeout if the SMTP server is not reachable, see [consts::SEND_EMAIL_TIMEOUT]. -pub async fn send_email( - email: &str, - title: &str, - message: &str, - smtp_relay_address: &str, - smtp_login: &str, - smtp_password: &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(smtp_login.to_string(), smtp_password.to_string()); - - let mailer = AsyncSmtpTransport::::relay(smtp_relay_address)? - .credentials(credentials) - .timeout(Some(consts::SEND_EMAIL_TIMEOUT)) - .build(); - - if let Err(error) = mailer.send(email).await { - error!("Error when sending E-mail: {}", &error); - } - - Ok(()) +#[async_trait::async_trait] +pub trait EmailServiceTrait: Send + Sync { + async fn send_email(&self, email: &str, title: &str, message: &str) -> Result<(), Error>; +} + +pub struct EmailService { + smtp_relay_address: String, + smtp_login: String, + smtp_password: String, +} + +impl EmailService { + pub fn create_service( + smtp_relay_address: &str, + smtp_login: &str, + smtp_password: &str, + ) -> Arc { + Arc::new(Self { + smtp_relay_address: smtp_relay_address.to_string(), + smtp_login: smtp_login.to_string(), + smtp_password: smtp_password.to_string(), + }) + } +} + +#[async_trait::async_trait] +impl EmailServiceTrait for EmailService { + /// A function to send an email using the given SMTP address. + /// 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::::relay(&self.smtp_relay_address)? + .credentials(credentials) + .timeout(Some(consts::SEND_EMAIL_TIMEOUT)) + .build(); + + mailer.send(email).await?; + + Ok(()) + } } diff --git a/backend/src/lib.rs b/backend/src/lib.rs index b5ee36f..4380fe8 100644 --- a/backend/src/lib.rs +++ b/backend/src/lib.rs @@ -2,9 +2,9 @@ pub mod app; pub mod config; pub mod consts; pub mod data; +pub mod email; pub mod log; -mod email; mod hash; mod html_templates; mod ron_extractor; diff --git a/backend/src/main.rs b/backend/src/main.rs index 09a65c7..43a08c9 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -9,6 +9,7 @@ use tracing::{error, info}; use recipes::{ app, config, consts, data::{backup, db}, + email, log::Log, }; @@ -17,13 +18,12 @@ async fn main() -> Result<(), Box> { let config = config::load(); let log = Log::new_to_directory(&config.logs_directory); - info!("Configuration: {:?}", config); - if !process_args(&config.database_directory).await { return Ok(()); } info!("Starting Recipes as web server..."); + info!("Configuration: {:?}", config); let db_connection = db::Connection::new(&config.database_directory) .await @@ -42,9 +42,14 @@ async fn main() -> Result<(), Box> { let port = config.port; let state = app::AppState { - config, db_connection, 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); diff --git a/backend/src/services/user.rs b/backend/src/services/user.rs index 2223569..31c2630 100644 --- a/backend/src/services/user.rs +++ b/backend/src/services/user.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, net::SocketAddr}; +use std::{collections::HashMap, net::SocketAddr, sync::Arc}; use askama::Template; use axum::{ @@ -16,12 +16,10 @@ use axum_extra::extract::{ use chrono::Duration; use lettre::Address; use serde::Deserialize; -use strum_macros::Display; use tracing::{error, warn}; use crate::{ app::{AppState, Context, Result}, - config::Config, consts, data::db, email, @@ -66,21 +64,32 @@ pub struct SignUpFormData { password_2: String, } -#[derive(Display)] +#[derive(Debug, thiserror::Error)] enum SignUpError { + #[error("Invalid email")] InvalidEmail, + + #[error("Password not equal")] PasswordsNotEqual, + + #[error("Invalid password")] InvalidPassword, + + #[error("User already exists")] UserAlreadyExists, - DatabaseError, - UnableSendEmail, + + #[error("Database error: {0}")] + DatabaseError(db::DBError), + + #[error("Unable to send email: {0}")] + UnableToSendEmail(email::Error), } #[debug_handler(state = AppState)] pub async fn sign_up_post( Host(host): Host, State(connection): State, - State(config): State, + State(email_service): State>, Extension(context): Extension, Form(form_data): Form, ) -> Result { @@ -89,8 +98,8 @@ pub async fn sign_up_post( form_data: &SignUpFormData, context: Context, ) -> Result { - warn!( - "Unable to sign up with email {}: {}", + error!( + "Error during sign up (email={}): {}", form_data.email, error ); @@ -112,8 +121,9 @@ pub async fn sign_up_post( }, message: match error { SignUpError::UserAlreadyExists => context.tr.t(Sentence::EmailAlreadyTaken), - SignUpError::DatabaseError => context.tr.t(Sentence::DatabaseError), - SignUpError::UnableSendEmail => context.tr.t(Sentence::UnableToSendEmail), + // The error is not shown to the user (it is logged above). + SignUpError::DatabaseError(_) => context.tr.t(Sentence::DatabaseError), + SignUpError::UnableToSendEmail(_) => context.tr.t(Sentence::UnableToSendEmail), _ => "", }, context, @@ -159,31 +169,31 @@ pub async fn sign_up_post( Ok(db::user::SignUpResult::UserCreatedWaitingForValidation(token)) => { let url = utils::get_url_from_host(&host); let email = form_data.email.clone(); - match email::send_email( - &email, - context.tr.t(Sentence::SignUpEmailTitle), - &context.tr.tp( - Sentence::SignUpFollowEmailLink, - &[Box::new(format!( - "{}/validation?{}={}", - url, VALIDATION_TOKEN_KEY, token - ))], - ), - &config.smtp_relay_address, - &config.smtp_login, - &config.smtp_password, - ) - .await + match email_service + .send_email( + &email, + context.tr.t(Sentence::SignUpEmailTitle), + &context.tr.tp( + Sentence::SignUpFollowEmailLink, + &[Box::new(format!( + "{}/validation?{}={}", + url, VALIDATION_TOKEN_KEY, token + ))], + ), + ) + .await { Ok(()) => Ok(Html( MessageTemplate::new(context.tr.t(Sentence::SignUpEmailSent), context) .render()?, ) .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, } +#[derive(Debug, thiserror::Error)] enum AskResetPasswordError { + #[error("Invalid email")] InvalidEmail, + + #[error("Email already reset")] EmailAlreadyReset, + + #[error("Email unknown")] EmailUnknown, - UnableSendEmail, - DatabaseError, + + #[error("Database Error: {0}")] + DatabaseError(db::DBError), + + #[error("Unable to send email: {0}")] + UnableSendEmail(email::Error), } #[debug_handler(state = AppState)] pub async fn ask_reset_password_post( Host(host): Host, State(connection): State, - State(config): State, + State(email_service): State>, Extension(context): Extension, Form(form_data): Form, ) -> Result { @@ -436,6 +456,11 @@ pub async fn ask_reset_password_post( email: &str, context: Context, ) -> Result { + error!( + "Error when asking password reset (email={}): {}", + email, error + ); + Ok(Html( AskResetPasswordTemplate { email, @@ -448,10 +473,12 @@ pub async fn ask_reset_password_post( context.tr.t(Sentence::AskResetEmailAlreadyResetError) } AskResetPasswordError::EmailUnknown => context.tr.t(Sentence::EmailUnknown), - AskResetPasswordError::UnableSendEmail => { + AskResetPasswordError::UnableSendEmail(_) => { context.tr.t(Sentence::UnableToSendResetEmail) } - AskResetPasswordError::DatabaseError => context.tr.t(Sentence::DatabaseError), + AskResetPasswordError::DatabaseError(_) => { + context.tr.t(Sentence::DatabaseError) + } _ => "", }, context, @@ -489,45 +516,37 @@ pub async fn ask_reset_password_post( ), Ok(db::user::GetTokenResetPasswordResult::Ok(token)) => { let url = utils::get_url_from_host(&host); - match email::send_email( - &form_data.email, - context.tr.t(Sentence::AskResetEmailTitle), - &context.tr.tp( - Sentence::AskResetFollowEmailLink, - &[Box::new(format!( - "{}/reset_password?reset_token={}", - url, token - ))], - ), - &config.smtp_relay_address, - &config.smtp_login, - &config.smtp_password, - ) - .await + match email_service + .send_email( + &form_data.email, + context.tr.t(Sentence::AskResetEmailTitle), + &context.tr.tp( + Sentence::AskResetFollowEmailLink, + &[Box::new(format!( + "{}/reset_password?reset_token={}", + url, token + ))], + ), + ) + .await { Ok(()) => Ok(Html( MessageTemplate::new(context.tr.t(Sentence::AskResetEmailSent), context) .render()?, ) .into_response()), - Err(error) => { - error!("Email validation error: {}", error); - error_response( - AskResetPasswordError::UnableSendEmail, - &form_data.email, - context, - ) - } + Err(error) => error_response( + AskResetPasswordError::UnableSendEmail(error), + &form_data.email, + context, + ), } } - Err(error) => { - error!("{}", error); - error_response( - AskResetPasswordError::DatabaseError, - &form_data.email, - context, - ) - } + Err(error) => error_response( + AskResetPasswordError::DatabaseError(error), + &form_data.email, + context, + ), } } @@ -578,12 +597,19 @@ pub struct ResetPasswordForm { reset_token: String, } -#[derive(Display)] +#[derive(Debug, thiserror::Error)] enum ResetPasswordError { + #[error("Password not equal")] PasswordsNotEqual, + + #[error("Invalid password")] InvalidPassword, + + #[error("Token expired")] TokenExpired, - DatabaseError, + + #[error("Database error: {0}")] + DatabaseError(db::DBError), } #[debug_handler] @@ -597,8 +623,8 @@ pub async fn reset_password_post( form_data: &ResetPasswordForm, context: Context, ) -> Result { - warn!( - "Email: {}: {}", + error!( + "Error during password reset (email={}): {}", if let Some(ref user) = context.user { &user.email } else { @@ -624,7 +650,7 @@ pub async fn reset_password_post( ResetPasswordError::TokenExpired => { context.tr.t(Sentence::AskResetTokenExpired) } - ResetPasswordError::DatabaseError => context.tr.t(Sentence::DatabaseError), + ResetPasswordError::DatabaseError(_) => context.tr.t(Sentence::DatabaseError), _ => "", }, context, @@ -659,7 +685,11 @@ pub async fn reset_password_post( Ok(db::user::ResetPasswordResult::ResetTokenExpired) => { 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, } -#[derive(Display)] +#[derive(Debug, thiserror::Error)] enum ProfileUpdateError { + #[error("Invalid email")] InvalidEmail, + + #[error("Email already taken")] EmailAlreadyTaken, + + #[error("Password not equal")] PasswordsNotEqual, + + #[error("Invalid password")] InvalidPassword, - DatabaseError, - UnableSendEmail, + + #[error("Database error: {0}")] + DatabaseError(db::DBError), + + #[error("Unable to send email: {0}")] + UnableToSendEmail(email::Error), } #[debug_handler(state = AppState)] pub async fn edit_user_post( Host(host): Host, State(connection): State, - State(config): State, + State(email_service): State>, Extension(context): Extension, Form(form_data): Form, ) -> Result { @@ -721,8 +762,8 @@ pub async fn edit_user_post( form_data: &EditUserForm, context: Context, ) -> Result { - warn!( - "Email: {}: {}", + error!( + "Error during edit user (email={}): {}", if let Some(ref user) = context.user { &user.email } else { @@ -754,8 +795,10 @@ pub async fn edit_user_post( _ => "", }, message: match error { - ProfileUpdateError::DatabaseError => context.tr.t(Sentence::DatabaseError), - ProfileUpdateError::UnableSendEmail => { + ProfileUpdateError::DatabaseError(_) => { + context.tr.t(Sentence::DatabaseError) + } + ProfileUpdateError::UnableToSendEmail(_) => { context.tr.t(Sentence::UnableToSendEmail) } _ => "", @@ -805,29 +848,26 @@ pub async fn edit_user_post( Ok(db::user::UpdateUserResult::UserUpdatedWaitingForRevalidation(token)) => { let url = utils::get_url_from_host(&host); let email = form_data.email.clone(); - match email::send_email( - &email, - context.tr.t(Sentence::ProfileFollowEmailTitle), - &context.tr.tp( - Sentence::ProfileFollowEmailLink, - &[Box::new(format!( - "{}/revalidation?{}={}", - url, VALIDATION_TOKEN_KEY, token - ))], - ), - &config.smtp_relay_address, - &config.smtp_login, - &config.smtp_password, - ) - .await + match email_service + .send_email( + &email, + context.tr.t(Sentence::ProfileFollowEmailTitle), + &context.tr.tp( + Sentence::ProfileFollowEmailLink, + &[Box::new(format!( + "{}/revalidation?{}={}", + url, VALIDATION_TOKEN_KEY, token + ))], + ), + ) + .await { Ok(()) => { message = context.tr.t(Sentence::ProfileEmailSent); } Err(error) => { - error!("Email validation error: {}", error); return error_response( - ProfileUpdateError::UnableSendEmail, + ProfileUpdateError::UnableToSendEmail(error), &form_data, context, ); @@ -837,8 +877,12 @@ pub async fn edit_user_post( Ok(db::user::UpdateUserResult::Ok) => { message = context.tr.t(Sentence::ProfileSaved); } - Err(_) => { - return error_response(ProfileUpdateError::DatabaseError, &form_data, context); + Err(error) => { + return error_response( + ProfileUpdateError::DatabaseError(error), + &form_data, + context, + ); } } @@ -914,7 +958,7 @@ pub async fn email_revalidation( )) } error @ db::user::ValidationResult::ValidationExpired => { - warn!("Token: {}: {}", token, error); + error!("Token: {}: {}", token, error); Ok(( jar, Html( @@ -927,7 +971,7 @@ pub async fn email_revalidation( )) } error @ db::user::ValidationResult::UnknownUser => { - warn!("Email: {}: {}", token, error); + error!("(email={}): {}", token, error); Ok(( jar, Html( diff --git a/backend/tests/http.rs b/backend/tests/http.rs index 4978b7b..7db1e7c 100644 --- a/backend/tests/http.rs +++ b/backend/tests/http.rs @@ -5,6 +5,7 @@ use cookie::Cookie; use scraper::{ElementRef, Html, Selector}; use recipes::app; +use serde::Serialize; mod utils; @@ -92,3 +93,36 @@ async fn user_edit() -> Result<(), Box> { Ok(()) } + +#[derive(Serialize, Debug)] +pub struct SignUpFormData { + email: String, + password_1: String, + password_2: String, +} + +#[tokio::test] +async fn sign_up() -> Result<(), Box> { + // 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(()) +} diff --git a/backend/tests/utils/mock_email.rs b/backend/tests/utils/mock_email.rs new file mode 100644 index 0000000..0f5e074 --- /dev/null +++ b/backend/tests/utils/mock_email.rs @@ -0,0 +1,23 @@ +use std::sync::Arc; + +use recipes::email; + +pub struct MockEmailService; + +impl MockEmailService { + pub fn create_service() -> Arc { + 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(()) + } +} diff --git a/backend/tests/utils.rs b/backend/tests/utils/mod.rs similarity index 95% rename from backend/tests/utils.rs rename to backend/tests/utils/mod.rs index 9ff5651..213fef4 100644 --- a/backend/tests/utils.rs +++ b/backend/tests/utils/mod.rs @@ -2,6 +2,8 @@ use std::error::Error; use recipes::{app, config, data::db, log}; +mod mock_email; + pub async fn common_state() -> Result> { let db_connection = db::Connection::new_in_memory().await?; let config = config::Config::default(); @@ -10,6 +12,7 @@ pub async fn common_state() -> Result> { config, db_connection, log, + email_service: mock_email::MockEmailService::create_service(), }) }