diff options
Diffstat (limited to 'tests/approve.rs')
| -rw-r--r-- | tests/approve.rs | 77 |
1 files changed, 72 insertions, 5 deletions
diff --git a/tests/approve.rs b/tests/approve.rs index 5a515eb..61e42c2 100644 --- a/tests/approve.rs +++ b/tests/approve.rs @@ -75,7 +75,7 @@ fn a_stale_identity_hash_is_refused() { assert_ne!(now.identity_hash, c.identity_hash, "identity should have moved"); let err = approve(&env.db, "tester", c.id, &c.identity_hash, &dd, None).unwrap_err(); - assert!(matches!(err, Stale::Identity { .. }), "{err:?}"); + assert!(matches!(err, ApproveError::Stale(Stale::Identity { .. })), "{err:?}"); assert_eq!( env.query_i64("SELECT count(*) FROM approval", &[]), Some(0), @@ -92,7 +92,7 @@ fn a_stale_diff_digest_is_refused() { let (env, _fx) = env_with_fixture("plain-make"); let (c, dd) = open_review(&env, "plain-make"); let err = approve(&env.db, "tester", c.id, &c.identity_hash, "sha256:whatever", None).unwrap_err(); - assert!(matches!(err, Stale::Diff { .. }), "{err:?}"); + assert!(matches!(err, ApproveError::Stale(Stale::Diff { .. })), "{err:?}"); assert_eq!(env.query_i64("SELECT count(*) FROM approval", &[]), Some(0)); let _ = dd; } @@ -103,11 +103,78 @@ fn approving_twice_is_refused_not_duplicated() { let (c, dd) = open_review(&env, "plain-make"); approve(&env.db, "tester", c.id, &c.identity_hash, &dd, None).unwrap(); let err = approve(&env.db, "tester", c.id, &c.identity_hash, &dd, None).unwrap_err(); - assert!(matches!(err, Stale::NotPending { .. }), "{err:?}"); + assert!(matches!(err, ApproveError::Stale(Stale::NotPending { .. })), "{err:?}"); assert_eq!(env.query_i64("SELECT count(*) FROM approval", &[]), Some(1)); } #[test] +fn a_broken_database_is_not_reported_as_a_stale_review() { + // What this pins: every failure inside the approval transaction used to be + // mapped into `Stale::NotPending`, so a SQLite error reached the reviewer as + // "review is no longer pending (state: sqlite: …)" — the one message that + // tells them to look again when the truth is that the write failed (§6 M-3). + let (env, _fx) = env_with_fixture("plain-make"); + let (c, dd) = open_review(&env, "plain-make"); + // Make the write impossible halfway through the transaction: the approval + // row and the candidate update succeed, then the audit append has nowhere + // to go. + env.exec("DROP TABLE audit", &[]); + let err = approve(&env.db, "tester", c.id, &c.identity_hash, &dd, None).unwrap_err(); + assert!(matches!(err, ApproveError::System(_)), "a broken database was reported as a stale review: {err:?}"); + assert!(err.to_string().contains("could not be recorded"), "the message hides the cause: {err}"); + // And the refusal is atomic: the half-written approval rolled back with it. + assert_eq!(env.query_i64("SELECT count(*) FROM approval", &[]), Some(0)); + let after = candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap(); + assert_eq!(after.state, "pending_review", "a failed approval must not resolve the review"); +} + +#[test] +fn bulk_refusals_are_their_own_reasons() { + // Bulk used to bend `Stale::NotPending` into "high risk", "unknown decision" + // and "the database failed", and to hand back a fabricated `Approval { id: 0 }` + // for a rejection that recorded no approval at all (§6 M-3). + let (env, _fx) = env_with_fixture("plain-make"); + let (c, dd) = open_review(&env, "plain-make"); + candidates::set_risk(&env.db, c.id, "high", &["new source host".into()]).unwrap(); + let items = [(c.id, c.identity_hash.clone(), dd.clone())]; + + // High risk without the individual confirmation: the reason is the risk. + let out = bulk(&env.db, "tester", &items, "approve", &[]).unwrap(); + assert!( + matches!(out[0].outcome, BulkOutcome::Refused(BulkRefusal::HighRiskUnconfirmed)), + "{:?}", + out[0].outcome + ); + // A decision the form should never have sent is its own reason. + let out = bulk(&env.db, "tester", &items, "ship-it", &[]).unwrap(); + assert!( + matches!(out[0].outcome, BulkOutcome::Refused(BulkRefusal::UnknownDecision(_))), + "{:?}", + out[0].outcome + ); + // Confirming it individually is the answer to the first refusal. + let out = bulk(&env.db, "tester", &items, "approve", &[c.id]).unwrap(); + assert!(matches!(out[0].outcome, BulkOutcome::Approved(_)), "{:?}", out[0].outcome); + // Now it is no longer pending, and *that* is a staleness — the two reasons + // stay distinguishable in both directions. + let out = bulk(&env.db, "tester", &items, "approve", &[c.id]).unwrap(); + assert!(matches!(out[0].outcome, BulkOutcome::Refused(BulkRefusal::Stale(_))), "{:?}", out[0].outcome); + assert_eq!(env.query_i64("SELECT count(*) FROM approval", &[]), Some(1)); +} + +#[test] +fn a_bulk_rejection_hands_back_no_invented_approval() { + let (env, _fx) = env_with_fixture("plain-make"); + let (c, dd) = open_review(&env, "plain-make"); + let out = bulk(&env.db, "tester", &[(c.id, c.identity_hash.clone(), dd)], "reject", &[]).unwrap(); + assert!(matches!(out[0].outcome, BulkOutcome::Rejected), "{:?}", out[0].outcome); + // A rejection records a `rejected` row; there is no approval to return, and + // the old `Approval { id: 0 }` sentinel is gone. + assert_eq!(env.query_i64("SELECT count(*) FROM approval WHERE decision='rejected'", &[]), Some(1)); + assert_eq!(env.query_i64("SELECT count(*) FROM approval WHERE decision='approved'", &[]), Some(0)); +} + +#[test] fn collapse_in_place_bumps_revision_without_a_new_row() { let (env, fx) = env_with_fixture("plain-make"); let (c, _dd) = open_review(&env, "plain-make"); @@ -145,7 +212,7 @@ fn once_viewed_a_new_state_freezes_the_old_one_as_superseded() { // The frozen one can no longer be approved: approving it would build a // version nobody is looking at. let err = approve(&env.db, "tester", c.id, &frozen.identity_hash, &frozen.diff_digest, None).unwrap_err(); - assert!(matches!(err, Stale::NotPending { .. }), "{err:?}"); + assert!(matches!(err, ApproveError::Stale(Stale::NotPending { .. })), "{err:?}"); } #[test] @@ -158,7 +225,7 @@ fn a_rejected_identity_is_not_re_raised() { assert!(candidates::pending_candidates(&env.db).unwrap().is_empty()); // A different identity is a different question, and gets its own review. let err = approve(&env.db, "tester", c.id, &c.identity_hash, &c.diff_digest, None).unwrap_err(); - assert!(matches!(err, Stale::NotPending { .. })); + assert!(matches!(err, ApproveError::Stale(Stale::NotPending { .. }))); } #[test] |
