From 76df68153779c404d27c24da70ccb20b65fea95c Mon Sep 17 00:00:00 2001 From: Meng Zhang Date: Thu, 9 May 2024 15:36:24 -0700 Subject: [PATCH] test: switch mail test server to mail pit (#2084) --- CONTRIBUTING.md | 6 +-- ci/prepare_build_environment.sh | 23 +++++------ ee/tabby-webserver/src/service/email/mod.rs | 5 +-- .../src/service/email/testutils.rs | 40 ++++++++++++++----- 4 files changed, 44 insertions(+), 30 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 06c360c2d5ff..0c275e4b79db 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -29,11 +29,7 @@ apt-get install protobuf-compiler libopenblas-dev choco install protoc ``` -Some of the tests require mailtutan SMTP server which you can install with: - -```bash -cargo install mailtutan -``` +Some of the tests require mailpit SMTP server which you can install following this [instruction](https://github.com/axllent/mailpit?tab=readme-ov-file#installation) Before proceeding, ensure that all tests are passing locally: diff --git a/ci/prepare_build_environment.sh b/ci/prepare_build_environment.sh index 08d4700e8bc4..06832e62ea64 100755 --- a/ci/prepare_build_environment.sh +++ b/ci/prepare_build_environment.sh @@ -1,9 +1,5 @@ #!/bin/bash -if [[ "$OSTYPE" == "darwin"* ]]; then - brew install protobuf -fi - install_protobuf_centos() { PB_REL="https://github.com/protocolbuffers/protobuf/releases" curl -LO $PB_REL/download/v3.15.8/protoc-3.15.8-linux-x86_64.zip @@ -11,6 +7,15 @@ install_protobuf_centos() { rm protoc-3.15.8-linux-x86_64.zip } +install_mailpit() { + sudo bash < <(curl -sL https://raw.githubusercontent.com/axllent/mailpit/develop/install.sh) +} + +if [[ "$OSTYPE" == "darwin"* ]]; then + brew install protobuf + install_mailpit +fi + if [[ "$OSTYPE" == "linux"* ]]; then if command -v apt-get ; then sudo apt-get -y install protobuf-compiler libopenblas-dev sqlite3 graphviz @@ -23,12 +28,6 @@ if [[ "$OSTYPE" == "linux"* ]]; then install_protobuf_centos fi -fi - - -install_mailtutan() { - # For local smtp test. - cargo install mailtutan -} -install_mailtutan \ No newline at end of file + install_mailpit +fi \ No newline at end of file diff --git a/ee/tabby-webserver/src/service/email/mod.rs b/ee/tabby-webserver/src/service/email/mod.rs index 129d6cfa99e7..27a9461cd238 100644 --- a/ee/tabby-webserver/src/service/email/mod.rs +++ b/ee/tabby-webserver/src/service/email/mod.rs @@ -290,9 +290,6 @@ mod tests { service.delete_setting().await.unwrap(); } - /* - * Requires https://github.com/mailtutan/mailtutan - */ #[tokio::test] #[serial] async fn test_send_email() { @@ -314,7 +311,7 @@ mod tests { let mails = mail_server.list_mail().await; let default_from = service.read_setting().await.unwrap().unwrap().from_address; - assert!(mails[0].sender.contains(&default_from)); + assert!(mails[0].from.address.contains(&default_from)); } #[tokio::test] diff --git a/ee/tabby-webserver/src/service/email/testutils.rs b/ee/tabby-webserver/src/service/email/testutils.rs index 265a4904486d..fb0c11dadbaa 100644 --- a/ee/tabby-webserver/src/service/email/testutils.rs +++ b/ee/tabby-webserver/src/service/email/testutils.rs @@ -8,23 +8,37 @@ use tokio::process::{Child, Command}; use super::new_email_service; #[derive(Deserialize, Debug)] -pub struct Mail { - pub sender: String, +#[serde(rename_all = "PascalCase")] +pub struct Message { + pub from: MailAddress, pub subject: String, } +#[derive(Deserialize, Debug)] +#[serde(rename_all = "PascalCase")] +pub struct MailAddress { + pub name: Option, + pub address: String, +} + +#[derive(Deserialize, Debug)] +struct MessageList { + messages: Vec, +} + pub struct TestEmailServer { #[allow(unused)] child: Child, } impl TestEmailServer { - pub async fn list_mail(&self) -> Vec { - let mails = reqwest::get("http://localhost:1080/api/messages") + pub async fn list_mail(&self) -> Vec { + let mails = reqwest::get("http://localhost:8025/api/v1/messages") .await .unwrap(); - mails.json().await.unwrap() + let data = mails.json::().await.unwrap(); + data.messages } pub async fn create_test_email_service(&self, db_conn: DbConn) -> impl EmailService { @@ -38,13 +52,21 @@ impl TestEmailServer { pub async fn start() -> TestEmailServer { tokio::time::sleep(Duration::from_millis(500)).await; - let mut cmd = Command::new("mailtutan"); - cmd.kill_on_drop(true); + let mut cmd = Command::new("mailpit"); + cmd.kill_on_drop(true) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()); let child = cmd .spawn() - .expect("You need to run `cargo install mailtutan` before running this test"); - tokio::time::sleep(Duration::from_millis(500)).await; + .expect("You need to install `mailpit` before running this test"); + loop { + if reqwest::get("http://localhost:8025").await.is_ok() { + break; + } + + tokio::time::sleep(Duration::from_millis(1000)).await; + } TestEmailServer { child } } }