forgot password: handling testing environment, no emails when testing
This commit is contained in:
parent
feb9f16bc9
commit
a32d6b4807
5 changed files with 55 additions and 30 deletions
19
crates/ezidam/src/email.rs
Normal file
19
crates/ezidam/src/email.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
use email::Config;
|
||||
use rocket::figment::Figment;
|
||||
|
||||
pub fn init(config: &Figment) -> Result<Option<Config>, String> {
|
||||
match config.extract_inner::<Config>("email") {
|
||||
Ok(email_config) => match email_config.status() {
|
||||
Ok(status) => {
|
||||
println!("{status}");
|
||||
println!("Emails will be sent from \"{}\"", email_config.from);
|
||||
Ok(Some(email_config))
|
||||
}
|
||||
Err(e) => Err(format!("Invalid email configuration: {e}")),
|
||||
},
|
||||
Err(e) => {
|
||||
println!("Sending emails has been disabled: {e}");
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ use rocket::{Build, Rocket};
|
|||
mod cache;
|
||||
mod cors;
|
||||
mod database;
|
||||
mod email;
|
||||
mod error;
|
||||
mod file_from_bytes;
|
||||
mod guards;
|
||||
|
|
@ -19,6 +20,8 @@ mod tokens;
|
|||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
pub use crate::email::init as email_init;
|
||||
|
||||
pub fn rocket_setup(rocket_builder: Rocket<Build>) -> Rocket<Build> {
|
||||
use cache::CacheControl;
|
||||
use database::Database;
|
||||
|
|
|
|||
|
|
@ -12,17 +12,7 @@ async fn main() -> Result<(), String> {
|
|||
let config = rocket::Config::figment().merge(("ip_header", "x-forwarded-for"));
|
||||
|
||||
// Get email config
|
||||
let email_config = match config.extract_inner::<email::Config>("email") {
|
||||
Ok(email_config) => match email_config.status() {
|
||||
Ok(status) => {
|
||||
println!("{status}");
|
||||
email_config
|
||||
}
|
||||
Err(e) => return Err(format!("Invalid email configuration: {e}")),
|
||||
},
|
||||
Err(e) => return Err(format!("No email configuration was found: {e}")),
|
||||
};
|
||||
println!("Emails will be sent from \"{}\"", email_config.from);
|
||||
let email_config = ezidam::email_init(&config)?;
|
||||
|
||||
// Rocket with custom config
|
||||
let rocket_builder = rocket::custom(config);
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ const SUCCESS_MESSAGE: &str = "An email is on the way with instructions to reset
|
|||
pub async fn forgot_password_email_form(
|
||||
mut db: Connection<Database>,
|
||||
form: Form<ForgotPasswordEmailForm<'_>>,
|
||||
email_config: &State<email::Config>,
|
||||
email_config: &State<Option<email::Config>>,
|
||||
) -> Result<Flash<Redirect>> {
|
||||
if form.email.is_empty() {
|
||||
return Ok(Flash::new(
|
||||
|
|
@ -176,7 +176,8 @@ pub async fn forgot_password_email_form(
|
|||
};
|
||||
|
||||
// Send email
|
||||
let (flash_kind, flash_message) =
|
||||
let (flash_kind, flash_message) = match email_config.inner() {
|
||||
Some(email_config) => {
|
||||
match email::send_email(email_config, &user_for_email, email_title, html).await {
|
||||
Ok(okay) => {
|
||||
if okay.is_positive() {
|
||||
|
|
@ -189,6 +190,12 @@ pub async fn forgot_password_email_form(
|
|||
}
|
||||
}
|
||||
Err(e) => (FlashKind::Danger, e.to_string()),
|
||||
}
|
||||
}
|
||||
None => (
|
||||
FlashKind::Warning,
|
||||
"Email sending is disabled. Contact your administrator to reset your password.".into(),
|
||||
),
|
||||
};
|
||||
|
||||
Ok(Flash::new(
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
use crate::rocket_setup;
|
||||
use rocket::local::blocking::{Client, LocalResponse};
|
||||
use rocket::{Build, Rocket};
|
||||
|
||||
|
|
@ -6,21 +5,28 @@ pub use rocket::http::{ContentType, Status};
|
|||
pub use rocket::uri;
|
||||
|
||||
fn rocket_with_memory_database() -> Rocket<Build> {
|
||||
use crate::{email_init, rocket_setup};
|
||||
use rocket::figment::util::map;
|
||||
use rocket::figment::Figment;
|
||||
use rocket::Config;
|
||||
|
||||
// Custom config
|
||||
// - rocket defaults
|
||||
// - from `Rocket.toml`
|
||||
// - from env variables
|
||||
// - from code below
|
||||
let options = map!["url" => ":memory:"];
|
||||
let config = Config::figment().merge(("databases", map!["ezidam" => &options]));
|
||||
let database_options = map!["url" => ":memory:"];
|
||||
let config =
|
||||
Figment::from(Config::default()).merge(("databases", map!["ezidam" => &database_options]));
|
||||
|
||||
// Get email config
|
||||
let email_config = email_init(&config).unwrap_or(None);
|
||||
|
||||
let rocket_builder = rocket::custom(config);
|
||||
|
||||
// Setup server
|
||||
rocket_setup(rocket_builder)
|
||||
let rocket_builder = rocket_setup(rocket_builder);
|
||||
|
||||
// Attach email config
|
||||
rocket_builder.manage(email_config)
|
||||
}
|
||||
|
||||
pub fn setup_rocket_testing() -> Client {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue