diff options
| author | aur-build <aur-build@localhost> | 2026-11-18 08:23:14 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-18 08:23:14 +0000 |
| commit | eefbefed9cbea6b2edef65900fbe6cb6da0b48e4 (patch) | |
| tree | 1249788a49812fc15b644687af7c1384f94ba236 /tests | |
| parent | a6b652985f40cf022d6c522415c36fad13bc663d (diff) | |
| download | aur-build-eefbefed9cbea6b2edef65900fbe6cb6da0b48e4.tar.gz aur-build-eefbefed9cbea6b2edef65900fbe6cb6da0b48e4.zip | |
http: surfaces driven by live state — staleness, downgrades, deps, rebuild (I6)
Attention, the package page and the builds page now answer real questions
instead of rendering empty tables:
- Attention lists outdated dependencies derived from the recorded world
snapshots, naming the dependency that moved, what the artifact was built
against and what the index holds now; also missing dependencies and
downgrades (AUR offering a lower version than we serve), which is an
announcement for a human, not a build request.
- The package page shows depends-on from the recipe we actually serve and
needed-by from the packages whose recipes name it, plus staleness and the
aur_commit timeline, each release linking into cgit at that exact commit.
- Builds rows carry the version they are about, in both the live and the
recently-finished table.
- A manual rebuild reuses the approved source identity and mints no approval
(I6); the test asserts the approval count is unchanged before and after the
rebuild runs.
- A paused subscription is not observed at all: detection returns Paused
without polling, so pausing a broken package stops it opening reviews.
- Staleness stays computed on read (§10.2). Nothing stores a stale flag, so
nothing can serve a stale lie.
Also: dep-client fixture added for dependency pairs.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/common/mod.rs | 147 | ||||
| -rw-r--r-- | tests/http.rs | 110 | ||||
| -rw-r--r-- | tests/surfaces.rs | 177 |
3 files changed, 328 insertions, 106 deletions
diff --git a/tests/common/mod.rs b/tests/common/mod.rs index d2582c9..8d5497e 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -228,6 +228,29 @@ pub fn env_with_fixture(name: &str) -> (Env, Fixture) { (env, fx) } +/// Two fixtures sharing one server root, so a test can talk about packages that +/// depend on each other. The second fixture's manifest wins for the shim, which +/// is what you want when the second one is the dependent. +pub fn env_with_two_fixtures(a: &str, b: &str) -> (Env, Fixture, Fixture) { + let tmp = tempfile::tempdir().expect("tempdir"); + let fa = materialize_fixture(tmp.path(), a); + let fb = materialize_fixture(tmp.path(), b); + let cfg = Config { + root: tmp.path().to_path_buf(), + signing_key_id: "TESTKEY".into(), + path_prepend: Some(shim_dir()), + shim_manifest: Some(fb.manifest.clone()), + shim_dir: Some(tmp.path().join("shimlog")), + ..Config::default() + }; + std::fs::create_dir_all(cfg.shim_dir.as_ref().unwrap()).unwrap(); + cfg.ensure_dirs().unwrap(); + let env = Env::with_cfg(tmp, cfg); + env.subscribe(a); + env.subscribe(b); + (env, fa, fb) +} + /// Copy a corpus fixture into a server root: recipe files into a git repo that /// stands in for AUR, payload files beside it so their bytes can be swapped, /// and a local upstream repo when the recipe references one. @@ -421,3 +444,127 @@ pub fn git(dir: &Path, args: &[&str]) -> String { ); String::from_utf8_lossy(&out.stdout).trim().to_string() } + +// ------------------------------------------------------------- web driver -- + +/// A tiny HTTP client for the surface tests: keeps the session cookie and the +/// CSRF token, and hands back status + body. +pub struct Web { + pub env: Env, + pub fx: Fixture, + #[allow(dead_code)] + pub server: aur_build::http::Server, + pub cookie: String, + pub csrf: String, +} + +pub struct Page { + pub status: u16, + pub body: String, +} + +impl Web { + pub fn new(name: &str) -> Self { + Self::with_password(name, "") + } + + pub fn with_password(name: &str, password: &str) -> Self { + let (mut env, fx) = env_with_fixture(name); + env.cfg.password = password.into(); + Self::attach(env, fx) + } + + /// Start a server on an env the test already built (two fixtures, odd + /// configs) and pick up the session it hands out. + pub fn attach(mut env: Env, fx: Fixture) -> Self { + env.cfg.bind = "127.0.0.1:0".into(); + let server = aur_build::http::spawn(env.cfg.clone(), env.db.clone()).unwrap(); + let mut web = Self { env, fx, server, cookie: String::new(), csrf: String::new() }; + let first = web.raw_get("/"); + web.cookie = first + .0 + .iter() + .find(|(k, _)| k == "set-cookie") + .and_then(|(_, v)| v.split(';').next().map(|s| s.to_string())) + .unwrap_or_default(); + let body = web.get("/"); + web.csrf = extract(&body.body, "name=\"csrf\" content=\"", "\"").unwrap_or_default(); + web + } + + /// Subscribe, detect and render a review so an approval can be attempted. + pub fn open_review(&self, name: &str) -> aur_build::db::model::CandidateRow { + aur_build::watch::detect::check_pkgbase(&self.env.cfg, &self.env.db, name).unwrap(); + let c = aur_build::review::candidates::pending_candidates(&self.env.db).unwrap().remove(0); + aur_build::review::diff::render_and_store(&self.env.db, &self.env.cfg, &c).unwrap(); + aur_build::review::candidates::candidate_by_id(&self.env.db, c.id).unwrap().unwrap() + } + + /// Approve through the library and run the build to publication, so a test + /// can talk about "what the index serves" without re-implementing P0. + pub fn publish(&self, name: &str) { + let c = self.open_review(name); + aur_build::review::approve::approve(&self.env.db, "test", c.id, &c.identity_hash, &c.diff_digest, None) + .unwrap(); + let c = aur_build::review::candidates::candidate_by_id(&self.env.db, c.id).unwrap().unwrap(); + let id = aur_build::pipeline::enqueue_for_candidate(&self.env.cfg, &self.env.db, &c).unwrap(); + aur_build::pipeline::run(&self.env.cfg, &self.env.db, &id).unwrap(); + } + + pub fn url(&self, path: &str) -> String { + format!("http://{}{}", self.server.addr, path) + } + + pub fn raw_get(&self, path: &str) -> (Vec<(String, String)>, String) { + let resp = ureq::get(&self.url(path)).set("Cookie", &self.cookie).call(); + let r = match resp { + Ok(r) => r, + Err(ureq::Error::Status(_, r)) => r, + Err(e) => panic!("GET {path}: {e}"), + }; + let cookie = r.header("set-cookie").map(|s| s.to_string()).unwrap_or_default(); + (vec![("set-cookie".to_string(), cookie)], r.into_string().unwrap_or_default()) + } + + pub fn get(&self, path: &str) -> Page { + let resp = ureq::get(&self.url(path)).set("Cookie", &self.cookie).call(); + self.finish(resp, path) + } + + pub fn post(&self, path: &str, form: &[(&str, &str)]) -> Page { + let resp = ureq::post(&self.url(path)).set("Cookie", &self.cookie).send_form(form); + self.finish(resp, path) + } + + /// POST with exactly the fields given and nothing added — used to prove the + /// CSRF gate refuses a form that omitted the token. + pub fn post_bare(&self, path: &str, form: &[(&str, &str)]) -> Page { + let resp = ureq::post(&self.url(path)).set("Cookie", &self.cookie).send_form(form); + self.finish(resp, path) + } + + fn finish(&self, resp: Result<ureq::Response, ureq::Error>, path: &str) -> Page { + match resp { + Ok(r) => Page { status: r.status(), body: r.into_string().unwrap_or_default() }, + Err(ureq::Error::Status(code, r)) => Page { status: code, body: r.into_string().unwrap_or_default() }, + Err(e) => panic!("{path}: {e}"), + } + } +} + +/// Pull an attribute or meta value out of rendered HTML and undo the escaping +/// the templates applied — a page is the contract here, so we read it as one. +pub fn extract(hay: &str, open: &str, close: &str) -> Option<String> { + let i = hay.find(open)? + open.len(); + let rest = &hay[i..]; + let j = rest.find(close)?; + Some(unescape(&rest[..j])) +} + +fn unescape(s: &str) -> String { + s.replace("&", "&") + .replace(""", "\"") + .replace("<", "<") + .replace(">", ">") + .replace("'", "'") +} diff --git a/tests/http.rs b/tests/http.rs index 20dcbcb..d5387dc 100644 --- a/tests/http.rs +++ b/tests/http.rs @@ -8,109 +8,9 @@ mod common; -use aur_build::db::model::*; -use aur_build::http::{self, Server}; use aur_build::review::{candidates, diff}; use common::*; -struct Web { - env: Env, - fx: Fixture, - #[allow(dead_code)] - server: Server, - cookie: String, - csrf: String, -} - -struct Page { - status: u16, - body: String, -} - -impl Web { - fn new(name: &str) -> Self { - Self::with_password(name, "") - } - - fn with_password(name: &str, password: &str) -> Self { - let (mut env, fx) = env_with_fixture(name); - env.cfg.bind = "127.0.0.1:0".into(); - env.cfg.password = password.into(); - let server = http::spawn(env.cfg.clone(), env.db.clone()).unwrap(); - let mut web = Self { env, fx, server, cookie: String::new(), csrf: String::new() }; - // First contact mints the session cookie and the CSRF token. - let first = web.raw_get("/"); - web.cookie = first - .0 - .iter() - .find(|(k, _)| k == "set-cookie") - .and_then(|(_, v)| v.split(';').next().map(|s| s.to_string())) - .unwrap_or_default(); - let body = web.get("/"); - web.csrf = extract(&body.body, "name=\"csrf\" content=\"", "\"").unwrap_or_default(); - web - } - - fn url(&self, path: &str) -> String { - format!("http://{}{}", self.server.addr, path) - } - - fn raw_get(&self, path: &str) -> (Vec<(String, String)>, String) { - let resp = ureq::get(&self.url(path)).set("Cookie", &self.cookie).call(); - let r = match resp { - Ok(r) => r, - Err(ureq::Error::Status(_, r)) => r, - Err(e) => panic!("GET {path}: {e}"), - }; - let cookie = r.header("set-cookie").map(|s| s.to_string()).unwrap_or_default(); - (vec![("set-cookie".to_string(), cookie)], r.into_string().unwrap_or_default()) - } - - fn get(&self, path: &str) -> Page { - let resp = ureq::get(&self.url(path)).set("Cookie", &self.cookie).call(); - self.finish(resp, path) - } - - fn post(&self, path: &str, form: &[(&str, &str)]) -> Page { - let resp = ureq::post(&self.url(path)).set("Cookie", &self.cookie).send_form(form); - self.finish(resp, path) - } - - fn finish(&self, resp: Result<ureq::Response, ureq::Error>, path: &str) -> Page { - match resp { - Ok(r) => Page { status: r.status(), body: r.into_string().unwrap_or_default() }, - Err(ureq::Error::Status(code, r)) => Page { status: code, body: r.into_string().unwrap_or_default() }, - Err(e) => panic!("{path}: {e}"), - } - } - - /// Subscribe, detect and render a review so an approval can be attempted. - fn open_review(&self, name: &str) -> CandidateRow { - aur_build::watch::detect::check_pkgbase(&self.env.cfg, &self.env.db, name).unwrap(); - let c = candidates::pending_candidates(&self.env.db).unwrap().remove(0); - let dd = diff::render_and_store(&self.env.db, &self.env.cfg, &c).unwrap(); - let _ = dd; - candidates::candidate_by_id(&self.env.db, c.id).unwrap().unwrap() - } -} - -fn extract(hay: &str, open: &str, close: &str) -> Option<String> { - let i = hay.find(open)? + open.len(); - let rest = &hay[i..]; - let j = rest.find(close)?; - Some(unescape(&rest[..j])) -} - -/// Templates escape attribute values; a test comparing one back to a Rust -/// string has to undo that. -fn unescape(s: &str) -> String { - s.replace("&", "&") - .replace(""", "\"") - .replace("<", "<") - .replace(">", ">") - .replace("'", "'") -} - #[test] fn every_surface_renders() { let web = Web::new("plain-make"); @@ -192,12 +92,10 @@ fn every_post_requires_a_csrf_token() { let web = Web::new("plain-make"); let c = web.open_review("plain-make"); // A form post with no token at all is refused outright. - let page = { - let resp = ureq::post(&web.url(&format!("/reviews/{}/approve", c.id))) - .set("Cookie", &web.cookie) - .send_form(&[("identity_hash", "x"), ("diff_digest", "y")]); - web.finish(resp, "approve") - }; + let page = web.post_bare( + &format!("/reviews/{}/approve", c.id), + &[("identity_hash", "x"), ("diff_digest", "y")], + ); assert_eq!(page.status, 403, "a POST without CSRF must be refused"); // And so is one with a token from someone else's session. let page = web.post(&format!("/reviews/{}/approve", c.id), &[ diff --git a/tests/surfaces.rs b/tests/surfaces.rs new file mode 100644 index 0000000..659123f --- /dev/null +++ b/tests/surfaces.rs @@ -0,0 +1,177 @@ +//! The §13 surfaces driven by live state, not fixtures of HTML: Attention must +//! name outdated dependencies, downgrades and blocked builds; the package page +//! must answer "what does this depend on and what depends on it"; and a manual +//! rebuild must reuse the approved source identity without asking again (I6). + +mod common; + +use aur_build::db::{self, builds, model::*}; +use aur_build::pipeline; +use aur_build::review::candidates; +use common::*; + +#[test] +fn attention_lists_a_downgrade_when_aur_moves_below_what_we_serve() { + let web = Web::new("plain-make"); + web.publish("plain-make"); + assert!(!web.get("/attention").body.contains("Downgrades"), "nothing is stale yet"); + + // The maintainer reverts their version bump. We still serve the higher one. + web.fx.bump(&[("pkgver", "0.9")]); + aur_build::watch::detect::check_pkgbase(&web.env.cfg, &web.env.db, "plain-make").unwrap(); + + let page = web.get("/attention"); + assert_eq!(page.status, 200); + assert!(page.body.contains("Downgrade"), "no downgrade section: {}", &page.body[..400.min(page.body.len())]); + assert!(page.body.contains("0.9"), "the offered version is not shown"); + assert!(page.body.contains("1.0"), "the served version is not shown"); +} + +#[test] +fn attention_lists_outdated_dependencies_from_the_recorded_world() { + let (env, plain, client) = env_with_two_fixtures("plain-make", "dep-client"); + let web = Web::attach(env, client); + web.publish("plain-make"); + web.publish("dep-client"); + assert!( + !web.get("/attention").body.contains("Outdated:"), + "dep-client is fresh right after both were published" + ); + + // plain-make moves; dep-client was built against the old one. + plain.bump(&[("pkgver", "2.0")]); + web.publish("plain-make"); + + let page = web.get("/attention"); + assert!(page.body.contains("Outdated:"), "no outdated section: {}", &page.body[..600.min(page.body.len())]); + assert!(page.body.contains("dep-client"), "the stale package is not named"); + assert!(page.body.contains("plain-make"), "the dependency is not named"); + assert!(page.body.contains("1.0") && page.body.contains("2.0"), "the built-against/now pair is missing"); +} + +#[test] +fn the_package_page_answers_dependencies_and_staleness() { + let (env, plain, client) = env_with_two_fixtures("plain-make", "dep-client"); + let web = Web::attach(env, client); + web.publish("plain-make"); + web.publish("dep-client"); + plain.bump(&[("pkgver", "2.0")]); + web.publish("plain-make"); + + let page = web.get("/packages/dep-client"); + assert_eq!(page.status, 200); + assert!(page.body.contains("Depends on"), "no depends block"); + assert!(page.body.contains("plain-make"), "the dependency is not listed"); + assert!(page.body.contains("Stale"), "staleness is not shown on the package page"); + + // And from the other side: what would a change to plain-make disturb? + let other = web.get("/packages/plain-make"); + assert!(other.body.contains("Needed by"), "no needed-by block"); + assert!(other.body.contains("dep-client"), "the dependent is not listed"); + // The timeline is the aur_commit history: one row per release, each linking + // into AUR's cgit at that exact commit. + assert!(other.body.contains("aur.archlinux.org/cgit/aur.git/tree/?h=plain-make&id="), "no per-release cgit link"); + assert!(other.body.contains("serving 2.0"), "the summary does not say what is served"); +} + +#[test] +fn a_manual_rebuild_reuses_the_approved_identity_without_a_new_approval() { + // I6: rebuilding what is already approved is not a new decision. It must + // not create an approval, and it must not need one. + let web = Web::new("plain-make"); + web.publish("plain-make"); + let approvals_before = web.env.query_i64("SELECT count(*) FROM approval", &[]).unwrap(); + assert_eq!(approvals_before, 1); + + let res = web.post("/packages/plain-make/rebuild", &[("csrf", &web.csrf)]); + assert_eq!(res.status, 200, "rebuild should be accepted: {}", &res.body[..200.min(res.body.len())]); + assert!(res.body.contains("no new approval"), "the confirmation does not say why no approval is needed"); + assert_eq!( + web.env.query_i64("SELECT count(*) FROM approval", &[]).unwrap(), + approvals_before, + "a manual rebuild must not mint an approval" + ); + + let queued = web + .env + .query_i64("SELECT count(*) FROM build WHERE kind='manual_rebuild'", &[]) + .unwrap(); + assert_eq!(queued, 1, "the rebuild was not queued"); + + // Run it: it publishes the same version, still on the one approval. + let id: String = web + .env + .db + .read(|c| { + use rusqlite::OptionalExtension; + c.query_row( + "SELECT id FROM build WHERE kind='manual_rebuild'", + [], + |r| r.get::<_, String>(0), + ) + .optional() + .map_err(Into::into) + }) + .unwrap() + .unwrap(); + pipeline::run(&web.env.cfg, &web.env.db, &id).unwrap(); + let b = builds::get(&web.env.db, &id).unwrap().unwrap(); + assert_eq!(b.state, build_state::SUCCEEDED, "rebuild failed: {:?}", b.error); + assert_eq!( + web.env.query_i64("SELECT count(*) FROM approval", &[]).unwrap(), + approvals_before, + "running a rebuild must not mint an approval either" + ); + assert_eq!(db::index_version(&web.env.db, "plain-make").unwrap().as_deref(), Some("1.0-1")); +} + +#[test] +fn the_builds_page_names_the_version_and_the_audit_page_records_the_decision() { + let web = Web::new("plain-make"); + web.publish("plain-make"); + let page = web.get("/builds"); + assert_eq!(page.status, 200); + assert!(page.body.contains("plain-make"), "the build row has no pkgbase"); + assert!(page.body.contains("1.0-1"), "the build row has no version: {}", &page.body[..600.min(page.body.len())]); + assert!(page.body.contains("succeeded") || page.body.contains("Succeeded"), "the finished build is not shown"); + + let audit = web.get("/audit"); + assert!(audit.body.contains("approve"), "the approval is not in the audit log"); + assert!(audit.body.contains("test") || audit.body.contains("cli"), "no actor recorded"); +} + +#[test] +fn the_subscriptions_page_shows_state_and_last_check() { + let web = Web::new("plain-make"); + web.publish("plain-make"); + let page = web.get("/subscriptions"); + assert_eq!(page.status, 200); + assert!(page.body.contains("plain-make"), "the subscription is not listed"); + assert!(page.body.contains("active"), "no state shown"); + // Pause and remove are per-subscription POSTs, not links that mutate. + assert!(page.body.contains("action=\"/subscriptions/pause\""), "no pause form"); + assert!(page.body.contains("action=\"/subscriptions/remove\""), "no remove form"); +} + +#[test] +fn pausing_a_subscription_stops_detection_and_resuming_starts_it_again() { + let web = Web::new("plain-make"); + web.post("/subscriptions/pause", &[("csrf", &web.csrf), ("pkgbase", "plain-make")]); + assert_eq!( + web.env.query_i64("SELECT count(*) FROM subscription WHERE state='paused'", &[]), + Some(1), + "pause did not take" + ); + // A paused pkgbase is not re-detected even when upstream moves. + web.fx.bump(&[("pkgver", "3.0")]); + let report = aur_build::watch::detect::check_pkgbase(&web.env.cfg, &web.env.db, "plain-make").unwrap(); + assert!(matches!(report.outcome, aur_build::watch::detect::Outcome::Paused), "a paused subscription was still inspected"); + assert_eq!( + candidates::pending_candidates(&web.env.db).unwrap().len(), + 0, + "a paused subscription produced a candidate" + ); + web.post("/subscriptions/pause", &[("csrf", &web.csrf), ("pkgbase", "plain-make")]); + aur_build::watch::detect::check_pkgbase(&web.env.cfg, &web.env.db, "plain-make").unwrap(); + assert_eq!(candidates::pending_candidates(&web.env.db).unwrap().len(), 1, "resuming did not restore detection"); +} |
