Skip to content

Commit b397f42

Browse files
committed
test: add is_email_sent() helper
Allow tests to easily inspect which emails have been sent by a TestApp. This uses a rudimentary substring match to find an email with a matching subject and email recipient, but is good enough for current email usage.
1 parent a213489 commit b397f42

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/tests/util/test_app.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,36 @@ impl TestApp {
200200
.clone()
201201
.expect("ChaosProxy is not enabled on this test, call with_database during app init")
202202
}
203+
204+
/// Return true if an email with `to` as a recipient and a (plain-text,
205+
/// printable ascii) `subject` substring is found in the emails sent by this
206+
/// [`TestApp`].
207+
#[track_caller]
208+
pub(crate) fn is_email_sent(&self, to: &str, subject: &str) -> bool {
209+
let to = to.split('@').collect::<Vec<_>>();
210+
let to_user = to[0];
211+
let to_domain = to[1];
212+
213+
let emails = self
214+
.as_inner()
215+
.emails
216+
.mails_in_memory()
217+
.expect("not using in-memory email sink");
218+
219+
// Find all emails to "to" that contain the substring "Subject:
220+
// <subject>".
221+
emails.iter().any(|(envelope, body)| {
222+
let correct_recipient = envelope
223+
.to()
224+
.iter()
225+
.any(|v| v.user() == to_user && v.domain() == to_domain);
226+
227+
// Only works for plain-text, printable ASCII subjects.
228+
let correct_body = body.contains(&format!("Subject: {subject}"));
229+
230+
correct_recipient && correct_body
231+
})
232+
}
203233
}
204234

205235
pub struct TestAppBuilder {

0 commit comments

Comments
 (0)