Skip to content

Commit

Permalink
test: switch mail test server to mail pit (TabbyML#2084)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsxiaoys authored May 9, 2024
1 parent f6efbb7 commit 76df681
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
6 changes: 1 addition & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
23 changes: 11 additions & 12 deletions ci/prepare_build_environment.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#!/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
unzip protoc-3.15.8-linux-x86_64.zip -d /usr
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
Expand All @@ -23,12 +28,6 @@ if [[ "$OSTYPE" == "linux"* ]]; then

install_protobuf_centos
fi
fi


install_mailtutan() {
# For local smtp test.
cargo install mailtutan
}

install_mailtutan
install_mailpit
fi
5 changes: 1 addition & 4 deletions ee/tabby-webserver/src/service/email/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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]
Expand Down
40 changes: 31 additions & 9 deletions ee/tabby-webserver/src/service/email/testutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
pub address: String,
}

#[derive(Deserialize, Debug)]
struct MessageList {
messages: Vec<Message>,
}

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")
pub async fn list_mail(&self) -> Vec<Message> {
let mails = reqwest::get("http://localhost:8025/api/v1/messages")
.await
.unwrap();

mails.json().await.unwrap()
let data = mails.json::<MessageList>().await.unwrap();
data.messages
}

pub async fn create_test_email_service(&self, db_conn: DbConn) -> impl EmailService {
Expand All @@ -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 }
}
}
Expand Down

0 comments on commit 76df681

Please sign in to comment.