oxedyne/fe2o3/fe2o3_steel/tests/alert_mail.rs
2.4 KiB, 1 run
created by r3906835153:77319, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! The alert mail carries the headers a receiving provider requires of RFC 5322 mail. |
| 2 | |
| 3 | use oxedyne_fe2o3_steel::srv::alert::{ |
| 4 | alert_mail, |
| 5 | AlertEvent, |
| 6 | }; |
| 7 | |
| 8 | use oxedyne_fe2o3_core::prelude::*; |
| 9 | use oxedyne_fe2o3_net::email::header::{ |
| 10 | header_fields, |
| 11 | message_id, |
| 12 | split_headers_body, |
| 13 | }; |
| 14 | |
| 15 | /// Without a `Date` and a `Message-ID` Gmail refused every alert as "likely unsolicited mail". |
| 16 | #[test] |
| 17 | fn an_alert_mail_carries_a_date_and_a_message_id() -> Outcome<()> { |
| 18 | let to = vec![fmt!("op@gmail.com")]; |
| 19 | let msg = alert_mail("Steel <steel@oxegen.io>", &to, "karri", &AlertEvent::SealedStart { db_count: 1 }); |
| 20 | let (head, _) = split_headers_body(msg.as_bytes()); |
| 21 | let fields = res!(header_fields(head)); |
| 22 | let count = |name: &str| fields.iter().filter(|(n, _)| n.eq_ignore_ascii_case(name)).count(); |
| 23 | req!(1, count("Date"), "Date fields in: {}", msg); |
| 24 | req!(1, count("Message-ID"), "Message-ID fields in: {}", msg); |
| 25 | |
| 26 | // RFC 5322 section 3.3, as written: "Fri, 13 Nov 2026 10:00:00 +0000". |
| 27 | let date = res!(fields.iter() |
| 28 | .find(|(n, _)| n.eq_ignore_ascii_case("Date")) |
| 29 | .map(|(_, v)| v.trim().to_string()) |
| 30 | .ok_or_else(|| err!("no Date"; Missing))); |
| 31 | let parts: Vec<&str> = date.split(' ').collect(); |
| 32 | req!(6, parts.len(), "Date '{}' is not day, date, month, year, time and zone", date); |
| 33 | let days = ["Mon,", "Tue,", "Wed,", "Thu,", "Fri,", "Sat,", "Sun,"]; |
| 34 | let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; |
| 35 | req!(true, days.contains(&parts[0]), "day of week in '{}'", date); |
| 36 | req!(true, parts[1].len() == 2 && parts[1].chars().all(|c| c.is_ascii_digit()), "day in '{}'", date); |
| 37 | req!(true, months.contains(&parts[2]), "month in '{}'", date); |
| 38 | req!(true, parts[3].len() == 4 && parts[3].chars().all(|c| c.is_ascii_digit()), "year in '{}'", date); |
| 39 | let hms: Vec<&str> = parts[4].split(':').collect(); |
| 40 | req!(true, hms.len() == 3 && hms.iter().all(|t| t.len() == 2 && t.chars().all(|c| c.is_ascii_digit())), |
| 41 | "time in '{}'", date); |
| 42 | req!("+0000", parts[5], "zone in '{}'", date); |
| 43 | |
| 44 | let id = res!(message_id(msg.as_bytes()).ok_or_else(|| err!("no Message-ID"; Missing))); |
| 45 | req!(true, id.starts_with('<') && id.ends_with("@oxegen.io>"), "Message-ID '{}' not in the sending domain", id); |
| 46 | req!(1, count("MIME-Version"), "MIME-Version fields in: {}", msg); |
| 47 | Ok(()) |
| 48 | } |