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/common/mod.rs | |
| 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/common/mod.rs')
| -rw-r--r-- | tests/common/mod.rs | 147 |
1 files changed, 147 insertions, 0 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("'", "'") +} |
