diff options
Diffstat (limited to 'tests/http.rs')
| -rw-r--r-- | tests/http.rs | 110 |
1 files changed, 4 insertions, 106 deletions
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), &[ |
