test: improve test coverage
All checks were successful
Publish Docker Images / build-and-publish (push) Successful in 8m4s
All checks were successful
Publish Docker Images / build-and-publish (push) Successful in 8m4s
This commit is contained in:
@@ -235,11 +235,35 @@ impl ContactApi {
|
||||
.and_then(|_| mailer.send(&email_to_recipient))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Internal method for testing - sends emails using a provided transport
|
||||
#[cfg(test)]
|
||||
fn send_emails_with_transport<T: Transport>(
|
||||
&self,
|
||||
request: &ContactRequest,
|
||||
transport: &T,
|
||||
) -> Result<(), ContactError>
|
||||
where
|
||||
T::Error: std::fmt::Debug + std::fmt::Display,
|
||||
{
|
||||
let email_to_sender = self.make_email_sender(request)?;
|
||||
let email_to_recipient = self.make_email_recipient(request)?;
|
||||
|
||||
transport
|
||||
.send(&email_to_sender)
|
||||
.map_err(|e| ContactError::CouldNotSendEmail(format!("{e:?}")))?;
|
||||
transport
|
||||
.send(&email_to_recipient)
|
||||
.map_err(|e| ContactError::CouldNotSendEmail(format!("{e:?}")))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use lettre::transport::stub::StubTransport;
|
||||
|
||||
// Tests for ContactRequest validation
|
||||
#[test]
|
||||
@@ -548,4 +572,431 @@ mod tests {
|
||||
assert!(!json.success);
|
||||
assert!(json.message.eq("backend.contact.errors.validation.message"));
|
||||
}
|
||||
|
||||
// Tests for ContactRequest TryFrom to Mailbox
|
||||
#[test]
|
||||
fn contact_request_to_mailbox_success() {
|
||||
let request = ContactRequest {
|
||||
name: "John Doe".to_string(),
|
||||
email: "john@example.com".to_string(),
|
||||
message: "This is a test message.".to_string(),
|
||||
honeypot: None,
|
||||
};
|
||||
|
||||
let result: Result<lettre::message::Mailbox, ContactError> = (&request).try_into();
|
||||
assert!(result.is_ok());
|
||||
let mailbox = result.unwrap();
|
||||
assert_eq!(mailbox.name, Some("John Doe".to_string()));
|
||||
assert_eq!(mailbox.email.to_string(), "john@example.com");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn contact_request_to_mailbox_invalid_email() {
|
||||
let request = ContactRequest {
|
||||
name: "John Doe".to_string(),
|
||||
email: "not-an-email".to_string(),
|
||||
message: "This is a test message.".to_string(),
|
||||
honeypot: None,
|
||||
};
|
||||
|
||||
let result: Result<lettre::message::Mailbox, ContactError> = (&request).try_into();
|
||||
assert!(result.is_err());
|
||||
match result.unwrap_err() {
|
||||
ContactError::CouldNotParseRequestEmailAddress(email) => {
|
||||
assert_eq!(email, "not-an-email");
|
||||
}
|
||||
_ => panic!("Expected CouldNotParseRequestEmailAddress error"),
|
||||
}
|
||||
}
|
||||
|
||||
// Tests for ContactResponse factory methods
|
||||
#[test]
|
||||
fn contact_response_success_creates_correct_response() {
|
||||
let response = ContactResponse::success();
|
||||
assert!(response.success);
|
||||
assert_eq!(response.message, "backend.contact.success");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn contact_response_honeypot_creates_correct_response() {
|
||||
let response = ContactResponse::honeypot_response();
|
||||
assert!(response.success);
|
||||
assert_eq!(response.message, "backend.contact.honeypot");
|
||||
}
|
||||
|
||||
// Tests for ContactResponse to Json conversion
|
||||
#[test]
|
||||
fn contact_response_to_json() {
|
||||
let response = ContactResponse::success();
|
||||
let json: Json<ContactResponse> = response.into();
|
||||
assert!(json.0.success);
|
||||
assert_eq!(json.0.message, "backend.contact.success");
|
||||
}
|
||||
|
||||
// Tests for email building methods
|
||||
#[test]
|
||||
fn make_email_sender_builds_correct_message() {
|
||||
let settings = EmailSettings {
|
||||
host: "smtp.example.com".to_string(),
|
||||
port: 587,
|
||||
user: "user@example.com".to_string(),
|
||||
from: "noreply@example.com".to_string(),
|
||||
password: "password".to_string(),
|
||||
recipient: "admin@example.com".to_string(),
|
||||
starttls: Starttls::Never,
|
||||
tls: false,
|
||||
};
|
||||
|
||||
let api = ContactApi::from(settings);
|
||||
let request = ContactRequest {
|
||||
name: "John Doe".to_string(),
|
||||
email: "john@example.com".to_string(),
|
||||
message: "Test message content".to_string(),
|
||||
honeypot: None,
|
||||
};
|
||||
|
||||
let result = api.make_email_sender(&request);
|
||||
assert!(result.is_ok());
|
||||
|
||||
let message = result.unwrap();
|
||||
let message_str = format!("{message:?}");
|
||||
|
||||
// Check that the message contains key elements
|
||||
assert!(message_str.contains("john@example.com"));
|
||||
assert!(message_str.contains("John Doe"));
|
||||
assert!(message_str.contains("noreply@example.com"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn make_email_sender_fails_with_invalid_from_address() {
|
||||
let settings = EmailSettings {
|
||||
host: "smtp.example.com".to_string(),
|
||||
port: 587,
|
||||
user: "user@example.com".to_string(),
|
||||
from: "invalid-email".to_string(),
|
||||
password: "password".to_string(),
|
||||
recipient: "admin@example.com".to_string(),
|
||||
starttls: Starttls::Never,
|
||||
tls: false,
|
||||
};
|
||||
|
||||
let api = ContactApi::from(settings);
|
||||
let request = ContactRequest {
|
||||
name: "John Doe".to_string(),
|
||||
email: "john@example.com".to_string(),
|
||||
message: "Test message".to_string(),
|
||||
honeypot: None,
|
||||
};
|
||||
|
||||
let result = api.make_email_sender(&request);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn make_email_sender_fails_with_invalid_request_email() {
|
||||
let settings = EmailSettings {
|
||||
host: "smtp.example.com".to_string(),
|
||||
port: 587,
|
||||
user: "user@example.com".to_string(),
|
||||
from: "noreply@example.com".to_string(),
|
||||
password: "password".to_string(),
|
||||
recipient: "admin@example.com".to_string(),
|
||||
starttls: Starttls::Never,
|
||||
tls: false,
|
||||
};
|
||||
|
||||
let api = ContactApi::from(settings);
|
||||
let request = ContactRequest {
|
||||
name: "John Doe".to_string(),
|
||||
email: "invalid-email".to_string(),
|
||||
message: "Test message".to_string(),
|
||||
honeypot: None,
|
||||
};
|
||||
|
||||
let result = api.make_email_sender(&request);
|
||||
assert!(result.is_err());
|
||||
match result.unwrap_err() {
|
||||
ContactError::CouldNotParseRequestEmailAddress(_) => (),
|
||||
_ => panic!("Expected CouldNotParseRequestEmailAddress error"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn make_email_recipient_builds_correct_message() {
|
||||
let settings = EmailSettings {
|
||||
host: "smtp.example.com".to_string(),
|
||||
port: 587,
|
||||
user: "user@example.com".to_string(),
|
||||
from: "noreply@example.com".to_string(),
|
||||
password: "password".to_string(),
|
||||
recipient: "admin@example.com".to_string(),
|
||||
starttls: Starttls::Never,
|
||||
tls: false,
|
||||
};
|
||||
|
||||
let api = ContactApi::from(settings);
|
||||
let request = ContactRequest {
|
||||
name: "John Doe".to_string(),
|
||||
email: "john@example.com".to_string(),
|
||||
message: "Test message content".to_string(),
|
||||
honeypot: None,
|
||||
};
|
||||
|
||||
let result = api.make_email_recipient(&request);
|
||||
assert!(result.is_ok());
|
||||
|
||||
let message = result.unwrap();
|
||||
let message_str = format!("{message:?}");
|
||||
|
||||
// Check that the message contains key elements
|
||||
assert!(message_str.contains("admin@example.com"));
|
||||
assert!(message_str.contains("john@example.com")); // Reply-to
|
||||
assert!(message_str.contains("noreply@example.com"));
|
||||
assert!(message_str.contains("Contact Form: John Doe"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn make_email_recipient_fails_with_invalid_recipient() {
|
||||
let settings = EmailSettings {
|
||||
host: "smtp.example.com".to_string(),
|
||||
port: 587,
|
||||
user: "user@example.com".to_string(),
|
||||
from: "noreply@example.com".to_string(),
|
||||
password: "password".to_string(),
|
||||
recipient: "invalid-email".to_string(),
|
||||
starttls: Starttls::Never,
|
||||
tls: false,
|
||||
};
|
||||
|
||||
let api = ContactApi::from(settings);
|
||||
let request = ContactRequest {
|
||||
name: "John Doe".to_string(),
|
||||
email: "john@example.com".to_string(),
|
||||
message: "Test message".to_string(),
|
||||
honeypot: None,
|
||||
};
|
||||
|
||||
let result = api.make_email_recipient(&request);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn make_email_recipient_fails_with_invalid_from_address() {
|
||||
let settings = EmailSettings {
|
||||
host: "smtp.example.com".to_string(),
|
||||
port: 587,
|
||||
user: "user@example.com".to_string(),
|
||||
from: "invalid-email".to_string(),
|
||||
password: "password".to_string(),
|
||||
recipient: "admin@example.com".to_string(),
|
||||
starttls: Starttls::Never,
|
||||
tls: false,
|
||||
};
|
||||
|
||||
let api = ContactApi::from(settings);
|
||||
let request = ContactRequest {
|
||||
name: "John Doe".to_string(),
|
||||
email: "john@example.com".to_string(),
|
||||
message: "Test message".to_string(),
|
||||
honeypot: None,
|
||||
};
|
||||
|
||||
let result = api.make_email_recipient(&request);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn make_email_recipient_includes_message_content() {
|
||||
let settings = EmailSettings {
|
||||
host: "smtp.example.com".to_string(),
|
||||
port: 587,
|
||||
user: "user@example.com".to_string(),
|
||||
from: "noreply@example.com".to_string(),
|
||||
password: "password".to_string(),
|
||||
recipient: "admin@example.com".to_string(),
|
||||
starttls: Starttls::Never,
|
||||
tls: false,
|
||||
};
|
||||
|
||||
let api = ContactApi::from(settings);
|
||||
let request = ContactRequest {
|
||||
name: "Jane Smith".to_string(),
|
||||
email: "jane@example.com".to_string(),
|
||||
message: "This is a unique test message with specific content".to_string(),
|
||||
honeypot: None,
|
||||
};
|
||||
|
||||
let result = api.make_email_recipient(&request);
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn contact_api_from_email_settings() {
|
||||
let settings = EmailSettings {
|
||||
host: "smtp.example.com".to_string(),
|
||||
port: 587,
|
||||
user: "user@example.com".to_string(),
|
||||
from: "noreply@example.com".to_string(),
|
||||
password: "password".to_string(),
|
||||
recipient: "admin@example.com".to_string(),
|
||||
starttls: Starttls::Always,
|
||||
tls: false,
|
||||
};
|
||||
|
||||
let api = ContactApi::from(settings.clone());
|
||||
assert_eq!(api.settings.host, settings.host);
|
||||
assert_eq!(api.settings.port, settings.port);
|
||||
assert_eq!(api.settings.from, settings.from);
|
||||
}
|
||||
|
||||
// Tests for send_emails with mock transport
|
||||
#[test]
|
||||
fn send_emails_with_stub_transport_success() {
|
||||
let settings = EmailSettings {
|
||||
host: "smtp.example.com".to_string(),
|
||||
port: 587,
|
||||
user: "user@example.com".to_string(),
|
||||
from: "noreply@example.com".to_string(),
|
||||
password: "password".to_string(),
|
||||
recipient: "admin@example.com".to_string(),
|
||||
starttls: Starttls::Never,
|
||||
tls: false,
|
||||
};
|
||||
|
||||
let api = ContactApi::from(settings);
|
||||
let request = ContactRequest {
|
||||
name: "John Doe".to_string(),
|
||||
email: "john@example.com".to_string(),
|
||||
message: "Test message content".to_string(),
|
||||
honeypot: None,
|
||||
};
|
||||
|
||||
let transport = StubTransport::new_ok();
|
||||
let result = api.send_emails_with_transport(&request, &transport);
|
||||
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn send_emails_with_stub_transport_sends_two_emails() {
|
||||
let settings = EmailSettings {
|
||||
host: "smtp.example.com".to_string(),
|
||||
port: 587,
|
||||
user: "user@example.com".to_string(),
|
||||
from: "noreply@example.com".to_string(),
|
||||
password: "password".to_string(),
|
||||
recipient: "admin@example.com".to_string(),
|
||||
starttls: Starttls::Never,
|
||||
tls: false,
|
||||
};
|
||||
|
||||
let api = ContactApi::from(settings);
|
||||
let request = ContactRequest {
|
||||
name: "Jane Smith".to_string(),
|
||||
email: "jane@example.com".to_string(),
|
||||
message: "Another test message".to_string(),
|
||||
honeypot: None,
|
||||
};
|
||||
|
||||
let transport = StubTransport::new_ok();
|
||||
api.send_emails_with_transport(&request, &transport)
|
||||
.unwrap();
|
||||
|
||||
// StubTransport doesn't provide a way to count messages, but we verified it succeeded
|
||||
// If either email failed to build or send, the test would fail
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn send_emails_with_stub_transport_fails_with_invalid_from() {
|
||||
let settings = EmailSettings {
|
||||
host: "smtp.example.com".to_string(),
|
||||
port: 587,
|
||||
user: "user@example.com".to_string(),
|
||||
from: "invalid-email".to_string(),
|
||||
password: "password".to_string(),
|
||||
recipient: "admin@example.com".to_string(),
|
||||
starttls: Starttls::Never,
|
||||
tls: false,
|
||||
};
|
||||
|
||||
let api = ContactApi::from(settings);
|
||||
let request = ContactRequest {
|
||||
name: "John Doe".to_string(),
|
||||
email: "john@example.com".to_string(),
|
||||
message: "Test message".to_string(),
|
||||
honeypot: None,
|
||||
};
|
||||
|
||||
let transport = StubTransport::new_ok();
|
||||
let result = api.send_emails_with_transport(&request, &transport);
|
||||
|
||||
assert!(result.is_err());
|
||||
match result.unwrap_err() {
|
||||
ContactError::CouldNotParseSettingsEmail(_) => (),
|
||||
e => panic!("Expected CouldNotParseSettingsEmail, got {:?}", e),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn send_emails_with_stub_transport_fails_with_invalid_request_email() {
|
||||
let settings = EmailSettings {
|
||||
host: "smtp.example.com".to_string(),
|
||||
port: 587,
|
||||
user: "user@example.com".to_string(),
|
||||
from: "noreply@example.com".to_string(),
|
||||
password: "password".to_string(),
|
||||
recipient: "admin@example.com".to_string(),
|
||||
starttls: Starttls::Never,
|
||||
tls: false,
|
||||
};
|
||||
|
||||
let api = ContactApi::from(settings);
|
||||
let request = ContactRequest {
|
||||
name: "John Doe".to_string(),
|
||||
email: "not-an-email".to_string(),
|
||||
message: "Test message".to_string(),
|
||||
honeypot: None,
|
||||
};
|
||||
|
||||
let transport = StubTransport::new_ok();
|
||||
let result = api.send_emails_with_transport(&request, &transport);
|
||||
|
||||
assert!(result.is_err());
|
||||
match result.unwrap_err() {
|
||||
ContactError::CouldNotParseRequestEmailAddress(_) => (),
|
||||
e => panic!("Expected CouldNotParseRequestEmailAddress, got {:?}", e),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn send_emails_with_failing_transport() {
|
||||
let settings = EmailSettings {
|
||||
host: "smtp.example.com".to_string(),
|
||||
port: 587,
|
||||
user: "user@example.com".to_string(),
|
||||
from: "noreply@example.com".to_string(),
|
||||
password: "password".to_string(),
|
||||
recipient: "admin@example.com".to_string(),
|
||||
starttls: Starttls::Never,
|
||||
tls: false,
|
||||
};
|
||||
|
||||
let api = ContactApi::from(settings);
|
||||
let request = ContactRequest {
|
||||
name: "John Doe".to_string(),
|
||||
email: "john@example.com".to_string(),
|
||||
message: "Test message".to_string(),
|
||||
honeypot: None,
|
||||
};
|
||||
|
||||
// Create a transport that always fails
|
||||
let transport = StubTransport::new_error();
|
||||
let result = api.send_emails_with_transport(&request, &transport);
|
||||
|
||||
assert!(result.is_err());
|
||||
match result.unwrap_err() {
|
||||
ContactError::CouldNotSendEmail(_) => (),
|
||||
e => panic!("Expected CouldNotSendEmail, got {:?}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user