Author: Manos Pitsidianakis [manos@pitsidianak.is]
Hash: 451e48d7883bf25b2ca5fc0080386130c1486b94
Timestamp: Sat, 29 Apr 2023 10:33:02 +0000 (1 year ago)

+172 -71 +/-7 browse
core: add post rejection tests
1diff --git a/core/src/db/posts.rs b/core/src/db/posts.rs
2index 64c3e53..7b8cb59 100644
3--- a/core/src/db/posts.rs
4+++ b/core/src/db/posts.rs
5 @@ -232,7 +232,8 @@ impl Connection {
6 std::iter::once(Cow::Borrowed(f)),
7 )?;
8 }
9- return Err(PostRejected(reason).into());
10+ /* error handled by notifying submitter */
11+ return Ok(());
12 }
13 PostAction::Defer { reason } => {
14 trace!("PostAction::Defer {{ reason: {} }}", reason);
15 @@ -262,7 +263,7 @@ impl Connection {
16 &bytes,
17 Some(format!("PostAction::Defer {{ reason: {} }}", reason)),
18 )?)?;
19- return Err(PostRejected(reason).into());
20+ return Ok(());
21 }
22 PostAction::Hold => {
23 trace!("PostAction::Hold");
24 @@ -273,7 +274,7 @@ impl Connection {
25 &bytes,
26 Some("PostAction::Hold".to_string()),
27 )?)?;
28- return Err(PostRejected("Hold".into()).into());
29+ return Ok(());
30 }
31 }
32 }
33 diff --git a/core/tests/account.rs b/core/tests/account.rs
34index 5af5b56..1c803b5 100644
35--- a/core/tests/account.rs
36+++ b/core/tests/account.rs
37 @@ -76,8 +76,7 @@ fn test_accounts() {
38 To: <foo-chat+subscribe@example.com>
39 Subject: subscribe
40 Date: Thu, 29 Oct 2020 13:58:16 +0000
41- Message-ID:
42- <PS1PR0601MB36750BD00EA89E1482FA98A2D5140_2@PS1PR0601MB3675.apcprd06.example.com>
43+ Message-ID: <abcdefgh@sator.example.com>
44 Content-Language: en-US
45 Content-Type: text/html
46 Content-Transfer-Encoding: base64
47 @@ -100,8 +99,7 @@ MIME-Version: 1.0
48 To: <foo-chat+request@example.com>
49 Subject: password
50 Date: Thu, 29 Oct 2020 13:58:16 +0000
51- Message-ID:
52- <PS1PR0601MB36750BD00EA89E1482FA98A2D5140@PS1PR0601MB3675.apcprd06.example.com>
53+ Message-ID: <abcdefgh@sator.example.com>
54 Content-Language: en-US
55 Content-Type: text/plain; charset=ascii
56 Content-Transfer-Encoding: 8bit
57 diff --git a/core/tests/error_queue.rs b/core/tests/error_queue.rs
58index 32c6613..6d23fd5 100644
59--- a/core/tests/error_queue.rs
60+++ b/core/tests/error_queue.rs
61 @@ -80,13 +80,16 @@ fn test_error_queue() {
62
63 let input_bytes = include_bytes!("./test_sample_longmessage.eml");
64 let envelope = melib::Envelope::from_bytes(input_bytes, None).expect("Could not parse message");
65- match db
66- .post(&envelope, input_bytes, /* dry_run */ false)
67- .unwrap_err()
68- .kind()
69- {
70- mailpot::ErrorKind::PostRejected(_reason) => {}
71- other => panic!("Got unexpected error: {}", other),
72- }
73- assert_eq!(db.queue(Queue::Error).unwrap().len(), 1);
74+ db.post(&envelope, input_bytes, /* dry_run */ false)
75+ .expect("Got unexpected error");
76+ let out = db.queue(Queue::Out).unwrap();
77+ assert_eq!(out.len(), 1);
78+ const COMMENT_PREFIX: &str = "PostAction::Reject { reason: Only subscriptions";
79+ assert_eq!(
80+ out[0]
81+ .comment
82+ .as_ref()
83+ .and_then(|c| c.get(..COMMENT_PREFIX.len())),
84+ Some(COMMENT_PREFIX)
85+ );
86 }
87 diff --git a/core/tests/smtp.rs b/core/tests/smtp.rs
88index b8c6469..639a123 100644
89--- a/core/tests/smtp.rs
90+++ b/core/tests/smtp.rs
91 @@ -71,15 +71,19 @@ fn test_smtp() {
92 match melib::Envelope::from_bytes(input_bytes, None) {
93 Ok(envelope) => {
94 // eprintln!("envelope {:?}", &envelope);
95- match db
96- .post(&envelope, input_bytes, /* dry_run */ false)
97- .unwrap_err()
98- .kind()
99+ db.post(&envelope, input_bytes, /* dry_run */ false)
100+ .expect("Got unexpected error");
101 {
102- mailpot::ErrorKind::PostRejected(reason) => {
103- trace!("Non-subscription post succesfully rejected: '{reason}'");
104- }
105- other => panic!("Got unexpected error: {}", other),
106+ let out = db.queue(Queue::Out).unwrap();
107+ assert_eq!(out.len(), 1);
108+ const COMMENT_PREFIX: &str = "PostAction::Reject { reason: Only subscriptions";
109+ assert_eq!(
110+ out[0]
111+ .comment
112+ .as_ref()
113+ .and_then(|c| c.get(..COMMENT_PREFIX.len())),
114+ Some(COMMENT_PREFIX)
115+ );
116 }
117
118 db.add_subscription(
119 @@ -87,8 +91,8 @@ fn test_smtp() {
120 ListSubscription {
121 pk: 0,
122 list: foo_chat.pk(),
123- address: "japoeunp@example.com".into(),
124- name: Some("Jamaica Poe".into()),
125+ address: "paaoejunp@example.com".into(),
126+ name: Some("Cardholder Name".into()),
127 account: None,
128 digest: false,
129 verified: true,
130 @@ -138,7 +142,7 @@ fn test_smtp() {
131 }));
132 let stored = smtp_handler.stored.lock().unwrap();
133 assert_eq!(stored.len(), 3);
134- assert_eq!(&stored[0].0, "japoeunp@example.com");
135+ assert_eq!(&stored[0].0, "paaoejunp@example.com");
136 assert_eq!(
137 &stored[0].1.subject(),
138 "Your post to foo-chat was rejected."
139 @@ -235,8 +239,8 @@ fn test_smtp_mailcrab() {
140 ListSubscription {
141 pk: 0,
142 list: foo_chat.pk(),
143- address: "japoeunp@example.com".into(),
144- name: Some("Jamaica Poe".into()),
145+ address: "paaoejunp@example.com".into(),
146+ name: Some("Cardholder Name".into()),
147 account: None,
148 digest: false,
149 verified: true,
150 diff --git a/core/tests/subscription.rs b/core/tests/subscription.rs
151index 1515af9..234ce69 100644
152--- a/core/tests/subscription.rs
153+++ b/core/tests/subscription.rs
154 @@ -70,12 +70,11 @@ fn test_list_subscription() {
155
156 let mut db = db.untrusted();
157
158- let input_bytes_1 = b"From: Name <user@example.com>
159+ let post_bytes = b"From: Name <user@example.com>
160 To: <foo-chat@example.com>
161 Subject: This is a post
162 Date: Thu, 29 Oct 2020 13:58:16 +0000
163- Message-ID:
164- <PS1PR0601MB36750BD00EA89E1482FA98A2D5140@PS1PR0601MB3675.apcprd06.example.com>
165+ Message-ID: <abcdefgh@sator.example.com>
166 Content-Language: en-US
167 Content-Type: text/html
168 Content-Transfer-Encoding: base64
169 @@ -86,24 +85,25 @@ eT48dGFibGUgY2xhc3M9ImZvbyI+PHRoZWFkPjx0cj48dGQ+Zm9vPC90ZD48L3RoZWFk
170 Pjx0Ym9keT48dHI+PHRkPmZvbzE8L3RkPjwvdHI+PC90Ym9keT48L3RhYmxlPjwvYm9k
171 eT48L2h0bWw+
172 ";
173- let envelope =
174- melib::Envelope::from_bytes(input_bytes_1, None).expect("Could not parse message");
175- match db
176- .post(&envelope, input_bytes_1, /* dry_run */ false)
177- .unwrap_err()
178- .kind()
179- {
180- mailpot::ErrorKind::PostRejected(_reason) => {}
181- other => panic!("Got unexpected error: {}", other),
182- }
183- assert_eq!(db.queue(Queue::Error).unwrap().len(), 1);
184+ let envelope = melib::Envelope::from_bytes(post_bytes, None).expect("Could not parse message");
185+ db.post(&envelope, post_bytes, /* dry_run */ false)
186+ .expect("Got unexpected error");
187+ let out = db.queue(Queue::Out).unwrap();
188+ assert_eq!(out.len(), 1);
189+ const COMMENT_PREFIX: &str = "PostAction::Reject { reason: Only subscriptions";
190+ assert_eq!(
191+ out[0]
192+ .comment
193+ .as_ref()
194+ .and_then(|c| c.get(..COMMENT_PREFIX.len())),
195+ Some(COMMENT_PREFIX)
196+ );
197
198- let input_bytes_2 = b"From: Name <user@example.com>
199+ let subscribe_bytes = b"From: Name <user@example.com>
200 To: <foo-chat+subscribe@example.com>
201 Subject: subscribe
202 Date: Thu, 29 Oct 2020 13:58:16 +0000
203- Message-ID:
204- <PS1PR0601MB36750BD00EA89E1482FA98A2D5140_2@PS1PR0601MB3675.apcprd06.example.com>
205+ Message-ID: <abcdefgh@sator.example.com>
206 Content-Language: en-US
207 Content-Type: text/html
208 Content-Transfer-Encoding: base64
209 @@ -111,15 +111,117 @@ MIME-Version: 1.0
210
211 ";
212 let envelope =
213- melib::Envelope::from_bytes(input_bytes_2, None).expect("Could not parse message");
214- db.post(&envelope, input_bytes_2, /* dry_run */ false)
215+ melib::Envelope::from_bytes(subscribe_bytes, None).expect("Could not parse message");
216+ db.post(&envelope, subscribe_bytes, /* dry_run */ false)
217 .unwrap();
218 assert_eq!(db.list_subscriptions(foo_chat.pk()).unwrap().len(), 1);
219- assert_eq!(db.queue(Queue::Error).unwrap().len(), 1);
220- let envelope =
221- melib::Envelope::from_bytes(input_bytes_1, None).expect("Could not parse message");
222- db.post(&envelope, input_bytes_1, /* dry_run */ false)
223- .unwrap();
224- assert_eq!(db.queue(Queue::Error).unwrap().len(), 1);
225+ assert_eq!(db.queue(Queue::Out).unwrap().len(), 2);
226+ let envelope = melib::Envelope::from_bytes(post_bytes, None).expect("Could not parse message");
227+ db.post(&envelope, post_bytes, /* dry_run */ false).unwrap();
228+ assert_eq!(db.queue(Queue::Out).unwrap().len(), 2);
229 assert_eq!(db.list_posts(foo_chat.pk(), None).unwrap().len(), 1);
230 }
231+
232+ #[test]
233+ fn test_post_rejection() {
234+ init_stderr_logging();
235+
236+ const ANNOUNCE_ONLY_PREFIX: Option<&str> =
237+ Some("PostAction::Reject { reason: You are not allowed to post on this list.");
238+ const APPROVAL_ONLY_PREFIX: Option<&str> = Some(
239+ "PostAction::Defer { reason: Your posting has been deferred. Approval from the list's \
240+ moderators",
241+ );
242+
243+ for (q, mut post_policy) in [
244+ (
245+ [(Queue::Out, ANNOUNCE_ONLY_PREFIX)].as_slice(),
246+ PostPolicy {
247+ pk: -1,
248+ list: -1,
249+ announce_only: true,
250+ subscription_only: false,
251+ approval_needed: false,
252+ open: false,
253+ custom: false,
254+ },
255+ ),
256+ (
257+ [(Queue::Out, APPROVAL_ONLY_PREFIX), (Queue::Deferred, None)].as_slice(),
258+ PostPolicy {
259+ pk: -1,
260+ list: -1,
261+ announce_only: false,
262+ subscription_only: false,
263+ approval_needed: true,
264+ open: false,
265+ custom: false,
266+ },
267+ ),
268+ ] {
269+ let tmp_dir = TempDir::new().unwrap();
270+
271+ let db_path = tmp_dir.path().join("mpot.db");
272+ let config = Configuration {
273+ send_mail: SendMail::ShellCommand("/usr/bin/false".to_string()),
274+ db_path,
275+ data_path: tmp_dir.path().to_path_buf(),
276+ administrators: vec![],
277+ };
278+
279+ let db = Connection::open_or_create_db(config).unwrap().trusted();
280+ assert!(db.lists().unwrap().is_empty());
281+ let foo_chat = db
282+ .create_list(MailingList {
283+ pk: 0,
284+ name: "foobar chat".into(),
285+ id: "foo-chat".into(),
286+ address: "foo-chat@example.com".into(),
287+ description: None,
288+ archive_url: None,
289+ })
290+ .unwrap();
291+
292+ assert_eq!(foo_chat.pk(), 1);
293+ let lists = db.lists().unwrap();
294+ assert_eq!(lists.len(), 1);
295+ assert_eq!(lists[0], foo_chat);
296+ post_policy.list = foo_chat.pk();
297+ let post_policy = db.set_list_post_policy(post_policy).unwrap();
298+
299+ assert_eq!(post_policy.pk(), 1);
300+ assert_eq!(db.queue(Queue::Error).unwrap().len(), 0);
301+ assert_eq!(db.list_subscriptions(foo_chat.pk()).unwrap().len(), 0);
302+
303+ let mut db = db.untrusted();
304+
305+ let post_bytes = b"From: Name <user@example.com>
306+ To: <foo-chat@example.com>
307+ Subject: This is a post
308+ Date: Thu, 29 Oct 2020 13:58:16 +0000
309+ Message-ID: <abcdefgh@sator.example.com>
310+ Content-Language: en-US
311+ Content-Type: text/html
312+ Content-Transfer-Encoding: base64
313+ MIME-Version: 1.0
314+
315+ PCFET0NUWVBFPjxodG1sPjxoZWFkPjx0aXRsZT5mb288L3RpdGxlPjwvaGVhZD48Ym9k
316+ eT48dGFibGUgY2xhc3M9ImZvbyI+PHRoZWFkPjx0cj48dGQ+Zm9vPC90ZD48L3RoZWFk
317+ Pjx0Ym9keT48dHI+PHRkPmZvbzE8L3RkPjwvdHI+PC90Ym9keT48L3RhYmxlPjwvYm9k
318+ eT48L2h0bWw+
319+ ";
320+ let envelope =
321+ melib::Envelope::from_bytes(post_bytes, None).expect("Could not parse message");
322+ db.post(&envelope, post_bytes, /* dry_run */ false).unwrap();
323+ for &(q, prefix) in q {
324+ let q = db.queue(q).unwrap();
325+ assert_eq!(q.len(), 1);
326+ if let Some(prefix) = prefix {
327+ assert_eq!(
328+ q[0].comment.as_ref().and_then(|c| c.get(..prefix.len())),
329+ Some(prefix)
330+ );
331+ }
332+ }
333+ }
334+ }
335 diff --git a/core/tests/template_replies.rs b/core/tests/template_replies.rs
336index 8660b87..f68eff6 100644
337--- a/core/tests/template_replies.rs
338+++ b/core/tests/template_replies.rs
339 @@ -87,8 +87,7 @@ fn test_template_replies() {
340 To: <foo-chat+subscribe@example.com>
341 Subject: subscribe
342 Date: Thu, 29 Oct 2020 13:58:16 +0000
343- Message-ID:
344- <PS1PR0601MB36750BD00EA89E1482FA98A2D5140_2@PS1PR0601MB3675.apcprd06.example.com>
345+ Message-ID: <abcdefgh@sator.example.com>
346 Content-Language: en-US
347 Content-Type: text/html
348 Content-Transfer-Encoding: base64
349 @@ -127,8 +126,7 @@ MIME-Version: 1.0
350 To: <foo-chat+request@example.com>
351 Subject: unsubscribe
352 Date: Thu, 29 Oct 2020 13:58:17 +0000
353- Message-ID:
354- <PS1PR0601MB36750BD00EA89E1482FA98A2D5140_3@PS1PR0601MB3675.apcprd06.example.com>
355+ Message-ID: <abcdefgh@sator.example.com>
356 Content-Language: en-US
357 Content-Type: text/html
358 Content-Transfer-Encoding: base64
359 diff --git a/core/tests/test_sample_longmessage.eml b/core/tests/test_sample_longmessage.eml
360index 5323b9a..a41ff28 100644
361--- a/core/tests/test_sample_longmessage.eml
362+++ b/core/tests/test_sample_longmessage.eml
363 @@ -1,24 +1,19 @@
364- Return-Path: <japoeunp@example.com>
365- Delivered-To: jonnny@example.com
366+ Return-Path: <paaoejunp@example.com>
367+ Delivered-To: john@example.com
368 Received: from violet.example.com
369 by violet.example.com with LMTP
370 id qBHcI7LKml9FxzIAYrQLqw
371- (envelope-from <japoeunp@example.com>)
372- for <jonnny@example.com>; Thu, 29 Oct 2020 13:59:14 +0000
373- Return-path: <japoeunp@example.com>
374- Envelope-to: jonnny@example.com
375+ (envelope-from <paaoejunp@example.com>)
376+ for <john@example.com>; Thu, 29 Oct 2020 13:59:14 +0000
377+ Return-path: <paaoejunp@example.com>
378+ Envelope-to: john@example.com
379 Delivery-date: Thu, 29 Oct 2020 13:59:14 +0000
380- From: Jamaica Poe <japoeunp@example.com>
381+ From: Cardholder Name <paaoejunp@example.com>
382 To: <foo-chat@example.com>
383 Subject: thankful that I had the chance to written report, that I could learn
384 and let alone the chance $4454.32
385- Thread-Topic: thankful that I had the chance to written report, that I could
386- learn and let alone the chance $4454.32
387- Thread-Index: AQHWrfuHFQ6EC5DxDEG0hktDfP8BQg==
388 Date: Thu, 29 Oct 2020 13:58:16 +0000
389- Message-ID:
390- <PS1PR0601MB36750BD00EA89E1482FA98A2D5140@PS1PR0601MB3675.apcprd06.example.com>
391- Accept-Language: en-US
392+ Message-ID: <abcdefgh@sator.example.com>
393 Content-Language: en-US
394 Content-Type: text/html
395 Content-Transfer-Encoding: base64