-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(webserver): Refactor test SMTP server utils (#1471)
* refactor(webserver): Refactor test SMTP server utils * Apply suggested changes * Move module to testutils * Rename method * Separate start and create_test_email_service * Fix test
- Loading branch information
Showing
5 changed files
with
101 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ use async_trait::async_trait; | |
use chrono::{Duration, Utc}; | ||
use juniper::ID; | ||
use tabby_db::{DbConn, InvitationDAO}; | ||
use tokio::task::JoinHandle; | ||
use tracing::warn; | ||
use validator::{Validate, ValidationError}; | ||
|
||
|
@@ -219,11 +220,11 @@ impl AuthenticationService for AuthenticationServiceImpl { | |
Ok(resp) | ||
} | ||
|
||
async fn request_password_reset_email(&self, email: String) -> Result<()> { | ||
async fn request_password_reset_email(&self, email: String) -> Result<Option<JoinHandle<()>>> { | ||
let user = self.get_user_by_email(&email).await.ok(); | ||
|
||
let Some(user @ User { active: true, .. }) = user else { | ||
return Ok(()); | ||
return Ok(None); | ||
}; | ||
|
||
let id = user.id.as_rowid()?; | ||
|
@@ -236,10 +237,11 @@ impl AuthenticationService for AuthenticationServiceImpl { | |
} | ||
} | ||
let code = self.db.create_password_reset(id as i64).await?; | ||
self.mail | ||
let handle = self | ||
.send_password_reset_email(user.email, code.clone()) | ||
.await?; | ||
Ok(()) | ||
Ok(Some(handle)) | ||
} | ||
|
||
async fn password_reset(&self, code: &str, password: &str) -> Result<(), PasswordResetError> { | ||
|
@@ -594,15 +596,23 @@ mod tests { | |
} | ||
} | ||
|
||
async fn test_authentication_service_with_mail() -> (AuthenticationServiceImpl, TestEmailServer) | ||
{ | ||
let db = DbConn::new_in_memory().await.unwrap(); | ||
let smtp = TestEmailServer::start().await; | ||
let service = AuthenticationServiceImpl { | ||
db: db.clone(), | ||
mail: Arc::new(smtp.create_test_email_service(db).await), | ||
}; | ||
(service, smtp) | ||
} | ||
|
||
use assert_matches::assert_matches; | ||
use juniper_axum::relay::{self, Connection}; | ||
use serial_test::serial; | ||
|
||
use super::*; | ||
use crate::service::email::{ | ||
new_email_service, | ||
test_utils::{default_email_settings, start_smtp_server}, | ||
}; | ||
use crate::service::email::{new_email_service, testutils::TestEmailServer}; | ||
|
||
#[test] | ||
fn test_password_hash() { | ||
|
@@ -886,13 +896,7 @@ mod tests { | |
#[tokio::test] | ||
#[serial] | ||
async fn test_password_reset() { | ||
let service = test_authentication_service().await; | ||
service | ||
.update_email_setting(default_email_settings()) | ||
.await | ||
.unwrap(); | ||
let _smtp = start_smtp_server().await; | ||
let (service, smtp) = test_authentication_service_with_mail().await; | ||
|
||
// Test first reset, ensure wrong code fails | ||
service | ||
|
@@ -902,10 +906,16 @@ mod tests { | |
.unwrap(); | ||
let user = service.get_user_by_email("[email protected]").await.unwrap(); | ||
|
||
service | ||
let handle = service | ||
.request_password_reset_email("[email protected]".into()) | ||
.await | ||
.unwrap(); | ||
handle.unwrap().await.unwrap(); | ||
assert!(smtp.list_mail().await[0] | ||
.subject | ||
.to_lowercase() | ||
.contains("password")); | ||
|
||
let reset = service | ||
.db | ||
.get_password_reset_by_user_id(user.id.as_rowid().unwrap() as i64) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use std::time::Duration; | ||
|
||
use serde::Deserialize; | ||
use tabby_db::DbConn; | ||
use tokio::process::{Child, Command}; | ||
|
||
use super::new_email_service; | ||
use crate::schema::email::{AuthMethod, EmailService, EmailSettingInput, Encryption}; | ||
|
||
#[derive(Deserialize, Debug)] | ||
pub struct Mail { | ||
pub sender: String, | ||
pub subject: String, | ||
} | ||
|
||
pub struct TestEmailServer { | ||
#[allow(unused)] | ||
child: Child, | ||
} | ||
|
||
impl TestEmailServer { | ||
pub async fn list_mail(&self) -> Vec<Mail> { | ||
let mails = reqwest::get("http://localhost:1080/api/messages") | ||
.await | ||
.unwrap(); | ||
|
||
mails.json().await.unwrap() | ||
} | ||
|
||
pub async fn create_test_email_service(&self, db_conn: DbConn) -> impl EmailService { | ||
let service = new_email_service(db_conn).await.unwrap(); | ||
service | ||
.update_email_setting(default_email_settings()) | ||
.await | ||
.unwrap(); | ||
service | ||
} | ||
|
||
pub async fn start() -> TestEmailServer { | ||
let mut cmd = Command::new("mailtutan"); | ||
cmd.kill_on_drop(true); | ||
|
||
let child = cmd | ||
.spawn() | ||
.expect("You need to run `cargo install mailtutan` before running this test"); | ||
tokio::time::sleep(Duration::from_secs(1)).await; | ||
TestEmailServer { child } | ||
} | ||
} | ||
|
||
fn default_email_settings() -> EmailSettingInput { | ||
EmailSettingInput { | ||
smtp_username: "tabby".into(), | ||
smtp_server: "127.0.0.1".into(), | ||
smtp_port: 1025, | ||
from_address: "tabby@localhost".into(), | ||
encryption: Encryption::None, | ||
auth_method: AuthMethod::None, | ||
smtp_password: Some("fake".into()), | ||
} | ||
} |