From 451e48d7883bf25b2ca5fc0080386130c1486b94 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Sat, 29 Apr 2023 13:33:02 +0300 Subject: [PATCH] core: add post rejection tests --- core/src/db/posts.rs | 7 +- core/tests/account.rs | 6 +- core/tests/error_queue.rs | 21 ++-- core/tests/smtp.rs | 30 ++--- core/tests/subscription.rs | 152 +++++++++++++++++++++---- core/tests/template_replies.rs | 6 +- core/tests/test_sample_longmessage.eml | 21 ++-- 7 files changed, 172 insertions(+), 71 deletions(-) diff --git a/core/src/db/posts.rs b/core/src/db/posts.rs index 64c3e53..7b8cb59 100644 --- a/core/src/db/posts.rs +++ b/core/src/db/posts.rs @@ -232,7 +232,8 @@ impl Connection { std::iter::once(Cow::Borrowed(f)), )?; } - return Err(PostRejected(reason).into()); + /* error handled by notifying submitter */ + return Ok(()); } PostAction::Defer { reason } => { trace!("PostAction::Defer {{ reason: {} }}", reason); @@ -262,7 +263,7 @@ impl Connection { &bytes, Some(format!("PostAction::Defer {{ reason: {} }}", reason)), )?)?; - return Err(PostRejected(reason).into()); + return Ok(()); } PostAction::Hold => { trace!("PostAction::Hold"); @@ -273,7 +274,7 @@ impl Connection { &bytes, Some("PostAction::Hold".to_string()), )?)?; - return Err(PostRejected("Hold".into()).into()); + return Ok(()); } } } diff --git a/core/tests/account.rs b/core/tests/account.rs index 5af5b56..1c803b5 100644 --- a/core/tests/account.rs +++ b/core/tests/account.rs @@ -76,8 +76,7 @@ fn test_accounts() { To: Subject: subscribe Date: Thu, 29 Oct 2020 13:58:16 +0000 -Message-ID: - +Message-ID: Content-Language: en-US Content-Type: text/html Content-Transfer-Encoding: base64 @@ -100,8 +99,7 @@ MIME-Version: 1.0 To: Subject: password Date: Thu, 29 Oct 2020 13:58:16 +0000 -Message-ID: - +Message-ID: Content-Language: en-US Content-Type: text/plain; charset=ascii Content-Transfer-Encoding: 8bit diff --git a/core/tests/error_queue.rs b/core/tests/error_queue.rs index 32c6613..6d23fd5 100644 --- a/core/tests/error_queue.rs +++ b/core/tests/error_queue.rs @@ -80,13 +80,16 @@ fn test_error_queue() { let input_bytes = include_bytes!("./test_sample_longmessage.eml"); let envelope = melib::Envelope::from_bytes(input_bytes, None).expect("Could not parse message"); - match db - .post(&envelope, input_bytes, /* dry_run */ false) - .unwrap_err() - .kind() - { - mailpot::ErrorKind::PostRejected(_reason) => {} - other => panic!("Got unexpected error: {}", other), - } - assert_eq!(db.queue(Queue::Error).unwrap().len(), 1); + db.post(&envelope, input_bytes, /* dry_run */ false) + .expect("Got unexpected error"); + let out = db.queue(Queue::Out).unwrap(); + assert_eq!(out.len(), 1); + const COMMENT_PREFIX: &str = "PostAction::Reject { reason: Only subscriptions"; + assert_eq!( + out[0] + .comment + .as_ref() + .and_then(|c| c.get(..COMMENT_PREFIX.len())), + Some(COMMENT_PREFIX) + ); } diff --git a/core/tests/smtp.rs b/core/tests/smtp.rs index b8c6469..639a123 100644 --- a/core/tests/smtp.rs +++ b/core/tests/smtp.rs @@ -71,15 +71,19 @@ fn test_smtp() { match melib::Envelope::from_bytes(input_bytes, None) { Ok(envelope) => { // eprintln!("envelope {:?}", &envelope); - match db - .post(&envelope, input_bytes, /* dry_run */ false) - .unwrap_err() - .kind() + db.post(&envelope, input_bytes, /* dry_run */ false) + .expect("Got unexpected error"); { - mailpot::ErrorKind::PostRejected(reason) => { - trace!("Non-subscription post succesfully rejected: '{reason}'"); - } - other => panic!("Got unexpected error: {}", other), + let out = db.queue(Queue::Out).unwrap(); + assert_eq!(out.len(), 1); + const COMMENT_PREFIX: &str = "PostAction::Reject { reason: Only subscriptions"; + assert_eq!( + out[0] + .comment + .as_ref() + .and_then(|c| c.get(..COMMENT_PREFIX.len())), + Some(COMMENT_PREFIX) + ); } db.add_subscription( @@ -87,8 +91,8 @@ fn test_smtp() { ListSubscription { pk: 0, list: foo_chat.pk(), - address: "japoeunp@example.com".into(), - name: Some("Jamaica Poe".into()), + address: "paaoejunp@example.com".into(), + name: Some("Cardholder Name".into()), account: None, digest: false, verified: true, @@ -138,7 +142,7 @@ fn test_smtp() { })); let stored = smtp_handler.stored.lock().unwrap(); assert_eq!(stored.len(), 3); - assert_eq!(&stored[0].0, "japoeunp@example.com"); + assert_eq!(&stored[0].0, "paaoejunp@example.com"); assert_eq!( &stored[0].1.subject(), "Your post to foo-chat was rejected." @@ -235,8 +239,8 @@ fn test_smtp_mailcrab() { ListSubscription { pk: 0, list: foo_chat.pk(), - address: "japoeunp@example.com".into(), - name: Some("Jamaica Poe".into()), + address: "paaoejunp@example.com".into(), + name: Some("Cardholder Name".into()), account: None, digest: false, verified: true, diff --git a/core/tests/subscription.rs b/core/tests/subscription.rs index 1515af9..234ce69 100644 --- a/core/tests/subscription.rs +++ b/core/tests/subscription.rs @@ -70,12 +70,11 @@ fn test_list_subscription() { let mut db = db.untrusted(); - let input_bytes_1 = b"From: Name + let post_bytes = b"From: Name To: Subject: This is a post Date: Thu, 29 Oct 2020 13:58:16 +0000 -Message-ID: - +Message-ID: Content-Language: en-US Content-Type: text/html Content-Transfer-Encoding: base64 @@ -86,24 +85,25 @@ eT48dGFibGUgY2xhc3M9ImZvbyI+PHRoZWFkPjx0cj48dGQ+Zm9vPC90ZD48L3RoZWFk Pjx0Ym9keT48dHI+PHRkPmZvbzE8L3RkPjwvdHI+PC90Ym9keT48L3RhYmxlPjwvYm9k eT48L2h0bWw+ "; - let envelope = - melib::Envelope::from_bytes(input_bytes_1, None).expect("Could not parse message"); - match db - .post(&envelope, input_bytes_1, /* dry_run */ false) - .unwrap_err() - .kind() - { - mailpot::ErrorKind::PostRejected(_reason) => {} - other => panic!("Got unexpected error: {}", other), - } - assert_eq!(db.queue(Queue::Error).unwrap().len(), 1); + let envelope = melib::Envelope::from_bytes(post_bytes, None).expect("Could not parse message"); + db.post(&envelope, post_bytes, /* dry_run */ false) + .expect("Got unexpected error"); + let out = db.queue(Queue::Out).unwrap(); + assert_eq!(out.len(), 1); + const COMMENT_PREFIX: &str = "PostAction::Reject { reason: Only subscriptions"; + assert_eq!( + out[0] + .comment + .as_ref() + .and_then(|c| c.get(..COMMENT_PREFIX.len())), + Some(COMMENT_PREFIX) + ); - let input_bytes_2 = b"From: Name + let subscribe_bytes = b"From: Name To: Subject: subscribe Date: Thu, 29 Oct 2020 13:58:16 +0000 -Message-ID: - +Message-ID: Content-Language: en-US Content-Type: text/html Content-Transfer-Encoding: base64 @@ -111,15 +111,117 @@ MIME-Version: 1.0 "; let envelope = - melib::Envelope::from_bytes(input_bytes_2, None).expect("Could not parse message"); - db.post(&envelope, input_bytes_2, /* dry_run */ false) + melib::Envelope::from_bytes(subscribe_bytes, None).expect("Could not parse message"); + db.post(&envelope, subscribe_bytes, /* dry_run */ false) .unwrap(); assert_eq!(db.list_subscriptions(foo_chat.pk()).unwrap().len(), 1); - assert_eq!(db.queue(Queue::Error).unwrap().len(), 1); - let envelope = - melib::Envelope::from_bytes(input_bytes_1, None).expect("Could not parse message"); - db.post(&envelope, input_bytes_1, /* dry_run */ false) - .unwrap(); - assert_eq!(db.queue(Queue::Error).unwrap().len(), 1); + assert_eq!(db.queue(Queue::Out).unwrap().len(), 2); + let envelope = melib::Envelope::from_bytes(post_bytes, None).expect("Could not parse message"); + db.post(&envelope, post_bytes, /* dry_run */ false).unwrap(); + assert_eq!(db.queue(Queue::Out).unwrap().len(), 2); assert_eq!(db.list_posts(foo_chat.pk(), None).unwrap().len(), 1); } + +#[test] +fn test_post_rejection() { + init_stderr_logging(); + + const ANNOUNCE_ONLY_PREFIX: Option<&str> = + Some("PostAction::Reject { reason: You are not allowed to post on this list."); + const APPROVAL_ONLY_PREFIX: Option<&str> = Some( + "PostAction::Defer { reason: Your posting has been deferred. Approval from the list's \ + moderators", + ); + + for (q, mut post_policy) in [ + ( + [(Queue::Out, ANNOUNCE_ONLY_PREFIX)].as_slice(), + PostPolicy { + pk: -1, + list: -1, + announce_only: true, + subscription_only: false, + approval_needed: false, + open: false, + custom: false, + }, + ), + ( + [(Queue::Out, APPROVAL_ONLY_PREFIX), (Queue::Deferred, None)].as_slice(), + PostPolicy { + pk: -1, + list: -1, + announce_only: false, + subscription_only: false, + approval_needed: true, + open: false, + custom: false, + }, + ), + ] { + let tmp_dir = TempDir::new().unwrap(); + + let db_path = tmp_dir.path().join("mpot.db"); + let config = Configuration { + send_mail: SendMail::ShellCommand("/usr/bin/false".to_string()), + db_path, + data_path: tmp_dir.path().to_path_buf(), + administrators: vec![], + }; + + let db = Connection::open_or_create_db(config).unwrap().trusted(); + assert!(db.lists().unwrap().is_empty()); + let foo_chat = db + .create_list(MailingList { + pk: 0, + name: "foobar chat".into(), + id: "foo-chat".into(), + address: "foo-chat@example.com".into(), + description: None, + archive_url: None, + }) + .unwrap(); + + assert_eq!(foo_chat.pk(), 1); + let lists = db.lists().unwrap(); + assert_eq!(lists.len(), 1); + assert_eq!(lists[0], foo_chat); + post_policy.list = foo_chat.pk(); + let post_policy = db.set_list_post_policy(post_policy).unwrap(); + + assert_eq!(post_policy.pk(), 1); + assert_eq!(db.queue(Queue::Error).unwrap().len(), 0); + assert_eq!(db.list_subscriptions(foo_chat.pk()).unwrap().len(), 0); + + let mut db = db.untrusted(); + + let post_bytes = b"From: Name +To: +Subject: This is a post +Date: Thu, 29 Oct 2020 13:58:16 +0000 +Message-ID: +Content-Language: en-US +Content-Type: text/html +Content-Transfer-Encoding: base64 +MIME-Version: 1.0 + +PCFET0NUWVBFPjxodG1sPjxoZWFkPjx0aXRsZT5mb288L3RpdGxlPjwvaGVhZD48Ym9k +eT48dGFibGUgY2xhc3M9ImZvbyI+PHRoZWFkPjx0cj48dGQ+Zm9vPC90ZD48L3RoZWFk +Pjx0Ym9keT48dHI+PHRkPmZvbzE8L3RkPjwvdHI+PC90Ym9keT48L3RhYmxlPjwvYm9k +eT48L2h0bWw+ +"; + let envelope = + melib::Envelope::from_bytes(post_bytes, None).expect("Could not parse message"); + db.post(&envelope, post_bytes, /* dry_run */ false).unwrap(); + for &(q, prefix) in q { + let q = db.queue(q).unwrap(); + assert_eq!(q.len(), 1); + if let Some(prefix) = prefix { + assert_eq!( + q[0].comment.as_ref().and_then(|c| c.get(..prefix.len())), + Some(prefix) + ); + } + } + } +} diff --git a/core/tests/template_replies.rs b/core/tests/template_replies.rs index 8660b87..f68eff6 100644 --- a/core/tests/template_replies.rs +++ b/core/tests/template_replies.rs @@ -87,8 +87,7 @@ fn test_template_replies() { To: Subject: subscribe Date: Thu, 29 Oct 2020 13:58:16 +0000 -Message-ID: - +Message-ID: Content-Language: en-US Content-Type: text/html Content-Transfer-Encoding: base64 @@ -127,8 +126,7 @@ MIME-Version: 1.0 To: Subject: unsubscribe Date: Thu, 29 Oct 2020 13:58:17 +0000 -Message-ID: - +Message-ID: Content-Language: en-US Content-Type: text/html Content-Transfer-Encoding: base64 diff --git a/core/tests/test_sample_longmessage.eml b/core/tests/test_sample_longmessage.eml index 5323b9a..a41ff28 100644 --- a/core/tests/test_sample_longmessage.eml +++ b/core/tests/test_sample_longmessage.eml @@ -1,24 +1,19 @@ -Return-Path: -Delivered-To: jonnny@example.com +Return-Path: +Delivered-To: john@example.com Received: from violet.example.com by violet.example.com with LMTP id qBHcI7LKml9FxzIAYrQLqw - (envelope-from ) - for ; Thu, 29 Oct 2020 13:59:14 +0000 -Return-path: -Envelope-to: jonnny@example.com + (envelope-from ) + for ; Thu, 29 Oct 2020 13:59:14 +0000 +Return-path: +Envelope-to: john@example.com Delivery-date: Thu, 29 Oct 2020 13:59:14 +0000 -From: Jamaica Poe +From: Cardholder Name To: Subject: thankful that I had the chance to written report, that I could learn and let alone the chance $4454.32 -Thread-Topic: thankful that I had the chance to written report, that I could - learn and let alone the chance $4454.32 -Thread-Index: AQHWrfuHFQ6EC5DxDEG0hktDfP8BQg== Date: Thu, 29 Oct 2020 13:58:16 +0000 -Message-ID: - -Accept-Language: en-US +Message-ID: Content-Language: en-US Content-Type: text/html Content-Transfer-Encoding: base64