core: add post rejection tests

axum-login-upgrade
Manos Pitsidianakis 2023-04-29 13:33:02 +03:00
parent 6aeabd2c7f
commit 451e48d788
Signed by: Manos Pitsidianakis
GPG Key ID: 7729C7707F7E09D0
7 changed files with 172 additions and 71 deletions

View File

@ -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(());
}
}
}

View File

@ -76,8 +76,7 @@ fn test_accounts() {
To: <foo-chat+subscribe@example.com>
Subject: subscribe
Date: Thu, 29 Oct 2020 13:58:16 +0000
Message-ID:
<PS1PR0601MB36750BD00EA89E1482FA98A2D5140_2@PS1PR0601MB3675.apcprd06.example.com>
Message-ID: <abcdefgh@sator.example.com>
Content-Language: en-US
Content-Type: text/html
Content-Transfer-Encoding: base64
@ -100,8 +99,7 @@ MIME-Version: 1.0
To: <foo-chat+request@example.com>
Subject: password
Date: Thu, 29 Oct 2020 13:58:16 +0000
Message-ID:
<PS1PR0601MB36750BD00EA89E1482FA98A2D5140@PS1PR0601MB3675.apcprd06.example.com>
Message-ID: <abcdefgh@sator.example.com>
Content-Language: en-US
Content-Type: text/plain; charset=ascii
Content-Transfer-Encoding: 8bit

View File

@ -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)
);
}

View File

@ -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,

View File

@ -70,12 +70,11 @@ fn test_list_subscription() {
let mut db = db.untrusted();
let input_bytes_1 = b"From: Name <user@example.com>
let post_bytes = b"From: Name <user@example.com>
To: <foo-chat@example.com>
Subject: This is a post
Date: Thu, 29 Oct 2020 13:58:16 +0000
Message-ID:
<PS1PR0601MB36750BD00EA89E1482FA98A2D5140@PS1PR0601MB3675.apcprd06.example.com>
Message-ID: <abcdefgh@sator.example.com>
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 <user@example.com>
let subscribe_bytes = b"From: Name <user@example.com>
To: <foo-chat+subscribe@example.com>
Subject: subscribe
Date: Thu, 29 Oct 2020 13:58:16 +0000
Message-ID:
<PS1PR0601MB36750BD00EA89E1482FA98A2D5140_2@PS1PR0601MB3675.apcprd06.example.com>
Message-ID: <abcdefgh@sator.example.com>
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 <user@example.com>
To: <foo-chat@example.com>
Subject: This is a post
Date: Thu, 29 Oct 2020 13:58:16 +0000
Message-ID: <abcdefgh@sator.example.com>
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)
);
}
}
}
}

View File

@ -87,8 +87,7 @@ fn test_template_replies() {
To: <foo-chat+subscribe@example.com>
Subject: subscribe
Date: Thu, 29 Oct 2020 13:58:16 +0000
Message-ID:
<PS1PR0601MB36750BD00EA89E1482FA98A2D5140_2@PS1PR0601MB3675.apcprd06.example.com>
Message-ID: <abcdefgh@sator.example.com>
Content-Language: en-US
Content-Type: text/html
Content-Transfer-Encoding: base64
@ -127,8 +126,7 @@ MIME-Version: 1.0
To: <foo-chat+request@example.com>
Subject: unsubscribe
Date: Thu, 29 Oct 2020 13:58:17 +0000
Message-ID:
<PS1PR0601MB36750BD00EA89E1482FA98A2D5140_3@PS1PR0601MB3675.apcprd06.example.com>
Message-ID: <abcdefgh@sator.example.com>
Content-Language: en-US
Content-Type: text/html
Content-Transfer-Encoding: base64

View File

@ -1,24 +1,19 @@
Return-Path: <japoeunp@example.com>
Delivered-To: jonnny@example.com
Return-Path: <paaoejunp@example.com>
Delivered-To: john@example.com
Received: from violet.example.com
by violet.example.com with LMTP
id qBHcI7LKml9FxzIAYrQLqw
(envelope-from <japoeunp@example.com>)
for <jonnny@example.com>; Thu, 29 Oct 2020 13:59:14 +0000
Return-path: <japoeunp@example.com>
Envelope-to: jonnny@example.com
(envelope-from <paaoejunp@example.com>)
for <john@example.com>; Thu, 29 Oct 2020 13:59:14 +0000
Return-path: <paaoejunp@example.com>
Envelope-to: john@example.com
Delivery-date: Thu, 29 Oct 2020 13:59:14 +0000
From: Jamaica Poe <japoeunp@example.com>
From: Cardholder Name <paaoejunp@example.com>
To: <foo-chat@example.com>
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:
<PS1PR0601MB36750BD00EA89E1482FA98A2D5140@PS1PR0601MB3675.apcprd06.example.com>
Accept-Language: en-US
Message-ID: <abcdefgh@sator.example.com>
Content-Language: en-US
Content-Type: text/html
Content-Transfer-Encoding: base64