Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
wsxiaoys committed Feb 11, 2024
1 parent 35e9f0b commit 0c7c077
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 36 deletions.
2 changes: 1 addition & 1 deletion ci/prepare_build_environment.ps1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
choco install --yes protoc
choco install --yes protoc
8 changes: 8 additions & 0 deletions ci/prepare_build_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ if [[ "$OSTYPE" == "linux"* ]]; then
install_protobuf_centos
fi
fi


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

install_mailtutan
2 changes: 1 addition & 1 deletion ee/tabby-webserver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ validator = { version = "0.16.1", features = ["derive"] }
[dev-dependencies]
assert_matches = "1.5.0"
tokio = { workspace = true, features = ["macros"] }
tabby-db = { path = "../../ee/tabby-db", features = ["testutils"] }
tabby-db = { path = "../../ee/tabby-db", features = ["testutils"] }
82 changes: 48 additions & 34 deletions ee/tabby-webserver/src/service/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use lettre::{
client::{Tls, TlsParameters},
AsyncSmtpTransportBuilder,
},
Address, AsyncSmtpTransport, AsyncTransport, Tokio1Executor, Transport,
Address, AsyncSmtpTransport, AsyncTransport, Tokio1Executor,
};
use tabby_db::{DbConn, DbEnum};
use tokio::{sync::RwLock, task::JoinHandle};
Expand Down Expand Up @@ -77,12 +77,15 @@ impl EmailServiceImpl {
auth_method: AuthMethod,
) -> Result<()> {
let mut smtp_server = self.smtp_server.write().await;
*smtp_server = Some(
make_smtp_builder(host, port as u16, encryption)?

let mut builder = make_smtp_builder(host, port as u16, encryption)?;
let mechanism = auth_mechanism(auth_method);
if !mechanism.is_empty() {
builder = builder

Check warning on line 84 in ee/tabby-webserver/src/service/email.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/email.rs#L84

Added line #L84 was not covered by tests
.credentials(Credentials::new(username, password))
.authentication(auth_mechanism(auth_method))
.build(),
);
.authentication(mechanism);

Check warning on line 86 in ee/tabby-webserver/src/service/email.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/email.rs#L86

Added line #L86 was not covered by tests
}
*smtp_server = Some(builder.build());
*self.from.write().await = from_address.into();
Ok(())
}
Expand All @@ -109,22 +112,16 @@ impl EmailServiceImpl {
.body(message)
.map_err(anyhow::Error::msg)?;

let handle = tokio::task::spawn(async move {
let Some(smtp_server) = &*(smtp_server.read().await) else {
// Not enabled.
return;
};
println!("send done {:?}", smtp_server);
println!("send done {:?}", &msg);
if let Some(smtp_server) = &*(smtp_server.read().await) {
// Not enabled.
match smtp_server.send(msg).await.map_err(anyhow::Error::msg) {
Ok(_) => {}
Err(err) => {
warn!("Failed to send mail due to {}", err);
}
};
});

Ok(handle)
}

Check warning on line 123 in ee/tabby-webserver/src/service/email.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/email.rs#L123

Added line #L123 was not covered by tests
Ok(tokio::spawn(async move {}))
}
}

Expand Down Expand Up @@ -230,9 +227,10 @@ fn to_address(email: String) -> Result<Address> {

#[cfg(test)]
mod tests {
use tokio::process::{Child, Command};
use std::time::Duration;

use crate::schema::email;
use serde::Deserialize;
use tokio::process::{Child, Command};

use super::*;

Expand All @@ -247,7 +245,7 @@ mod tests {
smtp_server: "smtp://example.com".into(),
smtp_port: 578,
encryption: Encryption::SslTls,
auth_method: AuthMethod::Plain,
auth_method: AuthMethod::None,
smtp_password: Some("123456".to_owned()),
};
service.update_email_setting(update_input).await.unwrap();
Expand All @@ -259,27 +257,38 @@ mod tests {

fn default_email_setting() -> EmailSetting {
EmailSetting {
smtp_username: "fake_smtp".into(),
smtp_username: "tabby".into(),
smtp_server: "127.0.0.1".into(),
smtp_port: 1025,
from_address: "fake_smtp@localhost".into(),
from_address: "tabby@localhost".into(),
encryption: Encryption::None,
auth_method: AuthMethod::None,
}
}

struct ChildGuard(Child);
impl Drop for ChildGuard {
fn drop(&mut self) {
let _ = self.0.start_kill();
}
async fn start_smtp_server() -> Child {
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;
child
}

async fn start_smtp_server(setting: EmailSetting, smtp_password: String) -> ChildGuard {
let mut cmd = Command::new("mailtutan");
cmd.arg("--smtp-port").arg(setting.smtp_port.to_string());
#[derive(Deserialize)]
struct Mail {
sender: String,
}

ChildGuard(cmd.spawn().expect("Failed to start server"))
async fn read_mails() -> Vec<Mail> {
reqwest::get("http://localhost:1080/api/messages")
.await
.unwrap()
.json()
.await

Check warning on line 290 in ee/tabby-webserver/src/service/email.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/email.rs#L290

Added line #L290 was not covered by tests
.unwrap()
}

/*
Expand All @@ -289,15 +298,15 @@ mod tests {
async fn test_send_email() {
let email_setting = default_email_setting();
let smtp_password = "fake";
// let _ = start_smtp_server(email_setting.clone(), smtp_password.into()).await;
let mut child = start_smtp_server().await;

let db: DbConn = DbConn::new_in_memory().await.unwrap();
db.update_email_setting(
email_setting.smtp_username,
Some(smtp_password.into()),
email_setting.smtp_server,
email_setting.smtp_port,
email_setting.from_address,
email_setting.from_address.clone(),
email_setting.encryption.as_enum_str().into(),
email_setting.auth_method.as_enum_str().into(),
)
Expand All @@ -307,9 +316,14 @@ mod tests {
let service = new_email_service(db).await.unwrap();

let handle = service
.send_invitation_email("[email protected]".into(), "12345".into())
.await.unwrap();
.send_invitation_email("user@localhost".into(), "12345".into())
.await
.unwrap();

handle.await.unwrap();

let mails = read_mails().await;
assert!(mails[0].sender.contains(&email_setting.from_address));
child.kill().await.unwrap();
}
}

0 comments on commit 0c7c077

Please sign in to comment.