Lines of
tests/mail.rs
from check-in 98c5a42df0
that are changed by the sequence of edits moving toward
check-in 40a93e9a58:
1: use smtp2tg::mail::MailServer;
2:
3: use config::FileFormat::Toml;
4: use stacked_errors::{
5: Result,
6: StackableErr,
7: ensure,
8: ensure_eq,
9: };
10:
11: use tgbot::types::ChatPeerId;
12:
13: #[test]
14: fn get_id_properly_resolves_addresses () -> Result<()> {
15: let server = MailServer::new(config::Config::builder()
16: .add_source(config::File::from_str(r#"
17: api_key = "test-api-key"
18: api_gateway = "https://api.telegram.org"
19: default = 0
20: fields = ["date", "from", "subject"]
21: domains = ["example.com"]
22:
23: [recipients]
24: "someone@example.com" = 1
25: "root" = -1
26: "#, Toml))
27: .build()
28: .stack()?)?;
29: let cases = [
30: ("someone@example.com", 1),
31: ("someone", 0),
32: ("root", -1),
33: ("unknown@example.com", 0),
34: ("SOMEONE@example.com", 1), // uppercase local part
35: ("someone@EXAMPLE.COM", 1), // uppercase domain
98c5a42df0 2026-08-01 36: ("some.one@example.com", 1), // functionally equivalent to skipping '.'
37: ("some-one-2", 0), // Hyphens
38: ];
39: for (email, id) in cases {
40: ensure_eq!(*server.get_id(email)?, ChatPeerId::from(id), format!("email [{email}] expected to return id [{id}]"));
41: }
42: let cases = [
43: "someone@otherdomain.net",
44: "@example.com", // empty local part
45: "some@one@example.com", // more than one '@'
46: "someone@example.com.evil",
47: "someone@example.org",
48: ];
49: for email in cases {
50: ensure!(server.get_id(email).is_err(), format!("this email should be rejected: {email}"));
51: }
52: Ok(())
53: }
54:
55: #[test]
56: fn wrong_server_config () -> Result<()> {
57: let configs = [
58: "domains = []",
59: "",
60: "[recipents]\na = 1",
61: r#"
62: api_key = "test-api-key"
63: api_gateway = "https://api.telegram.org"
64: default = 0
65: fields = ["date", "from", "subject"]
66: domains = ["example.com"]
67: # no recipients
68: "#,
69: r#"
70: ap_key = "test-api-key" # bad one
71: api_gateway = "https://api.telegram.org"
72: default = 0
73: fields = ["date", "from", "subject"]
74: domains = ["example.com"]
75:
76: [recipients]
77: "someone@example.com" = 1
78: "root" = -1"#,
79: r#"
80: api_key = "test-api-key"
81: api_gateway = "https://api.telegram.org"
82: default = 0
83: fields = ["date", "from", "subject"]
84: domains = [] # empty
85:
86: [recipients]
87: "someone@example.com" = 1
88: "root" = -1"#,
89: ];
90: for config in configs {
91: let settings = config::Config::builder()
92: .add_source(config::File::from_str(config, Toml))
93: .build()
94: .stack()?;
95: ensure!(MailServer::new(settings).is_err(), format!("this config shouldn't be valid:\n{config}"));
96: }
97:
98: Ok(())
99: }