Files
mailks/tests/email_tests.rs
T
zhenyi 5fa7a82548 chore(project): initialize project with core configuration and dependencies
- Add .gitignore and .env.example files for project setup
- Create build script for proto compilation with tonic-prost
- Generate Cargo.lock with all project dependencies
- Configure project structure and ignore patterns for development environment
2026-06-07 22:46:30 +08:00

103 lines
2.8 KiB
Rust

use emailks::{
email_build::build_message_from_parts,
error::EmailError,
pb::email::v1::{EmailAddress, SendEmailRequest},
};
use std::time::Duration;
fn test_config() -> emailks::config::SmtpConfig {
emailks::config::SmtpConfig {
host: "localhost".into(),
port: 1025,
username: None,
password: None,
from_email: Some("sender@test.com".into()),
from_name: Some("Sender".into()),
reply_to: None,
tls: emailks::config::SmtpTls::None,
timeout: Duration::from_secs(5),
helo_name: None,
allow_request_from: false,
}
}
fn req_with_to(to: &str) -> SendEmailRequest {
SendEmailRequest {
from: None,
to: vec![EmailAddress {
email: to.into(),
name: String::new(),
}],
cc: vec![],
bcc: vec![],
subject: "Hello".into(),
text_body: "World".into(),
html_body: String::new(),
attachments: vec![],
priority: 0,
headers: Default::default(),
reply_to: String::new(),
}
}
#[test]
fn sender_from_request() {
let mut cfg = test_config();
cfg.allow_request_from = true;
let mut req = req_with_to("to@test.com");
req.from = Some(EmailAddress {
email: "req@test.com".into(),
name: "Request".into(),
});
let msg = build_message_from_parts(&cfg, &req).unwrap();
let body = String::from_utf8(msg.formatted()).unwrap();
assert!(body.contains("From: Request <req@test.com>"));
assert!(body.contains("To: to@test.com"));
assert!(body.contains("Subject: Hello"));
}
#[test]
fn sender_from_config() {
let mut req = req_with_to("to@test.com");
req.text_body = String::new();
req.html_body = "<p>Hi</p>".into();
let msg = build_message_from_parts(&test_config(), &req).unwrap();
let body = String::from_utf8(msg.formatted()).unwrap();
assert!(body.contains("From: Sender <sender@test.com>"));
assert!(body.contains("Content-Type: text/html"));
}
#[test]
fn missing_sender_error() {
let mut cfg = test_config();
cfg.from_email = None;
let req = req_with_to("to@test.com");
let err = build_message_from_parts(&cfg, &req).unwrap_err();
assert!(matches!(err, EmailError::MissingSender));
}
#[test]
fn missing_recipients_error() {
let req = SendEmailRequest {
from: Some(EmailAddress {
email: "x@x.com".into(),
name: String::new(),
}),
to: vec![],
cc: vec![],
bcc: vec![],
subject: "s".into(),
text_body: "b".into(),
html_body: String::new(),
attachments: vec![],
priority: 0,
headers: Default::default(),
reply_to: String::new(),
};
let err = build_message_from_parts(&test_config(), &req).unwrap_err();
assert!(matches!(err, EmailError::MissingRecipients));
}