E2e tests share one http harness instead of 28 copies
Browse files at this commit03e747925036c3eb45cdc42cd177ae21013f5d0cParent: 177e570- refactor
- e2e tests share one http harness instead of 28 copies
27 changed files +371 -2643
- crates/forge-web/tests/e2e/account_mail_e2e.rs +2 -55
- crates/forge-web/tests/e2e/advisories_e2e.rs +2 -89
- crates/forge-web/tests/e2e/branch_protection_e2e.rs +3 -110
- crates/forge-web/tests/e2e/ci_e2e.rs +4 -110
- crates/forge-web/tests/e2e/common/http.rs +176 -0
- crates/forge-web/tests/e2e/common/mod.rs +4 -0
- crates/forge-web/tests/e2e/discussions_e2e.rs +10 -103
- crates/forge-web/tests/e2e/git_e2e.rs +8 -139
- crates/forge-web/tests/e2e/i18n_e2e.rs +15 -109
- crates/forge-web/tests/e2e/issue_close_e2e.rs +4 -103
- crates/forge-web/tests/e2e/lfs_e2e.rs +2 -103
- crates/forge-web/tests/e2e/merge_queue_e2e.rs +4 -111
- crates/forge-web/tests/e2e/mirrors_e2e.rs +2 -100
- crates/forge-web/tests/e2e/orgs_core_e2e.rs +3 -76
- crates/forge-web/tests/e2e/orgs_live_e2e.rs +8 -127
- crates/forge-web/tests/e2e/orgs_ui_e2e.rs +16 -117
- crates/forge-web/tests/e2e/pulls_e2e.rs +10 -108
- crates/forge-web/tests/e2e/repo_collaborators_e2e.rs +9 -108
- crates/forge-web/tests/e2e/repo_rename_e2e.rs +8 -108
- crates/forge-web/tests/e2e/repo_settings_e2e.rs +14 -113
- crates/forge-web/tests/e2e/repo_transfer_e2e.rs +8 -108
- crates/forge-web/tests/e2e/section_nav_e2e.rs +3 -107
- crates/forge-web/tests/e2e/snippets_e2e.rs +14 -110
- crates/forge-web/tests/e2e/stars_e2e.rs +8 -109
- crates/forge-web/tests/e2e/ui_prefs_e2e.rs +15 -117
- crates/forge-web/tests/e2e/user_delete_e2e.rs +9 -94
- crates/forge-web/tests/e2e/wiki_e2e.rs +10 -109
crates/forge-web/tests/e2e/account_mail_e2e.rsmodified+2 -55
| @@ -2,72 +2,19 @@ | |||
| 2 | //! tests, this starts the real `forge` binary. The log transport writes each mail to | 2 | //! tests, this starts the real `forge` binary. The log transport writes each mail to |
| 3 | //! `<data_dir>/mail-log/`, which is where the tests read the reset link from. | 3 | //! `<data_dir>/mail-log/`, which is where the tests read the reset link from. |
| 4 | 4 | ||
| 5 | -use crate::common::{launch_with, pick_ports}; | 5 | +use crate::common::{Http, Resp, launch_with, pick_ports}; |
| 6 | use std::fs; | 6 | use std::fs; |
| 7 | use std::path::{Path, PathBuf}; | 7 | use std::path::{Path, PathBuf}; |
| 8 | use std::time::Duration; | 8 | use std::time::Duration; |
| 9 | 9 | ||
| 10 | use forge_core::Store; | 10 | use forge_core::Store; |
| 11 | use forge_core::password::hash_password; | 11 | use forge_core::password::hash_password; |
| 12 | -use reqwest::header::{COOKIE, HeaderMap, SET_COOKIE}; | 12 | +use reqwest::header::SET_COOKIE; |
| 13 | 13 | ||
| 14 | const LOG_TRANSPORT: &str = "\n[mail]\ntransport = \"log\"\n"; | 14 | const LOG_TRANSPORT: &str = "\n[mail]\ntransport = \"log\"\n"; |
| 15 | const OLD_PW: &str = "tahinli-old-password-1"; | 15 | const OLD_PW: &str = "tahinli-old-password-1"; |
| 16 | const NEW_PW: &str = "tahinli-new-password-2"; | 16 | const NEW_PW: &str = "tahinli-new-password-2"; |
| 17 | 17 | ||
| 18 | -struct Resp { | ||
| 19 | - status: u16, | ||
| 20 | - headers: HeaderMap, | ||
| 21 | - body: String, | ||
| 22 | -} | ||
| 23 | - | ||
| 24 | -struct Http { | ||
| 25 | - client: reqwest::Client, | ||
| 26 | - base: String, | ||
| 27 | -} | ||
| 28 | - | ||
| 29 | -impl Http { | ||
| 30 | - fn new(port: u16) -> Self { | ||
| 31 | - let client = reqwest::Client::builder() | ||
| 32 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 33 | - .timeout(Duration::from_secs(60)) | ||
| 34 | - .build() | ||
| 35 | - .expect("http client"); | ||
| 36 | - Self { | ||
| 37 | - client, | ||
| 38 | - base: format!("http://127.0.0.1:{port}"), | ||
| 39 | - } | ||
| 40 | - } | ||
| 41 | - | ||
| 42 | - async fn get(&self, path: &str, cookie: Option<&str>) -> Resp { | ||
| 43 | - let mut req = self.client.get(format!("{}{path}", self.base)); | ||
| 44 | - if let Some(cookie) = cookie { | ||
| 45 | - req = req.header(COOKIE, cookie); | ||
| 46 | - } | ||
| 47 | - Self::finish(req).await | ||
| 48 | - } | ||
| 49 | - | ||
| 50 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 51 | - let mut req = self.client.post(format!("{}{path}", self.base)).form(form); | ||
| 52 | - if let Some(cookie) = cookie { | ||
| 53 | - req = req.header(COOKIE, cookie); | ||
| 54 | - } | ||
| 55 | - Self::finish(req).await | ||
| 56 | - } | ||
| 57 | - | ||
| 58 | - async fn finish(req: reqwest::RequestBuilder) -> Resp { | ||
| 59 | - let res = req.send().await.expect("http request"); | ||
| 60 | - let status = res.status().as_u16(); | ||
| 61 | - let headers = res.headers().clone(); | ||
| 62 | - let body = res.text().await.expect("read body"); | ||
| 63 | - Resp { | ||
| 64 | - status, | ||
| 65 | - headers, | ||
| 66 | - body, | ||
| 67 | - } | ||
| 68 | - } | ||
| 69 | -} | ||
| 70 | - | ||
| 71 | /// The `name=value` pair of the Set-Cookie header whose name starts with `prefix`. | 18 | /// The `name=value` pair of the Set-Cookie header whose name starts with `prefix`. |
| 72 | fn cookie_named(resp: &Resp, prefix: &str) -> Option<String> { | 19 | fn cookie_named(resp: &Resp, prefix: &str) -> Option<String> { |
| 73 | resp.headers | 20 | resp.headers |
crates/forge-web/tests/e2e/advisories_e2e.rsmodified+2 -89
| @@ -9,7 +9,8 @@ | |||
| 9 | //! - a reader without Write gets a 404 on the page. | 9 | //! - a reader without Write gets a 404 on the page. |
| 10 | 10 | ||
| 11 | use crate::common::{ | 11 | use crate::common::{ |
| 12 | - BIN, Server, input_value, launch_with, pick_ports, tool_on_path, write_config_with, | 12 | + BIN, Http, Resp, Server, input_value, launch_with, login, pick_ports, tool_on_path, |
| 13 | + write_config_with, | ||
| 13 | }; | 14 | }; |
| 14 | use std::fs; | 15 | use std::fs; |
| 15 | use std::io::{Cursor, Write}; | 16 | use std::io::{Cursor, Write}; |
| @@ -37,73 +38,6 @@ const OSV_RECORD: &str = r#"{ | |||
| 37 | const LOCK_OLD: &str = "version = 4\n\n[[package]]\nname = \"demo\"\nversion = \"1.0.0\"\nsource = \"registry+https://github.com/rust-lang/crates.io-index\"\n"; | 38 | const LOCK_OLD: &str = "version = 4\n\n[[package]]\nname = \"demo\"\nversion = \"1.0.0\"\nsource = \"registry+https://github.com/rust-lang/crates.io-index\"\n"; |
| 38 | const LOCK_FIXED: &str = "version = 4\n\n[[package]]\nname = \"demo\"\nversion = \"2.0.0\"\nsource = \"registry+https://github.com/rust-lang/crates.io-index\"\n"; | 39 | const LOCK_FIXED: &str = "version = 4\n\n[[package]]\nname = \"demo\"\nversion = \"2.0.0\"\nsource = \"registry+https://github.com/rust-lang/crates.io-index\"\n"; |
| 39 | 40 | ||
| 40 | -struct Resp { | ||
| 41 | - status: u16, | ||
| 42 | - set_cookies: Vec<String>, | ||
| 43 | - body: String, | ||
| 44 | -} | ||
| 45 | - | ||
| 46 | -struct Http { | ||
| 47 | - client: reqwest::Client, | ||
| 48 | - base: String, | ||
| 49 | -} | ||
| 50 | - | ||
| 51 | -impl Http { | ||
| 52 | - fn new(port: u16) -> Self { | ||
| 53 | - let client = reqwest::Client::builder() | ||
| 54 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 55 | - .timeout(Duration::from_secs(60)) | ||
| 56 | - .build() | ||
| 57 | - .expect("http client"); | ||
| 58 | - Self { | ||
| 59 | - client, | ||
| 60 | - base: format!("http://127.0.0.1:{port}"), | ||
| 61 | - } | ||
| 62 | - } | ||
| 63 | - | ||
| 64 | - async fn get_cookie(&self, path: &str, cookie: &str) -> Resp { | ||
| 65 | - let req = self | ||
| 66 | - .client | ||
| 67 | - .get(format!("{}{path}", self.base)) | ||
| 68 | - .header(reqwest::header::COOKIE, cookie); | ||
| 69 | - send(req).await | ||
| 70 | - } | ||
| 71 | - | ||
| 72 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 73 | - let mut req = self.client.post(format!("{}{path}", self.base)).form(form); | ||
| 74 | - if let Some(cookie) = cookie { | ||
| 75 | - req = req.header(reqwest::header::COOKIE, cookie); | ||
| 76 | - } | ||
| 77 | - send(req).await | ||
| 78 | - } | ||
| 79 | -} | ||
| 80 | - | ||
| 81 | -async fn send(req: reqwest::RequestBuilder) -> Resp { | ||
| 82 | - let res = req.send().await.expect("http request"); | ||
| 83 | - let status = res.status().as_u16(); | ||
| 84 | - let set_cookies = res | ||
| 85 | - .headers() | ||
| 86 | - .get_all(reqwest::header::SET_COOKIE) | ||
| 87 | - .iter() | ||
| 88 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 89 | - .collect(); | ||
| 90 | - let body = res.text().await.unwrap_or_default(); | ||
| 91 | - Resp { | ||
| 92 | - status, | ||
| 93 | - set_cookies, | ||
| 94 | - body, | ||
| 95 | - } | ||
| 96 | -} | ||
| 97 | - | ||
| 98 | -/// Returns `name=value` for the cookie whose name ends with `suffix`. Cleared cookies are ignored. | ||
| 99 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 100 | - resp.set_cookies.iter().find_map(|set| { | ||
| 101 | - let pair = set.split(';').next()?; | ||
| 102 | - let (name, value) = pair.split_once('=')?; | ||
| 103 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 104 | - }) | ||
| 105 | -} | ||
| 106 | - | ||
| 107 | /// The value of the first `<input name="...">` on a page. | 41 | /// The value of the first `<input name="...">` on a page. |
| 108 | /// Runs git with credential helpers disabled. Panics on failure and returns trimmed stdout. | 42 | /// Runs git with credential helpers disabled. Panics on failure and returns trimmed stdout. |
| 109 | fn git(cwd: &Path, args: &[&str]) -> String { | 43 | fn git(cwd: &Path, args: &[&str]) -> String { |
| @@ -204,27 +138,6 @@ fn run_admin(config: &Path, username: &str, extra: &[&str], password: &str) { | |||
| 204 | ); | 138 | ); |
| 205 | } | 139 | } |
| 206 | 140 | ||
| 207 | -async fn login(http: &Http, username: &str, password: &str) -> String { | ||
| 208 | - let page = http.get_cookie("/login", "").await; | ||
| 209 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 210 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 211 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 212 | - let res = http | ||
| 213 | - .post_form( | ||
| 214 | - "/login", | ||
| 215 | - &[ | ||
| 216 | - ("username", username), | ||
| 217 | - ("password", password), | ||
| 218 | - ("next", "/"), | ||
| 219 | - ("precsrf", &precsrf), | ||
| 220 | - ], | ||
| 221 | - Some(&precsrf_cookie), | ||
| 222 | - ) | ||
| 223 | - .await; | ||
| 224 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 225 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 226 | -} | ||
| 227 | - | ||
| 228 | async fn create_token(http: &Http, session: &str) -> String { | 141 | async fn create_token(http: &Http, session: &str) -> String { |
| 229 | let page = http.get_cookie("/settings/tokens", session).await; | 142 | let page = http.get_cookie("/settings/tokens", session).await; |
| 230 | assert_eq!(page.status, 200, "GET /settings/tokens"); | 143 | assert_eq!(page.status, 200, "GET /settings/tokens"); |
crates/forge-web/tests/e2e/branch_protection_e2e.rsmodified+3 -110
| @@ -3,102 +3,17 @@ | |||
| 3 | //! while the server runs. Helpers mirror `git_e2e.rs`. | 3 | //! while the server runs. Helpers mirror `git_e2e.rs`. |
| 4 | 4 | ||
| 5 | use crate::common::{ | 5 | use crate::common::{ |
| 6 | - BIN, Ports, Server, describe, input_value, launch, pick_ports, relaunch, tool_on_path, | 6 | + BIN, Http, Ports, Server, describe, input_value, launch, login, pick_ports, relaunch, |
| 7 | - write_config, | 7 | + tool_on_path, write_config, |
| 8 | }; | 8 | }; |
| 9 | use std::fs; | 9 | use std::fs; |
| 10 | use std::path::{Path, PathBuf}; | 10 | use std::path::{Path, PathBuf}; |
| 11 | use std::process::{Command, Output}; | 11 | use std::process::{Command, Output}; |
| 12 | -use std::time::Duration; | ||
| 13 | 12 | ||
| 14 | use forge_core::Store; | 13 | use forge_core::Store; |
| 15 | -use reqwest::header::{LOCATION, SET_COOKIE}; | ||
| 16 | 14 | ||
| 17 | const TAHINLI_PW: &str = "tahinli-password-123"; | 15 | const TAHINLI_PW: &str = "tahinli-password-123"; |
| 18 | 16 | ||
| 19 | -struct Resp { | ||
| 20 | - status: u16, | ||
| 21 | - location: Option<String>, | ||
| 22 | - set_cookies: Vec<String>, | ||
| 23 | - body: String, | ||
| 24 | -} | ||
| 25 | - | ||
| 26 | -struct Http { | ||
| 27 | - client: reqwest::Client, | ||
| 28 | - base: String, | ||
| 29 | -} | ||
| 30 | - | ||
| 31 | -impl Http { | ||
| 32 | - fn new(port: u16) -> Self { | ||
| 33 | - let client = reqwest::Client::builder() | ||
| 34 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 35 | - .timeout(Duration::from_secs(60)) | ||
| 36 | - .build() | ||
| 37 | - .expect("http client"); | ||
| 38 | - Self { | ||
| 39 | - client, | ||
| 40 | - base: format!("http://127.0.0.1:{port}"), | ||
| 41 | - } | ||
| 42 | - } | ||
| 43 | - | ||
| 44 | - async fn get(&self, path: &str) -> Resp { | ||
| 45 | - self.send(self.client.get(format!("{}{path}", self.base)), None) | ||
| 46 | - .await | ||
| 47 | - } | ||
| 48 | - | ||
| 49 | - async fn get_cookie(&self, path: &str, cookie: &str) -> Resp { | ||
| 50 | - self.send( | ||
| 51 | - self.client.get(format!("{}{path}", self.base)), | ||
| 52 | - Some(cookie), | ||
| 53 | - ) | ||
| 54 | - .await | ||
| 55 | - } | ||
| 56 | - | ||
| 57 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 58 | - self.send( | ||
| 59 | - self.client.post(format!("{}{path}", self.base)).form(form), | ||
| 60 | - cookie, | ||
| 61 | - ) | ||
| 62 | - .await | ||
| 63 | - } | ||
| 64 | - | ||
| 65 | - async fn send(&self, req: reqwest::RequestBuilder, cookie: Option<&str>) -> Resp { | ||
| 66 | - let mut req = req; | ||
| 67 | - if let Some(cookie) = cookie { | ||
| 68 | - req = req.header(reqwest::header::COOKIE, cookie); | ||
| 69 | - } | ||
| 70 | - let res = req.send().await.expect("http request"); | ||
| 71 | - let status = res.status().as_u16(); | ||
| 72 | - let location = res | ||
| 73 | - .headers() | ||
| 74 | - .get(LOCATION) | ||
| 75 | - .and_then(|v| v.to_str().ok()) | ||
| 76 | - .map(str::to_owned); | ||
| 77 | - let set_cookies = res | ||
| 78 | - .headers() | ||
| 79 | - .get_all(SET_COOKIE) | ||
| 80 | - .iter() | ||
| 81 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 82 | - .collect(); | ||
| 83 | - let body = res.text().await.unwrap_or_default(); | ||
| 84 | - Resp { | ||
| 85 | - status, | ||
| 86 | - location, | ||
| 87 | - set_cookies, | ||
| 88 | - body, | ||
| 89 | - } | ||
| 90 | - } | ||
| 91 | -} | ||
| 92 | - | ||
| 93 | -/// Returns `name=value` for the cookie whose name ends with `suffix`. Cleared cookies are ignored. | ||
| 94 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 95 | - resp.set_cookies.iter().find_map(|set| { | ||
| 96 | - let pair = set.split(';').next()?; | ||
| 97 | - let (name, value) = pair.split_once('=')?; | ||
| 98 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 99 | - }) | ||
| 100 | -} | ||
| 101 | - | ||
| 102 | /// The value of the first `<input name="...">` on a page. | 17 | /// The value of the first `<input name="...">` on a page. |
| 103 | fn run_admin(config: &Path, username: &str, extra: &[&str], password: &str) { | 18 | fn run_admin(config: &Path, username: &str, extra: &[&str], password: &str) { |
| 104 | let out = Command::new(BIN) | 19 | let out = Command::new(BIN) |
| @@ -183,28 +98,6 @@ fn remote_sha(repo: &Path, reference: &str) -> String { | |||
| 183 | .to_owned() | 98 | .to_owned() |
| 184 | } | 99 | } |
| 185 | 100 | ||
| 186 | -/// Logs in through the real `/login` form and returns the `__Host-forge_session` cookie header. | ||
| 187 | -async fn login(http: &Http, username: &str, password: &str) -> String { | ||
| 188 | - let page = http.get("/login").await; | ||
| 189 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 190 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 191 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 192 | - let res = http | ||
| 193 | - .post_form( | ||
| 194 | - "/login", | ||
| 195 | - &[ | ||
| 196 | - ("username", username), | ||
| 197 | - ("password", password), | ||
| 198 | - ("next", "/"), | ||
| 199 | - ("precsrf", &precsrf), | ||
| 200 | - ], | ||
| 201 | - Some(&precsrf_cookie), | ||
| 202 | - ) | ||
| 203 | - .await; | ||
| 204 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 205 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 206 | -} | ||
| 207 | - | ||
| 208 | /// Creates an access token through `/settings/tokens` and returns the token shown once. | 101 | /// Creates an access token through `/settings/tokens` and returns the token shown once. |
| 209 | async fn create_token(http: &Http, session: &str, name: &str) -> String { | 102 | async fn create_token(http: &Http, session: &str, name: &str) -> String { |
| 210 | let page = http.get_cookie("/settings/tokens", session).await; | 103 | let page = http.get_cookie("/settings/tokens", session).await; |
| @@ -250,7 +143,7 @@ async fn create_repo(http: &Http, session: &str, name: &str) { | |||
| 250 | .await; | 143 | .await; |
| 251 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); | 144 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); |
| 252 | assert_eq!( | 145 | assert_eq!( |
| 253 | - res.location.as_deref(), | 146 | + res.location().as_deref(), |
| 254 | Some(format!("/tahinli/{name}").as_str()) | 147 | Some(format!("/tahinli/{name}").as_str()) |
| 255 | ); | 148 | ); |
| 256 | } | 149 | } |
crates/forge-web/tests/e2e/ci_e2e.rsmodified+4 -110
| @@ -10,7 +10,9 @@ | |||
| 10 | //! - a fork pull request: it waits for approval, then runs without secrets; | 10 | //! - a fork pull request: it waits for approval, then runs without secrets; |
| 11 | //! - a live log: `log.txt` streams while a job runs and ends when the job finishes. | 11 | //! - a live log: `log.txt` streams while a job runs and ends when the job finishes. |
| 12 | 12 | ||
| 13 | -use crate::common::{BIN, FAST_CLAIMS, Server, input_value, launch_with, pick_ports, write_config}; | 13 | +use crate::common::{ |
| 14 | + BIN, FAST_CLAIMS, Http, Server, input_value, launch_with, login, pick_ports, write_config, | ||
| 15 | +}; | ||
| 14 | use forge_ci::proto::{JobAssignment, Outcome, RegisterRequest}; | 16 | use forge_ci::proto::{JobAssignment, Outcome, RegisterRequest}; |
| 15 | use forge_runner::client::ForgeClient; | 17 | use forge_runner::client::ForgeClient; |
| 16 | use forge_runner::config::{Limits, RunnerConfig}; | 18 | use forge_runner::config::{Limits, RunnerConfig}; |
| @@ -24,8 +26,6 @@ use std::sync::Arc; | |||
| 24 | use std::time::{Duration, Instant}; | 26 | use std::time::{Duration, Instant}; |
| 25 | use tokio_util::sync::CancellationToken; | 27 | use tokio_util::sync::CancellationToken; |
| 26 | 28 | ||
| 27 | -use reqwest::header::{LOCATION, SET_COOKIE}; | ||
| 28 | - | ||
| 29 | const TAHINLI_PW: &str = "tahinli-password-123"; | 29 | const TAHINLI_PW: &str = "tahinli-password-123"; |
| 30 | const BOB_PW: &str = "bob-password-4567"; | 30 | const BOB_PW: &str = "bob-password-4567"; |
| 31 | const BOB_EMAIL: &str = "bob@users.noreply.127.0.0.1"; | 31 | const BOB_EMAIL: &str = "bob@users.noreply.127.0.0.1"; |
| @@ -90,90 +90,6 @@ run = "echo never" | |||
| 90 | needs = ["broken"] | 90 | needs = ["broken"] |
| 91 | "#; | 91 | "#; |
| 92 | 92 | ||
| 93 | -/// A parsed HTTP response. Only the parts the test needs are kept. | ||
| 94 | -struct Resp { | ||
| 95 | - status: u16, | ||
| 96 | - location: Option<String>, | ||
| 97 | - set_cookies: Vec<String>, | ||
| 98 | - body: String, | ||
| 99 | -} | ||
| 100 | - | ||
| 101 | -struct Http { | ||
| 102 | - client: reqwest::Client, | ||
| 103 | - base: String, | ||
| 104 | -} | ||
| 105 | - | ||
| 106 | -impl Http { | ||
| 107 | - fn new(port: u16) -> Self { | ||
| 108 | - let client = reqwest::Client::builder() | ||
| 109 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 110 | - .timeout(Duration::from_secs(60)) | ||
| 111 | - .build() | ||
| 112 | - .expect("http client"); | ||
| 113 | - Self { | ||
| 114 | - client, | ||
| 115 | - base: format!("http://127.0.0.1:{port}"), | ||
| 116 | - } | ||
| 117 | - } | ||
| 118 | - | ||
| 119 | - async fn get(&self, path: &str, cookie: Option<&str>) -> Resp { | ||
| 120 | - self.send(self.client.get(format!("{}{path}", self.base)), cookie) | ||
| 121 | - .await | ||
| 122 | - } | ||
| 123 | - | ||
| 124 | - async fn get_bearer(&self, path: &str, token: &str) -> Resp { | ||
| 125 | - let req = self | ||
| 126 | - .client | ||
| 127 | - .get(format!("{}{path}", self.base)) | ||
| 128 | - .bearer_auth(token); | ||
| 129 | - self.send(req, None).await | ||
| 130 | - } | ||
| 131 | - | ||
| 132 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 133 | - self.send( | ||
| 134 | - self.client.post(format!("{}{path}", self.base)).form(form), | ||
| 135 | - cookie, | ||
| 136 | - ) | ||
| 137 | - .await | ||
| 138 | - } | ||
| 139 | - | ||
| 140 | - async fn send(&self, req: reqwest::RequestBuilder, cookie: Option<&str>) -> Resp { | ||
| 141 | - let req = match cookie { | ||
| 142 | - Some(cookie) => req.header(reqwest::header::COOKIE, cookie), | ||
| 143 | - None => req, | ||
| 144 | - }; | ||
| 145 | - let res = req.send().await.expect("http request"); | ||
| 146 | - let status = res.status().as_u16(); | ||
| 147 | - let location = res | ||
| 148 | - .headers() | ||
| 149 | - .get(LOCATION) | ||
| 150 | - .and_then(|v| v.to_str().ok()) | ||
| 151 | - .map(str::to_owned); | ||
| 152 | - let set_cookies = res | ||
| 153 | - .headers() | ||
| 154 | - .get_all(SET_COOKIE) | ||
| 155 | - .iter() | ||
| 156 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 157 | - .collect(); | ||
| 158 | - let body = res.text().await.unwrap_or_default(); | ||
| 159 | - Resp { | ||
| 160 | - status, | ||
| 161 | - location, | ||
| 162 | - set_cookies, | ||
| 163 | - body, | ||
| 164 | - } | ||
| 165 | - } | ||
| 166 | -} | ||
| 167 | - | ||
| 168 | -/// Returns `name=value` for the cookie whose name ends with `suffix`. Cleared cookies are ignored. | ||
| 169 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 170 | - resp.set_cookies.iter().find_map(|set| { | ||
| 171 | - let pair = set.split(';').next()?; | ||
| 172 | - let (name, value) = pair.split_once('=')?; | ||
| 173 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 174 | - }) | ||
| 175 | -} | ||
| 176 | - | ||
| 177 | /// The value of the first `<input name="...">` on a page. | 93 | /// The value of the first `<input name="...">` on a page. |
| 178 | /// The text of the first element matching `selector`. | 94 | /// The text of the first element matching `selector`. |
| 179 | fn first_text(body: &str, selector: &str) -> String { | 95 | fn first_text(body: &str, selector: &str) -> String { |
| @@ -282,28 +198,6 @@ async fn csrf_from(http: &Http, path: &str, session: &str) -> String { | |||
| 282 | input_value(&page.body, "csrf") | 198 | input_value(&page.body, "csrf") |
| 283 | } | 199 | } |
| 284 | 200 | ||
| 285 | -/// Logs in through the real `/login` form and returns the `__Host-forge_session` cookie header. | ||
| 286 | -async fn login(http: &Http, username: &str, password: &str) -> String { | ||
| 287 | - let page = http.get("/login", None).await; | ||
| 288 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 289 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 290 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 291 | - let res = http | ||
| 292 | - .post_form( | ||
| 293 | - "/login", | ||
| 294 | - &[ | ||
| 295 | - ("username", username), | ||
| 296 | - ("password", password), | ||
| 297 | - ("next", "/"), | ||
| 298 | - ("precsrf", &precsrf), | ||
| 299 | - ], | ||
| 300 | - Some(&precsrf_cookie), | ||
| 301 | - ) | ||
| 302 | - .await; | ||
| 303 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 304 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 305 | -} | ||
| 306 | - | ||
| 307 | /// Creates an access token through `/settings/tokens` and returns the token shown once. | 201 | /// Creates an access token through `/settings/tokens` and returns the token shown once. |
| 308 | async fn create_token(http: &Http, session: &str, name: &str) -> String { | 202 | async fn create_token(http: &Http, session: &str, name: &str) -> String { |
| 309 | let csrf = csrf_from(http, "/settings/tokens", session).await; | 203 | let csrf = csrf_from(http, "/settings/tokens", session).await; |
| @@ -340,7 +234,7 @@ async fn create_repo(http: &Http, session: &str, name: &str) { | |||
| 340 | .await; | 234 | .await; |
| 341 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); | 235 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); |
| 342 | assert_eq!( | 236 | assert_eq!( |
| 343 | - res.location.as_deref(), | 237 | + res.location().as_deref(), |
| 344 | Some(format!("/tahinli/{name}").as_str()) | 238 | Some(format!("/tahinli/{name}").as_str()) |
| 345 | ); | 239 | ); |
| 346 | } | 240 | } |
crates/forge-web/tests/e2e/common/http.rsadded+176 -0
| @@ -0,0 +1,176 @@ | |||
| 1 | +//! The HTTP client the e2e tests use against a running `forge serve`. A request can carry a | ||
| 2 | +//! cookie or a bearer or basic credential, and a response keeps its status, headers and body | ||
| 3 | +//! text. The login flow every test starts with lives here too. | ||
| 4 | + | ||
| 5 | +use std::time::Duration; | ||
| 6 | + | ||
| 7 | +use reqwest::header::{COOKIE, HeaderMap, HeaderName, LOCATION, SET_COOKIE}; | ||
| 8 | + | ||
| 9 | +use super::input_value; | ||
| 10 | + | ||
| 11 | +/// A received response. Headers are kept whole, so a test can read any of them. | ||
| 12 | +pub struct Resp { | ||
| 13 | + pub status: u16, | ||
| 14 | + pub headers: HeaderMap, | ||
| 15 | + pub body: String, | ||
| 16 | +} | ||
| 17 | + | ||
| 18 | +impl Resp { | ||
| 19 | + /// The first value of a header, when it is valid text. | ||
| 20 | + pub fn header(&self, name: HeaderName) -> Option<String> { | ||
| 21 | + self.headers | ||
| 22 | + .get(name) | ||
| 23 | + .and_then(|v| v.to_str().ok()) | ||
| 24 | + .map(str::to_owned) | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + /// The `Location` header, when there is one. | ||
| 28 | + pub fn location(&self) -> Option<String> { | ||
| 29 | + self.header(LOCATION) | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + /// Every `Set-Cookie` value, in the order the server sent them. Cleared cookies included. | ||
| 33 | + pub fn set_cookies(&self) -> Vec<String> { | ||
| 34 | + self.headers | ||
| 35 | + .get_all(SET_COOKIE) | ||
| 36 | + .iter() | ||
| 37 | + .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 38 | + .collect() | ||
| 39 | + } | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +/// A client bound to one listener. It never follows redirects. | ||
| 43 | +pub struct Http { | ||
| 44 | + pub client: reqwest::Client, | ||
| 45 | + pub base: String, | ||
| 46 | +} | ||
| 47 | + | ||
| 48 | +impl Http { | ||
| 49 | + pub fn new(port: u16) -> Self { | ||
| 50 | + Self::with_base(format!("http://127.0.0.1:{port}")) | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + /// A client for an explicit base URL, such as a listener reached through another host name. | ||
| 54 | + pub fn with_base(base: String) -> Self { | ||
| 55 | + let client = reqwest::Client::builder() | ||
| 56 | + .redirect(reqwest::redirect::Policy::none()) | ||
| 57 | + .timeout(Duration::from_secs(60)) | ||
| 58 | + .build() | ||
| 59 | + .expect("http client"); | ||
| 60 | + Self { client, base } | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + pub async fn get(&self, path: &str, cookie: Option<&str>) -> Resp { | ||
| 64 | + self.get_language(path, cookie, None).await | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + /// `get` with an optional `Accept-Language` value. | ||
| 68 | + pub async fn get_language( | ||
| 69 | + &self, | ||
| 70 | + path: &str, | ||
| 71 | + cookie: Option<&str>, | ||
| 72 | + language: Option<&str>, | ||
| 73 | + ) -> Resp { | ||
| 74 | + let mut req = self.client.get(format!("{}{path}", self.base)); | ||
| 75 | + if let Some(language) = language { | ||
| 76 | + req = req.header(reqwest::header::ACCEPT_LANGUAGE, language); | ||
| 77 | + } | ||
| 78 | + self.send(req, cookie).await | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + /// `get` with the cookie header set to `cookie` exactly, even when it is empty. | ||
| 82 | + pub async fn get_cookie(&self, path: &str, cookie: &str) -> Resp { | ||
| 83 | + self.get(path, Some(cookie)).await | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + pub async fn get_bearer(&self, path: &str, token: &str) -> Resp { | ||
| 87 | + let req = self | ||
| 88 | + .client | ||
| 89 | + .get(format!("{}{path}", self.base)) | ||
| 90 | + .bearer_auth(token); | ||
| 91 | + self.send(req, None).await | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + pub async fn get_basic(&self, path: &str, user: &str, password: &str) -> Resp { | ||
| 95 | + let req = self | ||
| 96 | + .client | ||
| 97 | + .get(format!("{}{path}", self.base)) | ||
| 98 | + .basic_auth(user, Some(password)); | ||
| 99 | + self.send(req, None).await | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + pub async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 103 | + self.send( | ||
| 104 | + self.client.post(format!("{}{path}", self.base)).form(form), | ||
| 105 | + cookie, | ||
| 106 | + ) | ||
| 107 | + .await | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + /// Opens a live stream as `cookie`, or anonymously. A stream that opens sends its `retry` | ||
| 111 | + /// field first, which is read here, not treated as an event. | ||
| 112 | + pub async fn stream(&self, path: &str, cookie: Option<&str>) -> reqwest::Response { | ||
| 113 | + let mut req = self.client.get(format!("{}{path}", self.base)); | ||
| 114 | + if let Some(cookie) = cookie { | ||
| 115 | + req = req.header(COOKIE, cookie); | ||
| 116 | + } | ||
| 117 | + let mut res = req.send().await.expect("stream request"); | ||
| 118 | + if res.status().is_success() { | ||
| 119 | + let retry = tokio::time::timeout(Duration::from_secs(5), res.chunk()) | ||
| 120 | + .await | ||
| 121 | + .expect("the retry field") | ||
| 122 | + .expect("stream chunk") | ||
| 123 | + .expect("stream still open"); | ||
| 124 | + assert!(String::from_utf8_lossy(&retry).contains("retry: 1000")); | ||
| 125 | + } | ||
| 126 | + res | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + /// Sends a prepared request, with `cookie` as its Cookie header when there is one. | ||
| 130 | + pub async fn send(&self, req: reqwest::RequestBuilder, cookie: Option<&str>) -> Resp { | ||
| 131 | + let req = match cookie { | ||
| 132 | + Some(cookie) => req.header(COOKIE, cookie), | ||
| 133 | + None => req, | ||
| 134 | + }; | ||
| 135 | + let res = req.send().await.expect("http request"); | ||
| 136 | + let status = res.status().as_u16(); | ||
| 137 | + let headers = res.headers().clone(); | ||
| 138 | + let body = res.text().await.expect("read body"); | ||
| 139 | + Resp { | ||
| 140 | + status, | ||
| 141 | + headers, | ||
| 142 | + body, | ||
| 143 | + } | ||
| 144 | + } | ||
| 145 | +} | ||
| 146 | + | ||
| 147 | +/// Returns `name=value` for the cookie whose name ends with `suffix`. Cleared cookies are ignored. | ||
| 148 | +pub fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 149 | + resp.set_cookies().iter().find_map(|set| { | ||
| 150 | + let pair = set.split(';').next()?; | ||
| 151 | + let (name, value) = pair.split_once('=')?; | ||
| 152 | + (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 153 | + }) | ||
| 154 | +} | ||
| 155 | + | ||
| 156 | +/// Signs in as `username` through the `/login` form and returns the session cookie. | ||
| 157 | +pub async fn login(http: &Http, username: &str, password: &str) -> String { | ||
| 158 | + let page = http.get("/login", None).await; | ||
| 159 | + assert_eq!(page.status, 200, "GET /login"); | ||
| 160 | + let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 161 | + let precsrf = input_value(&page.body, "precsrf"); | ||
| 162 | + let res = http | ||
| 163 | + .post_form( | ||
| 164 | + "/login", | ||
| 165 | + &[ | ||
| 166 | + ("username", username), | ||
| 167 | + ("password", password), | ||
| 168 | + ("next", "/"), | ||
| 169 | + ("precsrf", &precsrf), | ||
| 170 | + ], | ||
| 171 | + Some(&precsrf_cookie), | ||
| 172 | + ) | ||
| 173 | + .await; | ||
| 174 | + assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 175 | + cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 176 | +} | ||
crates/forge-web/tests/e2e/common/mod.rsmodified+4 -0
| @@ -15,6 +15,10 @@ use std::path::{Path, PathBuf}; | |||
| 15 | use std::process::{Child, Command, ExitStatus, Output, Stdio}; | 15 | use std::process::{Child, Command, ExitStatus, Output, Stdio}; |
| 16 | use std::time::{Duration, Instant}; | 16 | use std::time::{Duration, Instant}; |
| 17 | 17 | ||
| 18 | +mod http; | ||
| 19 | + | ||
| 20 | +pub use http::{Http, Resp, cookie_named, login}; | ||
| 21 | + | ||
| 18 | pub const BIN: &str = env!("CARGO_BIN_EXE_forge"); | 22 | pub const BIN: &str = env!("CARGO_BIN_EXE_forge"); |
| 19 | 23 | ||
| 20 | /// How long a freshly spawned server gets to answer on all three listeners. | 24 | /// How long a freshly spawned server gets to answer on all three listeners. |
crates/forge-web/tests/e2e/discussions_e2e.rsmodified+10 -103
| @@ -3,90 +3,15 @@ | |||
| 3 | //! an answer in Q&A, and the lock: once locked, a plain viewer's comment is refused while the | 3 | //! an answer in Q&A, and the lock: once locked, a plain viewer's comment is refused while the |
| 4 | //! owner can still write. | 4 | //! owner can still write. |
| 5 | 5 | ||
| 6 | -use crate::common::{BIN, input_value, launch, pick_ports, tool_on_path, write_config}; | 6 | +use crate::common::{ |
| 7 | + BIN, Http, input_value, launch, login, pick_ports, tool_on_path, write_config, | ||
| 8 | +}; | ||
| 7 | use std::path::Path; | 9 | use std::path::Path; |
| 8 | use std::process::Command; | 10 | use std::process::Command; |
| 9 | -use std::time::Duration; | ||
| 10 | - | ||
| 11 | -use reqwest::header::{LOCATION, SET_COOKIE}; | ||
| 12 | 11 | ||
| 13 | const TAHINLI_PW: &str = "tahinli-password-123"; | 12 | const TAHINLI_PW: &str = "tahinli-password-123"; |
| 14 | const BOB_PW: &str = "bob-password-4567"; | 13 | const BOB_PW: &str = "bob-password-4567"; |
| 15 | 14 | ||
| 16 | -struct Resp { | ||
| 17 | - status: u16, | ||
| 18 | - location: Option<String>, | ||
| 19 | - set_cookies: Vec<String>, | ||
| 20 | - body: String, | ||
| 21 | -} | ||
| 22 | - | ||
| 23 | -struct Http { | ||
| 24 | - client: reqwest::Client, | ||
| 25 | - base: String, | ||
| 26 | -} | ||
| 27 | - | ||
| 28 | -impl Http { | ||
| 29 | - fn new(port: u16) -> Self { | ||
| 30 | - let client = reqwest::Client::builder() | ||
| 31 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 32 | - .timeout(Duration::from_secs(60)) | ||
| 33 | - .build() | ||
| 34 | - .expect("http client"); | ||
| 35 | - Self { | ||
| 36 | - client, | ||
| 37 | - base: format!("http://127.0.0.1:{port}"), | ||
| 38 | - } | ||
| 39 | - } | ||
| 40 | - | ||
| 41 | - async fn get(&self, path: &str, cookie: Option<&str>) -> Resp { | ||
| 42 | - let mut req = self.client.get(format!("{}{path}", self.base)); | ||
| 43 | - if let Some(cookie) = cookie { | ||
| 44 | - req = req.header(reqwest::header::COOKIE, cookie); | ||
| 45 | - } | ||
| 46 | - send(req).await | ||
| 47 | - } | ||
| 48 | - | ||
| 49 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 50 | - let mut req = self.client.post(format!("{}{path}", self.base)).form(form); | ||
| 51 | - if let Some(cookie) = cookie { | ||
| 52 | - req = req.header(reqwest::header::COOKIE, cookie); | ||
| 53 | - } | ||
| 54 | - send(req).await | ||
| 55 | - } | ||
| 56 | -} | ||
| 57 | - | ||
| 58 | -async fn send(req: reqwest::RequestBuilder) -> Resp { | ||
| 59 | - let res = req.send().await.expect("http request"); | ||
| 60 | - let status = res.status().as_u16(); | ||
| 61 | - let location = res | ||
| 62 | - .headers() | ||
| 63 | - .get(LOCATION) | ||
| 64 | - .and_then(|v| v.to_str().ok()) | ||
| 65 | - .map(str::to_owned); | ||
| 66 | - let set_cookies = res | ||
| 67 | - .headers() | ||
| 68 | - .get_all(SET_COOKIE) | ||
| 69 | - .iter() | ||
| 70 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 71 | - .collect(); | ||
| 72 | - let body = res.text().await.unwrap_or_default(); | ||
| 73 | - Resp { | ||
| 74 | - status, | ||
| 75 | - location, | ||
| 76 | - set_cookies, | ||
| 77 | - body, | ||
| 78 | - } | ||
| 79 | -} | ||
| 80 | - | ||
| 81 | -/// Returns `name=value` for the cookie whose name ends with `suffix`. Cleared cookies are ignored. | ||
| 82 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 83 | - resp.set_cookies.iter().find_map(|set| { | ||
| 84 | - let pair = set.split(';').next()?; | ||
| 85 | - let (name, value) = pair.split_once('=')?; | ||
| 86 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 87 | - }) | ||
| 88 | -} | ||
| 89 | - | ||
| 90 | /// The value of the first `<input name="...">` on a page. | 15 | /// The value of the first `<input name="...">` on a page. |
| 91 | fn run_admin(config: &Path, username: &str, extra: &[&str], password: &str) { | 16 | fn run_admin(config: &Path, username: &str, extra: &[&str], password: &str) { |
| 92 | let out = Command::new(BIN) | 17 | let out = Command::new(BIN) |
| @@ -104,27 +29,6 @@ fn run_admin(config: &Path, username: &str, extra: &[&str], password: &str) { | |||
| 104 | ); | 29 | ); |
| 105 | } | 30 | } |
| 106 | 31 | ||
| 107 | -async fn login(http: &Http, username: &str, password: &str) -> String { | ||
| 108 | - let page = http.get("/login", None).await; | ||
| 109 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 110 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 111 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 112 | - let res = http | ||
| 113 | - .post_form( | ||
| 114 | - "/login", | ||
| 115 | - &[ | ||
| 116 | - ("username", username), | ||
| 117 | - ("password", password), | ||
| 118 | - ("next", "/"), | ||
| 119 | - ("precsrf", &precsrf), | ||
| 120 | - ], | ||
| 121 | - Some(precsrf_cookie.as_str()), | ||
| 122 | - ) | ||
| 123 | - .await; | ||
| 124 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 125 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 126 | -} | ||
| 127 | - | ||
| 128 | /// Creates a public repo through `/new`. | 32 | /// Creates a public repo through `/new`. |
| 129 | async fn create_repo(http: &Http, session: &str, name: &str) { | 33 | async fn create_repo(http: &Http, session: &str, name: &str) { |
| 130 | let page = http.get("/new", Some(session)).await; | 34 | let page = http.get("/new", Some(session)).await; |
| @@ -145,7 +49,7 @@ async fn create_repo(http: &Http, session: &str, name: &str) { | |||
| 145 | .await; | 49 | .await; |
| 146 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); | 50 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); |
| 147 | assert_eq!( | 51 | assert_eq!( |
| 148 | - res.location.as_deref(), | 52 | + res.location().as_deref(), |
| 149 | Some(format!("/tahinli/{name}").as_str()) | 53 | Some(format!("/tahinli/{name}").as_str()) |
| 150 | ); | 54 | ); |
| 151 | } | 55 | } |
| @@ -233,7 +137,10 @@ async fn discussions_create_reply_answer_lock_and_toggle() { | |||
| 233 | ) | 137 | ) |
| 234 | .await; | 138 | .await; |
| 235 | assert_eq!(res.status, 303, "create: {}", res.body); | 139 | assert_eq!(res.status, 303, "create: {}", res.body); |
| 236 | - assert_eq!(res.location.as_deref(), Some("/tahinli/pub/discussions/1")); | 140 | + assert_eq!( |
| 141 | + res.location().as_deref(), | ||
| 142 | + Some("/tahinli/pub/discussions/1") | ||
| 143 | + ); | ||
| 237 | let discussion = "/tahinli/pub/discussions/1"; | 144 | let discussion = "/tahinli/pub/discussions/1"; |
| 238 | 145 | ||
| 239 | // Bob answers at the top level, then replies to that comment. | 146 | // Bob answers at the top level, then replies to that comment. |
| @@ -248,7 +155,7 @@ async fn discussions_create_reply_answer_lock_and_toggle() { | |||
| 248 | ) | 155 | ) |
| 249 | .await; | 156 | .await; |
| 250 | assert_eq!(res.status, 303, "top comment: {}", res.body); | 157 | assert_eq!(res.status, 303, "top comment: {}", res.body); |
| 251 | - let top = anchored_id(res.location.as_deref()); | 158 | + let top = anchored_id(res.location().as_deref()); |
| 252 | 159 | ||
| 253 | let res = http | 160 | let res = http |
| 254 | .post_form( | 161 | .post_form( |
| @@ -262,7 +169,7 @@ async fn discussions_create_reply_answer_lock_and_toggle() { | |||
| 262 | ) | 169 | ) |
| 263 | .await; | 170 | .await; |
| 264 | assert_eq!(res.status, 303, "reply: {}", res.body); | 171 | assert_eq!(res.status, 303, "reply: {}", res.body); |
| 265 | - let reply = anchored_id(res.location.as_deref()); | 172 | + let reply = anchored_id(res.location().as_deref()); |
| 266 | 173 | ||
| 267 | // A reply to a reply is refused, and the page says why. | 174 | // A reply to a reply is refused, and the page says why. |
| 268 | let res = http | 175 | let res = http |
crates/forge-web/tests/e2e/git_e2e.rsmodified+8 -139
| @@ -4,127 +4,18 @@ | |||
| 4 | //! Cookies are handled by hand: the session cookie is `__Host-forge_session` with `Secure`, so a | 4 | //! Cookies are handled by hand: the session cookie is `__Host-forge_session` with `Secure`, so a |
| 5 | //! cookie jar would not send it over plain http. The test parses `Set-Cookie` and sends `Cookie`. | 5 | //! cookie jar would not send it over plain http. The test parses `Set-Cookie` and sends `Cookie`. |
| 6 | 6 | ||
| 7 | -use crate::common::{BIN, describe, input_value, launch, pick_ports, tool_on_path, write_config}; | 7 | +use crate::common::{ |
| 8 | + BIN, Http, describe, input_value, launch, login, pick_ports, tool_on_path, write_config, | ||
| 9 | +}; | ||
| 8 | use std::fs; | 10 | use std::fs; |
| 9 | use std::path::{Path, PathBuf}; | 11 | use std::path::{Path, PathBuf}; |
| 10 | use std::process::{Command, Output}; | 12 | use std::process::{Command, Output}; |
| 11 | -use std::time::Duration; | ||
| 12 | 13 | ||
| 13 | -use reqwest::header::{LOCATION, SET_COOKIE, WWW_AUTHENTICATE}; | 14 | +use reqwest::header::WWW_AUTHENTICATE; |
| 14 | 15 | ||
| 15 | const TAHINLI_PW: &str = "tahinli-password-123"; | 16 | const TAHINLI_PW: &str = "tahinli-password-123"; |
| 16 | const BOB_PW: &str = "bob-password-4567"; | 17 | const BOB_PW: &str = "bob-password-4567"; |
| 17 | 18 | ||
| 18 | -/// A parsed HTTP response. Only the parts the test needs are kept. | ||
| 19 | -struct Resp { | ||
| 20 | - status: u16, | ||
| 21 | - location: Option<String>, | ||
| 22 | - www_authenticate: Option<String>, | ||
| 23 | - set_cookies: Vec<String>, | ||
| 24 | - body: String, | ||
| 25 | -} | ||
| 26 | - | ||
| 27 | -struct Http { | ||
| 28 | - client: reqwest::Client, | ||
| 29 | - base: String, | ||
| 30 | -} | ||
| 31 | - | ||
| 32 | -impl Http { | ||
| 33 | - fn new(port: u16) -> Self { | ||
| 34 | - let client = reqwest::Client::builder() | ||
| 35 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 36 | - .timeout(Duration::from_secs(60)) | ||
| 37 | - .build() | ||
| 38 | - .expect("http client"); | ||
| 39 | - Self { | ||
| 40 | - client, | ||
| 41 | - base: format!("http://127.0.0.1:{port}"), | ||
| 42 | - } | ||
| 43 | - } | ||
| 44 | - | ||
| 45 | - async fn get(&self, path: &str) -> Resp { | ||
| 46 | - self.send(self.client.get(format!("{}{path}", self.base)), None, None) | ||
| 47 | - .await | ||
| 48 | - } | ||
| 49 | - | ||
| 50 | - async fn get_cookie(&self, path: &str, cookie: &str) -> Resp { | ||
| 51 | - self.send( | ||
| 52 | - self.client.get(format!("{}{path}", self.base)), | ||
| 53 | - Some(cookie), | ||
| 54 | - None, | ||
| 55 | - ) | ||
| 56 | - .await | ||
| 57 | - } | ||
| 58 | - | ||
| 59 | - async fn get_basic(&self, path: &str, user: &str, password: &str) -> Resp { | ||
| 60 | - self.send( | ||
| 61 | - self.client.get(format!("{}{path}", self.base)), | ||
| 62 | - None, | ||
| 63 | - Some((user, password)), | ||
| 64 | - ) | ||
| 65 | - .await | ||
| 66 | - } | ||
| 67 | - | ||
| 68 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 69 | - self.send( | ||
| 70 | - self.client.post(format!("{}{path}", self.base)).form(form), | ||
| 71 | - cookie, | ||
| 72 | - None, | ||
| 73 | - ) | ||
| 74 | - .await | ||
| 75 | - } | ||
| 76 | - | ||
| 77 | - async fn send( | ||
| 78 | - &self, | ||
| 79 | - req: reqwest::RequestBuilder, | ||
| 80 | - cookie: Option<&str>, | ||
| 81 | - basic: Option<(&str, &str)>, | ||
| 82 | - ) -> Resp { | ||
| 83 | - let mut req = req; | ||
| 84 | - if let Some(cookie) = cookie { | ||
| 85 | - req = req.header(reqwest::header::COOKIE, cookie); | ||
| 86 | - } | ||
| 87 | - if let Some((user, password)) = basic { | ||
| 88 | - req = req.basic_auth(user, Some(password)); | ||
| 89 | - } | ||
| 90 | - let res = req.send().await.expect("http request"); | ||
| 91 | - let status = res.status().as_u16(); | ||
| 92 | - let location = header_text(&res, LOCATION); | ||
| 93 | - let www_authenticate = header_text(&res, WWW_AUTHENTICATE); | ||
| 94 | - let set_cookies = res | ||
| 95 | - .headers() | ||
| 96 | - .get_all(SET_COOKIE) | ||
| 97 | - .iter() | ||
| 98 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 99 | - .collect(); | ||
| 100 | - let body = res.text().await.unwrap_or_default(); | ||
| 101 | - Resp { | ||
| 102 | - status, | ||
| 103 | - location, | ||
| 104 | - www_authenticate, | ||
| 105 | - set_cookies, | ||
| 106 | - body, | ||
| 107 | - } | ||
| 108 | - } | ||
| 109 | -} | ||
| 110 | - | ||
| 111 | -fn header_text(res: &reqwest::Response, name: reqwest::header::HeaderName) -> Option<String> { | ||
| 112 | - res.headers() | ||
| 113 | - .get(name) | ||
| 114 | - .and_then(|v| v.to_str().ok()) | ||
| 115 | - .map(str::to_owned) | ||
| 116 | -} | ||
| 117 | - | ||
| 118 | -/// Returns `name=value` for the cookie whose name ends with `suffix`, e.g. `forge_session` | ||
| 119 | -/// matches `__Host-forge_session`. Cleared cookies (empty value) are ignored. | ||
| 120 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 121 | - resp.set_cookies.iter().find_map(|set| { | ||
| 122 | - let pair = set.split(';').next()?; | ||
| 123 | - let (name, value) = pair.split_once('=')?; | ||
| 124 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 125 | - }) | ||
| 126 | -} | ||
| 127 | - | ||
| 128 | /// The value of the first `<input name="...">` on a page. | 19 | /// The value of the first `<input name="...">` on a page. |
| 129 | fn run_admin(config: &Path, username: &str, extra: &[&str], password: &str) { | 20 | fn run_admin(config: &Path, username: &str, extra: &[&str], password: &str) { |
| 130 | let out = Command::new(BIN) | 21 | let out = Command::new(BIN) |
| @@ -241,28 +132,6 @@ fn read(path: PathBuf) -> String { | |||
| 241 | fs::read_to_string(&path).unwrap_or_else(|e| panic!("read {}: {e}", path.display())) | 132 | fs::read_to_string(&path).unwrap_or_else(|e| panic!("read {}: {e}", path.display())) |
| 242 | } | 133 | } |
| 243 | 134 | ||
| 244 | -/// Logs in through the real `/login` form and returns the `__Host-forge_session` cookie header. | ||
| 245 | -async fn login(http: &Http, username: &str, password: &str) -> String { | ||
| 246 | - let page = http.get("/login").await; | ||
| 247 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 248 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 249 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 250 | - let res = http | ||
| 251 | - .post_form( | ||
| 252 | - "/login", | ||
| 253 | - &[ | ||
| 254 | - ("username", username), | ||
| 255 | - ("password", password), | ||
| 256 | - ("next", "/"), | ||
| 257 | - ("precsrf", &precsrf), | ||
| 258 | - ], | ||
| 259 | - Some(&precsrf_cookie), | ||
| 260 | - ) | ||
| 261 | - .await; | ||
| 262 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 263 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 264 | -} | ||
| 265 | - | ||
| 266 | /// Creates an access token through `/settings/tokens` and returns the token shown once. | 135 | /// Creates an access token through `/settings/tokens` and returns the token shown once. |
| 267 | async fn create_token(http: &Http, session: &str, name: &str) -> String { | 136 | async fn create_token(http: &Http, session: &str, name: &str) -> String { |
| 268 | let page = http.get_cookie("/settings/tokens", session).await; | 137 | let page = http.get_cookie("/settings/tokens", session).await; |
| @@ -328,7 +197,7 @@ async fn create_repo(http: &Http, session: &str, name: &str, visibility: &str) { | |||
| 328 | .await; | 197 | .await; |
| 329 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); | 198 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); |
| 330 | assert_eq!( | 199 | assert_eq!( |
| 331 | - res.location.as_deref(), | 200 | + res.location().as_deref(), |
| 332 | Some(format!("/tahinli/{name}").as_str()) | 201 | Some(format!("/tahinli/{name}").as_str()) |
| 333 | ); | 202 | ); |
| 334 | } | 203 | } |
| @@ -438,10 +307,10 @@ async fn git_end_to_end() { | |||
| 438 | "anonymous clone of private repo", | 307 | "anonymous clone of private repo", |
| 439 | ); | 308 | ); |
| 440 | let refs_path = "/tahinli/priv.git/info/refs?service=git-upload-pack"; | 309 | let refs_path = "/tahinli/priv.git/info/refs?service=git-upload-pack"; |
| 441 | - let anon_refs = http.get(refs_path).await; | 310 | + let anon_refs = http.get(refs_path, None).await; |
| 442 | assert_eq!(anon_refs.status, 401, "anonymous info/refs on private repo"); | 311 | assert_eq!(anon_refs.status, 401, "anonymous info/refs on private repo"); |
| 443 | assert!( | 312 | assert!( |
| 444 | - anon_refs.www_authenticate.is_some(), | 313 | + anon_refs.header(WWW_AUTHENTICATE).is_some(), |
| 445 | "401 must carry WWW-Authenticate" | 314 | "401 must carry WWW-Authenticate" |
| 446 | ); | 315 | ); |
| 447 | 316 | ||
| @@ -545,7 +414,7 @@ async fn git_end_to_end() { | |||
| 545 | assert_eq!(no_csrf.status, 403, "POST /new without csrf"); | 414 | assert_eq!(no_csrf.status, 403, "POST /new without csrf"); |
| 546 | 415 | ||
| 547 | // Negative: the dumb protocol (info/refs without ?service=) is forbidden. | 416 | // Negative: the dumb protocol (info/refs without ?service=) is forbidden. |
| 548 | - let dumb = http.get("/tahinli/pub.git/info/refs").await; | 417 | + let dumb = http.get("/tahinli/pub.git/info/refs", None).await; |
| 549 | assert_eq!(dumb.status, 403, "info/refs without service"); | 418 | assert_eq!(dumb.status, 403, "info/refs without service"); |
| 550 | assert!( | 419 | assert!( |
| 551 | dumb.body.contains("dumb protocol"), | 420 | dumb.body.contains("dumb protocol"), |
crates/forge-web/tests/e2e/i18n_e2e.rsmodified+15 -109
| @@ -3,90 +3,14 @@ | |||
| 3 | 3 | ||
| 4 | use std::path::{Path, PathBuf}; | 4 | use std::path::{Path, PathBuf}; |
| 5 | use std::process::Command; | 5 | use std::process::Command; |
| 6 | -use std::time::Duration; | ||
| 7 | 6 | ||
| 8 | -use crate::common::{BIN, Server, describe, input_value, launch, pick_ports, write_config}; | 7 | +use crate::common::{ |
| 8 | + BIN, Http, Server, describe, input_value, launch, login, pick_ports, write_config, | ||
| 9 | +}; | ||
| 9 | use forge_core::Store; | 10 | use forge_core::Store; |
| 10 | -use reqwest::header::{ACCEPT_LANGUAGE, COOKIE, HeaderMap, SET_COOKIE}; | ||
| 11 | 11 | ||
| 12 | const PW: &str = "tahinli-password-123"; | 12 | const PW: &str = "tahinli-password-123"; |
| 13 | 13 | ||
| 14 | -struct Resp { | ||
| 15 | - status: u16, | ||
| 16 | - headers: HeaderMap, | ||
| 17 | - body: String, | ||
| 18 | -} | ||
| 19 | - | ||
| 20 | -impl Resp { | ||
| 21 | - fn set_cookies(&self) -> Vec<String> { | ||
| 22 | - self.headers | ||
| 23 | - .get_all(SET_COOKIE) | ||
| 24 | - .iter() | ||
| 25 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 26 | - .collect() | ||
| 27 | - } | ||
| 28 | -} | ||
| 29 | - | ||
| 30 | -struct Http { | ||
| 31 | - client: reqwest::Client, | ||
| 32 | - base: String, | ||
| 33 | -} | ||
| 34 | - | ||
| 35 | -impl Http { | ||
| 36 | - fn new(port: u16) -> Self { | ||
| 37 | - let client = reqwest::Client::builder() | ||
| 38 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 39 | - .timeout(Duration::from_secs(60)) | ||
| 40 | - .build() | ||
| 41 | - .expect("http client"); | ||
| 42 | - Self { | ||
| 43 | - client, | ||
| 44 | - base: format!("http://127.0.0.1:{port}"), | ||
| 45 | - } | ||
| 46 | - } | ||
| 47 | - | ||
| 48 | - async fn get(&self, path: &str, cookie: Option<&str>, language: Option<&str>) -> Resp { | ||
| 49 | - let mut req = self.client.get(format!("{}{path}", self.base)); | ||
| 50 | - if let Some(language) = language { | ||
| 51 | - req = req.header(ACCEPT_LANGUAGE, language); | ||
| 52 | - } | ||
| 53 | - self.send(req, cookie).await | ||
| 54 | - } | ||
| 55 | - | ||
| 56 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 57 | - self.send( | ||
| 58 | - self.client.post(format!("{}{path}", self.base)).form(form), | ||
| 59 | - cookie, | ||
| 60 | - ) | ||
| 61 | - .await | ||
| 62 | - } | ||
| 63 | - | ||
| 64 | - async fn send(&self, req: reqwest::RequestBuilder, cookie: Option<&str>) -> Resp { | ||
| 65 | - let req = match cookie { | ||
| 66 | - Some(cookie) => req.header(COOKIE, cookie), | ||
| 67 | - None => req, | ||
| 68 | - }; | ||
| 69 | - let res = req.send().await.expect("http request"); | ||
| 70 | - let status = res.status().as_u16(); | ||
| 71 | - let headers = res.headers().clone(); | ||
| 72 | - let body = res.text().await.expect("read body"); | ||
| 73 | - Resp { | ||
| 74 | - status, | ||
| 75 | - headers, | ||
| 76 | - body, | ||
| 77 | - } | ||
| 78 | - } | ||
| 79 | -} | ||
| 80 | - | ||
| 81 | -/// `name=value` for the cookie whose name ends with `suffix`, ignoring cleared cookies. | ||
| 82 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 83 | - resp.set_cookies().iter().find_map(|set| { | ||
| 84 | - let pair = set.split(';').next()?; | ||
| 85 | - let (name, value) = pair.split_once('=')?; | ||
| 86 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 87 | - }) | ||
| 88 | -} | ||
| 89 | - | ||
| 90 | /// The value of the first `<input name="...">` on a page. | 14 | /// The value of the first `<input name="...">` on a page. |
| 91 | /// Runs an admin subcommand with the password in FORGE_PASSWORD and asserts it succeeded. | 15 | /// Runs an admin subcommand with the password in FORGE_PASSWORD and asserts it succeeded. |
| 92 | fn admin(config: &Path, args: &[&str]) { | 16 | fn admin(config: &Path, args: &[&str]) { |
| @@ -105,27 +29,6 @@ fn admin(config: &Path, args: &[&str]) { | |||
| 105 | ); | 29 | ); |
| 106 | } | 30 | } |
| 107 | 31 | ||
| 108 | -async fn login(http: &Http, username: &str) -> String { | ||
| 109 | - let page = http.get("/login", None, None).await; | ||
| 110 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 111 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 112 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 113 | - let res = http | ||
| 114 | - .post_form( | ||
| 115 | - "/login", | ||
| 116 | - &[ | ||
| 117 | - ("username", username), | ||
| 118 | - ("password", PW), | ||
| 119 | - ("next", "/"), | ||
| 120 | - ("precsrf", &precsrf), | ||
| 121 | - ], | ||
| 122 | - Some(&precsrf_cookie), | ||
| 123 | - ) | ||
| 124 | - .await; | ||
| 125 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 126 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 127 | -} | ||
| 128 | - | ||
| 129 | /// A running server plus the temp dir it lives in. The dir lasts as long as the test. | 32 | /// A running server plus the temp dir it lives in. The dir lasts as long as the test. |
| 130 | struct Env { | 33 | struct Env { |
| 131 | _root: tempfile::TempDir, | 34 | _root: tempfile::TempDir, |
| @@ -163,7 +66,7 @@ async fn anonymous_pages_follow_the_browser_language() { | |||
| 163 | 66 | ||
| 164 | let turkish = env | 67 | let turkish = env |
| 165 | .http | 68 | .http |
| 166 | - .get("/login", None, Some("tr-TR,tr;q=0.9,en;q=0.8")) | 69 | + .get_language("/login", None, Some("tr-TR,tr;q=0.9,en;q=0.8")) |
| 167 | .await; | 70 | .await; |
| 168 | assert_eq!(turkish.status, 200); | 71 | assert_eq!(turkish.status, 200); |
| 169 | assert!( | 72 | assert!( |
| @@ -180,7 +83,10 @@ async fn anonymous_pages_follow_the_browser_language() { | |||
| 180 | "no English label left" | 83 | "no English label left" |
| 181 | ); | 84 | ); |
| 182 | 85 | ||
| 183 | - let english = env.http.get("/login", None, Some("en-US,en;q=0.9")).await; | 86 | + let english = env |
| 87 | + .http | ||
| 88 | + .get_language("/login", None, Some("en-US,en;q=0.9")) | ||
| 89 | + .await; | ||
| 184 | assert!( | 90 | assert!( |
| 185 | english.body.contains("<html lang=\"en\""), | 91 | english.body.contains("<html lang=\"en\""), |
| 186 | "html lang is en" | 92 | "html lang is en" |
| @@ -188,9 +94,9 @@ async fn anonymous_pages_follow_the_browser_language() { | |||
| 188 | assert!(english.body.contains("Username"), "English label"); | 94 | assert!(english.body.contains("Username"), "English label"); |
| 189 | 95 | ||
| 190 | // No header, and a language this build does not have: English. | 96 | // No header, and a language this build does not have: English. |
| 191 | - let plain = env.http.get("/login", None, None).await; | 97 | + let plain = env.http.get_language("/login", None, None).await; |
| 192 | assert!(plain.body.contains("<html lang=\"en\"")); | 98 | assert!(plain.body.contains("<html lang=\"en\"")); |
| 193 | - let german = env.http.get("/login", None, Some("de-DE")).await; | 99 | + let german = env.http.get_language("/login", None, Some("de-DE")).await; |
| 194 | assert!(german.body.contains("<html lang=\"en\"")); | 100 | assert!(german.body.contains("<html lang=\"en\"")); |
| 195 | } | 101 | } |
| 196 | 102 | ||
| @@ -208,12 +114,12 @@ async fn stored_language_beats_the_browser_for_a_signed_in_user() { | |||
| 208 | .expect("set language"); | 114 | .expect("set language"); |
| 209 | }) | 115 | }) |
| 210 | .await; | 116 | .await; |
| 211 | - let cookie = login(&env.http, "tahinli").await; | 117 | + let cookie = login(&env.http, "tahinli", PW).await; |
| 212 | 118 | ||
| 213 | // The browser asks for English, but tahinli's stored preference is Turkish. | 119 | // The browser asks for English, but tahinli's stored preference is Turkish. |
| 214 | let page = env | 120 | let page = env |
| 215 | .http | 121 | .http |
| 216 | - .get("/settings/security", Some(&cookie), Some("en-US,en;q=0.9")) | 122 | + .get_language("/settings/security", Some(&cookie), Some("en-US,en;q=0.9")) |
| 217 | .await; | 123 | .await; |
| 218 | assert_eq!(page.status, 200, "security settings page"); | 124 | assert_eq!(page.status, 200, "security settings page"); |
| 219 | assert!(page.body.contains("<html lang=\"tr\""), "html lang is tr"); | 125 | assert!(page.body.contains("<html lang=\"tr\""), "html lang is tr"); |
| @@ -235,9 +141,9 @@ async fn releases_page_follows_the_stored_turkish_language() { | |||
| 235 | .expect("set language"); | 141 | .expect("set language"); |
| 236 | }) | 142 | }) |
| 237 | .await; | 143 | .await; |
| 238 | - let cookie = login(&env.http, "tahinli").await; | 144 | + let cookie = login(&env.http, "tahinli", PW).await; |
| 239 | 145 | ||
| 240 | - let new_repo = env.http.get("/new", Some(&cookie), None).await; | 146 | + let new_repo = env.http.get_language("/new", Some(&cookie), None).await; |
| 241 | assert_eq!(new_repo.status, 200, "GET /new"); | 147 | assert_eq!(new_repo.status, 200, "GET /new"); |
| 242 | let csrf = input_value(&new_repo.body, "csrf"); | 148 | let csrf = input_value(&new_repo.body, "csrf"); |
| 243 | let created = env | 149 | let created = env |
| @@ -259,7 +165,7 @@ async fn releases_page_follows_the_stored_turkish_language() { | |||
| 259 | // The browser asks for English, but tahinli's stored preference is Turkish. | 165 | // The browser asks for English, but tahinli's stored preference is Turkish. |
| 260 | let releases = env | 166 | let releases = env |
| 261 | .http | 167 | .http |
| 262 | - .get("/tahinli/r/releases", Some(&cookie), Some("en-US,en;q=0.9")) | 168 | + .get_language("/tahinli/r/releases", Some(&cookie), Some("en-US,en;q=0.9")) |
| 263 | .await; | 169 | .await; |
| 264 | assert_eq!(releases.status, 200, "releases page"); | 170 | assert_eq!(releases.status, 200, "releases page"); |
| 265 | assert!( | 171 | assert!( |
crates/forge-web/tests/e2e/issue_close_e2e.rsmodified+4 -103
| @@ -3,7 +3,9 @@ | |||
| 3 | //! The server runs as a subprocess. Issues are created through the store on the same database | 3 | //! The server runs as a subprocess. Issues are created through the store on the same database |
| 4 | //! file, and every push is a real `git push` through the git HTTP endpoints. | 4 | //! file, and every push is a real `git push` through the git HTTP endpoints. |
| 5 | 5 | ||
| 6 | -use crate::common::{BIN, input_value, launch, pick_ports, relaunch, tool_on_path, write_config}; | 6 | +use crate::common::{ |
| 7 | + BIN, Http, input_value, launch, login, pick_ports, relaunch, tool_on_path, write_config, | ||
| 8 | +}; | ||
| 7 | use std::fs; | 9 | use std::fs; |
| 8 | use std::future::Future; | 10 | use std::future::Future; |
| 9 | use std::path::Path; | 11 | use std::path::Path; |
| @@ -11,89 +13,10 @@ use std::process::{Command, Output}; | |||
| 11 | use std::time::{Duration, Instant}; | 13 | use std::time::{Duration, Instant}; |
| 12 | 14 | ||
| 13 | use forge_core::Store; | 15 | use forge_core::Store; |
| 14 | -use reqwest::header::{LOCATION, SET_COOKIE}; | ||
| 15 | 16 | ||
| 16 | const TAHINLI_PW: &str = "tahinli-password-123"; | 17 | const TAHINLI_PW: &str = "tahinli-password-123"; |
| 17 | const BOB_PW: &str = "bob-password-4567"; | 18 | const BOB_PW: &str = "bob-password-4567"; |
| 18 | 19 | ||
| 19 | -struct Resp { | ||
| 20 | - status: u16, | ||
| 21 | - location: Option<String>, | ||
| 22 | - set_cookies: Vec<String>, | ||
| 23 | - body: String, | ||
| 24 | -} | ||
| 25 | - | ||
| 26 | -struct Http { | ||
| 27 | - client: reqwest::Client, | ||
| 28 | - base: String, | ||
| 29 | -} | ||
| 30 | - | ||
| 31 | -impl Http { | ||
| 32 | - fn new(port: u16) -> Self { | ||
| 33 | - let client = reqwest::Client::builder() | ||
| 34 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 35 | - .timeout(Duration::from_secs(60)) | ||
| 36 | - .build() | ||
| 37 | - .expect("http client"); | ||
| 38 | - Self { | ||
| 39 | - client, | ||
| 40 | - base: format!("http://127.0.0.1:{port}"), | ||
| 41 | - } | ||
| 42 | - } | ||
| 43 | - | ||
| 44 | - async fn get_cookie(&self, path: &str, cookie: &str) -> Resp { | ||
| 45 | - let req = self | ||
| 46 | - .client | ||
| 47 | - .get(format!("{}{path}", self.base)) | ||
| 48 | - .header(reqwest::header::COOKIE, cookie); | ||
| 49 | - send(req).await | ||
| 50 | - } | ||
| 51 | - | ||
| 52 | - async fn get(&self, path: &str) -> Resp { | ||
| 53 | - send(self.client.get(format!("{}{path}", self.base))).await | ||
| 54 | - } | ||
| 55 | - | ||
| 56 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 57 | - let mut req = self.client.post(format!("{}{path}", self.base)).form(form); | ||
| 58 | - if let Some(cookie) = cookie { | ||
| 59 | - req = req.header(reqwest::header::COOKIE, cookie); | ||
| 60 | - } | ||
| 61 | - send(req).await | ||
| 62 | - } | ||
| 63 | -} | ||
| 64 | - | ||
| 65 | -async fn send(req: reqwest::RequestBuilder) -> Resp { | ||
| 66 | - let res = req.send().await.expect("http request"); | ||
| 67 | - let status = res.status().as_u16(); | ||
| 68 | - let location = res | ||
| 69 | - .headers() | ||
| 70 | - .get(LOCATION) | ||
| 71 | - .and_then(|v| v.to_str().ok()) | ||
| 72 | - .map(str::to_owned); | ||
| 73 | - let set_cookies = res | ||
| 74 | - .headers() | ||
| 75 | - .get_all(SET_COOKIE) | ||
| 76 | - .iter() | ||
| 77 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 78 | - .collect(); | ||
| 79 | - let body = res.text().await.unwrap_or_default(); | ||
| 80 | - Resp { | ||
| 81 | - status, | ||
| 82 | - location, | ||
| 83 | - set_cookies, | ||
| 84 | - body, | ||
| 85 | - } | ||
| 86 | -} | ||
| 87 | - | ||
| 88 | -/// Returns `name=value` for the cookie whose name ends with `suffix`. Cleared cookies are ignored. | ||
| 89 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 90 | - resp.set_cookies.iter().find_map(|set| { | ||
| 91 | - let pair = set.split(';').next()?; | ||
| 92 | - let (name, value) = pair.split_once('=')?; | ||
| 93 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 94 | - }) | ||
| 95 | -} | ||
| 96 | - | ||
| 97 | /// The value of the first `<input name="...">` on a page. | 20 | /// The value of the first `<input name="...">` on a page. |
| 98 | fn run_admin(config: &Path, username: &str, extra: &[&str], password: &str) { | 21 | fn run_admin(config: &Path, username: &str, extra: &[&str], password: &str) { |
| 99 | let out = Command::new(BIN) | 22 | let out = Command::new(BIN) |
| @@ -156,28 +79,6 @@ fn commit_file(repo: &Path, name: &str, message: &str) -> String { | |||
| 156 | git(repo, &["rev-parse", "HEAD"]) | 79 | git(repo, &["rev-parse", "HEAD"]) |
| 157 | } | 80 | } |
| 158 | 81 | ||
| 159 | -/// Logs in through the real `/login` form and returns the session cookie header. | ||
| 160 | -async fn login(http: &Http, username: &str, password: &str) -> String { | ||
| 161 | - let page = http.get("/login").await; | ||
| 162 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 163 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 164 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 165 | - let res = http | ||
| 166 | - .post_form( | ||
| 167 | - "/login", | ||
| 168 | - &[ | ||
| 169 | - ("username", username), | ||
| 170 | - ("password", password), | ||
| 171 | - ("next", "/"), | ||
| 172 | - ("precsrf", &precsrf), | ||
| 173 | - ], | ||
| 174 | - Some(&precsrf_cookie), | ||
| 175 | - ) | ||
| 176 | - .await; | ||
| 177 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 178 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 179 | -} | ||
| 180 | - | ||
| 181 | /// Creates an access token through `/settings/tokens` and returns the token shown once. | 82 | /// Creates an access token through `/settings/tokens` and returns the token shown once. |
| 182 | async fn create_token(http: &Http, session: &str, name: &str) -> String { | 83 | async fn create_token(http: &Http, session: &str, name: &str) -> String { |
| 183 | let page = http.get_cookie("/settings/tokens", session).await; | 84 | let page = http.get_cookie("/settings/tokens", session).await; |
| @@ -224,7 +125,7 @@ async fn create_repo(http: &Http, session: &str, name: &str) { | |||
| 224 | .await; | 125 | .await; |
| 225 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); | 126 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); |
| 226 | assert_eq!( | 127 | assert_eq!( |
| 227 | - res.location.as_deref(), | 128 | + res.location().as_deref(), |
| 228 | Some(format!("/tahinli/{name}").as_str()) | 129 | Some(format!("/tahinli/{name}").as_str()) |
| 229 | ); | 130 | ); |
| 230 | } | 131 | } |
crates/forge-web/tests/e2e/lfs_e2e.rsmodified+2 -103
| @@ -5,93 +5,14 @@ | |||
| 5 | //! Git config is isolated from the developer's machine: `HOME` and `GIT_CONFIG_GLOBAL` point into | 5 | //! Git config is isolated from the developer's machine: `HOME` and `GIT_CONFIG_GLOBAL` point into |
| 6 | //! the test's temp dir. | 6 | //! the test's temp dir. |
| 7 | 7 | ||
| 8 | -use crate::common::{BIN, describe, input_value, launch, pick_ports, write_config}; | 8 | +use crate::common::{BIN, Http, describe, input_value, launch, login, pick_ports, write_config}; |
| 9 | use std::fs; | 9 | use std::fs; |
| 10 | use std::path::Path; | 10 | use std::path::Path; |
| 11 | use std::process::{Command, Output}; | 11 | use std::process::{Command, Output}; |
| 12 | -use std::time::Duration; | ||
| 13 | - | ||
| 14 | -use reqwest::header::{LOCATION, SET_COOKIE}; | ||
| 15 | 12 | ||
| 16 | const TAHINLI_PW: &str = "tahinli-password-123"; | 13 | const TAHINLI_PW: &str = "tahinli-password-123"; |
| 17 | const FILE_SIZE: usize = 100 * 1024; | 14 | const FILE_SIZE: usize = 100 * 1024; |
| 18 | 15 | ||
| 19 | -/// A parsed HTTP response. Only the parts the test needs are kept. | ||
| 20 | -struct Resp { | ||
| 21 | - status: u16, | ||
| 22 | - location: Option<String>, | ||
| 23 | - set_cookies: Vec<String>, | ||
| 24 | - body: String, | ||
| 25 | -} | ||
| 26 | - | ||
| 27 | -struct Http { | ||
| 28 | - client: reqwest::Client, | ||
| 29 | - base: String, | ||
| 30 | -} | ||
| 31 | - | ||
| 32 | -impl Http { | ||
| 33 | - fn new(port: u16) -> Self { | ||
| 34 | - let client = reqwest::Client::builder() | ||
| 35 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 36 | - .timeout(Duration::from_secs(60)) | ||
| 37 | - .build() | ||
| 38 | - .expect("http client"); | ||
| 39 | - Self { | ||
| 40 | - client, | ||
| 41 | - base: format!("http://127.0.0.1:{port}"), | ||
| 42 | - } | ||
| 43 | - } | ||
| 44 | - | ||
| 45 | - async fn get(&self, path: &str, cookie: Option<&str>) -> Resp { | ||
| 46 | - self.send(self.client.get(format!("{}{path}", self.base)), cookie) | ||
| 47 | - .await | ||
| 48 | - } | ||
| 49 | - | ||
| 50 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 51 | - self.send( | ||
| 52 | - self.client.post(format!("{}{path}", self.base)).form(form), | ||
| 53 | - cookie, | ||
| 54 | - ) | ||
| 55 | - .await | ||
| 56 | - } | ||
| 57 | - | ||
| 58 | - async fn send(&self, req: reqwest::RequestBuilder, cookie: Option<&str>) -> Resp { | ||
| 59 | - let req = match cookie { | ||
| 60 | - Some(cookie) => req.header(reqwest::header::COOKIE, cookie), | ||
| 61 | - None => req, | ||
| 62 | - }; | ||
| 63 | - let res = req.send().await.expect("http request"); | ||
| 64 | - let status = res.status().as_u16(); | ||
| 65 | - let location = res | ||
| 66 | - .headers() | ||
| 67 | - .get(LOCATION) | ||
| 68 | - .and_then(|v| v.to_str().ok()) | ||
| 69 | - .map(str::to_owned); | ||
| 70 | - let set_cookies = res | ||
| 71 | - .headers() | ||
| 72 | - .get_all(SET_COOKIE) | ||
| 73 | - .iter() | ||
| 74 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 75 | - .collect(); | ||
| 76 | - let body = res.text().await.unwrap_or_default(); | ||
| 77 | - Resp { | ||
| 78 | - status, | ||
| 79 | - location, | ||
| 80 | - set_cookies, | ||
| 81 | - body, | ||
| 82 | - } | ||
| 83 | - } | ||
| 84 | -} | ||
| 85 | - | ||
| 86 | -/// Returns `name=value` for the cookie whose name ends with `suffix`. Cleared cookies are ignored. | ||
| 87 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 88 | - resp.set_cookies.iter().find_map(|set| { | ||
| 89 | - let pair = set.split(';').next()?; | ||
| 90 | - let (name, value) = pair.split_once('=')?; | ||
| 91 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 92 | - }) | ||
| 93 | -} | ||
| 94 | - | ||
| 95 | /// The value of the first `<input name="...">` on a page. | 16 | /// The value of the first `<input name="...">` on a page. |
| 96 | fn run_admin(config: &Path, username: &str, extra: &[&str], password: &str) { | 17 | fn run_admin(config: &Path, username: &str, extra: &[&str], password: &str) { |
| 97 | let out = Command::new(BIN) | 18 | let out = Command::new(BIN) |
| @@ -140,28 +61,6 @@ fn payload() -> Vec<u8> { | |||
| 140 | .collect() | 61 | .collect() |
| 141 | } | 62 | } |
| 142 | 63 | ||
| 143 | -/// Logs in through the real `/login` form and returns the `__Host-forge_session` cookie header. | ||
| 144 | -async fn login(http: &Http, username: &str, password: &str) -> String { | ||
| 145 | - let page = http.get("/login", None).await; | ||
| 146 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 147 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 148 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 149 | - let res = http | ||
| 150 | - .post_form( | ||
| 151 | - "/login", | ||
| 152 | - &[ | ||
| 153 | - ("username", username), | ||
| 154 | - ("password", password), | ||
| 155 | - ("next", "/"), | ||
| 156 | - ("precsrf", &precsrf), | ||
| 157 | - ], | ||
| 158 | - Some(&precsrf_cookie), | ||
| 159 | - ) | ||
| 160 | - .await; | ||
| 161 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 162 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 163 | -} | ||
| 164 | - | ||
| 165 | /// Creates an access token through `/settings/tokens` and returns the token shown once. | 64 | /// Creates an access token through `/settings/tokens` and returns the token shown once. |
| 166 | async fn create_token(http: &Http, session: &str, name: &str) -> String { | 65 | async fn create_token(http: &Http, session: &str, name: &str) -> String { |
| 167 | let page = http.get("/settings/tokens", Some(session)).await; | 66 | let page = http.get("/settings/tokens", Some(session)).await; |
| @@ -208,7 +107,7 @@ async fn create_repo(http: &Http, session: &str, name: &str, visibility: &str) { | |||
| 208 | .await; | 107 | .await; |
| 209 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); | 108 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); |
| 210 | assert_eq!( | 109 | assert_eq!( |
| 211 | - res.location.as_deref(), | 110 | + res.location().as_deref(), |
| 212 | Some(format!("/tahinli/{name}").as_str()) | 111 | Some(format!("/tahinli/{name}").as_str()) |
| 213 | ); | 112 | ); |
| 214 | } | 113 | } |
crates/forge-web/tests/e2e/merge_queue_e2e.rsmodified+4 -111
| @@ -10,7 +10,8 @@ | |||
| 10 | #![allow(dead_code)] | 10 | #![allow(dead_code)] |
| 11 | 11 | ||
| 12 | use crate::common::{ | 12 | use crate::common::{ |
| 13 | - BIN, FAST_CLAIMS, Server, input_value, launch_with, pick_ports, tool_on_path, write_config, | 13 | + BIN, FAST_CLAIMS, Http, Server, input_value, launch_with, login, pick_ports, tool_on_path, |
| 14 | + write_config, | ||
| 14 | }; | 15 | }; |
| 15 | use forge_ci::proto::{JobAssignment, RegisterRequest}; | 16 | use forge_ci::proto::{JobAssignment, RegisterRequest}; |
| 16 | use forge_runner::client::ForgeClient; | 17 | use forge_runner::client::ForgeClient; |
| @@ -25,98 +26,12 @@ use std::sync::Arc; | |||
| 25 | use std::time::{Duration, Instant}; | 26 | use std::time::{Duration, Instant}; |
| 26 | use tokio_util::sync::CancellationToken; | 27 | use tokio_util::sync::CancellationToken; |
| 27 | 28 | ||
| 28 | -use reqwest::header::{LOCATION, SET_COOKIE}; | ||
| 29 | - | ||
| 30 | const TAHINLI_PW: &str = "tahinli-password-123"; | 29 | const TAHINLI_PW: &str = "tahinli-password-123"; |
| 31 | const BOB_PW: &str = "bob-password-4567"; | 30 | const BOB_PW: &str = "bob-password-4567"; |
| 32 | const BOB_EMAIL: &str = "bob@users.noreply.127.0.0.1"; | 31 | const BOB_EMAIL: &str = "bob@users.noreply.127.0.0.1"; |
| 33 | const REPO_PATH: &str = "/tahinli/r"; | 32 | const REPO_PATH: &str = "/tahinli/r"; |
| 34 | const LABELS: &[&str] = &["linux"]; | 33 | const LABELS: &[&str] = &["linux"]; |
| 35 | 34 | ||
| 36 | -/// A parsed HTTP response. Only the parts the test needs are kept. | ||
| 37 | -struct Resp { | ||
| 38 | - status: u16, | ||
| 39 | - location: Option<String>, | ||
| 40 | - set_cookies: Vec<String>, | ||
| 41 | - body: String, | ||
| 42 | -} | ||
| 43 | - | ||
| 44 | -struct Http { | ||
| 45 | - client: reqwest::Client, | ||
| 46 | - base: String, | ||
| 47 | -} | ||
| 48 | - | ||
| 49 | -impl Http { | ||
| 50 | - fn new(port: u16) -> Self { | ||
| 51 | - let client = reqwest::Client::builder() | ||
| 52 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 53 | - .timeout(Duration::from_secs(60)) | ||
| 54 | - .build() | ||
| 55 | - .expect("http client"); | ||
| 56 | - Self { | ||
| 57 | - client, | ||
| 58 | - base: format!("http://127.0.0.1:{port}"), | ||
| 59 | - } | ||
| 60 | - } | ||
| 61 | - | ||
| 62 | - async fn get(&self, path: &str, cookie: Option<&str>) -> Resp { | ||
| 63 | - self.send(self.client.get(format!("{}{path}", self.base)), cookie) | ||
| 64 | - .await | ||
| 65 | - } | ||
| 66 | - | ||
| 67 | - async fn get_bearer(&self, path: &str, token: &str) -> Resp { | ||
| 68 | - let req = self | ||
| 69 | - .client | ||
| 70 | - .get(format!("{}{path}", self.base)) | ||
| 71 | - .bearer_auth(token); | ||
| 72 | - self.send(req, None).await | ||
| 73 | - } | ||
| 74 | - | ||
| 75 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 76 | - self.send( | ||
| 77 | - self.client.post(format!("{}{path}", self.base)).form(form), | ||
| 78 | - cookie, | ||
| 79 | - ) | ||
| 80 | - .await | ||
| 81 | - } | ||
| 82 | - | ||
| 83 | - async fn send(&self, req: reqwest::RequestBuilder, cookie: Option<&str>) -> Resp { | ||
| 84 | - let req = match cookie { | ||
| 85 | - Some(cookie) => req.header(reqwest::header::COOKIE, cookie), | ||
| 86 | - None => req, | ||
| 87 | - }; | ||
| 88 | - let res = req.send().await.expect("http request"); | ||
| 89 | - let status = res.status().as_u16(); | ||
| 90 | - let location = res | ||
| 91 | - .headers() | ||
| 92 | - .get(LOCATION) | ||
| 93 | - .and_then(|v| v.to_str().ok()) | ||
| 94 | - .map(str::to_owned); | ||
| 95 | - let set_cookies = res | ||
| 96 | - .headers() | ||
| 97 | - .get_all(SET_COOKIE) | ||
| 98 | - .iter() | ||
| 99 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 100 | - .collect(); | ||
| 101 | - let body = res.text().await.unwrap_or_default(); | ||
| 102 | - Resp { | ||
| 103 | - status, | ||
| 104 | - location, | ||
| 105 | - set_cookies, | ||
| 106 | - body, | ||
| 107 | - } | ||
| 108 | - } | ||
| 109 | -} | ||
| 110 | - | ||
| 111 | -/// Returns `name=value` for the cookie whose name ends with `suffix`. Cleared cookies are ignored. | ||
| 112 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 113 | - resp.set_cookies.iter().find_map(|set| { | ||
| 114 | - let pair = set.split(';').next()?; | ||
| 115 | - let (name, value) = pair.split_once('=')?; | ||
| 116 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 117 | - }) | ||
| 118 | -} | ||
| 119 | - | ||
| 120 | /// The value of the first `<input name="...">` on a page. | 35 | /// The value of the first `<input name="...">` on a page. |
| 121 | /// The text of the first element matching `selector`. | 36 | /// The text of the first element matching `selector`. |
| 122 | fn first_text(body: &str, selector: &str) -> String { | 37 | fn first_text(body: &str, selector: &str) -> String { |
| @@ -225,28 +140,6 @@ async fn csrf_from(http: &Http, path: &str, session: &str) -> String { | |||
| 225 | input_value(&page.body, "csrf") | 140 | input_value(&page.body, "csrf") |
| 226 | } | 141 | } |
| 227 | 142 | ||
| 228 | -/// Logs in through the real `/login` form and returns the `__Host-forge_session` cookie header. | ||
| 229 | -async fn login(http: &Http, username: &str, password: &str) -> String { | ||
| 230 | - let page = http.get("/login", None).await; | ||
| 231 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 232 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 233 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 234 | - let res = http | ||
| 235 | - .post_form( | ||
| 236 | - "/login", | ||
| 237 | - &[ | ||
| 238 | - ("username", username), | ||
| 239 | - ("password", password), | ||
| 240 | - ("next", "/"), | ||
| 241 | - ("precsrf", &precsrf), | ||
| 242 | - ], | ||
| 243 | - Some(&precsrf_cookie), | ||
| 244 | - ) | ||
| 245 | - .await; | ||
| 246 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 247 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 248 | -} | ||
| 249 | - | ||
| 250 | /// Creates an access token through `/settings/tokens` and returns the token shown once. | 143 | /// Creates an access token through `/settings/tokens` and returns the token shown once. |
| 251 | async fn create_token(http: &Http, session: &str, name: &str) -> String { | 144 | async fn create_token(http: &Http, session: &str, name: &str) -> String { |
| 252 | let csrf = csrf_from(http, "/settings/tokens", session).await; | 145 | let csrf = csrf_from(http, "/settings/tokens", session).await; |
| @@ -283,7 +176,7 @@ async fn create_repo(http: &Http, session: &str, name: &str) { | |||
| 283 | .await; | 176 | .await; |
| 284 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); | 177 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); |
| 285 | assert_eq!( | 178 | assert_eq!( |
| 286 | - res.location.as_deref(), | 179 | + res.location().as_deref(), |
| 287 | Some(format!("/tahinli/{name}").as_str()) | 180 | Some(format!("/tahinli/{name}").as_str()) |
| 288 | ); | 181 | ); |
| 289 | } | 182 | } |
| @@ -569,7 +462,7 @@ async fn open_pull(http: &Http, session: &str, work: &Path, branch: &str, file: | |||
| 569 | ) | 462 | ) |
| 570 | .await; | 463 | .await; |
| 571 | assert_eq!(res.status, 303, "open pull for {branch}: {}", res.body); | 464 | assert_eq!(res.status, 303, "open pull for {branch}: {}", res.body); |
| 572 | - let location = res.location.expect("pull location"); | 465 | + let location = res.location().expect("pull location"); |
| 573 | location | 466 | location |
| 574 | .rsplit('/') | 467 | .rsplit('/') |
| 575 | .next() | 468 | .next() |
crates/forge-web/tests/e2e/mirrors_e2e.rsmodified+2 -100
| @@ -10,7 +10,7 @@ | |||
| 10 | //! - the mirror pages and the server log never contain the access token. | 10 | //! - the mirror pages and the server log never contain the access token. |
| 11 | 11 | ||
| 12 | use crate::common::{ | 12 | use crate::common::{ |
| 13 | - BIN, Server, input_value, launch_with, pick_ports, tool_on_path, write_config_with, | 13 | + BIN, Http, Server, input_value, launch_with, login, pick_ports, tool_on_path, write_config_with, |
| 14 | }; | 14 | }; |
| 15 | use std::collections::BTreeSet; | 15 | use std::collections::BTreeSet; |
| 16 | use std::fs; | 16 | use std::fs; |
| @@ -18,8 +18,6 @@ use std::path::{Path, PathBuf}; | |||
| 18 | use std::process::{Command, Output}; | 18 | use std::process::{Command, Output}; |
| 19 | use std::time::{Duration, Instant}; | 19 | use std::time::{Duration, Instant}; |
| 20 | 20 | ||
| 21 | -use reqwest::header::{LOCATION, SET_COOKIE}; | ||
| 22 | - | ||
| 23 | const TAHINLI_PW: &str = "tahinli-password-123"; | 21 | const TAHINLI_PW: &str = "tahinli-password-123"; |
| 24 | const WEBHOOK_TOML: &str = "\n[webhooks]\nallow_http = true\nallowed_hosts = [\"127.0.0.1\"]\n\n[mirrors]\npush_debounce_secs = 0\n"; | 22 | const WEBHOOK_TOML: &str = "\n[webhooks]\nallow_http = true\nallowed_hosts = [\"127.0.0.1\"]\n\n[mirrors]\npush_debounce_secs = 0\n"; |
| 25 | const SYNC_TIMEOUT: Duration = Duration::from_secs(30); | 23 | const SYNC_TIMEOUT: Duration = Duration::from_secs(30); |
| @@ -27,80 +25,6 @@ const SYNC_TIMEOUT: Duration = Duration::from_secs(30); | |||
| 27 | /// worker wakes on the push itself, so this only needs room for the git sync. | 25 | /// worker wakes on the push itself, so this only needs room for the git sync. |
| 28 | const PUSH_TIMEOUT: Duration = Duration::from_secs(90); | 26 | const PUSH_TIMEOUT: Duration = Duration::from_secs(90); |
| 29 | 27 | ||
| 30 | -struct Resp { | ||
| 31 | - status: u16, | ||
| 32 | - location: Option<String>, | ||
| 33 | - set_cookies: Vec<String>, | ||
| 34 | - body: String, | ||
| 35 | -} | ||
| 36 | - | ||
| 37 | -struct Http { | ||
| 38 | - client: reqwest::Client, | ||
| 39 | - base: String, | ||
| 40 | -} | ||
| 41 | - | ||
| 42 | -impl Http { | ||
| 43 | - fn new(port: u16) -> Self { | ||
| 44 | - let client = reqwest::Client::builder() | ||
| 45 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 46 | - .timeout(Duration::from_secs(60)) | ||
| 47 | - .build() | ||
| 48 | - .expect("http client"); | ||
| 49 | - Self { | ||
| 50 | - client, | ||
| 51 | - base: format!("http://127.0.0.1:{port}"), | ||
| 52 | - } | ||
| 53 | - } | ||
| 54 | - | ||
| 55 | - async fn get_cookie(&self, path: &str, cookie: &str) -> Resp { | ||
| 56 | - let req = self | ||
| 57 | - .client | ||
| 58 | - .get(format!("{}{path}", self.base)) | ||
| 59 | - .header(reqwest::header::COOKIE, cookie); | ||
| 60 | - send(req).await | ||
| 61 | - } | ||
| 62 | - | ||
| 63 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 64 | - let mut req = self.client.post(format!("{}{path}", self.base)).form(form); | ||
| 65 | - if let Some(cookie) = cookie { | ||
| 66 | - req = req.header(reqwest::header::COOKIE, cookie); | ||
| 67 | - } | ||
| 68 | - send(req).await | ||
| 69 | - } | ||
| 70 | -} | ||
| 71 | - | ||
| 72 | -async fn send(req: reqwest::RequestBuilder) -> Resp { | ||
| 73 | - let res = req.send().await.expect("http request"); | ||
| 74 | - let status = res.status().as_u16(); | ||
| 75 | - let location = res | ||
| 76 | - .headers() | ||
| 77 | - .get(LOCATION) | ||
| 78 | - .and_then(|v| v.to_str().ok()) | ||
| 79 | - .map(str::to_owned); | ||
| 80 | - let set_cookies = res | ||
| 81 | - .headers() | ||
| 82 | - .get_all(SET_COOKIE) | ||
| 83 | - .iter() | ||
| 84 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 85 | - .collect(); | ||
| 86 | - let body = res.text().await.unwrap_or_default(); | ||
| 87 | - Resp { | ||
| 88 | - status, | ||
| 89 | - location, | ||
| 90 | - set_cookies, | ||
| 91 | - body, | ||
| 92 | - } | ||
| 93 | -} | ||
| 94 | - | ||
| 95 | -/// Returns `name=value` for the cookie whose name ends with `suffix`. Cleared cookies are ignored. | ||
| 96 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 97 | - resp.set_cookies.iter().find_map(|set| { | ||
| 98 | - let pair = set.split(';').next()?; | ||
| 99 | - let (name, value) = pair.split_once('=')?; | ||
| 100 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 101 | - }) | ||
| 102 | -} | ||
| 103 | - | ||
| 104 | /// The value of the first `<input name="...">` on a page. | 28 | /// The value of the first `<input name="...">` on a page. |
| 105 | /// The `action` of every form whose action ends with `suffix`, in page order. | 29 | /// The `action` of every form whose action ends with `suffix`, in page order. |
| 106 | fn form_actions(body: &str, suffix: &str) -> Vec<String> { | 30 | fn form_actions(body: &str, suffix: &str) -> Vec<String> { |
| @@ -244,28 +168,6 @@ async fn wait_for(what: &str, check: impl Fn() -> bool) { | |||
| 244 | } | 168 | } |
| 245 | } | 169 | } |
| 246 | 170 | ||
| 247 | -/// Logs in through the real `/login` form and returns the session cookie header. | ||
| 248 | -async fn login(http: &Http, username: &str, password: &str) -> String { | ||
| 249 | - let page = http.get_cookie("/login", "").await; | ||
| 250 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 251 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 252 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 253 | - let res = http | ||
| 254 | - .post_form( | ||
| 255 | - "/login", | ||
| 256 | - &[ | ||
| 257 | - ("username", username), | ||
| 258 | - ("password", password), | ||
| 259 | - ("next", "/"), | ||
| 260 | - ("precsrf", &precsrf), | ||
| 261 | - ], | ||
| 262 | - Some(&precsrf_cookie), | ||
| 263 | - ) | ||
| 264 | - .await; | ||
| 265 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 266 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 267 | -} | ||
| 268 | - | ||
| 269 | /// The session's CSRF token, scraped from the token settings page. | 171 | /// The session's CSRF token, scraped from the token settings page. |
| 270 | async fn csrf_token(http: &Http, session: &str) -> String { | 172 | async fn csrf_token(http: &Http, session: &str) -> String { |
| 271 | let page = http.get_cookie("/settings/tokens", session).await; | 173 | let page = http.get_cookie("/settings/tokens", session).await; |
| @@ -320,7 +222,7 @@ async fn new_repo(http: &Http, session: &str, name: &str, mirror: Option<(&str, | |||
| 320 | let res = http.post_form("/new", &form, Some(session)).await; | 222 | let res = http.post_form("/new", &form, Some(session)).await; |
| 321 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); | 223 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); |
| 322 | assert_eq!( | 224 | assert_eq!( |
| 323 | - res.location.as_deref(), | 225 | + res.location().as_deref(), |
| 324 | Some(format!("/tahinli/{name}").as_str()) | 226 | Some(format!("/tahinli/{name}").as_str()) |
| 325 | ); | 227 | ); |
| 326 | } | 228 | } |
crates/forge-web/tests/e2e/orgs_core_e2e.rsmodified+3 -76
| @@ -3,7 +3,9 @@ | |||
| 3 | //! this starts the real `forge` binary and drives it over HTTP. The org rows and their credentials | 3 | //! this starts the real `forge` binary and drives it over HTTP. The org rows and their credentials |
| 4 | //! are written straight into the database before the server starts. | 4 | //! are written straight into the database before the server starts. |
| 5 | 5 | ||
| 6 | -use crate::common::{BIN, Server, describe, input_value, launch, pick_ports, write_config}; | 6 | +use crate::common::{ |
| 7 | + BIN, Http, Resp, Server, cookie_named, describe, input_value, launch, pick_ports, write_config, | ||
| 8 | +}; | ||
| 7 | use std::future::Future; | 9 | use std::future::Future; |
| 8 | use std::path::{Path, PathBuf}; | 10 | use std::path::{Path, PathBuf}; |
| 9 | use std::process::Command; | 11 | use std::process::Command; |
| @@ -11,7 +13,6 @@ use std::time::Duration; | |||
| 11 | 13 | ||
| 12 | use forge_core::token::hash_token; | 14 | use forge_core::token::hash_token; |
| 13 | use forge_core::{Store, Visibility}; | 15 | use forge_core::{Store, Visibility}; |
| 14 | -use reqwest::header::{COOKIE, HeaderMap, SET_COOKIE}; | ||
| 15 | 16 | ||
| 16 | /// Each test starts a server on ports picked just before, so the tests run one at a time. | 17 | /// Each test starts a server on ports picked just before, so the tests run one at a time. |
| 17 | static SERIAL: std::sync::LazyLock<tokio::sync::Mutex<()>> = | 18 | static SERIAL: std::sync::LazyLock<tokio::sync::Mutex<()>> = |
| @@ -22,80 +23,6 @@ const PW: &str = "tahinli-password-123"; | |||
| 22 | const ORG_TOKEN: &str = "forge_org_token_for_tests_0123456789"; | 23 | const ORG_TOKEN: &str = "forge_org_token_for_tests_0123456789"; |
| 23 | const ORG_SESSION: &str = "org-session-token-for-tests-0123456789"; | 24 | const ORG_SESSION: &str = "org-session-token-for-tests-0123456789"; |
| 24 | 25 | ||
| 25 | -struct Resp { | ||
| 26 | - status: u16, | ||
| 27 | - headers: HeaderMap, | ||
| 28 | - body: String, | ||
| 29 | -} | ||
| 30 | - | ||
| 31 | -impl Resp { | ||
| 32 | - fn set_cookies(&self) -> Vec<String> { | ||
| 33 | - self.headers | ||
| 34 | - .get_all(SET_COOKIE) | ||
| 35 | - .iter() | ||
| 36 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 37 | - .collect() | ||
| 38 | - } | ||
| 39 | -} | ||
| 40 | - | ||
| 41 | -struct Http { | ||
| 42 | - client: reqwest::Client, | ||
| 43 | - base: String, | ||
| 44 | -} | ||
| 45 | - | ||
| 46 | -impl Http { | ||
| 47 | - fn new(port: u16) -> Self { | ||
| 48 | - let client = reqwest::Client::builder() | ||
| 49 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 50 | - .timeout(Duration::from_secs(60)) | ||
| 51 | - .build() | ||
| 52 | - .expect("http client"); | ||
| 53 | - Self { | ||
| 54 | - client, | ||
| 55 | - base: format!("http://127.0.0.1:{port}"), | ||
| 56 | - } | ||
| 57 | - } | ||
| 58 | - | ||
| 59 | - async fn get(&self, path: &str, cookie: Option<&str>) -> Resp { | ||
| 60 | - let req = self.client.get(format!("{}{path}", self.base)); | ||
| 61 | - let req = match cookie { | ||
| 62 | - Some(cookie) => req.header(COOKIE, cookie), | ||
| 63 | - None => req, | ||
| 64 | - }; | ||
| 65 | - Self::finish(req).await | ||
| 66 | - } | ||
| 67 | - | ||
| 68 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 69 | - let req = self.client.post(format!("{}{path}", self.base)).form(form); | ||
| 70 | - let req = match cookie { | ||
| 71 | - Some(cookie) => req.header(COOKIE, cookie), | ||
| 72 | - None => req, | ||
| 73 | - }; | ||
| 74 | - Self::finish(req).await | ||
| 75 | - } | ||
| 76 | - | ||
| 77 | - async fn finish(req: reqwest::RequestBuilder) -> Resp { | ||
| 78 | - let res = req.send().await.expect("http request"); | ||
| 79 | - let status = res.status().as_u16(); | ||
| 80 | - let headers = res.headers().clone(); | ||
| 81 | - let body = res.text().await.expect("read body"); | ||
| 82 | - Resp { | ||
| 83 | - status, | ||
| 84 | - headers, | ||
| 85 | - body, | ||
| 86 | - } | ||
| 87 | - } | ||
| 88 | -} | ||
| 89 | - | ||
| 90 | -/// `name=value` for the cookie whose name ends with `suffix`, ignoring cleared cookies. | ||
| 91 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 92 | - resp.set_cookies().iter().find_map(|set| { | ||
| 93 | - let pair = set.split(';').next()?; | ||
| 94 | - let (name, value) = pair.split_once('=')?; | ||
| 95 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 96 | - }) | ||
| 97 | -} | ||
| 98 | - | ||
| 99 | /// The value of the first `<input name="...">` on a page. | 26 | /// The value of the first `<input name="...">` on a page. |
| 100 | /// Runs an admin subcommand with the password in FORGE_PASSWORD and asserts it succeeded. | 27 | /// Runs an admin subcommand with the password in FORGE_PASSWORD and asserts it succeeded. |
| 101 | fn admin(config: &Path, args: &[&str]) { | 28 | fn admin(config: &Path, args: &[&str]) { |
crates/forge-web/tests/e2e/orgs_live_e2e.rsmodified+8 -127
| @@ -2,14 +2,15 @@ | |||
| 2 | //! with a seeded org (owner tahinli, member bob, a team), and the test drives the writes over HTTP | 2 | //! with a seeded org (owner tahinli, member bob, a team), and the test drives the writes over HTTP |
| 3 | //! and reads each stream. It also checks that each stream refuses the viewers its page refuses. | 3 | //! and reads each stream. It also checks that each stream refuses the viewers its page refuses. |
| 4 | 4 | ||
| 5 | -use crate::common::{BIN, describe, input_value, launch, pick_ports, tool_on_path, write_config}; | 5 | +use crate::common::{ |
| 6 | + BIN, Http, describe, input_value, launch, login, pick_ports, tool_on_path, write_config, | ||
| 7 | +}; | ||
| 6 | use std::path::Path; | 8 | use std::path::Path; |
| 7 | use std::process::{Command, Output}; | 9 | use std::process::{Command, Output}; |
| 8 | use std::time::Duration; | 10 | use std::time::Duration; |
| 9 | 11 | ||
| 10 | use forge_core::store::orgs::{DefaultRepoRole, OrgMemberRole, OrgSettings}; | 12 | use forge_core::store::orgs::{DefaultRepoRole, OrgMemberRole, OrgSettings}; |
| 11 | use forge_core::{Role, Store, Visibility}; | 13 | use forge_core::{Role, Store, Visibility}; |
| 12 | -use reqwest::header::{COOKIE, HeaderMap, LOCATION, SET_COOKIE}; | ||
| 13 | 14 | ||
| 14 | /// Each test starts its own server, so the tests run one at a time. | 15 | /// Each test starts its own server, so the tests run one at a time. |
| 15 | static SERIAL: std::sync::LazyLock<tokio::sync::Mutex<()>> = | 16 | static SERIAL: std::sync::LazyLock<tokio::sync::Mutex<()>> = |
| @@ -17,105 +18,6 @@ static SERIAL: std::sync::LazyLock<tokio::sync::Mutex<()>> = | |||
| 17 | 18 | ||
| 18 | const PW: &str = "tahinli-password-123"; | 19 | const PW: &str = "tahinli-password-123"; |
| 19 | 20 | ||
| 20 | -struct Resp { | ||
| 21 | - status: u16, | ||
| 22 | - headers: HeaderMap, | ||
| 23 | - body: String, | ||
| 24 | -} | ||
| 25 | - | ||
| 26 | -impl Resp { | ||
| 27 | - fn location(&self) -> Option<String> { | ||
| 28 | - self.headers | ||
| 29 | - .get(LOCATION) | ||
| 30 | - .and_then(|v| v.to_str().ok()) | ||
| 31 | - .map(str::to_owned) | ||
| 32 | - } | ||
| 33 | - | ||
| 34 | - fn set_cookies(&self) -> Vec<String> { | ||
| 35 | - self.headers | ||
| 36 | - .get_all(SET_COOKIE) | ||
| 37 | - .iter() | ||
| 38 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 39 | - .collect() | ||
| 40 | - } | ||
| 41 | -} | ||
| 42 | - | ||
| 43 | -struct Http { | ||
| 44 | - client: reqwest::Client, | ||
| 45 | - base: String, | ||
| 46 | -} | ||
| 47 | - | ||
| 48 | -impl Http { | ||
| 49 | - fn new(port: u16) -> Self { | ||
| 50 | - let client = reqwest::Client::builder() | ||
| 51 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 52 | - .timeout(Duration::from_secs(60)) | ||
| 53 | - .build() | ||
| 54 | - .expect("http client"); | ||
| 55 | - Self { | ||
| 56 | - client, | ||
| 57 | - base: format!("http://127.0.0.1:{port}"), | ||
| 58 | - } | ||
| 59 | - } | ||
| 60 | - | ||
| 61 | - async fn get(&self, path: &str, cookie: Option<&str>) -> Resp { | ||
| 62 | - self.send(self.client.get(format!("{}{path}", self.base)), cookie) | ||
| 63 | - .await | ||
| 64 | - } | ||
| 65 | - | ||
| 66 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 67 | - self.send( | ||
| 68 | - self.client.post(format!("{}{path}", self.base)).form(form), | ||
| 69 | - cookie, | ||
| 70 | - ) | ||
| 71 | - .await | ||
| 72 | - } | ||
| 73 | - | ||
| 74 | - async fn send(&self, req: reqwest::RequestBuilder, cookie: Option<&str>) -> Resp { | ||
| 75 | - let req = match cookie { | ||
| 76 | - Some(cookie) => req.header(COOKIE, cookie), | ||
| 77 | - None => req, | ||
| 78 | - }; | ||
| 79 | - let res = req.send().await.expect("http request"); | ||
| 80 | - let status = res.status().as_u16(); | ||
| 81 | - let headers = res.headers().clone(); | ||
| 82 | - let body = res.text().await.expect("read body"); | ||
| 83 | - Resp { | ||
| 84 | - status, | ||
| 85 | - headers, | ||
| 86 | - body, | ||
| 87 | - } | ||
| 88 | - } | ||
| 89 | - | ||
| 90 | - /// Opens a live stream as `cookie`, or anonymously. A stream that opens sends its `retry` | ||
| 91 | - /// field first, which is read here, not treated as an event. | ||
| 92 | - async fn stream(&self, path: &str, cookie: Option<&str>) -> reqwest::Response { | ||
| 93 | - let mut req = self.client.get(format!("{}{path}", self.base)); | ||
| 94 | - if let Some(cookie) = cookie { | ||
| 95 | - req = req.header(COOKIE, cookie); | ||
| 96 | - } | ||
| 97 | - let mut res = req.send().await.expect("stream request"); | ||
| 98 | - if res.status().is_success() { | ||
| 99 | - let retry = tokio::time::timeout(Duration::from_secs(5), res.chunk()) | ||
| 100 | - .await | ||
| 101 | - .expect("the retry field") | ||
| 102 | - .expect("stream chunk") | ||
| 103 | - .expect("stream still open"); | ||
| 104 | - assert!(String::from_utf8_lossy(&retry).contains("retry: 1000")); | ||
| 105 | - } | ||
| 106 | - res | ||
| 107 | - } | ||
| 108 | -} | ||
| 109 | - | ||
| 110 | -/// `name=value` for the cookie whose name ends with `suffix`, ignoring cleared cookies. | ||
| 111 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 112 | - resp.set_cookies().iter().find_map(|set| { | ||
| 113 | - let pair = set.split(';').next()?; | ||
| 114 | - let (name, value) = pair.split_once('=')?; | ||
| 115 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 116 | - }) | ||
| 117 | -} | ||
| 118 | - | ||
| 119 | /// The value of the first `<input name="...">` on a page. | 21 | /// The value of the first `<input name="...">` on a page. |
| 120 | /// The `data-live` value of the first element that names a stream, if any. | 22 | /// The `data-live` value of the first element that names a stream, if any. |
| 121 | fn live_url(body: &str) -> Option<String> { | 23 | fn live_url(body: &str) -> Option<String> { |
| @@ -145,27 +47,6 @@ fn admin(config: &Path, args: &[&str]) -> Output { | |||
| 145 | out | 47 | out |
| 146 | } | 48 | } |
| 147 | 49 | ||
| 148 | -async fn login(http: &Http, username: &str) -> String { | ||
| 149 | - let page = http.get("/login", None).await; | ||
| 150 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 151 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 152 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 153 | - let res = http | ||
| 154 | - .post_form( | ||
| 155 | - "/login", | ||
| 156 | - &[ | ||
| 157 | - ("username", username), | ||
| 158 | - ("password", PW), | ||
| 159 | - ("next", "/"), | ||
| 160 | - ("precsrf", &precsrf), | ||
| 161 | - ], | ||
| 162 | - Some(&precsrf_cookie), | ||
| 163 | - ) | ||
| 164 | - .await; | ||
| 165 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 166 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 167 | -} | ||
| 168 | - | ||
| 169 | /// The session's CSRF token, read from a page every signed-in user can open. | 50 | /// The session's CSRF token, read from a page every signed-in user can open. |
| 170 | async fn csrf_for(http: &Http, session: &str) -> String { | 51 | async fn csrf_for(http: &Http, session: &str) -> String { |
| 171 | let page = http.get("/settings/tokens", Some(session)).await; | 52 | let page = http.get("/settings/tokens", Some(session)).await; |
| @@ -235,9 +116,9 @@ async fn org_pages_follow_writes_live_and_refuse_outsiders() { | |||
| 235 | 116 | ||
| 236 | let (_server, ports) = launch(&dir, ports).await; | 117 | let (_server, ports) = launch(&dir, ports).await; |
| 237 | let http = Http::new(ports.http); | 118 | let http = Http::new(ports.http); |
| 238 | - let tahinli = login(&http, "tahinli").await; | 119 | + let tahinli = login(&http, "tahinli", PW).await; |
| 239 | - let bob = login(&http, "bob").await; | 120 | + let bob = login(&http, "bob", PW).await; |
| 240 | - let dana = login(&http, "dana").await; | 121 | + let dana = login(&http, "dana", PW).await; |
| 241 | let csrf = csrf_for(&http, &tahinli).await; | 122 | let csrf = csrf_for(&http, &tahinli).await; |
| 242 | 123 | ||
| 243 | // Each page names its stream, and the stream is served at that URL. | 124 | // Each page names its stream, and the stream is served at that URL. |
| @@ -438,8 +319,8 @@ async fn private_linked_repo_is_hidden_from_a_viewer_who_cannot_read_it() { | |||
| 438 | 319 | ||
| 439 | let (_server, ports) = launch(&dir, ports).await; | 320 | let (_server, ports) = launch(&dir, ports).await; |
| 440 | let http = Http::new(ports.http); | 321 | let http = Http::new(ports.http); |
| 441 | - let tahinli = login(&http, "tahinli").await; | 322 | + let tahinli = login(&http, "tahinli", PW).await; |
| 442 | - let bob = login(&http, "bob").await; | 323 | + let bob = login(&http, "bob", PW).await; |
| 443 | let csrf = csrf_for(&http, &tahinli).await; | 324 | let csrf = csrf_for(&http, &tahinli).await; |
| 444 | let team_page = format!("/org/acme/teams/{team_id}"); | 325 | let team_page = format!("/org/acme/teams/{team_id}"); |
| 445 | 326 | ||
crates/forge-web/tests/e2e/orgs_ui_e2e.rsmodified+16 -117
| @@ -4,16 +4,16 @@ | |||
| 4 | //! handled by hand, as in `repo_collaborators_e2e.rs`. | 4 | //! handled by hand, as in `repo_collaborators_e2e.rs`. |
| 5 | 5 | ||
| 6 | use crate::common::{ | 6 | use crate::common::{ |
| 7 | - BIN, describe, input_value, launch, launch_with, pick_ports, tool_on_path, write_config, | 7 | + BIN, Http, Resp, describe, input_value, launch, launch_with, login, pick_ports, tool_on_path, |
| 8 | + write_config, | ||
| 8 | }; | 9 | }; |
| 9 | use std::fs; | 10 | use std::fs; |
| 10 | use std::path::Path; | 11 | use std::path::Path; |
| 11 | use std::process::{Command, Output}; | 12 | use std::process::{Command, Output}; |
| 12 | -use std::time::Duration; | ||
| 13 | 13 | ||
| 14 | use forge_core::store::orgs::{DefaultRepoRole, OrgMemberRole, OrgSettings}; | 14 | use forge_core::store::orgs::{DefaultRepoRole, OrgMemberRole, OrgSettings}; |
| 15 | use forge_core::{Role, Store, Visibility}; | 15 | use forge_core::{Role, Store, Visibility}; |
| 16 | -use reqwest::header::{COOKIE, HeaderMap, HeaderName, LOCATION, SET_COOKIE}; | 16 | +use reqwest::header::LOCATION; |
| 17 | 17 | ||
| 18 | /// Each test starts its own server, so the tests run one at a time. | 18 | /// Each test starts its own server, so the tests run one at a time. |
| 19 | static SERIAL: std::sync::LazyLock<tokio::sync::Mutex<()>> = | 19 | static SERIAL: std::sync::LazyLock<tokio::sync::Mutex<()>> = |
| @@ -21,86 +21,6 @@ static SERIAL: std::sync::LazyLock<tokio::sync::Mutex<()>> = | |||
| 21 | 21 | ||
| 22 | const PW: &str = "tahinli-password-123"; | 22 | const PW: &str = "tahinli-password-123"; |
| 23 | 23 | ||
| 24 | -struct Resp { | ||
| 25 | - status: u16, | ||
| 26 | - headers: HeaderMap, | ||
| 27 | - body: String, | ||
| 28 | -} | ||
| 29 | - | ||
| 30 | -impl Resp { | ||
| 31 | - fn header(&self, name: HeaderName) -> Option<String> { | ||
| 32 | - self.headers | ||
| 33 | - .get(name) | ||
| 34 | - .and_then(|v| v.to_str().ok()) | ||
| 35 | - .map(str::to_owned) | ||
| 36 | - } | ||
| 37 | - | ||
| 38 | - fn set_cookies(&self) -> Vec<String> { | ||
| 39 | - self.headers | ||
| 40 | - .get_all(SET_COOKIE) | ||
| 41 | - .iter() | ||
| 42 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 43 | - .collect() | ||
| 44 | - } | ||
| 45 | -} | ||
| 46 | - | ||
| 47 | -struct Http { | ||
| 48 | - client: reqwest::Client, | ||
| 49 | - base: String, | ||
| 50 | -} | ||
| 51 | - | ||
| 52 | -impl Http { | ||
| 53 | - fn new(port: u16) -> Self { | ||
| 54 | - let client = reqwest::Client::builder() | ||
| 55 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 56 | - .timeout(Duration::from_secs(60)) | ||
| 57 | - .build() | ||
| 58 | - .expect("http client"); | ||
| 59 | - Self { | ||
| 60 | - client, | ||
| 61 | - base: format!("http://127.0.0.1:{port}"), | ||
| 62 | - } | ||
| 63 | - } | ||
| 64 | - | ||
| 65 | - async fn get(&self, path: &str, cookie: Option<&str>) -> Resp { | ||
| 66 | - self.send(self.client.get(format!("{}{path}", self.base)), cookie) | ||
| 67 | - .await | ||
| 68 | - } | ||
| 69 | - | ||
| 70 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 71 | - self.send( | ||
| 72 | - self.client.post(format!("{}{path}", self.base)).form(form), | ||
| 73 | - cookie, | ||
| 74 | - ) | ||
| 75 | - .await | ||
| 76 | - } | ||
| 77 | - | ||
| 78 | - async fn send(&self, req: reqwest::RequestBuilder, cookie: Option<&str>) -> Resp { | ||
| 79 | - let req = match cookie { | ||
| 80 | - Some(cookie) => req.header(COOKIE, cookie), | ||
| 81 | - None => req, | ||
| 82 | - }; | ||
| 83 | - let res = req.send().await.expect("http request"); | ||
| 84 | - let status = res.status().as_u16(); | ||
| 85 | - let headers = res.headers().clone(); | ||
| 86 | - let body = res.text().await.expect("read body"); | ||
| 87 | - Resp { | ||
| 88 | - status, | ||
| 89 | - headers, | ||
| 90 | - body, | ||
| 91 | - } | ||
| 92 | - } | ||
| 93 | -} | ||
| 94 | - | ||
| 95 | -/// `name=value` for the cookie whose name ends with `suffix`, ignoring cleared cookies. | ||
| 96 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 97 | - resp.set_cookies().iter().find_map(|set| { | ||
| 98 | - let pair = set.split(';').next()?; | ||
| 99 | - let (name, value) = pair.split_once('=')?; | ||
| 100 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 101 | - }) | ||
| 102 | -} | ||
| 103 | - | ||
| 104 | /// The value of the first `<input name="...">` on a page. | 24 | /// The value of the first `<input name="...">` on a page. |
| 105 | /// The value of the selected option of the `<select name="...">` on a page. | 25 | /// The value of the selected option of the `<select name="...">` on a page. |
| 106 | fn selected_value(body: &str, name: &str) -> Option<String> { | 26 | fn selected_value(body: &str, name: &str) -> Option<String> { |
| @@ -140,27 +60,6 @@ fn admin(config: &Path, args: &[&str]) -> Output { | |||
| 140 | out | 60 | out |
| 141 | } | 61 | } |
| 142 | 62 | ||
| 143 | -async fn login(http: &Http, username: &str) -> String { | ||
| 144 | - let page = http.get("/login", None).await; | ||
| 145 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 146 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 147 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 148 | - let res = http | ||
| 149 | - .post_form( | ||
| 150 | - "/login", | ||
| 151 | - &[ | ||
| 152 | - ("username", username), | ||
| 153 | - ("password", PW), | ||
| 154 | - ("next", "/"), | ||
| 155 | - ("precsrf", &precsrf), | ||
| 156 | - ], | ||
| 157 | - Some(&precsrf_cookie), | ||
| 158 | - ) | ||
| 159 | - .await; | ||
| 160 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 161 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 162 | -} | ||
| 163 | - | ||
| 164 | /// The session's CSRF token, read from a page every signed-in user can open. | 63 | /// The session's CSRF token, read from a page every signed-in user can open. |
| 165 | async fn csrf_for(http: &Http, session: &str) -> String { | 64 | async fn csrf_for(http: &Http, session: &str) -> String { |
| 166 | let page = http.get("/settings/tokens", Some(session)).await; | 65 | let page = http.get("/settings/tokens", Some(session)).await; |
| @@ -260,10 +159,10 @@ async fn org_members_teams_and_access_end_to_end() { | |||
| 260 | 159 | ||
| 261 | let (_server, ports) = launch(&dir, ports).await; | 160 | let (_server, ports) = launch(&dir, ports).await; |
| 262 | let http = Http::new(ports.http); | 161 | let http = Http::new(ports.http); |
| 263 | - let tahinli = login(&http, "tahinli").await; | 162 | + let tahinli = login(&http, "tahinli", PW).await; |
| 264 | - let bob = login(&http, "bob").await; | 163 | + let bob = login(&http, "bob", PW).await; |
| 265 | - let carol = login(&http, "carol").await; | 164 | + let carol = login(&http, "carol", PW).await; |
| 266 | - let dana = login(&http, "dana").await; | 165 | + let dana = login(&http, "dana", PW).await; |
| 267 | let members = "/org/acme/members"; | 166 | let members = "/org/acme/members"; |
| 268 | 167 | ||
| 269 | // Anonymous visitors go to login. Non-members get 404 on the org pages. Members see the list | 168 | // Anonymous visitors go to login. Non-members get 404 on the org pages. Members see the list |
| @@ -467,10 +366,10 @@ async fn org_settings_delete_and_audit_end_to_end() { | |||
| 467 | 366 | ||
| 468 | let (_server, ports) = launch(&dir, ports).await; | 367 | let (_server, ports) = launch(&dir, ports).await; |
| 469 | let http = Http::new(ports.http); | 368 | let http = Http::new(ports.http); |
| 470 | - let tahinli = login(&http, "tahinli").await; | 369 | + let tahinli = login(&http, "tahinli", PW).await; |
| 471 | - let bob = login(&http, "bob").await; | 370 | + let bob = login(&http, "bob", PW).await; |
| 472 | - let dana = login(&http, "dana").await; | 371 | + let dana = login(&http, "dana", PW).await; |
| 473 | - let root_session = login(&http, "root").await; | 372 | + let root_session = login(&http, "root", PW).await; |
| 474 | let settings = "/org/acme/settings"; | 373 | let settings = "/org/acme/settings"; |
| 475 | 374 | ||
| 476 | // Owners only: members get 403, non-members 404, anonymous visitors go to login. | 375 | // Owners only: members get 403, non-members 404, anonymous visitors go to login. |
| @@ -589,8 +488,8 @@ async fn org_creation_can_be_limited_to_admins() { | |||
| 589 | 488 | ||
| 590 | let (_server, ports) = launch_with(&dir, ports, "[orgs]\ncreation = \"admin\"\n").await; | 489 | let (_server, ports) = launch_with(&dir, ports, "[orgs]\ncreation = \"admin\"\n").await; |
| 591 | let http = Http::new(ports.http); | 490 | let http = Http::new(ports.http); |
| 592 | - let tahinli = login(&http, "tahinli").await; | 491 | + let tahinli = login(&http, "tahinli", PW).await; |
| 593 | - let root_session = login(&http, "root").await; | 492 | + let root_session = login(&http, "root", PW).await; |
| 594 | 493 | ||
| 595 | assert_eq!( | 494 | assert_eq!( |
| 596 | http.get("/org/new", Some(&tahinli)).await.status, | 495 | http.get("/org/new", Some(&tahinli)).await.status, |
| @@ -748,9 +647,9 @@ async fn org_member_pushes_over_https_through_a_write_team() { | |||
| 748 | 647 | ||
| 749 | let (_server, ports) = launch(&dir, ports).await; | 648 | let (_server, ports) = launch(&dir, ports).await; |
| 750 | let http = Http::new(ports.http); | 649 | let http = Http::new(ports.http); |
| 751 | - let tahinli = login(&http, "tahinli").await; | 650 | + let tahinli = login(&http, "tahinli", PW).await; |
| 752 | - let bob = login(&http, "bob").await; | 651 | + let bob = login(&http, "bob", PW).await; |
| 753 | - let dana = login(&http, "dana").await; | 652 | + let dana = login(&http, "dana", PW).await; |
| 754 | 653 | ||
| 755 | // Tahinli creates a private repo under acme through the new repo form. | 654 | // Tahinli creates a private repo under acme through the new repo form. |
| 756 | let csrf = csrf_for(&http, &tahinli).await; | 655 | let csrf = csrf_for(&http, &tahinli).await; |
crates/forge-web/tests/e2e/pulls_e2e.rsmodified+10 -108
| @@ -5,97 +5,21 @@ | |||
| 5 | //! scenarios cover a same-repo merge that closes an issue, a push that updates an open pull, | 5 | //! scenarios cover a same-repo merge that closes an issue, a push that updates an open pull, |
| 6 | //! a fork pull merged by squash, and a conflict that blocks the merge. | 6 | //! a fork pull merged by squash, and a conflict that blocks the merge. |
| 7 | 7 | ||
| 8 | -use crate::common::{BIN, Server, input_value, launch, pick_ports, tool_on_path, write_config}; | 8 | +use crate::common::{ |
| 9 | + BIN, Http, Resp, Server, input_value, launch, login, pick_ports, tool_on_path, write_config, | ||
| 10 | +}; | ||
| 9 | use std::fs; | 11 | use std::fs; |
| 10 | use std::path::{Path, PathBuf}; | 12 | use std::path::{Path, PathBuf}; |
| 11 | use std::process::{Command, Output}; | 13 | use std::process::{Command, Output}; |
| 12 | use std::time::{Duration, Instant}; | 14 | use std::time::{Duration, Instant}; |
| 13 | 15 | ||
| 14 | -use reqwest::header::{AUTHORIZATION, CONTENT_TYPE, COOKIE, LOCATION, SET_COOKIE}; | 16 | +use reqwest::header::{AUTHORIZATION, CONTENT_TYPE, COOKIE}; |
| 15 | 17 | ||
| 16 | const TAHINLI_PW: &str = "tahinli-password-123"; | 18 | const TAHINLI_PW: &str = "tahinli-password-123"; |
| 17 | const BOB_PW: &str = "bob-password-4567"; | 19 | const BOB_PW: &str = "bob-password-4567"; |
| 18 | const BOB_EMAIL: &str = "bob@users.noreply.127.0.0.1"; | 20 | const BOB_EMAIL: &str = "bob@users.noreply.127.0.0.1"; |
| 19 | const REPO_PATH: &str = "/tahinli/r"; | 21 | const REPO_PATH: &str = "/tahinli/r"; |
| 20 | 22 | ||
| 21 | -struct Resp { | ||
| 22 | - status: u16, | ||
| 23 | - location: Option<String>, | ||
| 24 | - set_cookies: Vec<String>, | ||
| 25 | - body: String, | ||
| 26 | -} | ||
| 27 | - | ||
| 28 | -struct Http { | ||
| 29 | - client: reqwest::Client, | ||
| 30 | - base: String, | ||
| 31 | -} | ||
| 32 | - | ||
| 33 | -impl Http { | ||
| 34 | - fn new(port: u16) -> Self { | ||
| 35 | - let client = reqwest::Client::builder() | ||
| 36 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 37 | - .timeout(Duration::from_secs(60)) | ||
| 38 | - .build() | ||
| 39 | - .expect("http client"); | ||
| 40 | - Self { | ||
| 41 | - client, | ||
| 42 | - base: format!("http://127.0.0.1:{port}"), | ||
| 43 | - } | ||
| 44 | - } | ||
| 45 | - | ||
| 46 | - async fn get(&self, path: &str) -> Resp { | ||
| 47 | - send(self.client.get(format!("{}{path}", self.base))).await | ||
| 48 | - } | ||
| 49 | - | ||
| 50 | - async fn get_cookie(&self, path: &str, cookie: &str) -> Resp { | ||
| 51 | - let req = self | ||
| 52 | - .client | ||
| 53 | - .get(format!("{}{path}", self.base)) | ||
| 54 | - .header(reqwest::header::COOKIE, cookie); | ||
| 55 | - send(req).await | ||
| 56 | - } | ||
| 57 | - | ||
| 58 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 59 | - let mut req = self.client.post(format!("{}{path}", self.base)).form(form); | ||
| 60 | - if let Some(cookie) = cookie { | ||
| 61 | - req = req.header(reqwest::header::COOKIE, cookie); | ||
| 62 | - } | ||
| 63 | - send(req).await | ||
| 64 | - } | ||
| 65 | -} | ||
| 66 | - | ||
| 67 | -async fn send(req: reqwest::RequestBuilder) -> Resp { | ||
| 68 | - let res = req.send().await.expect("http request"); | ||
| 69 | - let status = res.status().as_u16(); | ||
| 70 | - let location = res | ||
| 71 | - .headers() | ||
| 72 | - .get(LOCATION) | ||
| 73 | - .and_then(|v| v.to_str().ok()) | ||
| 74 | - .map(str::to_owned); | ||
| 75 | - let set_cookies = res | ||
| 76 | - .headers() | ||
| 77 | - .get_all(SET_COOKIE) | ||
| 78 | - .iter() | ||
| 79 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 80 | - .collect(); | ||
| 81 | - let body = res.text().await.unwrap_or_default(); | ||
| 82 | - Resp { | ||
| 83 | - status, | ||
| 84 | - location, | ||
| 85 | - set_cookies, | ||
| 86 | - body, | ||
| 87 | - } | ||
| 88 | -} | ||
| 89 | - | ||
| 90 | -/// Returns `name=value` for the cookie whose name ends with `suffix`. Cleared cookies are ignored. | ||
| 91 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 92 | - resp.set_cookies.iter().find_map(|set| { | ||
| 93 | - let pair = set.split(';').next()?; | ||
| 94 | - let (name, value) = pair.split_once('=')?; | ||
| 95 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 96 | - }) | ||
| 97 | -} | ||
| 98 | - | ||
| 99 | /// The value of the first `<input name="...">` on a page. | 23 | /// The value of the first `<input name="...">` on a page. |
| 100 | /// Text of the first state badge on a page, e.g. `Open`, `Closed` or `Merged`. | 24 | /// Text of the first state badge on a page, e.g. `Open`, `Closed` or `Merged`. |
| 101 | fn state_badge(body: &str) -> String { | 25 | fn state_badge(body: &str) -> String { |
| @@ -201,28 +125,6 @@ fn parent_count(dir: &Path, commit: &str) -> usize { | |||
| 201 | line.split_whitespace().count() - 1 | 125 | line.split_whitespace().count() - 1 |
| 202 | } | 126 | } |
| 203 | 127 | ||
| 204 | -/// Logs in through the real `/login` form and returns the session cookie header. | ||
| 205 | -async fn login(http: &Http, username: &str, password: &str) -> String { | ||
| 206 | - let page = http.get("/login").await; | ||
| 207 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 208 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 209 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 210 | - let res = http | ||
| 211 | - .post_form( | ||
| 212 | - "/login", | ||
| 213 | - &[ | ||
| 214 | - ("username", username), | ||
| 215 | - ("password", password), | ||
| 216 | - ("next", "/"), | ||
| 217 | - ("precsrf", &precsrf), | ||
| 218 | - ], | ||
| 219 | - Some(&precsrf_cookie), | ||
| 220 | - ) | ||
| 221 | - .await; | ||
| 222 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 223 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 224 | -} | ||
| 225 | - | ||
| 226 | /// The session's CSRF token, scraped from the token settings page. | 128 | /// The session's CSRF token, scraped from the token settings page. |
| 227 | async fn csrf_token(http: &Http, session: &str) -> String { | 129 | async fn csrf_token(http: &Http, session: &str) -> String { |
| 228 | let page = http.get_cookie("/settings/tokens", session).await; | 130 | let page = http.get_cookie("/settings/tokens", session).await; |
| @@ -274,7 +176,7 @@ async fn create_repo(http: &Http, session: &str, name: &str) { | |||
| 274 | .await; | 176 | .await; |
| 275 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); | 177 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); |
| 276 | assert_eq!( | 178 | assert_eq!( |
| 277 | - res.location.as_deref(), | 179 | + res.location().as_deref(), |
| 278 | Some(format!("/tahinli/{name}").as_str()) | 180 | Some(format!("/tahinli/{name}").as_str()) |
| 279 | ); | 181 | ); |
| 280 | } | 182 | } |
| @@ -362,7 +264,7 @@ async fn open_pull(http: &Http, session: &str, base: &str, head: &str, title: &s | |||
| 362 | ) | 264 | ) |
| 363 | .await; | 265 | .await; |
| 364 | assert_eq!(res.status, 303, "open pull {head}: {}", res.body); | 266 | assert_eq!(res.status, 303, "open pull {head}: {}", res.body); |
| 365 | - let location = res.location.expect("redirect to the pull"); | 267 | + let location = res.location().expect("redirect to the pull"); |
| 366 | location | 268 | location |
| 367 | .rsplit('/') | 269 | .rsplit('/') |
| 368 | .next() | 270 | .next() |
| @@ -385,7 +287,7 @@ async fn open_issue(http: &Http, session: &str, title: &str) -> u32 { | |||
| 385 | ) | 287 | ) |
| 386 | .await; | 288 | .await; |
| 387 | assert_eq!(res.status, 303, "create issue: {}", res.body); | 289 | assert_eq!(res.status, 303, "create issue: {}", res.body); |
| 388 | - let location = res.location.expect("redirect to the issue"); | 290 | + let location = res.location().expect("redirect to the issue"); |
| 389 | location | 291 | location |
| 390 | .rsplit('/') | 292 | .rsplit('/') |
| 391 | .next() | 293 | .next() |
| @@ -465,7 +367,7 @@ async fn same_repo_pull_merges_and_closes_issue() { | |||
| 465 | 367 | ||
| 466 | let res = merge(&b.http, &tahinli, 2, "merge").await; | 368 | let res = merge(&b.http, &tahinli, 2, "merge").await; |
| 467 | assert_eq!(res.status, 303, "merge: {}", res.body); | 369 | assert_eq!(res.status, 303, "merge: {}", res.body); |
| 468 | - assert_eq!(res.location.as_deref(), Some("/tahinli/r/pulls/2")); | 370 | + assert_eq!(res.location().as_deref(), Some("/tahinli/r/pulls/2")); |
| 469 | 371 | ||
| 470 | let url = remote(b.http_port, "tahinli", &token, "tahinli", "r"); | 372 | let url = remote(b.http_port, "tahinli", &token, "tahinli", "r"); |
| 471 | let main_sha = remote_sha(&b.dir, &url, "main"); | 373 | let main_sha = remote_sha(&b.dir, &url, "main"); |
| @@ -735,7 +637,7 @@ async fn fork_pull_squash_merge_credits_contributor() { | |||
| 735 | .post_form(&format!("{REPO_PATH}/fork"), &[("csrf", &csrf)], Some(&bob)) | 637 | .post_form(&format!("{REPO_PATH}/fork"), &[("csrf", &csrf)], Some(&bob)) |
| 736 | .await; | 638 | .await; |
| 737 | assert_eq!(fork.status, 303, "fork: {}\n{}", fork.body, server_log(&b)); | 639 | assert_eq!(fork.status, 303, "fork: {}\n{}", fork.body, server_log(&b)); |
| 738 | - assert_eq!(fork.location.as_deref(), Some("/bob/r")); | 640 | + assert_eq!(fork.location().as_deref(), Some("/bob/r")); |
| 739 | 641 | ||
| 740 | let bob_url = remote(b.http_port, "bob", &bob_token, "bob", "r"); | 642 | let bob_url = remote(b.http_port, "bob", &bob_token, "bob", "r"); |
| 741 | let bob_work = clone(&bob_url, &b.dir.join("bob-work")); | 643 | let bob_work = clone(&bob_url, &b.dir.join("bob-work")); |
| @@ -823,7 +725,7 @@ async fn conflicting_pull_cannot_merge() { | |||
| 823 | server_log(&b) | 725 | server_log(&b) |
| 824 | ); | 726 | ); |
| 825 | assert_eq!( | 727 | assert_eq!( |
| 826 | - res.location.as_deref(), | 728 | + res.location().as_deref(), |
| 827 | Some(format!("{REPO_PATH}/pulls/{number}?merge_error=conflict").as_str()), | 729 | Some(format!("{REPO_PATH}/pulls/{number}?merge_error=conflict").as_str()), |
| 828 | "refusal redirects back to the pull with the conflict code" | 730 | "refusal redirects back to the pull with the conflict code" |
| 829 | ); | 731 | ); |
crates/forge-web/tests/e2e/repo_collaborators_e2e.rsmodified+9 -108
| @@ -2,97 +2,19 @@ | |||
| 2 | //! seed users and repos through the store before the server starts, and then drive the page and | 2 | //! seed users and repos through the store before the server starts, and then drive the page and |
| 3 | //! its POST forms over HTTP. Cookies are handled by hand (see `repo_settings_e2e.rs`). | 3 | //! its POST forms over HTTP. Cookies are handled by hand (see `repo_settings_e2e.rs`). |
| 4 | 4 | ||
| 5 | -use crate::common::{BIN, describe, input_value, launch, pick_ports, tool_on_path, write_config}; | 5 | +use crate::common::{ |
| 6 | + BIN, Http, Resp, cookie_named, describe, input_value, launch, login, pick_ports, tool_on_path, | ||
| 7 | + write_config, | ||
| 8 | +}; | ||
| 6 | use std::fs; | 9 | use std::fs; |
| 7 | use std::path::Path; | 10 | use std::path::Path; |
| 8 | use std::process::{Command, Output}; | 11 | use std::process::{Command, Output}; |
| 9 | -use std::time::Duration; | ||
| 10 | 12 | ||
| 11 | use forge_core::{Role, Store, Visibility}; | 13 | use forge_core::{Role, Store, Visibility}; |
| 12 | -use reqwest::header::{COOKIE, HeaderMap, HeaderName, LOCATION, SET_COOKIE}; | 14 | +use reqwest::header::LOCATION; |
| 13 | 15 | ||
| 14 | const PW: &str = "tahinli-password-123"; | 16 | const PW: &str = "tahinli-password-123"; |
| 15 | 17 | ||
| 16 | -struct Resp { | ||
| 17 | - status: u16, | ||
| 18 | - headers: HeaderMap, | ||
| 19 | - body: String, | ||
| 20 | -} | ||
| 21 | - | ||
| 22 | -impl Resp { | ||
| 23 | - fn header(&self, name: HeaderName) -> Option<String> { | ||
| 24 | - self.headers | ||
| 25 | - .get(name) | ||
| 26 | - .and_then(|v| v.to_str().ok()) | ||
| 27 | - .map(str::to_owned) | ||
| 28 | - } | ||
| 29 | - | ||
| 30 | - fn set_cookies(&self) -> Vec<String> { | ||
| 31 | - self.headers | ||
| 32 | - .get_all(SET_COOKIE) | ||
| 33 | - .iter() | ||
| 34 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 35 | - .collect() | ||
| 36 | - } | ||
| 37 | -} | ||
| 38 | - | ||
| 39 | -struct Http { | ||
| 40 | - client: reqwest::Client, | ||
| 41 | - base: String, | ||
| 42 | -} | ||
| 43 | - | ||
| 44 | -impl Http { | ||
| 45 | - fn new(port: u16) -> Self { | ||
| 46 | - let client = reqwest::Client::builder() | ||
| 47 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 48 | - .timeout(Duration::from_secs(60)) | ||
| 49 | - .build() | ||
| 50 | - .expect("http client"); | ||
| 51 | - Self { | ||
| 52 | - client, | ||
| 53 | - base: format!("http://127.0.0.1:{port}"), | ||
| 54 | - } | ||
| 55 | - } | ||
| 56 | - | ||
| 57 | - async fn get(&self, path: &str, cookie: Option<&str>) -> Resp { | ||
| 58 | - self.send(self.client.get(format!("{}{path}", self.base)), cookie) | ||
| 59 | - .await | ||
| 60 | - } | ||
| 61 | - | ||
| 62 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 63 | - self.send( | ||
| 64 | - self.client.post(format!("{}{path}", self.base)).form(form), | ||
| 65 | - cookie, | ||
| 66 | - ) | ||
| 67 | - .await | ||
| 68 | - } | ||
| 69 | - | ||
| 70 | - async fn send(&self, req: reqwest::RequestBuilder, cookie: Option<&str>) -> Resp { | ||
| 71 | - let req = match cookie { | ||
| 72 | - Some(cookie) => req.header(COOKIE, cookie), | ||
| 73 | - None => req, | ||
| 74 | - }; | ||
| 75 | - let res = req.send().await.expect("http request"); | ||
| 76 | - let status = res.status().as_u16(); | ||
| 77 | - let headers = res.headers().clone(); | ||
| 78 | - let body = res.text().await.expect("read body"); | ||
| 79 | - Resp { | ||
| 80 | - status, | ||
| 81 | - headers, | ||
| 82 | - body, | ||
| 83 | - } | ||
| 84 | - } | ||
| 85 | -} | ||
| 86 | - | ||
| 87 | -/// `name=value` for the cookie whose name ends with `suffix`, ignoring cleared cookies. | ||
| 88 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 89 | - resp.set_cookies().iter().find_map(|set| { | ||
| 90 | - let pair = set.split(';').next()?; | ||
| 91 | - let (name, value) = pair.split_once('=')?; | ||
| 92 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 93 | - }) | ||
| 94 | -} | ||
| 95 | - | ||
| 96 | /// The value of the first `<input name="...">` on a page. | 18 | /// The value of the first `<input name="...">` on a page. |
| 97 | /// Runs an admin subcommand with the password in FORGE_PASSWORD and asserts it succeeded. | 19 | /// Runs an admin subcommand with the password in FORGE_PASSWORD and asserts it succeeded. |
| 98 | fn admin(config: &Path, args: &[&str]) -> Output { | 20 | fn admin(config: &Path, args: &[&str]) -> Output { |
| @@ -112,27 +34,6 @@ fn admin(config: &Path, args: &[&str]) -> Output { | |||
| 112 | out | 34 | out |
| 113 | } | 35 | } |
| 114 | 36 | ||
| 115 | -async fn login(http: &Http, username: &str) -> String { | ||
| 116 | - let page = http.get("/login", None).await; | ||
| 117 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 118 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 119 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 120 | - let res = http | ||
| 121 | - .post_form( | ||
| 122 | - "/login", | ||
| 123 | - &[ | ||
| 124 | - ("username", username), | ||
| 125 | - ("password", PW), | ||
| 126 | - ("next", "/"), | ||
| 127 | - ("precsrf", &precsrf), | ||
| 128 | - ], | ||
| 129 | - Some(&precsrf_cookie), | ||
| 130 | - ) | ||
| 131 | - .await; | ||
| 132 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 133 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 134 | -} | ||
| 135 | - | ||
| 136 | /// The session's CSRF token, read from a page every signed-in user can open. | 37 | /// The session's CSRF token, read from a page every signed-in user can open. |
| 137 | async fn csrf_for(http: &Http, session: &str) -> String { | 38 | async fn csrf_for(http: &Http, session: &str) -> String { |
| 138 | let page = http.get("/settings/tokens", Some(session)).await; | 39 | let page = http.get("/settings/tokens", Some(session)).await; |
| @@ -238,10 +139,10 @@ async fn repo_collaborators_end_to_end() { | |||
| 238 | 139 | ||
| 239 | let (_server, ports) = launch(&dir, ports).await; | 140 | let (_server, ports) = launch(&dir, ports).await; |
| 240 | let http = Http::new(ports.http); | 141 | let http = Http::new(ports.http); |
| 241 | - let olivia = login(&http, "olivia").await; | 142 | + let olivia = login(&http, "olivia", PW).await; |
| 242 | - let bob = login(&http, "bob").await; | 143 | + let bob = login(&http, "bob", PW).await; |
| 243 | - let carol = login(&http, "carol").await; | 144 | + let carol = login(&http, "carol", PW).await; |
| 244 | - let wendy = login(&http, "wendy").await; | 145 | + let wendy = login(&http, "wendy", PW).await; |
| 245 | 146 | ||
| 246 | let page = "/olivia/priv/settings/collaborators"; | 147 | let page = "/olivia/priv/settings/collaborators"; |
| 247 | let add = "/olivia/priv/settings/collaborators/add"; | 148 | let add = "/olivia/priv/settings/collaborators/add"; |
crates/forge-web/tests/e2e/repo_rename_e2e.rsmodified+8 -108
| @@ -5,97 +5,18 @@ | |||
| 5 | //! Cookies are handled by hand because the session cookie is `__Host-forge_session` with `Secure`, | 5 | //! Cookies are handled by hand because the session cookie is `__Host-forge_session` with `Secure`, |
| 6 | //! which a cookie jar would not send over plain http. | 6 | //! which a cookie jar would not send over plain http. |
| 7 | 7 | ||
| 8 | -use crate::common::{BIN, describe, input_value, launch, pick_ports, tool_on_path, write_config}; | 8 | +use crate::common::{ |
| 9 | + BIN, Http, Resp, describe, input_value, launch, login, pick_ports, tool_on_path, write_config, | ||
| 10 | +}; | ||
| 9 | use std::fs; | 11 | use std::fs; |
| 10 | use std::path::Path; | 12 | use std::path::Path; |
| 11 | use std::process::{Command, Output}; | 13 | use std::process::{Command, Output}; |
| 12 | -use std::time::Duration; | ||
| 13 | 14 | ||
| 14 | use forge_core::{Role, Store, Visibility}; | 15 | use forge_core::{Role, Store, Visibility}; |
| 15 | -use reqwest::header::{COOKIE, HeaderMap, HeaderName, LOCATION, SET_COOKIE}; | 16 | +use reqwest::header::LOCATION; |
| 16 | 17 | ||
| 17 | const PW: &str = "tahinli-password-123"; | 18 | const PW: &str = "tahinli-password-123"; |
| 18 | 19 | ||
| 19 | -struct Resp { | ||
| 20 | - status: u16, | ||
| 21 | - headers: HeaderMap, | ||
| 22 | - body: String, | ||
| 23 | -} | ||
| 24 | - | ||
| 25 | -impl Resp { | ||
| 26 | - fn header(&self, name: HeaderName) -> Option<String> { | ||
| 27 | - self.headers | ||
| 28 | - .get(name) | ||
| 29 | - .and_then(|v| v.to_str().ok()) | ||
| 30 | - .map(str::to_owned) | ||
| 31 | - } | ||
| 32 | - | ||
| 33 | - fn set_cookies(&self) -> Vec<String> { | ||
| 34 | - self.headers | ||
| 35 | - .get_all(SET_COOKIE) | ||
| 36 | - .iter() | ||
| 37 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 38 | - .collect() | ||
| 39 | - } | ||
| 40 | -} | ||
| 41 | - | ||
| 42 | -struct Http { | ||
| 43 | - client: reqwest::Client, | ||
| 44 | - base: String, | ||
| 45 | -} | ||
| 46 | - | ||
| 47 | -impl Http { | ||
| 48 | - fn new(port: u16) -> Self { | ||
| 49 | - let client = reqwest::Client::builder() | ||
| 50 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 51 | - .timeout(Duration::from_secs(60)) | ||
| 52 | - .build() | ||
| 53 | - .expect("http client"); | ||
| 54 | - Self { | ||
| 55 | - client, | ||
| 56 | - base: format!("http://127.0.0.1:{port}"), | ||
| 57 | - } | ||
| 58 | - } | ||
| 59 | - | ||
| 60 | - async fn get(&self, path: &str, cookie: Option<&str>) -> Resp { | ||
| 61 | - self.send(self.client.get(format!("{}{path}", self.base)), cookie) | ||
| 62 | - .await | ||
| 63 | - } | ||
| 64 | - | ||
| 65 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 66 | - self.send( | ||
| 67 | - self.client.post(format!("{}{path}", self.base)).form(form), | ||
| 68 | - cookie, | ||
| 69 | - ) | ||
| 70 | - .await | ||
| 71 | - } | ||
| 72 | - | ||
| 73 | - async fn send(&self, req: reqwest::RequestBuilder, cookie: Option<&str>) -> Resp { | ||
| 74 | - let req = match cookie { | ||
| 75 | - Some(cookie) => req.header(COOKIE, cookie), | ||
| 76 | - None => req, | ||
| 77 | - }; | ||
| 78 | - let res = req.send().await.expect("http request"); | ||
| 79 | - let status = res.status().as_u16(); | ||
| 80 | - let headers = res.headers().clone(); | ||
| 81 | - let body = res.text().await.expect("read body"); | ||
| 82 | - Resp { | ||
| 83 | - status, | ||
| 84 | - headers, | ||
| 85 | - body, | ||
| 86 | - } | ||
| 87 | - } | ||
| 88 | -} | ||
| 89 | - | ||
| 90 | -/// `name=value` for the cookie whose name ends with `suffix`, ignoring cleared cookies. | ||
| 91 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 92 | - resp.set_cookies().iter().find_map(|set| { | ||
| 93 | - let pair = set.split(';').next()?; | ||
| 94 | - let (name, value) = pair.split_once('=')?; | ||
| 95 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 96 | - }) | ||
| 97 | -} | ||
| 98 | - | ||
| 99 | /// The value of the first `<input name="...">` on a page. | 20 | /// The value of the first `<input name="...">` on a page. |
| 100 | /// The `action` of the first form on a page whose action ends with `suffix`. | 21 | /// The `action` of the first form on a page whose action ends with `suffix`. |
| 101 | fn form_action(body: &str, suffix: &str) -> String { | 22 | fn form_action(body: &str, suffix: &str) -> String { |
| @@ -125,27 +46,6 @@ fn admin(config: &Path, args: &[&str]) { | |||
| 125 | ); | 46 | ); |
| 126 | } | 47 | } |
| 127 | 48 | ||
| 128 | -async fn login(http: &Http, username: &str) -> String { | ||
| 129 | - let page = http.get("/login", None).await; | ||
| 130 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 131 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 132 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 133 | - let res = http | ||
| 134 | - .post_form( | ||
| 135 | - "/login", | ||
| 136 | - &[ | ||
| 137 | - ("username", username), | ||
| 138 | - ("password", PW), | ||
| 139 | - ("next", "/"), | ||
| 140 | - ("precsrf", &precsrf), | ||
| 141 | - ], | ||
| 142 | - Some(&precsrf_cookie), | ||
| 143 | - ) | ||
| 144 | - .await; | ||
| 145 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 146 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 147 | -} | ||
| 148 | - | ||
| 149 | /// The session's CSRF token, read from a page every signed-in user can open. | 49 | /// The session's CSRF token, read from a page every signed-in user can open. |
| 150 | async fn csrf_for(http: &Http, session: &str) -> String { | 50 | async fn csrf_for(http: &Http, session: &str) -> String { |
| 151 | let page = http.get("/settings/tokens", Some(session)).await; | 51 | let page = http.get("/settings/tokens", Some(session)).await; |
| @@ -306,10 +206,10 @@ async fn repo_rename_end_to_end() { | |||
| 306 | 206 | ||
| 307 | let (server, ports) = launch(&dir, ports).await; | 207 | let (server, ports) = launch(&dir, ports).await; |
| 308 | let http = Http::new(ports.http); | 208 | let http = Http::new(ports.http); |
| 309 | - let bob = login(&http, "bob").await; | 209 | + let bob = login(&http, "bob", PW).await; |
| 310 | - let carol = login(&http, "carol").await; | 210 | + let carol = login(&http, "carol", PW).await; |
| 311 | - let dave = login(&http, "dave").await; | 211 | + let dave = login(&http, "dave", PW).await; |
| 312 | - let eve = login(&http, "eve").await; | 212 | + let eve = login(&http, "eve", PW).await; |
| 313 | let bob_token = create_token(&http, &bob, "rename-e2e").await; | 213 | let bob_token = create_token(&http, &bob, "rename-e2e").await; |
| 314 | 214 | ||
| 315 | // Bob pushes a commit with a `src` directory to the public repo, so the tree path can be checked. | 215 | // Bob pushes a commit with a `src` directory to the public repo, so the tree path can be checked. |
crates/forge-web/tests/e2e/repo_settings_e2e.rsmodified+14 -113
| @@ -4,97 +4,19 @@ | |||
| 4 | //! Cookies are handled by hand because the session cookie is `__Host-forge_session` with `Secure`, | 4 | //! Cookies are handled by hand because the session cookie is `__Host-forge_session` with `Secure`, |
| 5 | //! which a cookie jar would not send over plain http. | 5 | //! which a cookie jar would not send over plain http. |
| 6 | 6 | ||
| 7 | -use crate::common::{BIN, describe, input_value, launch, pick_ports, tool_on_path, write_config}; | 7 | +use crate::common::{ |
| 8 | + BIN, Http, Resp, cookie_named, describe, input_value, launch, login, pick_ports, tool_on_path, | ||
| 9 | + write_config, | ||
| 10 | +}; | ||
| 8 | use std::fs; | 11 | use std::fs; |
| 9 | use std::path::{Path, PathBuf}; | 12 | use std::path::{Path, PathBuf}; |
| 10 | use std::process::{Command, Output}; | 13 | use std::process::{Command, Output}; |
| 11 | -use std::time::Duration; | ||
| 12 | 14 | ||
| 13 | use forge_core::{Role, Store, Visibility}; | 15 | use forge_core::{Role, Store, Visibility}; |
| 14 | -use reqwest::header::{COOKIE, HeaderMap, HeaderName, LOCATION, SET_COOKIE}; | 16 | +use reqwest::header::LOCATION; |
| 15 | 17 | ||
| 16 | const PW: &str = "tahinli-password-123"; | 18 | const PW: &str = "tahinli-password-123"; |
| 17 | 19 | ||
| 18 | -struct Resp { | ||
| 19 | - status: u16, | ||
| 20 | - headers: HeaderMap, | ||
| 21 | - body: String, | ||
| 22 | -} | ||
| 23 | - | ||
| 24 | -impl Resp { | ||
| 25 | - fn header(&self, name: HeaderName) -> Option<String> { | ||
| 26 | - self.headers | ||
| 27 | - .get(name) | ||
| 28 | - .and_then(|v| v.to_str().ok()) | ||
| 29 | - .map(str::to_owned) | ||
| 30 | - } | ||
| 31 | - | ||
| 32 | - fn set_cookies(&self) -> Vec<String> { | ||
| 33 | - self.headers | ||
| 34 | - .get_all(SET_COOKIE) | ||
| 35 | - .iter() | ||
| 36 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 37 | - .collect() | ||
| 38 | - } | ||
| 39 | -} | ||
| 40 | - | ||
| 41 | -struct Http { | ||
| 42 | - client: reqwest::Client, | ||
| 43 | - base: String, | ||
| 44 | -} | ||
| 45 | - | ||
| 46 | -impl Http { | ||
| 47 | - fn new(port: u16) -> Self { | ||
| 48 | - let client = reqwest::Client::builder() | ||
| 49 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 50 | - .timeout(Duration::from_secs(60)) | ||
| 51 | - .build() | ||
| 52 | - .expect("http client"); | ||
| 53 | - Self { | ||
| 54 | - client, | ||
| 55 | - base: format!("http://127.0.0.1:{port}"), | ||
| 56 | - } | ||
| 57 | - } | ||
| 58 | - | ||
| 59 | - async fn get(&self, path: &str, cookie: Option<&str>) -> Resp { | ||
| 60 | - self.send(self.client.get(format!("{}{path}", self.base)), cookie) | ||
| 61 | - .await | ||
| 62 | - } | ||
| 63 | - | ||
| 64 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 65 | - self.send( | ||
| 66 | - self.client.post(format!("{}{path}", self.base)).form(form), | ||
| 67 | - cookie, | ||
| 68 | - ) | ||
| 69 | - .await | ||
| 70 | - } | ||
| 71 | - | ||
| 72 | - async fn send(&self, req: reqwest::RequestBuilder, cookie: Option<&str>) -> Resp { | ||
| 73 | - let req = match cookie { | ||
| 74 | - Some(cookie) => req.header(COOKIE, cookie), | ||
| 75 | - None => req, | ||
| 76 | - }; | ||
| 77 | - let res = req.send().await.expect("http request"); | ||
| 78 | - let status = res.status().as_u16(); | ||
| 79 | - let headers = res.headers().clone(); | ||
| 80 | - let body = res.text().await.expect("read body"); | ||
| 81 | - Resp { | ||
| 82 | - status, | ||
| 83 | - headers, | ||
| 84 | - body, | ||
| 85 | - } | ||
| 86 | - } | ||
| 87 | -} | ||
| 88 | - | ||
| 89 | -/// `name=value` for the cookie whose name ends with `suffix`, ignoring cleared cookies. | ||
| 90 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 91 | - resp.set_cookies().iter().find_map(|set| { | ||
| 92 | - let pair = set.split(';').next()?; | ||
| 93 | - let (name, value) = pair.split_once('=')?; | ||
| 94 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 95 | - }) | ||
| 96 | -} | ||
| 97 | - | ||
| 98 | /// The value of the first `<input name="...">` on a page. | 20 | /// The value of the first `<input name="...">` on a page. |
| 99 | /// Runs an admin subcommand with the password in FORGE_PASSWORD and asserts it succeeded. | 21 | /// Runs an admin subcommand with the password in FORGE_PASSWORD and asserts it succeeded. |
| 100 | fn admin(config: &Path, args: &[&str]) -> Output { | 22 | fn admin(config: &Path, args: &[&str]) -> Output { |
| @@ -114,27 +36,6 @@ fn admin(config: &Path, args: &[&str]) -> Output { | |||
| 114 | out | 36 | out |
| 115 | } | 37 | } |
| 116 | 38 | ||
| 117 | -async fn login(http: &Http, username: &str) -> String { | ||
| 118 | - let page = http.get("/login", None).await; | ||
| 119 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 120 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 121 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 122 | - let res = http | ||
| 123 | - .post_form( | ||
| 124 | - "/login", | ||
| 125 | - &[ | ||
| 126 | - ("username", username), | ||
| 127 | - ("password", PW), | ||
| 128 | - ("next", "/"), | ||
| 129 | - ("precsrf", &precsrf), | ||
| 130 | - ], | ||
| 131 | - Some(&precsrf_cookie), | ||
| 132 | - ) | ||
| 133 | - .await; | ||
| 134 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 135 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 136 | -} | ||
| 137 | - | ||
| 138 | /// The session's CSRF token, read from a page every signed-in user can open. | 39 | /// The session's CSRF token, read from a page every signed-in user can open. |
| 139 | async fn csrf_for(http: &Http, session: &str) -> String { | 40 | async fn csrf_for(http: &Http, session: &str) -> String { |
| 140 | let page = http.get("/settings/tokens", Some(session)).await; | 41 | let page = http.get("/settings/tokens", Some(session)).await; |
| @@ -223,9 +124,9 @@ async fn repo_settings_end_to_end() { | |||
| 223 | let (server, ports) = launch(&dir, ports).await; | 124 | let (server, ports) = launch(&dir, ports).await; |
| 224 | let http = Http::new(ports.http); | 125 | let http = Http::new(ports.http); |
| 225 | 126 | ||
| 226 | - let bob = login(&http, "bob").await; | 127 | + let bob = login(&http, "bob", PW).await; |
| 227 | - let carol = login(&http, "carol").await; | 128 | + let carol = login(&http, "carol", PW).await; |
| 228 | - let tahinli = login(&http, "tahinli").await; | 129 | + let tahinli = login(&http, "tahinli", PW).await; |
| 229 | 130 | ||
| 230 | // Anonymous on a private repo: 404, the same as a missing repo. | 131 | // Anonymous on a private repo: 404, the same as a missing repo. |
| 231 | let anon = http.get("/bob/priv/settings", None).await; | 132 | let anon = http.get("/bob/priv/settings", None).await; |
| @@ -424,7 +325,7 @@ async fn repo_settings_deletes_rows_when_repo_dir_is_missing() { | |||
| 424 | 325 | ||
| 425 | let (server, ports) = launch(&dir, ports).await; | 326 | let (server, ports) = launch(&dir, ports).await; |
| 426 | let http = Http::new(ports.http); | 327 | let http = Http::new(ports.http); |
| 427 | - let bob = login(&http, "bob").await; | 328 | + let bob = login(&http, "bob", PW).await; |
| 428 | let csrf = csrf_for(&http, &bob).await; | 329 | let csrf = csrf_for(&http, &bob).await; |
| 429 | let deleted = http | 330 | let deleted = http |
| 430 | .post_form( | 331 | .post_form( |
| @@ -538,9 +439,9 @@ async fn branch_protection_end_to_end() { | |||
| 538 | 439 | ||
| 539 | let (server, ports) = launch(&dir, ports).await; | 440 | let (server, ports) = launch(&dir, ports).await; |
| 540 | let http = Http::new(ports.http); | 441 | let http = Http::new(ports.http); |
| 541 | - let bob = login(&http, "bob").await; | 442 | + let bob = login(&http, "bob", PW).await; |
| 542 | - let carol = login(&http, "carol").await; | 443 | + let carol = login(&http, "carol", PW).await; |
| 543 | - let dan = login(&http, "dan").await; | 444 | + let dan = login(&http, "dan", PW).await; |
| 544 | 445 | ||
| 545 | // Owner: the built-in row is there before any rule, and the danger zone is shown. | 446 | // Owner: the built-in row is there before any rule, and the danger zone is shown. |
| 546 | let page = http.get("/bob/app/settings", Some(&bob)).await; | 447 | let page = http.get("/bob/app/settings", Some(&bob)).await; |
| @@ -910,8 +811,8 @@ async fn general_settings_end_to_end() { | |||
| 910 | 811 | ||
| 911 | let (server, ports) = launch(&dir, ports).await; | 812 | let (server, ports) = launch(&dir, ports).await; |
| 912 | let http = Http::new(ports.http); | 813 | let http = Http::new(ports.http); |
| 913 | - let bob = login(&http, "bob").await; | 814 | + let bob = login(&http, "bob", PW).await; |
| 914 | - let carol = login(&http, "carol").await; | 815 | + let carol = login(&http, "carol", PW).await; |
| 915 | 816 | ||
| 916 | // The General section is the first card on the page, with the sub-navigation above it. | 817 | // The General section is the first card on the page, with the sub-navigation above it. |
| 917 | let page = http.get("/bob/gen/settings", Some(&bob)).await; | 818 | let page = http.get("/bob/gen/settings", Some(&bob)).await; |
crates/forge-web/tests/e2e/repo_transfer_e2e.rsmodified+8 -108
| @@ -5,97 +5,18 @@ | |||
| 5 | //! Cookies are handled by hand because the session cookie is `__Host-forge_session` with `Secure`, | 5 | //! Cookies are handled by hand because the session cookie is `__Host-forge_session` with `Secure`, |
| 6 | //! which a cookie jar would not send over plain http. | 6 | //! which a cookie jar would not send over plain http. |
| 7 | 7 | ||
| 8 | -use crate::common::{BIN, describe, input_value, launch, pick_ports, tool_on_path, write_config}; | 8 | +use crate::common::{ |
| 9 | + BIN, Http, Resp, describe, input_value, launch, login, pick_ports, tool_on_path, write_config, | ||
| 10 | +}; | ||
| 9 | use std::fs; | 11 | use std::fs; |
| 10 | use std::process::Command; | 12 | use std::process::Command; |
| 11 | -use std::time::Duration; | ||
| 12 | 13 | ||
| 13 | use forge_core::store::orgs::OrgMemberRole; | 14 | use forge_core::store::orgs::OrgMemberRole; |
| 14 | use forge_core::{Store, Visibility}; | 15 | use forge_core::{Store, Visibility}; |
| 15 | -use reqwest::header::{COOKIE, HeaderMap, LOCATION, SET_COOKIE}; | 16 | +use reqwest::header::LOCATION; |
| 16 | 17 | ||
| 17 | const PW: &str = "tahinli-password-123"; | 18 | const PW: &str = "tahinli-password-123"; |
| 18 | 19 | ||
| 19 | -struct Resp { | ||
| 20 | - status: u16, | ||
| 21 | - headers: HeaderMap, | ||
| 22 | - body: String, | ||
| 23 | -} | ||
| 24 | - | ||
| 25 | -impl Resp { | ||
| 26 | - fn header(&self, name: reqwest::header::HeaderName) -> Option<String> { | ||
| 27 | - self.headers | ||
| 28 | - .get(name) | ||
| 29 | - .and_then(|v| v.to_str().ok()) | ||
| 30 | - .map(str::to_owned) | ||
| 31 | - } | ||
| 32 | - | ||
| 33 | - fn set_cookies(&self) -> Vec<String> { | ||
| 34 | - self.headers | ||
| 35 | - .get_all(SET_COOKIE) | ||
| 36 | - .iter() | ||
| 37 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 38 | - .collect() | ||
| 39 | - } | ||
| 40 | -} | ||
| 41 | - | ||
| 42 | -struct Http { | ||
| 43 | - client: reqwest::Client, | ||
| 44 | - base: String, | ||
| 45 | -} | ||
| 46 | - | ||
| 47 | -impl Http { | ||
| 48 | - fn new(port: u16) -> Self { | ||
| 49 | - let client = reqwest::Client::builder() | ||
| 50 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 51 | - .timeout(Duration::from_secs(60)) | ||
| 52 | - .build() | ||
| 53 | - .expect("http client"); | ||
| 54 | - Self { | ||
| 55 | - client, | ||
| 56 | - base: format!("http://127.0.0.1:{port}"), | ||
| 57 | - } | ||
| 58 | - } | ||
| 59 | - | ||
| 60 | - async fn get(&self, path: &str, cookie: Option<&str>) -> Resp { | ||
| 61 | - self.send(self.client.get(format!("{}{path}", self.base)), cookie) | ||
| 62 | - .await | ||
| 63 | - } | ||
| 64 | - | ||
| 65 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 66 | - self.send( | ||
| 67 | - self.client.post(format!("{}{path}", self.base)).form(form), | ||
| 68 | - cookie, | ||
| 69 | - ) | ||
| 70 | - .await | ||
| 71 | - } | ||
| 72 | - | ||
| 73 | - async fn send(&self, req: reqwest::RequestBuilder, cookie: Option<&str>) -> Resp { | ||
| 74 | - let req = match cookie { | ||
| 75 | - Some(cookie) => req.header(COOKIE, cookie), | ||
| 76 | - None => req, | ||
| 77 | - }; | ||
| 78 | - let res = req.send().await.expect("http request"); | ||
| 79 | - let status = res.status().as_u16(); | ||
| 80 | - let headers = res.headers().clone(); | ||
| 81 | - let body = res.text().await.expect("read body"); | ||
| 82 | - Resp { | ||
| 83 | - status, | ||
| 84 | - headers, | ||
| 85 | - body, | ||
| 86 | - } | ||
| 87 | - } | ||
| 88 | -} | ||
| 89 | - | ||
| 90 | -/// `name=value` for the cookie whose name ends with `suffix`, ignoring cleared cookies. | ||
| 91 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 92 | - resp.set_cookies().iter().find_map(|set| { | ||
| 93 | - let pair = set.split(';').next()?; | ||
| 94 | - let (name, value) = pair.split_once('=')?; | ||
| 95 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 96 | - }) | ||
| 97 | -} | ||
| 98 | - | ||
| 99 | /// The value of the first `<input name="...">` on a page. | 20 | /// The value of the first `<input name="...">` on a page. |
| 100 | /// The `action` of the first form on a page whose action ends with `suffix`. Panics when none. | 21 | /// The `action` of the first form on a page whose action ends with `suffix`. Panics when none. |
| 101 | fn form_action(body: &str, suffix: &str) -> String { | 22 | fn form_action(body: &str, suffix: &str) -> String { |
| @@ -125,27 +46,6 @@ fn admin(config: &std::path::Path, args: &[&str]) { | |||
| 125 | ); | 46 | ); |
| 126 | } | 47 | } |
| 127 | 48 | ||
| 128 | -async fn login(http: &Http, username: &str) -> String { | ||
| 129 | - let page = http.get("/login", None).await; | ||
| 130 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 131 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 132 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 133 | - let res = http | ||
| 134 | - .post_form( | ||
| 135 | - "/login", | ||
| 136 | - &[ | ||
| 137 | - ("username", username), | ||
| 138 | - ("password", PW), | ||
| 139 | - ("next", "/"), | ||
| 140 | - ("precsrf", &precsrf), | ||
| 141 | - ], | ||
| 142 | - Some(&precsrf_cookie), | ||
| 143 | - ) | ||
| 144 | - .await; | ||
| 145 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 146 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 147 | -} | ||
| 148 | - | ||
| 149 | /// The session's CSRF token, read from a page every signed-in user can open. | 49 | /// The session's CSRF token, read from a page every signed-in user can open. |
| 150 | async fn csrf_for(http: &Http, session: &str) -> String { | 50 | async fn csrf_for(http: &Http, session: &str) -> String { |
| 151 | let page = http.get("/settings/tokens", Some(session)).await; | 51 | let page = http.get("/settings/tokens", Some(session)).await; |
| @@ -237,10 +137,10 @@ async fn repo_transfer_offers_declines_cancels_and_org_targets() { | |||
| 237 | 137 | ||
| 238 | let (_server, ports) = launch(&dir, ports).await; | 138 | let (_server, ports) = launch(&dir, ports).await; |
| 239 | let http = Http::new(ports.http); | 139 | let http = Http::new(ports.http); |
| 240 | - let bob = login(&http, "bob").await; | 140 | + let bob = login(&http, "bob", PW).await; |
| 241 | - let carol = login(&http, "carol").await; | 141 | + let carol = login(&http, "carol", PW).await; |
| 242 | - let dave = login(&http, "dave").await; | 142 | + let dave = login(&http, "dave", PW).await; |
| 243 | - let eve = login(&http, "eve").await; | 143 | + let eve = login(&http, "eve", PW).await; |
| 244 | 144 | ||
| 245 | // Offer, then decline as carol from the inbox. The repo stays with bob. | 145 | // Offer, then decline as carol from the inbox. The repo stays with bob. |
| 246 | let offered = offer(&http, &bob, "bob", "priv", "carol").await; | 146 | let offered = offer(&http, &bob, "bob", "priv", "carol").await; |
crates/forge-web/tests/e2e/section_nav_e2e.rsmodified+3 -107
| @@ -4,95 +4,12 @@ | |||
| 4 | 4 | ||
| 5 | #![allow(dead_code)] | 5 | #![allow(dead_code)] |
| 6 | 6 | ||
| 7 | -use crate::common::{BIN, Server, describe, input_value, launch, pick_ports, write_config}; | 7 | +use crate::common::{BIN, Http, Server, describe, launch, login, pick_ports, write_config}; |
| 8 | use std::path::Path; | 8 | use std::path::Path; |
| 9 | use std::process::Command; | 9 | use std::process::Command; |
| 10 | -use std::time::Duration; | ||
| 11 | - | ||
| 12 | -use reqwest::header::{COOKIE, HeaderMap, HeaderName, SET_COOKIE}; | ||
| 13 | 10 | ||
| 14 | const PW: &str = "tahinli-password-123"; | 11 | const PW: &str = "tahinli-password-123"; |
| 15 | 12 | ||
| 16 | -struct Resp { | ||
| 17 | - status: u16, | ||
| 18 | - headers: HeaderMap, | ||
| 19 | - body: String, | ||
| 20 | -} | ||
| 21 | - | ||
| 22 | -impl Resp { | ||
| 23 | - fn header(&self, name: HeaderName) -> Option<String> { | ||
| 24 | - self.headers | ||
| 25 | - .get(name) | ||
| 26 | - .and_then(|v| v.to_str().ok()) | ||
| 27 | - .map(str::to_owned) | ||
| 28 | - } | ||
| 29 | - | ||
| 30 | - fn set_cookies(&self) -> Vec<String> { | ||
| 31 | - self.headers | ||
| 32 | - .get_all(SET_COOKIE) | ||
| 33 | - .iter() | ||
| 34 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 35 | - .collect() | ||
| 36 | - } | ||
| 37 | -} | ||
| 38 | - | ||
| 39 | -struct Http { | ||
| 40 | - client: reqwest::Client, | ||
| 41 | - base: String, | ||
| 42 | -} | ||
| 43 | - | ||
| 44 | -impl Http { | ||
| 45 | - fn new(port: u16) -> Self { | ||
| 46 | - let client = reqwest::Client::builder() | ||
| 47 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 48 | - .timeout(Duration::from_secs(60)) | ||
| 49 | - .build() | ||
| 50 | - .expect("http client"); | ||
| 51 | - Self { | ||
| 52 | - client, | ||
| 53 | - base: format!("http://127.0.0.1:{port}"), | ||
| 54 | - } | ||
| 55 | - } | ||
| 56 | - | ||
| 57 | - async fn get(&self, path: &str, cookie: Option<&str>) -> Resp { | ||
| 58 | - self.send(self.client.get(format!("{}{path}", self.base)), cookie) | ||
| 59 | - .await | ||
| 60 | - } | ||
| 61 | - | ||
| 62 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 63 | - self.send( | ||
| 64 | - self.client.post(format!("{}{path}", self.base)).form(form), | ||
| 65 | - cookie, | ||
| 66 | - ) | ||
| 67 | - .await | ||
| 68 | - } | ||
| 69 | - | ||
| 70 | - async fn send(&self, req: reqwest::RequestBuilder, cookie: Option<&str>) -> Resp { | ||
| 71 | - let req = match cookie { | ||
| 72 | - Some(cookie) => req.header(COOKIE, cookie), | ||
| 73 | - None => req, | ||
| 74 | - }; | ||
| 75 | - let res = req.send().await.expect("http request"); | ||
| 76 | - let status = res.status().as_u16(); | ||
| 77 | - let headers = res.headers().clone(); | ||
| 78 | - let body = res.text().await.expect("read body"); | ||
| 79 | - Resp { | ||
| 80 | - status, | ||
| 81 | - headers, | ||
| 82 | - body, | ||
| 83 | - } | ||
| 84 | - } | ||
| 85 | -} | ||
| 86 | - | ||
| 87 | -/// `name=value` for the cookie whose name ends with `suffix`, ignoring cleared cookies. | ||
| 88 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 89 | - resp.set_cookies().iter().find_map(|set| { | ||
| 90 | - let pair = set.split(';').next()?; | ||
| 91 | - let (name, value) = pair.split_once('=')?; | ||
| 92 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 93 | - }) | ||
| 94 | -} | ||
| 95 | - | ||
| 96 | /// The value of the first `<input name="...">` on a page. | 13 | /// The value of the first `<input name="...">` on a page. |
| 97 | /// The `data-theme` attribute of `<html>`, if the page sets one. | 14 | /// The `data-theme` attribute of `<html>`, if the page sets one. |
| 98 | fn html_theme(body: &str) -> Option<String> { | 15 | fn html_theme(body: &str) -> Option<String> { |
| @@ -192,27 +109,6 @@ fn admin(config: &Path, args: &[&str]) { | |||
| 192 | ); | 109 | ); |
| 193 | } | 110 | } |
| 194 | 111 | ||
| 195 | -async fn login(http: &Http, username: &str) -> String { | ||
| 196 | - let page = http.get("/login", None).await; | ||
| 197 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 198 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 199 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 200 | - let res = http | ||
| 201 | - .post_form( | ||
| 202 | - "/login", | ||
| 203 | - &[ | ||
| 204 | - ("username", username), | ||
| 205 | - ("password", PW), | ||
| 206 | - ("next", "/"), | ||
| 207 | - ("precsrf", &precsrf), | ||
| 208 | - ], | ||
| 209 | - Some(&precsrf_cookie), | ||
| 210 | - ) | ||
| 211 | - .await; | ||
| 212 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 213 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 214 | -} | ||
| 215 | - | ||
| 216 | /// A running server with user `tahinli`. The temp dir lives as long as the test. | 112 | /// A running server with user `tahinli`. The temp dir lives as long as the test. |
| 217 | struct Env { | 113 | struct Env { |
| 218 | _root: tempfile::TempDir, | 114 | _root: tempfile::TempDir, |
| @@ -267,7 +163,7 @@ fn assert_section(body: &str, current: &str, hrefs: &[&str]) { | |||
| 267 | #[tokio::test] | 163 | #[tokio::test] |
| 268 | async fn admin_pages_get_the_admin_sidebar() { | 164 | async fn admin_pages_get_the_admin_sidebar() { |
| 269 | let env = start().await; | 165 | let env = start().await; |
| 270 | - let session = login(&env.http, "tahinli").await; | 166 | + let session = login(&env.http, "tahinli", PW).await; |
| 271 | let want = [ | 167 | let want = [ |
| 272 | "/admin/users", | 168 | "/admin/users", |
| 273 | "/admin/invites", | 169 | "/admin/invites", |
| @@ -292,7 +188,7 @@ async fn admin_pages_get_the_admin_sidebar() { | |||
| 292 | #[tokio::test] | 188 | #[tokio::test] |
| 293 | async fn settings_pages_get_the_settings_sidebar() { | 189 | async fn settings_pages_get_the_settings_sidebar() { |
| 294 | let env = start().await; | 190 | let env = start().await; |
| 295 | - let session = login(&env.http, "tahinli").await; | 191 | + let session = login(&env.http, "tahinli", PW).await; |
| 296 | let want = [ | 192 | let want = [ |
| 297 | "/settings/profile", | 193 | "/settings/profile", |
| 298 | "/settings/keys", | 194 | "/settings/keys", |
crates/forge-web/tests/e2e/snippets_e2e.rsmodified+14 -110
| @@ -2,92 +2,17 @@ | |||
| 2 | //! drives it over HTTP. Cookies are set by hand because the session cookie is `__Host-` and | 2 | //! drives it over HTTP. Cookies are set by hand because the session cookie is `__Host-` and |
| 3 | //! `Secure`. Files are fetched from the user-content listener, as a browser would. | 3 | //! `Secure`. Files are fetched from the user-content listener, as a browser would. |
| 4 | 4 | ||
| 5 | -use crate::common::{BIN, Server, describe, input_value, launch, pick_ports, write_config}; | 5 | +use crate::common::{ |
| 6 | + BIN, Http, Server, describe, input_value, launch, login, pick_ports, write_config, | ||
| 7 | +}; | ||
| 6 | use std::path::{Path, PathBuf}; | 8 | use std::path::{Path, PathBuf}; |
| 7 | use std::process::Command; | 9 | use std::process::Command; |
| 8 | use std::time::Duration; | 10 | use std::time::Duration; |
| 9 | 11 | ||
| 10 | -use reqwest::header::{CONTENT_TYPE, COOKIE, HeaderMap, HeaderName, LOCATION, SET_COOKIE}; | 12 | +use reqwest::header::{CONTENT_TYPE, COOKIE, LOCATION}; |
| 11 | 13 | ||
| 12 | const PW: &str = "tahinli-password-123"; | 14 | const PW: &str = "tahinli-password-123"; |
| 13 | 15 | ||
| 14 | -struct Resp { | ||
| 15 | - status: u16, | ||
| 16 | - headers: HeaderMap, | ||
| 17 | - body: String, | ||
| 18 | -} | ||
| 19 | - | ||
| 20 | -impl Resp { | ||
| 21 | - fn header(&self, name: HeaderName) -> Option<String> { | ||
| 22 | - self.headers | ||
| 23 | - .get(name) | ||
| 24 | - .and_then(|v| v.to_str().ok()) | ||
| 25 | - .map(str::to_owned) | ||
| 26 | - } | ||
| 27 | - | ||
| 28 | - fn set_cookies(&self) -> Vec<String> { | ||
| 29 | - self.headers | ||
| 30 | - .get_all(SET_COOKIE) | ||
| 31 | - .iter() | ||
| 32 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 33 | - .collect() | ||
| 34 | - } | ||
| 35 | -} | ||
| 36 | - | ||
| 37 | -struct Http { | ||
| 38 | - client: reqwest::Client, | ||
| 39 | - base: String, | ||
| 40 | -} | ||
| 41 | - | ||
| 42 | -impl Http { | ||
| 43 | - fn new(base: String) -> Self { | ||
| 44 | - let client = reqwest::Client::builder() | ||
| 45 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 46 | - .timeout(Duration::from_secs(60)) | ||
| 47 | - .build() | ||
| 48 | - .expect("http client"); | ||
| 49 | - Self { client, base } | ||
| 50 | - } | ||
| 51 | - | ||
| 52 | - async fn get(&self, path: &str, cookie: Option<&str>) -> Resp { | ||
| 53 | - self.send(self.client.get(format!("{}{path}", self.base)), cookie) | ||
| 54 | - .await | ||
| 55 | - } | ||
| 56 | - | ||
| 57 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 58 | - self.send( | ||
| 59 | - self.client.post(format!("{}{path}", self.base)).form(form), | ||
| 60 | - cookie, | ||
| 61 | - ) | ||
| 62 | - .await | ||
| 63 | - } | ||
| 64 | - | ||
| 65 | - async fn send(&self, req: reqwest::RequestBuilder, cookie: Option<&str>) -> Resp { | ||
| 66 | - let req = match cookie { | ||
| 67 | - Some(cookie) => req.header(COOKIE, cookie), | ||
| 68 | - None => req, | ||
| 69 | - }; | ||
| 70 | - let res = req.send().await.expect("http request"); | ||
| 71 | - let status = res.status().as_u16(); | ||
| 72 | - let headers = res.headers().clone(); | ||
| 73 | - let body = res.text().await.expect("read body"); | ||
| 74 | - Resp { | ||
| 75 | - status, | ||
| 76 | - headers, | ||
| 77 | - body, | ||
| 78 | - } | ||
| 79 | - } | ||
| 80 | -} | ||
| 81 | - | ||
| 82 | -/// `name=value` for the cookie whose name ends with `suffix`, ignoring cleared cookies. | ||
| 83 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 84 | - resp.set_cookies().iter().find_map(|set| { | ||
| 85 | - let pair = set.split(';').next()?; | ||
| 86 | - let (name, value) = pair.split_once('=')?; | ||
| 87 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 88 | - }) | ||
| 89 | -} | ||
| 90 | - | ||
| 91 | /// The value of the first `<input name="...">` on a page. | 16 | /// The value of the first `<input name="...">` on a page. |
| 92 | /// The path and query of an absolute URL, so it can be requested through a base-bound client. | 17 | /// The path and query of an absolute URL, so it can be requested through a base-bound client. |
| 93 | fn path_of(url: &str) -> String { | 18 | fn path_of(url: &str) -> String { |
| @@ -126,27 +51,6 @@ fn admin(config: &Path, args: &[&str]) { | |||
| 126 | ); | 51 | ); |
| 127 | } | 52 | } |
| 128 | 53 | ||
| 129 | -async fn login(http: &Http, username: &str) -> String { | ||
| 130 | - let page = http.get("/login", None).await; | ||
| 131 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 132 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 133 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 134 | - let res = http | ||
| 135 | - .post_form( | ||
| 136 | - "/login", | ||
| 137 | - &[ | ||
| 138 | - ("username", username), | ||
| 139 | - ("password", PW), | ||
| 140 | - ("next", "/"), | ||
| 141 | - ("precsrf", &precsrf), | ||
| 142 | - ], | ||
| 143 | - Some(&precsrf_cookie), | ||
| 144 | - ) | ||
| 145 | - .await; | ||
| 146 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 147 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 148 | -} | ||
| 149 | - | ||
| 150 | /// A running server. The temp dir lives as long as the test. | 54 | /// A running server. The temp dir lives as long as the test. |
| 151 | struct Env { | 55 | struct Env { |
| 152 | _root: tempfile::TempDir, | 56 | _root: tempfile::TempDir, |
| @@ -166,8 +70,8 @@ async fn start() -> Env { | |||
| 166 | Env { | 70 | Env { |
| 167 | _root: root, | 71 | _root: root, |
| 168 | _server: server, | 72 | _server: server, |
| 169 | - app: Http::new(format!("http://127.0.0.1:{}", ports.http)), | 73 | + app: Http::with_base(format!("http://127.0.0.1:{}", ports.http)), |
| 170 | - content: Http::new(format!("http://127.0.0.1:{}", ports.content)), | 74 | + content: Http::with_base(format!("http://127.0.0.1:{}", ports.content)), |
| 171 | } | 75 | } |
| 172 | } | 76 | } |
| 173 | 77 | ||
| @@ -239,8 +143,8 @@ async fn next_change(res: &mut reqwest::Response, what: &str) { | |||
| 239 | #[tokio::test] | 143 | #[tokio::test] |
| 240 | async fn snippet_streams_follow_writes() { | 144 | async fn snippet_streams_follow_writes() { |
| 241 | let env = start().await; | 145 | let env = start().await; |
| 242 | - let tahinli = login(&env.app, "tahinli").await; | 146 | + let tahinli = login(&env.app, "tahinli", PW).await; |
| 243 | - let bob = login(&env.app, "bob").await; | 147 | + let bob = login(&env.app, "bob", PW).await; |
| 244 | 148 | ||
| 245 | let id = create( | 149 | let id = create( |
| 246 | &env.app, | 150 | &env.app, |
| @@ -363,7 +267,7 @@ async fn snippet_streams_follow_writes() { | |||
| 363 | #[tokio::test] | 267 | #[tokio::test] |
| 364 | async fn create_edit_and_revisions() { | 268 | async fn create_edit_and_revisions() { |
| 365 | let env = start().await; | 269 | let env = start().await; |
| 366 | - let tahinli = login(&env.app, "tahinli").await; | 270 | + let tahinli = login(&env.app, "tahinli", PW).await; |
| 367 | let id = create( | 271 | let id = create( |
| 368 | &env.app, | 272 | &env.app, |
| 369 | &tahinli, | 273 | &tahinli, |
| @@ -423,8 +327,8 @@ async fn create_edit_and_revisions() { | |||
| 423 | #[tokio::test] | 327 | #[tokio::test] |
| 424 | async fn private_snippet_is_hidden_from_others() { | 328 | async fn private_snippet_is_hidden_from_others() { |
| 425 | let env = start().await; | 329 | let env = start().await; |
| 426 | - let tahinli = login(&env.app, "tahinli").await; | 330 | + let tahinli = login(&env.app, "tahinli", PW).await; |
| 427 | - let bob = login(&env.app, "bob").await; | 331 | + let bob = login(&env.app, "bob", PW).await; |
| 428 | let id = create( | 332 | let id = create( |
| 429 | &env.app, | 333 | &env.app, |
| 430 | &tahinli, | 334 | &tahinli, |
| @@ -486,7 +390,7 @@ async fn private_snippet_is_hidden_from_others() { | |||
| 486 | #[tokio::test] | 390 | #[tokio::test] |
| 487 | async fn raw_files_use_the_user_content_origin_and_private_tokens() { | 391 | async fn raw_files_use_the_user_content_origin_and_private_tokens() { |
| 488 | let env = start().await; | 392 | let env = start().await; |
| 489 | - let tahinli = login(&env.app, "tahinli").await; | 393 | + let tahinli = login(&env.app, "tahinli", PW).await; |
| 490 | let public = create( | 394 | let public = create( |
| 491 | &env.app, | 395 | &env.app, |
| 492 | &tahinli, | 396 | &tahinli, |
| @@ -544,7 +448,7 @@ async fn raw_files_use_the_user_content_origin_and_private_tokens() { | |||
| 544 | #[tokio::test] | 448 | #[tokio::test] |
| 545 | async fn unlisted_is_by_link_only_and_delete_removes_it() { | 449 | async fn unlisted_is_by_link_only_and_delete_removes_it() { |
| 546 | let env = start().await; | 450 | let env = start().await; |
| 547 | - let tahinli = login(&env.app, "tahinli").await; | 451 | + let tahinli = login(&env.app, "tahinli", PW).await; |
| 548 | let id = create( | 452 | let id = create( |
| 549 | &env.app, | 453 | &env.app, |
| 550 | &tahinli, | 454 | &tahinli, |
| @@ -584,7 +488,7 @@ async fn unlisted_is_by_link_only_and_delete_removes_it() { | |||
| 584 | #[tokio::test] | 488 | #[tokio::test] |
| 585 | async fn bad_input_redisplays_the_form_with_an_error() { | 489 | async fn bad_input_redisplays_the_form_with_an_error() { |
| 586 | let env = start().await; | 490 | let env = start().await; |
| 587 | - let tahinli = login(&env.app, "tahinli").await; | 491 | + let tahinli = login(&env.app, "tahinli", PW).await; |
| 588 | let page = env.app.get("/snippets/new", Some(&tahinli)).await; | 492 | let page = env.app.get("/snippets/new", Some(&tahinli)).await; |
| 589 | let csrf = input_value(&page.body, "csrf"); | 493 | let csrf = input_value(&page.body, "csrf"); |
| 590 | let res = env | 494 | let res = env |
crates/forge-web/tests/e2e/stars_e2e.rsmodified+8 -109
| @@ -3,98 +3,18 @@ | |||
| 3 | //! HTTP. Cookies are set by hand because the session cookie is `__Host-` and `Secure`. | 3 | //! HTTP. Cookies are set by hand because the session cookie is `__Host-` and `Secure`. |
| 4 | 4 | ||
| 5 | use crate::common::{ | 5 | use crate::common::{ |
| 6 | - BIN, Server, describe, input_value, launch, pick_ports, tool_on_path, write_config, | 6 | + BIN, Http, Resp, Server, describe, input_value, launch, login, pick_ports, tool_on_path, |
| 7 | + write_config, | ||
| 7 | }; | 8 | }; |
| 8 | use std::fs; | 9 | use std::fs; |
| 9 | use std::path::{Path, PathBuf}; | 10 | use std::path::{Path, PathBuf}; |
| 10 | use std::process::{Command, Stdio}; | 11 | use std::process::{Command, Stdio}; |
| 11 | -use std::time::Duration; | ||
| 12 | 12 | ||
| 13 | use forge_core::{Store, Visibility}; | 13 | use forge_core::{Store, Visibility}; |
| 14 | -use reqwest::header::{COOKIE, HeaderMap, HeaderName, LOCATION, SET_COOKIE}; | 14 | +use reqwest::header::LOCATION; |
| 15 | 15 | ||
| 16 | const PW: &str = "tahinli-password-123"; | 16 | const PW: &str = "tahinli-password-123"; |
| 17 | 17 | ||
| 18 | -struct Resp { | ||
| 19 | - status: u16, | ||
| 20 | - headers: HeaderMap, | ||
| 21 | - body: String, | ||
| 22 | -} | ||
| 23 | - | ||
| 24 | -impl Resp { | ||
| 25 | - fn header(&self, name: HeaderName) -> Option<String> { | ||
| 26 | - self.headers | ||
| 27 | - .get(name) | ||
| 28 | - .and_then(|v| v.to_str().ok()) | ||
| 29 | - .map(str::to_owned) | ||
| 30 | - } | ||
| 31 | - | ||
| 32 | - fn set_cookies(&self) -> Vec<String> { | ||
| 33 | - self.headers | ||
| 34 | - .get_all(SET_COOKIE) | ||
| 35 | - .iter() | ||
| 36 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 37 | - .collect() | ||
| 38 | - } | ||
| 39 | -} | ||
| 40 | - | ||
| 41 | -struct Http { | ||
| 42 | - client: reqwest::Client, | ||
| 43 | - base: String, | ||
| 44 | -} | ||
| 45 | - | ||
| 46 | -impl Http { | ||
| 47 | - fn new(port: u16) -> Self { | ||
| 48 | - let client = reqwest::Client::builder() | ||
| 49 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 50 | - .timeout(Duration::from_secs(60)) | ||
| 51 | - .build() | ||
| 52 | - .expect("http client"); | ||
| 53 | - Self { | ||
| 54 | - client, | ||
| 55 | - base: format!("http://127.0.0.1:{port}"), | ||
| 56 | - } | ||
| 57 | - } | ||
| 58 | - | ||
| 59 | - async fn get(&self, path: &str, cookie: Option<&str>) -> Resp { | ||
| 60 | - self.send(self.client.get(format!("{}{path}", self.base)), cookie) | ||
| 61 | - .await | ||
| 62 | - } | ||
| 63 | - | ||
| 64 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 65 | - self.send( | ||
| 66 | - self.client.post(format!("{}{path}", self.base)).form(form), | ||
| 67 | - cookie, | ||
| 68 | - ) | ||
| 69 | - .await | ||
| 70 | - } | ||
| 71 | - | ||
| 72 | - async fn send(&self, req: reqwest::RequestBuilder, cookie: Option<&str>) -> Resp { | ||
| 73 | - let req = match cookie { | ||
| 74 | - Some(cookie) => req.header(COOKIE, cookie), | ||
| 75 | - None => req, | ||
| 76 | - }; | ||
| 77 | - let res = req.send().await.expect("http request"); | ||
| 78 | - let status = res.status().as_u16(); | ||
| 79 | - let headers = res.headers().clone(); | ||
| 80 | - let body = res.text().await.expect("read body"); | ||
| 81 | - Resp { | ||
| 82 | - status, | ||
| 83 | - headers, | ||
| 84 | - body, | ||
| 85 | - } | ||
| 86 | - } | ||
| 87 | -} | ||
| 88 | - | ||
| 89 | -/// `name=value` for the cookie whose name ends with `suffix`, ignoring cleared cookies. | ||
| 90 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 91 | - resp.set_cookies().iter().find_map(|set| { | ||
| 92 | - let pair = set.split(';').next()?; | ||
| 93 | - let (name, value) = pair.split_once('=')?; | ||
| 94 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 95 | - }) | ||
| 96 | -} | ||
| 97 | - | ||
| 98 | /// The value of the first `<input name="...">` on a page. | 18 | /// The value of the first `<input name="...">` on a page. |
| 99 | /// Runs an admin subcommand with the password in FORGE_PASSWORD and asserts it succeeded. | 19 | /// Runs an admin subcommand with the password in FORGE_PASSWORD and asserts it succeeded. |
| 100 | fn admin(config: &Path, args: &[&str]) { | 20 | fn admin(config: &Path, args: &[&str]) { |
| @@ -113,27 +33,6 @@ fn admin(config: &Path, args: &[&str]) { | |||
| 113 | ); | 33 | ); |
| 114 | } | 34 | } |
| 115 | 35 | ||
| 116 | -async fn login(http: &Http, username: &str) -> String { | ||
| 117 | - let page = http.get("/login", None).await; | ||
| 118 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 119 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 120 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 121 | - let res = http | ||
| 122 | - .post_form( | ||
| 123 | - "/login", | ||
| 124 | - &[ | ||
| 125 | - ("username", username), | ||
| 126 | - ("password", PW), | ||
| 127 | - ("next", "/"), | ||
| 128 | - ("precsrf", &precsrf), | ||
| 129 | - ], | ||
| 130 | - Some(&precsrf_cookie), | ||
| 131 | - ) | ||
| 132 | - .await; | ||
| 133 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 134 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 135 | -} | ||
| 136 | - | ||
| 137 | /// Creates a bare git repo for `repo_id` at its data-dir location, as the create-repo page does. | 36 | /// Creates a bare git repo for `repo_id` at its data-dir location, as the create-repo page does. |
| 138 | fn init_bare(data_dir: &Path, repo_id: &str) { | 37 | fn init_bare(data_dir: &Path, repo_id: &str) { |
| 139 | let path = forge_git::repo::repo_path(data_dir, repo_id).expect("repo path"); | 38 | let path = forge_git::repo::repo_path(data_dir, repo_id).expect("repo path"); |
| @@ -276,7 +175,7 @@ async fn star_round_trip_shows_the_count() { | |||
| 276 | return; | 175 | return; |
| 277 | } | 176 | } |
| 278 | let env = start(seed_repos).await; | 177 | let env = start(seed_repos).await; |
| 279 | - let bob = login(&env.http, "bob").await; | 178 | + let bob = login(&env.http, "bob", PW).await; |
| 280 | 179 | ||
| 281 | let page = env.http.get("/tahinli/pub", Some(&bob)).await; | 180 | let page = env.http.get("/tahinli/pub", Some(&bob)).await; |
| 282 | assert_eq!(page.status, 200, "repo page"); | 181 | assert_eq!(page.status, 200, "repo page"); |
| @@ -339,7 +238,7 @@ async fn anonymous_star_redirects_to_login() { | |||
| 339 | #[tokio::test] | 238 | #[tokio::test] |
| 340 | async fn star_without_a_valid_csrf_token_is_403() { | 239 | async fn star_without_a_valid_csrf_token_is_403() { |
| 341 | let env = start(seed_repos).await; | 240 | let env = start(seed_repos).await; |
| 342 | - let bob = login(&env.http, "bob").await; | 241 | + let bob = login(&env.http, "bob", PW).await; |
| 343 | let res = post_star(&env.http, "/tahinli/pub/star", "wrong", Some(&bob), None).await; | 242 | let res = post_star(&env.http, "/tahinli/pub/star", "wrong", Some(&bob), None).await; |
| 344 | assert_eq!(res.status, 403); | 243 | assert_eq!(res.status, 403); |
| 345 | let page = env.http.get("/tahinli/pub", Some(&bob)).await; | 244 | let page = env.http.get("/tahinli/pub", Some(&bob)).await; |
| @@ -349,7 +248,7 @@ async fn star_without_a_valid_csrf_token_is_403() { | |||
| 349 | #[tokio::test] | 248 | #[tokio::test] |
| 350 | async fn star_on_a_private_repo_is_404_for_a_non_reader() { | 249 | async fn star_on_a_private_repo_is_404_for_a_non_reader() { |
| 351 | let env = start(seed_repos).await; | 250 | let env = start(seed_repos).await; |
| 352 | - let bob = login(&env.http, "bob").await; | 251 | + let bob = login(&env.http, "bob", PW).await; |
| 353 | let res = post_star( | 252 | let res = post_star( |
| 354 | &env.http, | 253 | &env.http, |
| 355 | "/tahinli/priv/star", | 254 | "/tahinli/priv/star", |
| @@ -380,7 +279,7 @@ async fn explore_lists_public_repos_and_searches_literally() { | |||
| 380 | assert_eq!(repo_names(&page.body), vec!["tahinli/pct%x".to_owned()]); | 279 | assert_eq!(repo_names(&page.body), vec!["tahinli/pct%x".to_owned()]); |
| 381 | 280 | ||
| 382 | // Sorting by stars puts a starred repo first; the default (name) puts `pct%x` first. | 281 | // Sorting by stars puts a starred repo first; the default (name) puts `pct%x` first. |
| 383 | - let bob = login(&env.http, "bob").await; | 282 | + let bob = login(&env.http, "bob", PW).await; |
| 384 | let page = env.http.get("/tahinli/pub", Some(&bob)).await; | 283 | let page = env.http.get("/tahinli/pub", Some(&bob)).await; |
| 385 | let csrf = input_value(&page.body, "csrf"); | 284 | let csrf = input_value(&page.body, "csrf"); |
| 386 | let res = post_star(&env.http, "/tahinli/pub/star", &csrf, Some(&bob), None).await; | 285 | let res = post_star(&env.http, "/tahinli/pub/star", &csrf, Some(&bob), None).await; |
| @@ -426,7 +325,7 @@ async fn profile_stars_tab_hides_repos_the_viewer_cannot_read() { | |||
| 426 | }; | 325 | }; |
| 427 | assert_eq!(starred(&anon.body), vec!["tahinli/pub".to_owned()]); | 326 | assert_eq!(starred(&anon.body), vec!["tahinli/pub".to_owned()]); |
| 428 | 327 | ||
| 429 | - let tahinli = login(&env.http, "tahinli").await; | 328 | + let tahinli = login(&env.http, "tahinli", PW).await; |
| 430 | let owner = env.http.get("/bob?tab=stars", Some(&tahinli)).await; | 329 | let owner = env.http.get("/bob?tab=stars", Some(&tahinli)).await; |
| 431 | let mut names = starred(&owner.body); | 330 | let mut names = starred(&owner.body); |
| 432 | names.sort(); | 331 | names.sort(); |
crates/forge-web/tests/e2e/ui_prefs_e2e.rsmodified+15 -117
| @@ -3,95 +3,14 @@ | |||
| 3 | //! tests, this starts the real `forge` binary and drives it over HTTP. Cookies are set by hand | 3 | //! tests, this starts the real `forge` binary and drives it over HTTP. Cookies are set by hand |
| 4 | //! because the session cookie is `__Host-` and `Secure`. | 4 | //! because the session cookie is `__Host-` and `Secure`. |
| 5 | 5 | ||
| 6 | -use crate::common::{BIN, Server, describe, input_value, launch, pick_ports, write_config}; | 6 | +use crate::common::{ |
| 7 | + BIN, Http, Server, cookie_named, describe, input_value, launch, login, pick_ports, write_config, | ||
| 8 | +}; | ||
| 7 | use std::path::Path; | 9 | use std::path::Path; |
| 8 | use std::process::Command; | 10 | use std::process::Command; |
| 9 | -use std::time::Duration; | ||
| 10 | - | ||
| 11 | -use reqwest::header::{COOKIE, HeaderMap, HeaderName, SET_COOKIE}; | ||
| 12 | 11 | ||
| 13 | const PW: &str = "tahinli-password-123"; | 12 | const PW: &str = "tahinli-password-123"; |
| 14 | 13 | ||
| 15 | -struct Resp { | ||
| 16 | - status: u16, | ||
| 17 | - headers: HeaderMap, | ||
| 18 | - body: String, | ||
| 19 | -} | ||
| 20 | - | ||
| 21 | -impl Resp { | ||
| 22 | - fn header(&self, name: HeaderName) -> Option<String> { | ||
| 23 | - self.headers | ||
| 24 | - .get(name) | ||
| 25 | - .and_then(|v| v.to_str().ok()) | ||
| 26 | - .map(str::to_owned) | ||
| 27 | - } | ||
| 28 | - | ||
| 29 | - fn set_cookies(&self) -> Vec<String> { | ||
| 30 | - self.headers | ||
| 31 | - .get_all(SET_COOKIE) | ||
| 32 | - .iter() | ||
| 33 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 34 | - .collect() | ||
| 35 | - } | ||
| 36 | -} | ||
| 37 | - | ||
| 38 | -struct Http { | ||
| 39 | - client: reqwest::Client, | ||
| 40 | - base: String, | ||
| 41 | -} | ||
| 42 | - | ||
| 43 | -impl Http { | ||
| 44 | - fn new(port: u16) -> Self { | ||
| 45 | - let client = reqwest::Client::builder() | ||
| 46 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 47 | - .timeout(Duration::from_secs(60)) | ||
| 48 | - .build() | ||
| 49 | - .expect("http client"); | ||
| 50 | - Self { | ||
| 51 | - client, | ||
| 52 | - base: format!("http://127.0.0.1:{port}"), | ||
| 53 | - } | ||
| 54 | - } | ||
| 55 | - | ||
| 56 | - async fn get(&self, path: &str, cookie: Option<&str>) -> Resp { | ||
| 57 | - self.send(self.client.get(format!("{}{path}", self.base)), cookie) | ||
| 58 | - .await | ||
| 59 | - } | ||
| 60 | - | ||
| 61 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 62 | - self.send( | ||
| 63 | - self.client.post(format!("{}{path}", self.base)).form(form), | ||
| 64 | - cookie, | ||
| 65 | - ) | ||
| 66 | - .await | ||
| 67 | - } | ||
| 68 | - | ||
| 69 | - async fn send(&self, req: reqwest::RequestBuilder, cookie: Option<&str>) -> Resp { | ||
| 70 | - let req = match cookie { | ||
| 71 | - Some(cookie) => req.header(COOKIE, cookie), | ||
| 72 | - None => req, | ||
| 73 | - }; | ||
| 74 | - let res = req.send().await.expect("http request"); | ||
| 75 | - let status = res.status().as_u16(); | ||
| 76 | - let headers = res.headers().clone(); | ||
| 77 | - let body = res.text().await.expect("read body"); | ||
| 78 | - Resp { | ||
| 79 | - status, | ||
| 80 | - headers, | ||
| 81 | - body, | ||
| 82 | - } | ||
| 83 | - } | ||
| 84 | -} | ||
| 85 | - | ||
| 86 | -/// `name=value` for the cookie whose name ends with `suffix`, ignoring cleared cookies. | ||
| 87 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 88 | - resp.set_cookies().iter().find_map(|set| { | ||
| 89 | - let pair = set.split(';').next()?; | ||
| 90 | - let (name, value) = pair.split_once('=')?; | ||
| 91 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 92 | - }) | ||
| 93 | -} | ||
| 94 | - | ||
| 95 | /// The value of the first `<input name="...">` on a page. | 14 | /// The value of the first `<input name="...">` on a page. |
| 96 | /// The `data-theme` attribute of `<html>`, if the page sets one. | 15 | /// The `data-theme` attribute of `<html>`, if the page sets one. |
| 97 | fn html_theme(body: &str) -> Option<String> { | 16 | fn html_theme(body: &str) -> Option<String> { |
| @@ -191,27 +110,6 @@ fn admin(config: &Path, args: &[&str]) { | |||
| 191 | ); | 110 | ); |
| 192 | } | 111 | } |
| 193 | 112 | ||
| 194 | -async fn login(http: &Http, username: &str) -> String { | ||
| 195 | - let page = http.get("/login", None).await; | ||
| 196 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 197 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 198 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 199 | - let res = http | ||
| 200 | - .post_form( | ||
| 201 | - "/login", | ||
| 202 | - &[ | ||
| 203 | - ("username", username), | ||
| 204 | - ("password", PW), | ||
| 205 | - ("next", "/"), | ||
| 206 | - ("precsrf", &precsrf), | ||
| 207 | - ], | ||
| 208 | - Some(&precsrf_cookie), | ||
| 209 | - ) | ||
| 210 | - .await; | ||
| 211 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 212 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 213 | -} | ||
| 214 | - | ||
| 215 | /// A running server with user `tahinli`. The temp dir lives as long as the test. | 113 | /// A running server with user `tahinli`. The temp dir lives as long as the test. |
| 216 | struct Env { | 114 | struct Env { |
| 217 | _root: tempfile::TempDir, | 115 | _root: tempfile::TempDir, |
| @@ -237,7 +135,7 @@ async fn start() -> Env { | |||
| 237 | #[tokio::test] | 135 | #[tokio::test] |
| 238 | async fn appearance_setting_persists_and_renders_the_html_attribute() { | 136 | async fn appearance_setting_persists_and_renders_the_html_attribute() { |
| 239 | let env = start().await; | 137 | let env = start().await; |
| 240 | - let session = login(&env.http, "tahinli").await; | 138 | + let session = login(&env.http, "tahinli", PW).await; |
| 241 | 139 | ||
| 242 | let page = env.http.get("/settings/appearance", Some(&session)).await; | 140 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 243 | assert_eq!(page.status, 200); | 141 | assert_eq!(page.status, 200); |
| @@ -282,7 +180,7 @@ async fn appearance_setting_persists_and_renders_the_html_attribute() { | |||
| 282 | #[tokio::test] | 180 | #[tokio::test] |
| 283 | async fn the_form_has_no_save_button_and_every_field_autosubmits() { | 181 | async fn the_form_has_no_save_button_and_every_field_autosubmits() { |
| 284 | let env = start().await; | 182 | let env = start().await; |
| 285 | - let session = login(&env.http, "tahinli").await; | 183 | + let session = login(&env.http, "tahinli", PW).await; |
| 286 | let page = env.http.get("/settings/appearance", Some(&session)).await; | 184 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 287 | let doc = scraper::Html::parse_document(&page.body); | 185 | let doc = scraper::Html::parse_document(&page.body); |
| 288 | 186 | ||
| @@ -315,7 +213,7 @@ async fn the_form_has_no_save_button_and_every_field_autosubmits() { | |||
| 315 | #[tokio::test] | 213 | #[tokio::test] |
| 316 | async fn light_and_dark_colours_save_independently_and_render_on_html() { | 214 | async fn light_and_dark_colours_save_independently_and_render_on_html() { |
| 317 | let env = start().await; | 215 | let env = start().await; |
| 318 | - let session = login(&env.http, "tahinli").await; | 216 | + let session = login(&env.http, "tahinli", PW).await; |
| 319 | 217 | ||
| 320 | let page = env.http.get("/settings/appearance", Some(&session)).await; | 218 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 321 | let defaults = ["sage", "sage", "fern", "fern"].map(str::to_owned); | 219 | let defaults = ["sage", "sage", "fern", "fern"].map(str::to_owned); |
| @@ -442,7 +340,7 @@ async fn light_and_dark_colours_save_independently_and_render_on_html() { | |||
| 442 | #[tokio::test] | 340 | #[tokio::test] |
| 443 | async fn a_save_without_colour_fields_keeps_the_stored_colours() { | 341 | async fn a_save_without_colour_fields_keeps_the_stored_colours() { |
| 444 | let env = start().await; | 342 | let env = start().await; |
| 445 | - let session = login(&env.http, "tahinli").await; | 343 | + let session = login(&env.http, "tahinli", PW).await; |
| 446 | 344 | ||
| 447 | let page = env.http.get("/settings/appearance", Some(&session)).await; | 345 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 448 | let csrf = input_value(&page.body, "csrf"); | 346 | let csrf = input_value(&page.body, "csrf"); |
| @@ -489,7 +387,7 @@ async fn a_save_without_colour_fields_keeps_the_stored_colours() { | |||
| 489 | #[tokio::test] | 387 | #[tokio::test] |
| 490 | async fn appearance_save_without_a_valid_csrf_token_is_403() { | 388 | async fn appearance_save_without_a_valid_csrf_token_is_403() { |
| 491 | let env = start().await; | 389 | let env = start().await; |
| 492 | - let session = login(&env.http, "tahinli").await; | 390 | + let session = login(&env.http, "tahinli", PW).await; |
| 493 | let res = env | 391 | let res = env |
| 494 | .http | 392 | .http |
| 495 | .post_form( | 393 | .post_form( |
| @@ -510,7 +408,7 @@ async fn appearance_save_without_a_valid_csrf_token_is_403() { | |||
| 510 | #[tokio::test] | 408 | #[tokio::test] |
| 511 | async fn unoffered_theme_or_language_is_rejected_and_nothing_is_saved() { | 409 | async fn unoffered_theme_or_language_is_rejected_and_nothing_is_saved() { |
| 512 | let env = start().await; | 410 | let env = start().await; |
| 513 | - let session = login(&env.http, "tahinli").await; | 411 | + let session = login(&env.http, "tahinli", PW).await; |
| 514 | let page = env.http.get("/settings/appearance", Some(&session)).await; | 412 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 515 | let csrf = input_value(&page.body, "csrf"); | 413 | let csrf = input_value(&page.body, "csrf"); |
| 516 | for form in [ | 414 | for form in [ |
| @@ -582,7 +480,7 @@ async fn anonymous_theme_cookie_ignores_unknown_values() { | |||
| 582 | #[tokio::test] | 480 | #[tokio::test] |
| 583 | async fn signed_in_preference_wins_over_the_anonymous_cookie() { | 481 | async fn signed_in_preference_wins_over_the_anonymous_cookie() { |
| 584 | let env = start().await; | 482 | let env = start().await; |
| 585 | - let session = login(&env.http, "tahinli").await; | 483 | + let session = login(&env.http, "tahinli", PW).await; |
| 586 | let page = env.http.get("/settings/appearance", Some(&session)).await; | 484 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 587 | assert!( | 485 | assert!( |
| 588 | !page.body.contains("class=\"theme-picker\""), | 486 | !page.body.contains("class=\"theme-picker\""), |
| @@ -611,7 +509,7 @@ async fn appearance_page_needs_a_session() { | |||
| 611 | #[tokio::test] | 509 | #[tokio::test] |
| 612 | async fn signed_in_toggle_saves_the_preference_and_returns_to_back() { | 510 | async fn signed_in_toggle_saves_the_preference_and_returns_to_back() { |
| 613 | let env = start().await; | 511 | let env = start().await; |
| 614 | - let session = login(&env.http, "tahinli").await; | 512 | + let session = login(&env.http, "tahinli", PW).await; |
| 615 | let page = env.http.get("/settings/appearance", Some(&session)).await; | 513 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 616 | let csrf = input_value(&page.body, "csrf"); | 514 | let csrf = input_value(&page.body, "csrf"); |
| 617 | let saved = env | 515 | let saved = env |
| @@ -666,7 +564,7 @@ async fn signed_in_toggle_saves_the_preference_and_returns_to_back() { | |||
| 666 | #[tokio::test] | 564 | #[tokio::test] |
| 667 | async fn theme_toggle_with_an_unsafe_back_returns_to_the_root() { | 565 | async fn theme_toggle_with_an_unsafe_back_returns_to_the_root() { |
| 668 | let env = start().await; | 566 | let env = start().await; |
| 669 | - let session = login(&env.http, "tahinli").await; | 567 | + let session = login(&env.http, "tahinli", PW).await; |
| 670 | let page = env.http.get("/settings/appearance", Some(&session)).await; | 568 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 671 | let csrf = input_value(&page.body, "csrf"); | 569 | let csrf = input_value(&page.body, "csrf"); |
| 672 | for back in ["//evil.com", "https://evil.com", "/\\evil"] { | 570 | for back in ["//evil.com", "https://evil.com", "/\\evil"] { |
| @@ -690,7 +588,7 @@ async fn theme_toggle_with_an_unsafe_back_returns_to_the_root() { | |||
| 690 | #[tokio::test] | 588 | #[tokio::test] |
| 691 | async fn signed_in_theme_toggle_without_a_valid_csrf_is_refused() { | 589 | async fn signed_in_theme_toggle_without_a_valid_csrf_is_refused() { |
| 692 | let env = start().await; | 590 | let env = start().await; |
| 693 | - let session = login(&env.http, "tahinli").await; | 591 | + let session = login(&env.http, "tahinli", PW).await; |
| 694 | for form in [ | 592 | for form in [ |
| 695 | vec![("csrf", "forged"), ("theme", "dark")], | 593 | vec![("csrf", "forged"), ("theme", "dark")], |
| 696 | vec![("theme", "dark")], | 594 | vec![("theme", "dark")], |
| @@ -725,7 +623,7 @@ async fn anonymous_theme_toggle_sets_the_cookie_and_returns_to_back() { | |||
| 725 | #[tokio::test] | 623 | #[tokio::test] |
| 726 | async fn theme_button_cycles_light_dark_auto_light_for_signed_in_users() { | 624 | async fn theme_button_cycles_light_dark_auto_light_for_signed_in_users() { |
| 727 | let env = start().await; | 625 | let env = start().await; |
| 728 | - let session = login(&env.http, "tahinli").await; | 626 | + let session = login(&env.http, "tahinli", PW).await; |
| 729 | let back = "/settings/appearance"; | 627 | let back = "/settings/appearance"; |
| 730 | 628 | ||
| 731 | // A new user starts on light, the default, so the button offers dark next. | 629 | // A new user starts on light, the default, so the button offers dark next. |
| @@ -823,7 +721,7 @@ async fn theme_button_cycles_for_anonymous_visitors_through_the_cookie() { | |||
| 823 | #[tokio::test] | 721 | #[tokio::test] |
| 824 | async fn appearance_sidebar_side_saves_and_renders_on_html() { | 722 | async fn appearance_sidebar_side_saves_and_renders_on_html() { |
| 825 | let env = start().await; | 723 | let env = start().await; |
| 826 | - let session = login(&env.http, "tahinli").await; | 724 | + let session = login(&env.http, "tahinli", PW).await; |
| 827 | 725 | ||
| 828 | let page = env.http.get("/settings/appearance", Some(&session)).await; | 726 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 829 | assert_eq!( | 727 | assert_eq!( |
crates/forge-web/tests/e2e/user_delete_e2e.rsmodified+9 -94
| @@ -3,15 +3,17 @@ | |||
| 3 | //! the real `forge` binary and drives it over HTTP. Cookies are set by hand because the | 3 | //! the real `forge` binary and drives it over HTTP. Cookies are set by hand because the |
| 4 | //! session cookie is `__Host-` and `Secure`. | 4 | //! session cookie is `__Host-` and `Secure`. |
| 5 | 5 | ||
| 6 | -use crate::common::{BIN, Server, describe, input_value, launch, pick_ports, write_config}; | 6 | +use crate::common::{ |
| 7 | + BIN, Http, Resp, Server, cookie_named, describe, input_value, launch, login, pick_ports, | ||
| 8 | + write_config, | ||
| 9 | +}; | ||
| 7 | use std::fs; | 10 | use std::fs; |
| 8 | use std::future::Future; | 11 | use std::future::Future; |
| 9 | use std::path::{Path, PathBuf}; | 12 | use std::path::{Path, PathBuf}; |
| 10 | use std::process::Command; | 13 | use std::process::Command; |
| 11 | -use std::time::Duration; | ||
| 12 | 14 | ||
| 13 | use forge_core::{Store, Visibility}; | 15 | use forge_core::{Store, Visibility}; |
| 14 | -use reqwest::header::{COOKIE, HeaderMap, HeaderName, LOCATION, SET_COOKIE}; | 16 | +use reqwest::header::LOCATION; |
| 15 | 17 | ||
| 16 | /// Each test starts a server on ports picked just before, so the tests run one at a time. | 18 | /// Each test starts a server on ports picked just before, so the tests run one at a time. |
| 17 | static SERIAL: std::sync::LazyLock<tokio::sync::Mutex<()>> = | 19 | static SERIAL: std::sync::LazyLock<tokio::sync::Mutex<()>> = |
| @@ -19,86 +21,6 @@ static SERIAL: std::sync::LazyLock<tokio::sync::Mutex<()>> = | |||
| 19 | 21 | ||
| 20 | const PW: &str = "tahinli-password-123"; | 22 | const PW: &str = "tahinli-password-123"; |
| 21 | 23 | ||
| 22 | -struct Resp { | ||
| 23 | - status: u16, | ||
| 24 | - headers: HeaderMap, | ||
| 25 | - body: String, | ||
| 26 | -} | ||
| 27 | - | ||
| 28 | -impl Resp { | ||
| 29 | - fn header(&self, name: HeaderName) -> Option<String> { | ||
| 30 | - self.headers | ||
| 31 | - .get(name) | ||
| 32 | - .and_then(|v| v.to_str().ok()) | ||
| 33 | - .map(str::to_owned) | ||
| 34 | - } | ||
| 35 | - | ||
| 36 | - fn set_cookies(&self) -> Vec<String> { | ||
| 37 | - self.headers | ||
| 38 | - .get_all(SET_COOKIE) | ||
| 39 | - .iter() | ||
| 40 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 41 | - .collect() | ||
| 42 | - } | ||
| 43 | -} | ||
| 44 | - | ||
| 45 | -struct Http { | ||
| 46 | - client: reqwest::Client, | ||
| 47 | - base: String, | ||
| 48 | -} | ||
| 49 | - | ||
| 50 | -impl Http { | ||
| 51 | - fn new(port: u16) -> Self { | ||
| 52 | - let client = reqwest::Client::builder() | ||
| 53 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 54 | - .timeout(Duration::from_secs(60)) | ||
| 55 | - .build() | ||
| 56 | - .expect("http client"); | ||
| 57 | - Self { | ||
| 58 | - client, | ||
| 59 | - base: format!("http://127.0.0.1:{port}"), | ||
| 60 | - } | ||
| 61 | - } | ||
| 62 | - | ||
| 63 | - async fn get(&self, path: &str, cookie: Option<&str>) -> Resp { | ||
| 64 | - self.send(self.client.get(format!("{}{path}", self.base)), cookie) | ||
| 65 | - .await | ||
| 66 | - } | ||
| 67 | - | ||
| 68 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 69 | - self.send( | ||
| 70 | - self.client.post(format!("{}{path}", self.base)).form(form), | ||
| 71 | - cookie, | ||
| 72 | - ) | ||
| 73 | - .await | ||
| 74 | - } | ||
| 75 | - | ||
| 76 | - async fn send(&self, req: reqwest::RequestBuilder, cookie: Option<&str>) -> Resp { | ||
| 77 | - let req = match cookie { | ||
| 78 | - Some(cookie) => req.header(COOKIE, cookie), | ||
| 79 | - None => req, | ||
| 80 | - }; | ||
| 81 | - let res = req.send().await.expect("http request"); | ||
| 82 | - let status = res.status().as_u16(); | ||
| 83 | - let headers = res.headers().clone(); | ||
| 84 | - let body = res.text().await.expect("read body"); | ||
| 85 | - Resp { | ||
| 86 | - status, | ||
| 87 | - headers, | ||
| 88 | - body, | ||
| 89 | - } | ||
| 90 | - } | ||
| 91 | -} | ||
| 92 | - | ||
| 93 | -/// `name=value` for the cookie whose name ends with `suffix`, ignoring cleared cookies. | ||
| 94 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 95 | - resp.set_cookies().iter().find_map(|set| { | ||
| 96 | - let pair = set.split(';').next()?; | ||
| 97 | - let (name, value) = pair.split_once('=')?; | ||
| 98 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 99 | - }) | ||
| 100 | -} | ||
| 101 | - | ||
| 102 | /// The value of the first `<input name="...">` on a page. | 24 | /// The value of the first `<input name="...">` on a page. |
| 103 | /// Runs an admin subcommand with the password in FORGE_PASSWORD and asserts it succeeded. | 25 | /// Runs an admin subcommand with the password in FORGE_PASSWORD and asserts it succeeded. |
| 104 | fn admin(config: &Path, args: &[&str]) { | 26 | fn admin(config: &Path, args: &[&str]) { |
| @@ -136,13 +58,6 @@ async fn try_login(http: &Http, username: &str) -> Resp { | |||
| 136 | .await | 58 | .await |
| 137 | } | 59 | } |
| 138 | 60 | ||
| 139 | -/// Signs in and returns the session cookie. | ||
| 140 | -async fn login(http: &Http, username: &str) -> String { | ||
| 141 | - let res = try_login(http, username).await; | ||
| 142 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 143 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 144 | -} | ||
| 145 | - | ||
| 146 | /// Creates the users (`dana` is the first user, so an administrator), then runs `seed` against | 61 | /// Creates the users (`dana` is the first user, so an administrator), then runs `seed` against |
| 147 | /// the store before the server starts. `seed` gets the data dir and the open store. | 62 | /// the store before the server starts. `seed` gets the data dir and the open store. |
| 148 | struct Env { | 63 | struct Env { |
| @@ -211,7 +126,7 @@ async fn admin_delete_moves_content_to_ghost_and_pages_still_render() { | |||
| 211 | let _serial = SERIAL.lock().await; | 126 | let _serial = SERIAL.lock().await; |
| 212 | let env = start(seed_issue_by_tahinli).await; | 127 | let env = start(seed_issue_by_tahinli).await; |
| 213 | let http = &env.http; | 128 | let http = &env.http; |
| 214 | - let dana = login(http, "dana").await; | 129 | + let dana = login(http, "dana", PW).await; |
| 215 | let tahinli_id = fs::read_to_string(env.dir.join("tahinli.id")).expect("tahinli id"); | 130 | let tahinli_id = fs::read_to_string(env.dir.join("tahinli.id")).expect("tahinli id"); |
| 216 | let path = format!("/admin/users/{tahinli_id}/delete"); | 131 | let path = format!("/admin/users/{tahinli_id}/delete"); |
| 217 | 132 | ||
| @@ -264,7 +179,7 @@ async fn self_delete_refuses_wrong_password_and_owned_repos() { | |||
| 264 | let http = &env.http; | 179 | let http = &env.http; |
| 265 | 180 | ||
| 266 | // Tahinli owns nothing: a wrong password is refused and the account stays. | 181 | // Tahinli owns nothing: a wrong password is refused and the account stays. |
| 267 | - let tahinli = login(http, "tahinli").await; | 182 | + let tahinli = login(http, "tahinli", PW).await; |
| 268 | let security = http.get("/settings/security", Some(&tahinli)).await; | 183 | let security = http.get("/settings/security", Some(&tahinli)).await; |
| 269 | assert_eq!(security.status, 200); | 184 | assert_eq!(security.status, 200); |
| 270 | let csrf = input_value(&security.body, "csrf"); | 185 | let csrf = input_value(&security.body, "csrf"); |
| @@ -291,7 +206,7 @@ async fn self_delete_refuses_wrong_password_and_owned_repos() { | |||
| 291 | ); | 206 | ); |
| 292 | 207 | ||
| 293 | // Bob owns `proj`: deletion is refused and the repo is named. | 208 | // Bob owns `proj`: deletion is refused and the repo is named. |
| 294 | - let bob = login(http, "bob").await; | 209 | + let bob = login(http, "bob", PW).await; |
| 295 | let security = http.get("/settings/security", Some(&bob)).await; | 210 | let security = http.get("/settings/security", Some(&bob)).await; |
| 296 | let csrf = input_value(&security.body, "csrf"); | 211 | let csrf = input_value(&security.body, "csrf"); |
| 297 | let refused = http | 212 | let refused = http |
| @@ -325,7 +240,7 @@ async fn self_delete_with_correct_password_signs_out_and_removes_login() { | |||
| 325 | let _serial = SERIAL.lock().await; | 240 | let _serial = SERIAL.lock().await; |
| 326 | let env = start(seed_issue_by_tahinli).await; | 241 | let env = start(seed_issue_by_tahinli).await; |
| 327 | let http = &env.http; | 242 | let http = &env.http; |
| 328 | - let tahinli = login(http, "tahinli").await; | 243 | + let tahinli = login(http, "tahinli", PW).await; |
| 329 | let security = http.get("/settings/security", Some(&tahinli)).await; | 244 | let security = http.get("/settings/security", Some(&tahinli)).await; |
| 330 | let csrf = input_value(&security.body, "csrf"); | 245 | let csrf = input_value(&security.body, "csrf"); |
| 331 | let done = http | 246 | let done = http |
crates/forge-web/tests/e2e/wiki_e2e.rsmodified+10 -109
| @@ -5,97 +5,19 @@ | |||
| 5 | //! Cookies are handled by hand because the session cookie is `__Host-forge_session` with `Secure`, | 5 | //! Cookies are handled by hand because the session cookie is `__Host-forge_session` with `Secure`, |
| 6 | //! which a cookie jar would not send over plain http. | 6 | //! which a cookie jar would not send over plain http. |
| 7 | 7 | ||
| 8 | -use crate::common::{BIN, describe, input_value, launch, pick_ports, tool_on_path, write_config}; | 8 | +use crate::common::{ |
| 9 | + BIN, Http, Resp, describe, input_value, launch, login, pick_ports, tool_on_path, write_config, | ||
| 10 | +}; | ||
| 9 | use std::fs; | 11 | use std::fs; |
| 10 | use std::path::Path; | 12 | use std::path::Path; |
| 11 | use std::process::{Command, Output}; | 13 | use std::process::{Command, Output}; |
| 12 | use std::time::Duration; | 14 | use std::time::Duration; |
| 13 | 15 | ||
| 14 | use forge_core::{Role, Store, Visibility}; | 16 | use forge_core::{Role, Store, Visibility}; |
| 15 | -use reqwest::header::{COOKIE, HeaderMap, HeaderName, LOCATION, SET_COOKIE}; | 17 | +use reqwest::header::{COOKIE, LOCATION}; |
| 16 | 18 | ||
| 17 | const PW: &str = "tahinli-password-123"; | 19 | const PW: &str = "tahinli-password-123"; |
| 18 | 20 | ||
| 19 | -struct Resp { | ||
| 20 | - status: u16, | ||
| 21 | - headers: HeaderMap, | ||
| 22 | - body: String, | ||
| 23 | -} | ||
| 24 | - | ||
| 25 | -impl Resp { | ||
| 26 | - fn header(&self, name: HeaderName) -> Option<String> { | ||
| 27 | - self.headers | ||
| 28 | - .get(name) | ||
| 29 | - .and_then(|v| v.to_str().ok()) | ||
| 30 | - .map(str::to_owned) | ||
| 31 | - } | ||
| 32 | - | ||
| 33 | - fn set_cookies(&self) -> Vec<String> { | ||
| 34 | - self.headers | ||
| 35 | - .get_all(SET_COOKIE) | ||
| 36 | - .iter() | ||
| 37 | - .filter_map(|v| v.to_str().ok().map(str::to_owned)) | ||
| 38 | - .collect() | ||
| 39 | - } | ||
| 40 | -} | ||
| 41 | - | ||
| 42 | -struct Http { | ||
| 43 | - client: reqwest::Client, | ||
| 44 | - base: String, | ||
| 45 | -} | ||
| 46 | - | ||
| 47 | -impl Http { | ||
| 48 | - fn new(port: u16) -> Self { | ||
| 49 | - let client = reqwest::Client::builder() | ||
| 50 | - .redirect(reqwest::redirect::Policy::none()) | ||
| 51 | - .timeout(Duration::from_secs(60)) | ||
| 52 | - .build() | ||
| 53 | - .expect("http client"); | ||
| 54 | - Self { | ||
| 55 | - client, | ||
| 56 | - base: format!("http://127.0.0.1:{port}"), | ||
| 57 | - } | ||
| 58 | - } | ||
| 59 | - | ||
| 60 | - async fn get(&self, path: &str, cookie: Option<&str>) -> Resp { | ||
| 61 | - self.send(self.client.get(format!("{}{path}", self.base)), cookie) | ||
| 62 | - .await | ||
| 63 | - } | ||
| 64 | - | ||
| 65 | - async fn post_form(&self, path: &str, form: &[(&str, &str)], cookie: Option<&str>) -> Resp { | ||
| 66 | - self.send( | ||
| 67 | - self.client.post(format!("{}{path}", self.base)).form(form), | ||
| 68 | - cookie, | ||
| 69 | - ) | ||
| 70 | - .await | ||
| 71 | - } | ||
| 72 | - | ||
| 73 | - async fn send(&self, req: reqwest::RequestBuilder, cookie: Option<&str>) -> Resp { | ||
| 74 | - let req = match cookie { | ||
| 75 | - Some(cookie) => req.header(COOKIE, cookie), | ||
| 76 | - None => req, | ||
| 77 | - }; | ||
| 78 | - let res = req.send().await.expect("http request"); | ||
| 79 | - let status = res.status().as_u16(); | ||
| 80 | - let headers = res.headers().clone(); | ||
| 81 | - let body = res.text().await.expect("read body"); | ||
| 82 | - Resp { | ||
| 83 | - status, | ||
| 84 | - headers, | ||
| 85 | - body, | ||
| 86 | - } | ||
| 87 | - } | ||
| 88 | -} | ||
| 89 | - | ||
| 90 | -/// `name=value` for the cookie whose name ends with `suffix`, ignoring cleared cookies. | ||
| 91 | -fn cookie_named(resp: &Resp, suffix: &str) -> Option<String> { | ||
| 92 | - resp.set_cookies().iter().find_map(|set| { | ||
| 93 | - let pair = set.split(';').next()?; | ||
| 94 | - let (name, value) = pair.split_once('=')?; | ||
| 95 | - (name.ends_with(suffix) && !value.is_empty()).then(|| format!("{name}={value}")) | ||
| 96 | - }) | ||
| 97 | -} | ||
| 98 | - | ||
| 99 | /// The value of the first `<input name="...">` on a page. | 21 | /// The value of the first `<input name="...">` on a page. |
| 100 | /// Runs an admin subcommand with the password in FORGE_PASSWORD and asserts it succeeded. | 22 | /// Runs an admin subcommand with the password in FORGE_PASSWORD and asserts it succeeded. |
| 101 | fn admin(config: &Path, args: &[&str]) { | 23 | fn admin(config: &Path, args: &[&str]) { |
| @@ -114,27 +36,6 @@ fn admin(config: &Path, args: &[&str]) { | |||
| 114 | ); | 36 | ); |
| 115 | } | 37 | } |
| 116 | 38 | ||
| 117 | -async fn login(http: &Http, username: &str) -> String { | ||
| 118 | - let page = http.get("/login", None).await; | ||
| 119 | - assert_eq!(page.status, 200, "GET /login"); | ||
| 120 | - let precsrf_cookie = cookie_named(&page, "forge_precsrf").expect("precsrf cookie"); | ||
| 121 | - let precsrf = input_value(&page.body, "precsrf"); | ||
| 122 | - let res = http | ||
| 123 | - .post_form( | ||
| 124 | - "/login", | ||
| 125 | - &[ | ||
| 126 | - ("username", username), | ||
| 127 | - ("password", PW), | ||
| 128 | - ("next", "/"), | ||
| 129 | - ("precsrf", &precsrf), | ||
| 130 | - ], | ||
| 131 | - Some(&precsrf_cookie), | ||
| 132 | - ) | ||
| 133 | - .await; | ||
| 134 | - assert_eq!(res.status, 303, "login as {username}: {}", res.body); | ||
| 135 | - cookie_named(&res, "forge_session").expect("session cookie after login") | ||
| 136 | -} | ||
| 137 | - | ||
| 138 | /// The session's CSRF token, read from a page every signed-in user can open. | 39 | /// The session's CSRF token, read from a page every signed-in user can open. |
| 139 | async fn csrf_for(http: &Http, session: &str) -> String { | 40 | async fn csrf_for(http: &Http, session: &str) -> String { |
| 140 | let page = http.get("/settings/tokens", Some(session)).await; | 41 | let page = http.get("/settings/tokens", Some(session)).await; |
| @@ -267,9 +168,9 @@ async fn wiki_end_to_end() { | |||
| 267 | 168 | ||
| 268 | let (server, ports) = launch(&dir, ports).await; | 169 | let (server, ports) = launch(&dir, ports).await; |
| 269 | let http = Http::new(ports.http); | 170 | let http = Http::new(ports.http); |
| 270 | - let bob = login(&http, "bob").await; | 171 | + let bob = login(&http, "bob", PW).await; |
| 271 | - let carol = login(&http, "carol").await; | 172 | + let carol = login(&http, "carol", PW).await; |
| 272 | - let dave = login(&http, "dave").await; | 173 | + let dave = login(&http, "dave", PW).await; |
| 273 | 174 | ||
| 274 | // The wiki starts empty, and the tab is there. | 175 | // The wiki starts empty, and the tab is there. |
| 275 | let empty = page_body(&http, "/bob/proj/wiki", Some(&bob)).await; | 176 | let empty = page_body(&http, "/bob/proj/wiki", Some(&bob)).await; |
| @@ -336,7 +237,7 @@ async fn wiki_end_to_end() { | |||
| 336 | after_push.body | 237 | after_push.body |
| 337 | ); | 238 | ); |
| 338 | // The push is in the admin audit log (read over HTTP, the server holds the database open). | 239 | // The push is in the admin audit log (read over HTTP, the server holds the database open). |
| 339 | - let tahinli = login(&http, "tahinli").await; | 240 | + let tahinli = login(&http, "tahinli", PW).await; |
| 340 | let audit = http | 241 | let audit = http |
| 341 | .get("/admin/audit?action=wiki_pushed", Some(&tahinli)) | 242 | .get("/admin/audit?action=wiki_pushed", Some(&tahinli)) |
| 342 | .await; | 243 | .await; |
| @@ -498,8 +399,8 @@ async fn wiki_streams_follow_saves_and_pushes() { | |||
| 498 | 399 | ||
| 499 | let (server, ports) = launch(&dir, ports).await; | 400 | let (server, ports) = launch(&dir, ports).await; |
| 500 | let http = Http::new(ports.http); | 401 | let http = Http::new(ports.http); |
| 501 | - let bob = login(&http, "bob").await; | 402 | + let bob = login(&http, "bob", PW).await; |
| 502 | - let dave = login(&http, "dave").await; | 403 | + let dave = login(&http, "dave", PW).await; |
| 503 | 404 | ||
| 504 | // The page list follows the wiki stream. | 405 | // The page list follows the wiki stream. |
| 505 | let listing = page_body(&http, "/bob/proj/wiki/-/pages", Some(&bob)).await; | 406 | let listing = page_body(&http, "/bob/proj/wiki/-/pages", Some(&bob)).await; |