sunday_slate/mail/
render.rs1use askama::Template;
2
3use crate::mail::{MailError, OutgoingEmail};
4
5#[derive(Template)]
6#[template(path = "emails/password_reset.html")]
7struct PasswordResetHtml<'a> {
8 reset_link: &'a str,
9}
10
11#[derive(Template)]
12#[template(path = "emails/password_reset.txt")]
13struct PasswordResetText<'a> {
14 reset_link: &'a str,
15}
16
17pub fn password_reset_email(
19 from: &str,
20 to: &str,
21 subject: &str,
22 reset_link: &str,
23) -> Result<OutgoingEmail, MailError> {
24 let html = PasswordResetHtml { reset_link }.render()?;
25 let text = PasswordResetText { reset_link }.render()?;
26
27 Ok(OutgoingEmail {
28 from: from.to_string(),
29 to: to.to_string(),
30 subject: subject.to_string(),
31 html,
32 text,
33 })
34}
35
36#[derive(Template)]
37#[template(path = "emails/invite.html")]
38struct InviteHtml<'a> {
39 accept_link: &'a str,
40 league_name: &'a str,
41}
42
43#[derive(Template)]
44#[template(path = "emails/invite.txt")]
45struct InviteText<'a> {
46 accept_link: &'a str,
47 league_name: &'a str,
48}
49
50pub fn invite_email(
52 from: &str,
53 to: &str,
54 subject: &str,
55 accept_link: &str,
56 league_name: &str,
57) -> Result<OutgoingEmail, MailError> {
58 let html = InviteHtml {
59 accept_link,
60 league_name,
61 }
62 .render()?;
63 let text = InviteText {
64 accept_link,
65 league_name,
66 }
67 .render()?;
68
69 Ok(OutgoingEmail {
70 from: from.to_string(),
71 to: to.to_string(),
72 subject: subject.to_string(),
73 html,
74 text,
75 })
76}
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81
82 #[test]
83 fn password_reset_email_renders_both_parts() {
84 let link = "http://localhost:3000/reset-password?token=42.abc";
85 let email = password_reset_email("from@x.com", "to@x.com", "Reset", link).expect("renders");
86 assert!(
87 email.html.contains(link),
88 "html missing link: {}",
89 email.html
90 );
91 assert!(
92 email.text.contains(link),
93 "text missing link: {}",
94 email.text
95 );
96 assert!(
97 email.html.contains("Reset your password"),
98 "html missing heading: {}",
99 email.html
100 );
101 }
102
103 #[test]
104 fn invite_email_states_the_fourteen_day_window() {
105 let email = invite_email(
106 "from@x.com",
107 "to@x.com",
108 "Invite",
109 "http://localhost:3000/invite?token=7.abc",
110 "Sunday Funday",
111 )
112 .expect("renders");
113 assert!(
114 email.html.contains("expires in 14 days"),
115 "html missing expiry window: {}",
116 email.html
117 );
118 }
119
120 #[test]
121 fn invite_email_renders_both_parts() {
122 let link = "http://localhost:3000/invite?token=7.abc";
123 let email = invite_email("from@x.com", "to@x.com", "Invite", link, "Sunday Funday")
124 .expect("renders");
125 assert!(
126 email.html.contains(link),
127 "html missing link: {}",
128 email.html
129 );
130 assert!(
131 email.text.contains(link),
132 "text missing link: {}",
133 email.text
134 );
135 assert!(
136 email.html.contains("Sunday Funday"),
137 "html missing league: {}",
138 email.html
139 );
140 }
141}