| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use crate::common::{ |
| 9 | Http, Resp, bin, describe, input_value, launch, login, pick_ports, tool_on_path, write_config, |
| 10 | }; |
| 11 | use std::fs; |
| 12 | use std::process::Command; |
| 13 | |
| 14 | use forge_core::store::orgs::OrgMemberRole; |
| 15 | use forge_core::{Store, Visibility}; |
| 16 | use reqwest::header::LOCATION; |
| 17 | |
| 18 | const PW: &str = "tahinli-password-123"; |
| 19 | |
| 20 | |
| 21 | fn form_action(body: &str, suffix: &str) -> String { |
| 22 | let doc = scraper::Html::parse_document(body); |
| 23 | let selector = scraper::Selector::parse("form").expect("selector"); |
| 24 | doc.select(&selector) |
| 25 | .filter_map(|el| el.value().attr("action")) |
| 26 | .find(|action| action.ends_with(suffix)) |
| 27 | .unwrap_or_else(|| panic!("no form ending in {suffix} on page")) |
| 28 | .to_owned() |
| 29 | } |
| 30 | |
| 31 | |
| 32 | fn admin(config: &std::path::Path, args: &[&str]) { |
| 33 | let out = Command::new(bin()) |
| 34 | .arg("--config") |
| 35 | .arg(config) |
| 36 | .arg("admin") |
| 37 | .args(args) |
| 38 | .env("FORGE_PASSWORD", PW) |
| 39 | .output() |
| 40 | .expect("run admin command"); |
| 41 | assert!( |
| 42 | out.status.success(), |
| 43 | "admin {args:?} failed:\n{}", |
| 44 | describe(&out) |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | async fn csrf_for(http: &Http, session: &str) -> String { |
| 50 | let page = http.get("/settings/keys", Some(session)).await; |
| 51 | assert_eq!(page.status, 200, "GET /settings/keys"); |
| 52 | input_value(&page.body, "csrf") |
| 53 | } |
| 54 | |
| 55 | |
| 56 | async fn offer(http: &Http, session: &str, owner: &str, repo: &str, to: &str) -> Resp { |
| 57 | let csrf = csrf_for(http, session).await; |
| 58 | http.post_form( |
| 59 | &format!("/{owner}/{repo}/settings/transfer"), |
| 60 | &[ |
| 61 | ("csrf", &csrf), |
| 62 | ("new_owner", to), |
| 63 | ("confirm", &format!("{owner}/{repo}")), |
| 64 | ], |
| 65 | Some(session), |
| 66 | ) |
| 67 | .await |
| 68 | } |
| 69 | |
| 70 | |
| 71 | async fn act_from(http: &Http, session: &str, page: &str, suffix: &str) -> Resp { |
| 72 | let body = http.get(page, Some(session)).await.body; |
| 73 | let action = form_action(&body, suffix); |
| 74 | let csrf = csrf_for(http, session).await; |
| 75 | http.post_form(&action, &[("csrf", &csrf)], Some(session)) |
| 76 | .await |
| 77 | } |
| 78 | |
| 79 | #[tokio::test] |
| 80 | async fn repo_transfer_offers_declines_cancels_and_org_targets() { |
| 81 | if !tool_on_path("git") { |
| 82 | eprintln!("SKIP repo_transfer_e2e: `git` was not found on PATH"); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | let root = tempfile::Builder::new() |
| 88 | .prefix("frt") |
| 89 | .tempdir_in("/tmp") |
| 90 | .expect("temp dir"); |
| 91 | let dir = root.path().to_path_buf(); |
| 92 | let ports = pick_ports(); |
| 93 | let config = write_config(&dir, ports); |
| 94 | |
| 95 | |
| 96 | |
| 97 | admin(&config, &["create-user", "tahinli", "--admin"]); |
| 98 | for name in ["bob", "carol", "dave", "eve"] { |
| 99 | admin(&config, &["create-user", name]); |
| 100 | } |
| 101 | { |
| 102 | let store = Store::open(&dir.join("forge.db")) |
| 103 | .await |
| 104 | .expect("open store"); |
| 105 | let bob = store.user_by_username("bob").await.unwrap().expect("bob"); |
| 106 | let dave = store.user_by_username("dave").await.unwrap().expect("dave"); |
| 107 | let eve = store.user_by_username("eve").await.unwrap().expect("eve"); |
| 108 | let priv_repo = store |
| 109 | .create_repo(&bob.id, "priv", "", Visibility::Private, "main") |
| 110 | .await |
| 111 | .expect("priv repo"); |
| 112 | let pub_repo = store |
| 113 | .create_repo(&bob.id, "pub", "", Visibility::Public, "main") |
| 114 | .await |
| 115 | .expect("pub repo"); |
| 116 | let acme = store.create_org("acme", &bob.id).await.expect("acme"); |
| 117 | store |
| 118 | .add_org_member(&acme.id, &dave.id, OrgMemberRole::Owner) |
| 119 | .await |
| 120 | .expect("dave owner"); |
| 121 | store |
| 122 | .add_org_member(&acme.id, &eve.id, OrgMemberRole::Member) |
| 123 | .await |
| 124 | .expect("eve member"); |
| 125 | for repo in [&priv_repo, &pub_repo] { |
| 126 | let path = forge_git::repo::repo_path(&dir, &repo.id).expect("repo path"); |
| 127 | fs::create_dir_all(path.parent().expect("parent")).expect("create parent"); |
| 128 | let out = Command::new("git") |
| 129 | .args(["init", "--bare", "--quiet", "--initial-branch=main"]) |
| 130 | .arg(&path) |
| 131 | .output() |
| 132 | .expect("run git init"); |
| 133 | assert!(out.status.success(), "git init: {}", describe(&out)); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | let (_server, ports) = launch(&dir, ports).await; |
| 138 | let http = Http::new(ports.http); |
| 139 | let bob = login(&http, "bob", PW).await; |
| 140 | let carol = login(&http, "carol", PW).await; |
| 141 | let dave = login(&http, "dave", PW).await; |
| 142 | let eve = login(&http, "eve", PW).await; |
| 143 | |
| 144 | |
| 145 | let offered = offer(&http, &bob, "bob", "priv", "carol").await; |
| 146 | assert_eq!(offered.status, 303, "offer: {}", offered.body); |
| 147 | assert_eq!( |
| 148 | offered.header(LOCATION).as_deref(), |
| 149 | Some("/bob/priv/settings?section=danger") |
| 150 | ); |
| 151 | let status = http |
| 152 | .get("/bob/priv/settings?section=danger", Some(&bob)) |
| 153 | .await; |
| 154 | assert!(status.body.contains("Pending transfer to carol")); |
| 155 | |
| 156 | assert_eq!(http.get("/bob/priv", Some(&carol)).await.status, 404); |
| 157 | let declined = act_from(&http, &carol, "/notifications", "/decline").await; |
| 158 | assert_eq!(declined.status, 303, "decline: {}", declined.body); |
| 159 | assert_eq!(declined.header(LOCATION).as_deref(), Some("/notifications")); |
| 160 | let after = http |
| 161 | .get("/bob/priv/settings?section=danger", Some(&bob)) |
| 162 | .await; |
| 163 | assert!(!after.body.contains("Pending transfer")); |
| 164 | assert_eq!(http.get("/bob/priv", Some(&bob)).await.status, 200); |
| 165 | |
| 166 | |
| 167 | assert_eq!(offer(&http, &bob, "bob", "priv", "carol").await.status, 303); |
| 168 | let cancelled = act_from(&http, &bob, "/bob/priv/settings?section=danger", "/cancel").await; |
| 169 | assert_eq!(cancelled.status, 303, "cancel: {}", cancelled.body); |
| 170 | assert_eq!( |
| 171 | cancelled.header(LOCATION).as_deref(), |
| 172 | Some("/bob/priv/settings?section=danger") |
| 173 | ); |
| 174 | let after = http |
| 175 | .get("/bob/priv/settings?section=danger", Some(&bob)) |
| 176 | .await; |
| 177 | assert!(!after.body.contains("Pending transfer")); |
| 178 | |
| 179 | |
| 180 | let to_org = offer(&http, &bob, "bob", "pub", "acme").await; |
| 181 | assert_eq!(to_org.status, 303, "offer to org: {}", to_org.body); |
| 182 | let eve_inbox = http.get("/notifications", Some(&eve)).await; |
| 183 | assert!( |
| 184 | !eve_inbox.body.contains("/accept"), |
| 185 | "member sees no org offer" |
| 186 | ); |
| 187 | let outsider = offer(&http, &carol, "bob", "pub", "carol").await; |
| 188 | assert_eq!(outsider.status, 403, "non-admin outsider cannot offer"); |
| 189 | let accepted = act_from(&http, &dave, "/notifications", "/accept").await; |
| 190 | assert_eq!(accepted.status, 303, "accept: {}", accepted.body); |
| 191 | assert_eq!(accepted.header(LOCATION).as_deref(), Some("/acme/pub")); |
| 192 | assert_eq!(http.get("/acme/pub", Some(&bob)).await.status, 200); |
| 193 | let old = http.get("/bob/pub", Some(&bob)).await; |
| 194 | assert_eq!(old.status, 307, "old owner and name redirect"); |
| 195 | } |