Skip to content

Commit 46cd5c7

Browse files
committed
test(auth): an alert a newer release raised reads as one this release can't show
The alerts table stores each alert's payload version, as events do, so a release that meets a newer one reads it as unknown instead of failing. Nothing tested it. Now a test stores an alert at a version this release doesn't write, and checks that it reads as unknown, and that raising its kind again clears it without an event and raises what this release found.
1 parent fff6408 commit 46cd5c7

3 files changed

Lines changed: 82 additions & 0 deletions

File tree

‎apps/auth-service/src/alerts/tests.rs‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,50 @@ async fn an_acknowledged_alert_stays_quiet_until_its_condition_changes() {
198198
]
199199
);
200200
}
201+
202+
/// An alert a newer release raised, at a payload version this one does not
203+
/// write, reads as one it cannot show. Raising its kind again clears it without
204+
/// an event, which this release could not write, and raises what this release
205+
/// found.
206+
#[tokio::test]
207+
#[ignore = "requires TEST_DATABASE_URL"]
208+
async fn an_alert_of_a_newer_version_reads_as_unknown() {
209+
let database = Harness::isolated().await.database;
210+
found(&database, Some(broken(5))).await;
211+
let current = async || {
212+
let mut tx = database.begin().await.expect("transaction");
213+
let current = tx
214+
.alerts()
215+
.current(AlertKind::AuditChainBroken)
216+
.await
217+
.expect("current")
218+
.expect("current alert");
219+
tx.commit().await.expect("commit");
220+
current
221+
};
222+
let newer = current().await;
223+
database
224+
.alerts()
225+
.set_version(newer.id, i16::MAX)
226+
.await
227+
.expect("version");
228+
assert_eq!(current().await.alert, None);
229+
230+
found(&database, Some(broken(5))).await;
231+
let raised = current().await;
232+
assert_ne!(raised.id, newer.id);
233+
assert_eq!(raised.alert, Some(broken(5)));
234+
assert_eq!(
235+
recorded(&database).await,
236+
[
237+
DomainEvent::AlertRaised {
238+
alert_id: newer.id,
239+
alert: broken(5)
240+
},
241+
DomainEvent::AlertRaised {
242+
alert_id: raised.id,
243+
alert: broken(5)
244+
},
245+
]
246+
);
247+
}

‎crates/database/.sqlx/query-3b54e72c54f1a704bb26f06ada11d7ecb9cbd8d97e70288451b53ac5b9913d69.json‎

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/database/src/repositories/alerts.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,24 @@ impl AlertRepository<'_> {
217217
)?;
218218
rows.into_iter().map(StoredAlert::try_from).collect()
219219
}
220+
221+
/// Stores alert `id` as written at payload `version`, as a newer release
222+
/// writing a payload this one does not would: a fixture for reading one.
223+
///
224+
/// # Errors
225+
///
226+
/// Returns [`DatabaseError`] if the update fails.
227+
#[cfg(any(test, feature = "test-util"))]
228+
pub async fn set_version(&mut self, id: AlertId, version: i16) -> Result<(), DatabaseError> {
229+
run!(
230+
self,
231+
execute,
232+
sqlx::query!(
233+
"UPDATE alerts SET version = $2 WHERE id = $1",
234+
id.as_uuid(),
235+
version,
236+
)
237+
)?;
238+
Ok(())
239+
}
220240
}

0 commit comments

Comments
 (0)