oxedyne/fe2o3/fe2o3_steel/tests/publish_expiry.rs
8.1 KiB, 1 run
created by r1870400018:68856, 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 | //! The daily sweep: a sign-up that never confirmed is deleted, and a rate row whose window has |
| 2 | //! passed goes with it. |
| 3 | //! |
| 4 | //! Each runs `outbox::sweep` against a real Ozone instance with the clock an argument, so a month |
| 5 | //! passes in an instant. The sweep finds the rate rows by a scan of the store, which these tests |
| 6 | //! therefore exercise as well. They do not skip when no database starts. |
| 7 | //! |
| 8 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 9 | //! Anthropic Claude |
| 10 | |
| 11 | use oxedyne_fe2o3_core::prelude::*; |
| 12 | use oxedyne_fe2o3_iop_db::api::Database; |
| 13 | use oxedyne_fe2o3_jdat::prelude::*; |
| 14 | use oxedyne_fe2o3_steel::srv::{ |
| 15 | id, |
| 16 | publish::{ |
| 17 | PublishConfig, |
| 18 | Source, |
| 19 | comment, |
| 20 | outbox::{ |
| 21 | self, |
| 22 | Swept, |
| 23 | }, |
| 24 | rate::{ |
| 25 | self, |
| 26 | Window, |
| 27 | }, |
| 28 | send, |
| 29 | store, |
| 30 | subscribe::{ |
| 31 | self, |
| 32 | SubState, |
| 33 | }, |
| 34 | }, |
| 35 | }; |
| 36 | |
| 37 | use std::sync::{ |
| 38 | Arc, |
| 39 | RwLock, |
| 40 | }; |
| 41 | |
| 42 | mod common; |
| 43 | |
| 44 | const DAY: u64 = 86_400; |
| 45 | |
| 46 | type Handle = (Arc<RwLock<common::TestDb>>, id::Uid); |
| 47 | |
| 48 | fn cfg() -> PublishConfig { |
| 49 | PublishConfig { |
| 50 | path: fmt!("/news"), |
| 51 | base_url: fmt!("https://site.test"), |
| 52 | source: Source::Store, |
| 53 | pending_expiry_days: 7, |
| 54 | subscribe_rate_secs: 60, |
| 55 | subscribe_rate_hourly: 5, |
| 56 | comment_rate_secs: 30, |
| 57 | comment_rate_hourly: 10, |
| 58 | confirm_interval_secs: DAY, |
| 59 | confirm_max: 3, |
| 60 | confirm_window_days: 30, |
| 61 | ..Default::default() |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // A pending sign-up that was sent its confirmation `ago` seconds before `now`. |
| 66 | fn sent_pending(db: &Handle, email: &str, now: u64, ago: u64) -> Outcome<subscribe::Subscriber> { |
| 67 | let sub = res!(subscribe::add_pending(db, email, &Window::default(), now)); |
| 68 | let sub = res!(sub.ok_or_else(|| err!("no pending record was made for {}", email; Test, Missing))); |
| 69 | let at = res!(send::iso_of((now - ago) as i64)); |
| 70 | assert!(res!(subscribe::mark_sent(db, email, &at)), "the send of {} was not recorded", email); |
| 71 | Ok(sub) |
| 72 | } |
| 73 | |
| 74 | fn state_of(db: &Handle, email: &str) -> Outcome<Option<SubState>> { |
| 75 | Ok(res!(subscribe::get(db, email)).map(|s| s.state)) |
| 76 | } |
| 77 | |
| 78 | // The addresses the subscriber index names, read straight from the store. |
| 79 | fn indexed(db: &Handle) -> Outcome<Vec<String>> { |
| 80 | let guard = lock_read!(db.0); |
| 81 | let old = res!(guard.get(&dat!(subscribe::INDEX_KEY), None)).map(|(v, _)| v); |
| 82 | store::names_of(old, "subscriber index") |
| 83 | } |
| 84 | |
| 85 | fn row_exists(db: &Handle, key: &str) -> Outcome<bool> { |
| 86 | let guard = lock_read!(db.0); |
| 87 | Ok(res!(guard.get(&dat!(key.to_string()), None)).is_some()) |
| 88 | } |
| 89 | |
| 90 | #[test] |
| 91 | fn test_a_pending_sign_up_sent_eight_days_ago_is_deleted_from_record_and_index_00() -> Outcome<()> { |
| 92 | let (db, uid, _tmp) = res!(common::test_db()); |
| 93 | let handle = (db, uid); |
| 94 | let now = rate::now_secs(); |
| 95 | res!(sent_pending(&handle, "old@site.test", now, 8 * DAY)); |
| 96 | res!(sent_pending(&handle, "new@site.test", now, DAY)); |
| 97 | |
| 98 | let swept = outbox::sweep(&handle, &cfg(), now, "test", true); |
| 99 | |
| 100 | assert_eq!(swept.pending, 1, "the sweep removed {} sign-ups, not the one that had lapsed", swept.pending); |
| 101 | assert_eq!(state_of(&handle, "old@site.test")?, None, "the lapsed record is still in the store"); |
| 102 | assert_eq!(indexed(&handle)?, vec![fmt!("new@site.test")], "the index still names the lapsed address"); |
| 103 | assert_eq!(state_of(&handle, "new@site.test")?, Some(SubState::Pending), "a recent sign-up was removed"); |
| 104 | Ok(()) |
| 105 | } |
| 106 | |
| 107 | #[test] |
| 108 | fn test_a_pending_sign_up_never_sent_a_confirmation_is_kept_however_old_01() -> Outcome<()> { |
| 109 | let (db, uid, _tmp) = res!(common::test_db()); |
| 110 | let handle = (db, uid); |
| 111 | let now = rate::now_secs(); |
| 112 | // Held behind a ceiling of 0: queued, never sent, so it has no clock. |
| 113 | let held = res!(subscribe::add_pending(&handle, "held@site.test", &Window::default(), now)); |
| 114 | assert!(held.is_some_and(|s| s.sent.is_none()), "a record nothing was sent to has a sent time"); |
| 115 | |
| 116 | let swept = outbox::sweep(&handle, &cfg(), now + 400 * DAY, "test", false); |
| 117 | |
| 118 | assert_eq!(swept, Swept::default(), "the sweep removed {:?} from a store holding one held sign-up", swept); |
| 119 | assert_eq!(state_of(&handle, "held@site.test")?, Some(SubState::Pending), "a held sign-up expired"); |
| 120 | assert_eq!(indexed(&handle)?, vec![fmt!("held@site.test")]); |
| 121 | Ok(()) |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | fn test_confirmed_unsubscribed_and_bounced_records_are_never_expired_02() -> Outcome<()> { |
| 126 | let (db, uid, _tmp) = res!(common::test_db()); |
| 127 | let handle = (db, uid); |
| 128 | let now = rate::now_secs(); |
| 129 | let ok = res!(sent_pending(&handle, "ok@site.test", now, 30 * DAY)); |
| 130 | let _ = res!(subscribe::confirm(&handle, &ok.token, "test")); |
| 131 | res!(sent_pending(&handle, "left@site.test", now, 30 * DAY)); |
| 132 | assert!(res!(subscribe::unsubscribe_email(&handle, "left@site.test", "test"))); |
| 133 | res!(sent_pending(&handle, "bad@site.test", now, 30 * DAY)); |
| 134 | assert!(res!(subscribe::mark_bounced(&handle, "bad@site.test", "test"))); |
| 135 | |
| 136 | let swept = outbox::sweep(&handle, &cfg(), now, "test", true); |
| 137 | |
| 138 | assert_eq!(swept.pending, 0, "the sweep removed {} settled records", swept.pending); |
| 139 | assert_eq!(state_of(&handle, "ok@site.test")?, Some(SubState::Confirmed)); |
| 140 | assert_eq!(state_of(&handle, "left@site.test")?, Some(SubState::Unsubscribed)); |
| 141 | assert_eq!(state_of(&handle, "bad@site.test")?, Some(SubState::Bounced)); |
| 142 | assert_eq!(indexed(&handle)?.len(), 3, "the index lost a settled record"); |
| 143 | Ok(()) |
| 144 | } |
| 145 | |
| 146 | #[test] |
| 147 | fn test_a_rate_row_past_its_window_is_deleted_and_a_live_one_kept_03() -> Outcome<()> { |
| 148 | let (db, uid, _tmp) = res!(common::test_db()); |
| 149 | let handle = (db, uid); |
| 150 | let now = rate::now_secs(); |
| 151 | let cfg = cfg(); |
| 152 | let sender = Window::hourly(cfg.subscribe_rate_secs, cfg.subscribe_rate_hourly); |
| 153 | let comment_w = Window::hourly(cfg.comment_rate_secs, cfg.comment_rate_hourly); |
| 154 | let recipient = cfg.confirm_window(); |
| 155 | // (key, window it was counted in, how long ago it was last counted) |
| 156 | let rows = [ |
| 157 | (fmt!("{}old", subscribe::RATE_PREFIX), sender, 2 * 3600), |
| 158 | (fmt!("{}new", subscribe::RATE_PREFIX), sender, 10), |
| 159 | (fmt!("{}old", subscribe::TO_PREFIX), recipient, 31 * DAY), |
| 160 | (fmt!("{}new", subscribe::TO_PREFIX), recipient, 29 * DAY), |
| 161 | (fmt!("{}old", comment::RATE_PREFIX), comment_w, 2 * 3600), |
| 162 | ]; |
| 163 | for (key, w, ago) in &rows { |
| 164 | assert!(res!(rate::allow_at(&handle, key, w, now - ago)), "row {} was refused", key); |
| 165 | } |
| 166 | // A key under no rate prefix is not the sweep's to touch. |
| 167 | let other = fmt!("publish/subscribe-rates/old"); |
| 168 | assert!(res!(rate::allow_at(&handle, &other, &sender, now - 400 * DAY))); |
| 169 | |
| 170 | let swept = outbox::sweep(&handle, &cfg, now, "test", true); |
| 171 | |
| 172 | assert_eq!(swept.rows, 3, "the sweep removed {} rows, not the three that had run out", swept.rows); |
| 173 | for (key, _, ago) in &rows { |
| 174 | let spent = *ago == 2 * 3600 || *ago == 31 * DAY; |
| 175 | assert_eq!(row_exists(&handle, key)?, !spent, "row {} ({}s old) was wrongly {}", |
| 176 | key, ago, if spent { "kept" } else { "removed" }); |
| 177 | } |
| 178 | assert!(row_exists(&handle, &other)?, "a key outside the rate prefixes was removed"); |
| 179 | Ok(()) |
| 180 | } |
| 181 | |
| 182 | // A3: a pending record that was never sent a confirmation expires from its creation while the host is |
| 183 | // sending; while it is held (a ceiling of 0) nothing expires, which test 01 above shows. |
| 184 | #[test] |
| 185 | fn test_a_pending_sign_up_never_sent_a_confirmation_expires_from_creation_while_sending_04() -> Outcome<()> { |
| 186 | let (db, uid, _tmp) = res!(common::test_db()); |
| 187 | let handle = (db, uid); |
| 188 | let now = rate::now_secs(); |
| 189 | res!(subscribe::add_pending(&handle, "legacy@site.test", &Window::default(), now)); |
| 190 | |
| 191 | let swept = outbox::sweep(&handle, &cfg(), now + 3 * DAY, "test", true); |
| 192 | assert_eq!(swept.pending, 0, "a record three days old expired"); |
| 193 | let swept = outbox::sweep(&handle, &cfg(), now + 8 * DAY, "test", true); |
| 194 | |
| 195 | assert_eq!(swept.pending, 1, "a never-sent sign-up eight days old was kept while sending was open"); |
| 196 | assert_eq!(state_of(&handle, "legacy@site.test")?, None); |
| 197 | Ok(()) |
| 198 | } |