//! The web surface (§12, §13): routes render, the CSRF gate holds, and a stale //! approval comes back as a 409 with the current diff instead of a silent //! retarget. //! //! These drive a real `tiny_http` server on an ephemeral loopback port over a //! real fixture corpus — no mocked transport, because the transport is part of //! what we are claiming works. 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, 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 { 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"); web.open_review("plain-make"); for path in [ "/", "/reviews", "/reviews?state=pending", "/subscriptions", "/attention", "/packages", "/packages/plain-make", "/builds", "/audit", "/settings", ] { let page = web.get(path); assert_eq!(page.status, 200, "{path} did not render"); assert!(page.body.contains(""), "{path} is not a page"); assert!(page.body.contains("aur-build"), "{path} has no chrome"); } } #[test] fn the_review_detail_shows_the_panels_and_the_hashes() { let web = Web::new("plain-make"); // Publish a first version so the review has something to diff against; // every panel compares the accepted release to the candidate (§6). let first = web.open_review("plain-make"); let page = web.get(&format!("/reviews/{}", first.id)); let dd = extract(&page.body, "name=\"diff_digest\" value=\"", "\"").unwrap(); web.post( &format!("/reviews/{}/approve", first.id), &[("csrf", &web.csrf), ("identity_hash", &first.identity_hash), ("diff_digest", &dd)], ); let id = aur_build::pipeline::enqueue_for_candidate(&web.env.cfg, &web.env.db, &first).unwrap(); aur_build::pipeline::run(&web.env.cfg, &web.env.db, &id).unwrap(); web.fx.bump(&[("pkgver", "2.0")]); let c = web.open_review("plain-make"); let page = web.get(&format!("/reviews/{}", c.id)); assert_eq!(page.status, 200); // The panel set (§6), all comparing accepted → candidate. for want in ["Recipe diff", "Version delta", "Source digest delta", "Dependency delta", "Build-env delta"] { assert!(page.body.contains(want), "missing panel {want}"); } assert!(page.body.contains("previous accepted release"), "no base shown"); // The approve form carries both hashes (§7.1a) and is a plain POST form, // so it works with JavaScript switched off. assert!(page.body.contains("name=\"identity_hash\"")); assert!(page.body.contains("name=\"diff_digest\"")); assert!(page.body.contains("