1 parent
96a32ea8fb
commit
a643e7e3ab
106 files changed
+2223
-4669
No files matched your search
+1
-21
@@ -1,23 +1,15 @@
|
||||
//! Mock SMTP client for testing.
|
||||
|
||||
use super::{
|
||||
AttachmentData, SendableMessage, SmtpClient, SmtpCredentials, SmtpError, SmtpSendResult,
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
use std::sync::Mutex;
|
||||
|
||||
/// A mock SMTP client that records sent messages.
|
||||
///
|
||||
/// Uses interior mutability (`Mutex`) so it can be shared behind `&self`.
|
||||
/// Build it up with the `.with_*()` builder methods, then pass it wherever
|
||||
/// an `&dyn SmtpClient` is needed.
|
||||
pub struct MockSmtpClient {
|
||||
should_fail: Mutex<Option<SmtpError>>,
|
||||
sent_messages: Mutex<Vec<SendableMessage>>,
|
||||
}
|
||||
|
||||
impl MockSmtpClient {
|
||||
/// Create a new empty mock.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
should_fail: Mutex::new(None),
|
||||
@@ -25,18 +17,15 @@ impl MockSmtpClient {
|
||||
}
|
||||
}
|
||||
|
||||
/// Make every subsequent call return this error.
|
||||
pub fn with_error(self, error: SmtpError) -> Self {
|
||||
*self.should_fail.lock().unwrap() = Some(error);
|
||||
self
|
||||
}
|
||||
|
||||
/// Return the number of messages sent through this mock.
|
||||
pub fn sent_count(&self) -> usize {
|
||||
self.sent_messages.lock().unwrap().len()
|
||||
}
|
||||
|
||||
/// Return a clone of the most recently sent message, if any.
|
||||
pub fn last_sent(&self) -> Option<SendableMessage> {
|
||||
self.sent_messages.lock().unwrap().last().cloned()
|
||||
}
|
||||
@@ -48,8 +37,6 @@ impl Default for MockSmtpClient {
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper to clone an `SmtpError` for the mock (the real errors are not
|
||||
/// `Clone`, so we reconstruct them).
|
||||
fn clone_error(err: &SmtpError) -> SmtpError {
|
||||
match err {
|
||||
SmtpError::ConnectionFailed(msg) => SmtpError::ConnectionFailed(msg.clone()),
|
||||
@@ -86,15 +73,10 @@ impl SmtpClient for MockSmtpClient {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Convenience helper to build dummy credentials for tests.
|
||||
fn test_creds() -> SmtpCredentials {
|
||||
SmtpCredentials {
|
||||
host: "smtp.example.com".to_string(),
|
||||
@@ -106,7 +88,6 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Build a minimal test message.
|
||||
fn test_message() -> SendableMessage {
|
||||
SendableMessage {
|
||||
from: "user@example.com".to_string(),
|
||||
@@ -215,7 +196,6 @@ mod tests {
|
||||
.unwrap_err();
|
||||
assert!(matches!(err, SmtpError::AuthenticationFailed));
|
||||
|
||||
// Ensure no messages were recorded.
|
||||
assert_eq!(mock.sent_count(), 0);
|
||||
}
|
||||
|
||||
@@ -235,7 +215,7 @@ mod tests {
|
||||
let msg = test_message();
|
||||
|
||||
let err = client.send_message(&creds, &msg).await.unwrap_err();
|
||||
// With a fake host the connection should fail.
|
||||
|
||||
assert!(
|
||||
matches!(
|
||||
err,
|
||||
|
||||
Reference in new issue
Block a user