Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_steel/tests/comment_moderation_holds.rs

5.4 KiB, 1 run

created by r968201400:77807, 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//! A comment is published, and a regular keeps their trust, only as the moderation rules say.
2//!
3//! Three ways round the rules, each closed: a model reply read by substring ("DISAPPROVE" contains
4//! APPROVE), a model allowed to lift a hold the rules made for another reason than a stranger's first
5//! say, and a comment under a regular's address from somewhere else stripping that regular's trust.
6
7use oxedyne_fe2o3_core::prelude::*;
8use oxedyne_fe2o3_steel::srv::publish::{
9 ai::{
10 CommentVerdict,
11 parse_comment_verdict,
12 },
13 comment::{
14 self,
15 Comment,
16 CommentState,
17 Identity,
18 Moderator,
19 REASON_MISMATCH,
20 Received,
21 Submission,
22 Verdict,
23 },
24};
25
26mod common;
27
28fn by(email: Option<&str>, body: &str) -> Comment {
29 Comment {
30 author: Identity::Local {
31 name: fmt!("Ada"),
32 email: email.map(|e| e.to_string()),
33 },
34 body: body.to_string(),
35 ..Default::default()
36 }
37}
38
39#[test]
40fn a_model_reply_decides_only_when_it_is_the_word() -> Outcome<()> {
41 for reply in ["APPROVE", "approve", " Approve.\n", "\"APPROVE\"", "'approve'!", "APPROVE\"."] {
42 assert_eq!(parse_comment_verdict(reply), CommentVerdict::Approve, "{:?}", reply);
43 }
44 for reply in ["SPAM", "spam", "Spam.", "\u{201c}SPAM\u{201d}"] {
45 assert_eq!(parse_comment_verdict(reply), CommentVerdict::Spam, "{:?}", reply);
46 }
47 // Each of these contains a decisive word and decides nothing.
48 for reply in [
49 "DISAPPROVE",
50 "do not approve",
51 "I cannot approve this.",
52 "not spam",
53 "This looks fine, so APPROVE.",
54 "APPROVE or SPAM",
55 "HOLD",
56 "",
57 ] {
58 assert_eq!(parse_comment_verdict(reply), CommentVerdict::Hold, "{:?}", reply);
59 }
60 Ok(())
61}
62
63#[test]
64fn the_model_may_release_only_a_strangers_hold() -> Outcome<()> {
65 let m = Moderator::default();
66 let first = by(Some("ada@example.com"), "A fair point, well made.");
67 assert!(m.judge(&first, None).model_may_decide(), "a stranger's first say is the model's to decide");
68 let anon = by(None, "A fair point, well made.");
69 assert!(m.judge(&anon, None).model_may_decide(), "an anonymous say is the model's to decide");
70
71 // Over the link limit: held by the rules, not for want of knowing the commenter.
72 let links = by(Some("ada@example.com"),
73 "https://a.example https://b.example https://c.example");
74 let v = m.judge(&links, None);
75 assert_eq!(v.state(), CommentState::Pending);
76 assert!(!v.model_may_decide(), "the model may lift a link-limit hold: {:?}", v);
77
78 // A security hold laid over a stranger's hold outranks it.
79 for why in [REASON_MISMATCH, "no proof of work was sent"] {
80 let v = m.judge(&first, None).and_then(Verdict::Hold(why.to_string()));
81 assert!(!v.model_may_decide(), "the model may lift '{}': {:?}", why, v);
82 assert_eq!(v.reason().as_deref(), Some(why));
83 }
84 Ok(())
85}
86
87#[tokio::test]
88async fn a_mismatched_sender_leaves_the_regular_trusted() -> Outcome<()> {
89 let (db, uid, _tmp) = res!(common::test_db());
90 let db = (db, uid);
91 let salt: &[u8] = b"salt";
92 let handle = "email:ada@example.com";
93 let home = comment::from_hash("192.0.2.1", salt);
94 res!(comment::set_trust(&db, handle, true, Some(&home), "2026-11-01T00:00:00Z"));
95
96 let sub = Submission {
97 slug: "on-rent",
98 parent: None,
99 name: fmt!("Ada"),
100 email: Some(fmt!("ada@example.com")),
101 body: fmt!("It is me, honestly."),
102 honeypot: String::new(),
103 challenge: String::new(),
104 nonce: String::new(),
105 from: Some(fmt!("198.51.100.7")),
106 now: fmt!("2026-11-15T00:00:00Z"),
107 };
108 let (told, id) = res!(comment::receive(
109 &db, &Moderator::default(), None, &None, (0, 100), sub, salt, b"secret", "test").await);
110 assert!(matches!(told, Received::Held), "not held");
111 let k = res!(res!(comment::commenter(&db, handle)).ok_or_else(|| err!("forgotten"; Missing)));
112 assert!(k.trusted, "the regular lost their trust");
113 assert!(!k.blocked);
114 assert_eq!(k.from.as_deref(), Some(home.as_str()));
115 assert_eq!(k.first_seen, "2026-11-01T00:00:00Z");
116
117 let id = res!(id.ok_or_else(|| err!("a held comment has an id"; Missing)));
118 let c = res!(res!(comment::get(&db, "on-rent", &id)).ok_or_else(|| err!("not stored"; Missing)));
119 assert_eq!(c.state, CommentState::Pending);
120 assert_eq!(c.reason.as_deref(), Some(REASON_MISMATCH));
121
122 // A true stranger is still remembered, untrusted, at their first say.
123 let sub = Submission {
124 slug: "on-rent",
125 parent: None,
126 name: fmt!("Bo"),
127 email: Some(fmt!("bo@example.com")),
128 body: fmt!("First time here."),
129 honeypot: String::new(),
130 challenge: String::new(),
131 nonce: String::new(),
132 from: Some(fmt!("203.0.113.9")),
133 now: fmt!("2026-11-15T00:01:00Z"),
134 };
135 let (told, _) = res!(comment::receive(
136 &db, &Moderator::default(), None, &None, (0, 100), sub, salt, b"secret", "test").await);
137 assert!(matches!(told, Received::Held), "not held");
138 let k = res!(res!(comment::commenter(&db, "email:bo@example.com")).ok_or_else(|| err!("not remembered"; Missing)));
139 assert!(!k.trusted);
140 assert_eq!(k.first_seen, "2026-11-15T00:01:00Z");
141 Ok(())
142}