forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching·0 forks·Rust·AGPL-3.0·7.1 MiB·490f099·Tahinli·7 hours ago·322 commits
| 1 | //! Black-box tests for the organization pages: members, teams, settings, the creation policy and |
| 2 | //! the owner choice on the new repo form. They start the real `forge` binary, seed the org rows |
| 3 | //! through the store before the server starts, and then drive the pages over HTTP. Cookies are |
| 4 | //! handled by hand, as in `repo_collaborators_e2e.rs`. |
| 5 | |
| 6 | use crate::common::{ |
| 7 | Http, Resp, bin, describe, input_value, launch, launch_with, login, pick_ports, tool_on_path, |
| 8 | write_config, |
| 9 | }; |
| 10 | use std::fs; |
| 11 | use std::path::Path; |
| 12 | use std::process::{Command, Output}; |
| 13 | |
| 14 | use forge_core::store::orgs::{DefaultRepoRole, OrgMemberRole, OrgSettings}; |
| 15 | use forge_core::{Role, Store, Visibility}; |
| 16 | use reqwest::header::LOCATION; |
| 17 | |
| 18 | /// Each test starts its own server, so the tests run one at a time. |
| 19 | static SERIAL: std::sync::LazyLock<tokio::sync::Mutex<()>> = |
| 20 | std::sync::LazyLock::new(|| tokio::sync::Mutex::new(())); |
| 21 | |
| 22 | const PW: &str = "tahinli-password-123"; |
| 23 | |
| 24 | /// The value of the selected option of the `<select name="...">` on a page. |
| 25 | fn selected_value(body: &str, name: &str) -> Option<String> { |
| 26 | let doc = scraper::Html::parse_document(body); |
| 27 | let selector = scraper::Selector::parse(&format!("select[name=\"{name}\"] option[selected]")) |
| 28 | .expect("selector"); |
| 29 | doc.select(&selector) |
| 30 | .next() |
| 31 | .and_then(|el| el.value().attr("value")) |
| 32 | .map(str::to_owned) |
| 33 | } |
| 34 | |
| 35 | /// The values of every option of the owner select on the new repo form. |
| 36 | fn owner_options(body: &str) -> Vec<String> { |
| 37 | let doc = scraper::Html::parse_document(body); |
| 38 | let selector = scraper::Selector::parse("select[name=\"owner\"] option").expect("selector"); |
| 39 | doc.select(&selector) |
| 40 | .filter_map(|el| el.value().attr("value").map(str::to_owned)) |
| 41 | .collect() |
| 42 | } |
| 43 | |
| 44 | /// Runs an admin subcommand with the password in FORGE_PASSWORD and asserts it succeeded. |
| 45 | fn admin(config: &Path, args: &[&str]) -> Output { |
| 46 | let out = Command::new(bin()) |
| 47 | .arg("--config") |
| 48 | .arg(config) |
| 49 | .arg("admin") |
| 50 | .args(args) |
| 51 | .env("FORGE_PASSWORD", PW) |
| 52 | .output() |
| 53 | .expect("run admin command"); |
| 54 | assert!( |
| 55 | out.status.success(), |
| 56 | "admin {args:?} failed:\n{}", |
| 57 | describe(&out) |
| 58 | ); |
| 59 | out |
| 60 | } |
| 61 | |
| 62 | /// The session's CSRF token, read from a page every signed-in user can open. |
| 63 | async fn csrf_for(http: &Http, session: &str) -> String { |
| 64 | let page = http.get("/settings/keys", Some(session)).await; |
| 65 | assert_eq!(page.status, 200, "GET /settings/keys"); |
| 66 | input_value(&page.body, "csrf") |
| 67 | } |
| 68 | |
| 69 | /// Creates a bare git repo for `repo_id` at its data-dir location, as the create-repo page does. |
| 70 | fn init_bare(data_dir: &Path, repo_id: &str) { |
| 71 | let path = forge_git::repo::repo_path(data_dir, repo_id).expect("repo path"); |
| 72 | fs::create_dir_all(path.parent().expect("parent")).expect("create repo parent"); |
| 73 | let out = Command::new("git") |
| 74 | .args(["init", "--bare", "--quiet"]) |
| 75 | .arg(&path) |
| 76 | .output() |
| 77 | .expect("run git init"); |
| 78 | assert!(out.status.success(), "git init --bare: {}", describe(&out)); |
| 79 | } |
| 80 | |
| 81 | /// The redirect target of a write, which carries the notice in its query: the next GET of it shows the |
| 82 | /// notice. |
| 83 | fn notice_location(redirect: &Resp) -> String { |
| 84 | redirect.header(LOCATION).expect("notice redirect") |
| 85 | } |
| 86 | |
| 87 | /// A temp dir short enough for the unix socket path limit. |
| 88 | fn short_temp_dir() -> tempfile::TempDir { |
| 89 | tempfile::Builder::new() |
| 90 | .prefix("forg") |
| 91 | .tempdir_in("/tmp") |
| 92 | .expect("temp dir") |
| 93 | } |
| 94 | |
| 95 | #[tokio::test] |
| 96 | async fn org_members_teams_and_access_end_to_end() { |
| 97 | let _guard = SERIAL.lock().await; |
| 98 | if !tool_on_path("git") { |
| 99 | eprintln!("SKIP orgs_ui_e2e: `git` was not found on PATH"); |
| 100 | return; |
| 101 | } |
| 102 | let root = short_temp_dir(); |
| 103 | let dir = root.path().to_path_buf(); |
| 104 | let ports = pick_ports(); |
| 105 | let config = write_config(&dir, ports); |
| 106 | |
| 107 | admin(&config, &["create-user", "root", "--admin"]); |
| 108 | for name in ["tahinli", "bob", "carol", "dana"] { |
| 109 | admin(&config, &["create-user", name]); |
| 110 | } |
| 111 | |
| 112 | // Acme is tahinli's org. Bob is a member with no default access, and a team gives him read on |
| 113 | // acme's private repo. Initech is an empty org, used later for deletion. |
| 114 | let (tahinli_id, team_id) = { |
| 115 | let store = Store::open(&dir.join("forge.db")) |
| 116 | .await |
| 117 | .expect("open store"); |
| 118 | let tahinli = store |
| 119 | .user_by_username("tahinli") |
| 120 | .await |
| 121 | .unwrap() |
| 122 | .expect("tahinli"); |
| 123 | let bob = store.user_by_username("bob").await.unwrap().expect("bob"); |
| 124 | let acme = store.create_org("acme", &tahinli.id).await.expect("acme"); |
| 125 | store |
| 126 | .add_org_member(&acme.id, &bob.id, OrgMemberRole::Member) |
| 127 | .await |
| 128 | .expect("bob joins acme"); |
| 129 | store |
| 130 | .set_org_settings( |
| 131 | &acme.id, |
| 132 | OrgSettings { |
| 133 | default_repo_role: DefaultRepoRole::None, |
| 134 | members_can_create_repos: false, |
| 135 | }, |
| 136 | ) |
| 137 | .await |
| 138 | .expect("acme settings"); |
| 139 | let proj = store |
| 140 | .create_repo(&acme.id, "proj", "", Visibility::Private, "main") |
| 141 | .await |
| 142 | .expect("acme proj"); |
| 143 | init_bare(&dir, &proj.id); |
| 144 | let team = store |
| 145 | .create_team(&acme.id, "devs", "", Role::Read, false) |
| 146 | .await |
| 147 | .expect("devs team"); |
| 148 | store |
| 149 | .add_team_repo(&team.id, &proj.id) |
| 150 | .await |
| 151 | .expect("devs covers proj"); |
| 152 | store |
| 153 | .create_org("initech", &tahinli.id) |
| 154 | .await |
| 155 | .expect("initech"); |
| 156 | (tahinli.id, team.id) |
| 157 | }; |
| 158 | |
| 159 | let (_server, ports) = launch(&dir, ports).await; |
| 160 | let http = Http::new(ports.http); |
| 161 | let tahinli = login(&http, "tahinli", PW).await; |
| 162 | let bob = login(&http, "bob", PW).await; |
| 163 | let carol = login(&http, "carol", PW).await; |
| 164 | let dana = login(&http, "dana", PW).await; |
| 165 | let members = "/org/acme/members"; |
| 166 | |
| 167 | // Anonymous visitors go to login. Non-members get 404 on the org pages. Members see the list |
| 168 | // but no owner controls. |
| 169 | assert_eq!(http.get(members, None).await.status, 303, "anonymous"); |
| 170 | assert_eq!( |
| 171 | http.get(members, Some(&carol)).await.status, |
| 172 | 404, |
| 173 | "carol is not yet a member" |
| 174 | ); |
| 175 | let bob_view = http.get(members, Some(&bob)).await; |
| 176 | assert_eq!(bob_view.status, 200, "bob sees the members list"); |
| 177 | assert!( |
| 178 | !bob_view.body.contains("Add a member"), |
| 179 | "members get no add form" |
| 180 | ); |
| 181 | |
| 182 | // A member cannot add people. The owner adds carol, and the page shows the notice. |
| 183 | let bob_csrf = csrf_for(&http, &bob).await; |
| 184 | let res = http |
| 185 | .post_form( |
| 186 | "/org/acme/members/add", |
| 187 | &[ |
| 188 | ("csrf", &bob_csrf), |
| 189 | ("username", "carol"), |
| 190 | ("role", "member"), |
| 191 | ], |
| 192 | Some(&bob), |
| 193 | ) |
| 194 | .await; |
| 195 | assert_eq!(res.status, 403, "member add is refused: {}", res.body); |
| 196 | |
| 197 | let tahinli_csrf = csrf_for(&http, &tahinli).await; |
| 198 | let res = http |
| 199 | .post_form( |
| 200 | "/org/acme/members/add", |
| 201 | &[ |
| 202 | ("csrf", &tahinli_csrf), |
| 203 | ("username", "carol"), |
| 204 | ("role", "member"), |
| 205 | ], |
| 206 | Some(&tahinli), |
| 207 | ) |
| 208 | .await; |
| 209 | assert_eq!(res.status, 303, "owner adds carol: {}", res.body); |
| 210 | assert!( |
| 211 | notice_location(&res).starts_with(&format!("{members}?notice=member_added")), |
| 212 | "the redirect carries the notice" |
| 213 | ); |
| 214 | let shown = http.get(¬ice_location(&res), Some(&tahinli)).await; |
| 215 | assert!(shown.body.contains("Member added."), "notice after add"); |
| 216 | assert!(shown.body.contains("href=\"/carol\""), "carol listed"); |
| 217 | assert_eq!( |
| 218 | http.get(members, Some(&carol)).await.status, |
| 219 | 200, |
| 220 | "carol is a member now" |
| 221 | ); |
| 222 | |
| 223 | // The last owner cannot leave. The redirect carries a notice that says why. |
| 224 | let res = http |
| 225 | .post_form( |
| 226 | "/org/acme/members/remove", |
| 227 | &[("csrf", &tahinli_csrf), ("user_id", &tahinli_id)], |
| 228 | Some(&tahinli), |
| 229 | ) |
| 230 | .await; |
| 231 | assert_eq!(res.status, 303, "leave is answered with a redirect"); |
| 232 | let shown = http.get(¬ice_location(&res), Some(&tahinli)).await; |
| 233 | assert!( |
| 234 | shown |
| 235 | .body |
| 236 | .contains("An organization needs at least one owner."), |
| 237 | "last owner notice: {}", |
| 238 | shown.body |
| 239 | ); |
| 240 | |
| 241 | // Team coverage: bob cannot see acme's private repo until he joins the devs team. Dana is not |
| 242 | // in the org and never can see it through a team. |
| 243 | assert_eq!( |
| 244 | http.get("/acme/proj", Some(&bob)).await.status, |
| 245 | 404, |
| 246 | "bob before the team" |
| 247 | ); |
| 248 | assert_eq!( |
| 249 | http.get("/acme/proj", Some(&dana)).await.status, |
| 250 | 404, |
| 251 | "a non-member never sees the private org repo" |
| 252 | ); |
| 253 | let team_page = format!("/org/acme/teams/{team_id}"); |
| 254 | assert_eq!( |
| 255 | http.get(&team_page, Some(&bob)).await.status, |
| 256 | 200, |
| 257 | "members see teams" |
| 258 | ); |
| 259 | let res = http |
| 260 | .post_form( |
| 261 | &format!("{team_page}/members/add"), |
| 262 | &[("csrf", &tahinli_csrf), ("username", "bob")], |
| 263 | Some(&tahinli), |
| 264 | ) |
| 265 | .await; |
| 266 | assert_eq!(res.status, 303, "owner adds bob to devs"); |
| 267 | assert_eq!( |
| 268 | http.get("/acme/proj", Some(&bob)).await.status, |
| 269 | 200, |
| 270 | "bob reads the repo through devs" |
| 271 | ); |
| 272 | assert_eq!( |
| 273 | http.get("/acme/proj", Some(&carol)).await.status, |
| 274 | 404, |
| 275 | "carol is not in devs" |
| 276 | ); |
| 277 | |
| 278 | // Dana is not an org member, so the team refuses her. |
| 279 | let res = http |
| 280 | .post_form( |
| 281 | &format!("{team_page}/members/add"), |
| 282 | &[("csrf", &tahinli_csrf), ("username", "dana")], |
| 283 | Some(&tahinli), |
| 284 | ) |
| 285 | .await; |
| 286 | assert_eq!(res.status, 303, "refused add still redirects"); |
| 287 | let shown = http.get(¬ice_location(&res), Some(&tahinli)).await; |
| 288 | assert!( |
| 289 | shown |
| 290 | .body |
| 291 | .contains("Only members of the organization can join its teams."), |
| 292 | "non-member refused by the team" |
| 293 | ); |
| 294 | |
| 295 | // The new repo form offers the owner's own name and the orgs it may create in. Bob may not |
| 296 | // create in acme, so acme is not offered to him, and a forged owner is refused. |
| 297 | let tahinli_new = http.get("/new", Some(&tahinli)).await; |
| 298 | assert!( |
| 299 | owner_options(&tahinli_new.body).contains(&"acme".to_string()), |
| 300 | "owner choice lists acme" |
| 301 | ); |
| 302 | let bob_new = http.get("/new", Some(&bob)).await; |
| 303 | assert_eq!(owner_options(&bob_new.body), vec!["bob".to_string()]); |
| 304 | let res = http |
| 305 | .post_form( |
| 306 | "/new", |
| 307 | &[ |
| 308 | ("csrf", &bob_csrf), |
| 309 | ("name", "sneaky"), |
| 310 | ("visibility", "private"), |
| 311 | ("default_branch", "main"), |
| 312 | ("owner", "acme"), |
| 313 | ], |
| 314 | Some(&bob), |
| 315 | ) |
| 316 | .await; |
| 317 | assert_eq!(res.status, 200, "forged owner re-renders the form"); |
| 318 | assert!( |
| 319 | res.body |
| 320 | .contains("you cannot create repositories for that owner"), |
| 321 | "forged owner refused" |
| 322 | ); |
| 323 | } |
| 324 | |
| 325 | #[tokio::test] |
| 326 | async fn org_settings_delete_and_audit_end_to_end() { |
| 327 | let _guard = SERIAL.lock().await; |
| 328 | if !tool_on_path("git") { |
| 329 | eprintln!("SKIP orgs_ui_e2e: `git` was not found on PATH"); |
| 330 | return; |
| 331 | } |
| 332 | let root = short_temp_dir(); |
| 333 | let dir = root.path().to_path_buf(); |
| 334 | let ports = pick_ports(); |
| 335 | let config = write_config(&dir, ports); |
| 336 | |
| 337 | admin(&config, &["create-user", "root", "--admin"]); |
| 338 | for name in ["tahinli", "bob", "dana"] { |
| 339 | admin(&config, &["create-user", name]); |
| 340 | } |
| 341 | { |
| 342 | let store = Store::open(&dir.join("forge.db")) |
| 343 | .await |
| 344 | .expect("open store"); |
| 345 | let tahinli = store |
| 346 | .user_by_username("tahinli") |
| 347 | .await |
| 348 | .unwrap() |
| 349 | .expect("tahinli"); |
| 350 | let bob = store.user_by_username("bob").await.unwrap().expect("bob"); |
| 351 | let acme = store.create_org("acme", &tahinli.id).await.expect("acme"); |
| 352 | store |
| 353 | .add_org_member(&acme.id, &bob.id, OrgMemberRole::Member) |
| 354 | .await |
| 355 | .expect("bob joins acme"); |
| 356 | store |
| 357 | .create_repo(&acme.id, "proj", "", Visibility::Public, "main") |
| 358 | .await |
| 359 | .expect("acme proj"); |
| 360 | store |
| 361 | .create_org("initech", &tahinli.id) |
| 362 | .await |
| 363 | .expect("initech"); |
| 364 | } |
| 365 | |
| 366 | let (_server, ports) = launch(&dir, ports).await; |
| 367 | let http = Http::new(ports.http); |
| 368 | let tahinli = login(&http, "tahinli", PW).await; |
| 369 | let bob = login(&http, "bob", PW).await; |
| 370 | let dana = login(&http, "dana", PW).await; |
| 371 | let root_session = login(&http, "root", PW).await; |
| 372 | let settings = "/org/acme/settings"; |
| 373 | |
| 374 | // Owners only: members get 403, non-members 404, anonymous visitors go to login. |
| 375 | assert_eq!(http.get(settings, None).await.status, 303, "anonymous"); |
| 376 | assert_eq!(http.get(settings, Some(&bob)).await.status, 403, "member"); |
| 377 | assert_eq!( |
| 378 | http.get(settings, Some(&dana)).await.status, |
| 379 | 404, |
| 380 | "outsider" |
| 381 | ); |
| 382 | |
| 383 | // The owner saves the description and the default role. The page shows the new values. |
| 384 | let csrf = csrf_for(&http, &tahinli).await; |
| 385 | let res = http |
| 386 | .post_form( |
| 387 | settings, |
| 388 | &[ |
| 389 | ("csrf", &csrf), |
| 390 | ("display_name", "Acme Inc"), |
| 391 | ("description", "Anvils and more"), |
| 392 | ("default_repo_role", "write"), |
| 393 | ("members_can_create", "on"), |
| 394 | ], |
| 395 | Some(&tahinli), |
| 396 | ) |
| 397 | .await; |
| 398 | assert_eq!(res.status, 303, "save settings: {}", res.body); |
| 399 | let shown = http.get(¬ice_location(&res), Some(&tahinli)).await; |
| 400 | assert!(shown.body.contains("Settings saved."), "saved notice"); |
| 401 | assert!(shown.body.contains("Anvils and more"), "description saved"); |
| 402 | let profile = http.get("/acme", Some(&tahinli)).await; |
| 403 | assert!( |
| 404 | profile.body.contains("Acme Inc"), |
| 405 | "display name on the profile" |
| 406 | ); |
| 407 | assert_eq!( |
| 408 | selected_value(&shown.body, "default_repo_role").as_deref(), |
| 409 | Some("write"), |
| 410 | "default role saved" |
| 411 | ); |
| 412 | |
| 413 | // Deleting acme is refused while it owns a repo, and the name must be typed again. |
| 414 | let res = http |
| 415 | .post_form( |
| 416 | "/org/acme/settings/delete", |
| 417 | &[("csrf", &csrf), ("confirm", "acme")], |
| 418 | Some(&tahinli), |
| 419 | ) |
| 420 | .await; |
| 421 | assert_eq!(res.status, 303, "refused delete still redirects"); |
| 422 | let shown = http.get(¬ice_location(&res), Some(&tahinli)).await; |
| 423 | assert!( |
| 424 | shown.body.contains("Transfer or delete the organization"), |
| 425 | "repos block the delete" |
| 426 | ); |
| 427 | assert_eq!( |
| 428 | http.get("/acme", None).await.status, |
| 429 | 200, |
| 430 | "acme still exists" |
| 431 | ); |
| 432 | |
| 433 | // Initech has no repos, so tahinli can delete it once she types its name. |
| 434 | let initech_settings = "/org/initech/settings"; |
| 435 | let res = http |
| 436 | .post_form( |
| 437 | "/org/initech/settings/delete", |
| 438 | &[("csrf", &csrf), ("confirm", "wrong")], |
| 439 | Some(&tahinli), |
| 440 | ) |
| 441 | .await; |
| 442 | assert_eq!(res.status, 303, "wrong name still redirects"); |
| 443 | assert_eq!( |
| 444 | http.get(initech_settings, Some(&tahinli)).await.status, |
| 445 | 200, |
| 446 | "initech survives a wrong name" |
| 447 | ); |
| 448 | let res = http |
| 449 | .post_form( |
| 450 | "/org/initech/settings/delete", |
| 451 | &[("csrf", &csrf), ("confirm", "initech")], |
| 452 | Some(&tahinli), |
| 453 | ) |
| 454 | .await; |
| 455 | assert_eq!(res.status, 303, "delete initech"); |
| 456 | assert_eq!(res.header(LOCATION).as_deref(), Some("/")); |
| 457 | assert_eq!( |
| 458 | http.get("/initech", None).await.status, |
| 459 | 404, |
| 460 | "initech is gone" |
| 461 | ); |
| 462 | |
| 463 | // The admin audit log holds the org actions, by their stored names. |
| 464 | let audit = http |
| 465 | .get( |
| 466 | "/admin/audit?action=org_settings_updated", |
| 467 | Some(&root_session), |
| 468 | ) |
| 469 | .await; |
| 470 | assert_eq!(audit.status, 200, "audit page"); |
| 471 | assert!( |
| 472 | audit.body.contains("data-action=\"org_settings_updated\""), |
| 473 | "settings change is audited" |
| 474 | ); |
| 475 | let audit = http |
| 476 | .get("/admin/audit?action=org_deleted", Some(&root_session)) |
| 477 | .await; |
| 478 | assert!( |
| 479 | audit.body.contains("data-action=\"org_deleted\""), |
| 480 | "org deletion is audited" |
| 481 | ); |
| 482 | } |
| 483 | |
| 484 | #[tokio::test] |
| 485 | async fn org_creation_can_be_limited_to_admins() { |
| 486 | let _guard = SERIAL.lock().await; |
| 487 | let root = short_temp_dir(); |
| 488 | let dir = root.path().to_path_buf(); |
| 489 | let ports = pick_ports(); |
| 490 | let config = write_config(&dir, ports); |
| 491 | admin(&config, &["create-user", "root", "--admin"]); |
| 492 | admin(&config, &["create-user", "tahinli"]); |
| 493 | |
| 494 | let (_server, ports) = launch_with(&dir, ports, "[orgs]\ncreation = \"admin\"\n").await; |
| 495 | let http = Http::new(ports.http); |
| 496 | let tahinli = login(&http, "tahinli", PW).await; |
| 497 | let root_session = login(&http, "root", PW).await; |
| 498 | |
| 499 | assert!( |
| 500 | !http |
| 501 | .get("/tahinli?tab=organizations", Some(&tahinli)) |
| 502 | .await |
| 503 | .body |
| 504 | .contains("href=\"/org/new\""), |
| 505 | "a person who may not create orgs sees no New organization link" |
| 506 | ); |
| 507 | assert!( |
| 508 | http.get("/root?tab=organizations", Some(&root_session)) |
| 509 | .await |
| 510 | .body |
| 511 | .contains("href=\"/org/new\""), |
| 512 | "an admin sees the New organization link" |
| 513 | ); |
| 514 | |
| 515 | assert_eq!( |
| 516 | http.get("/org/new", Some(&tahinli)).await.status, |
| 517 | 403, |
| 518 | "a person may not create orgs under the admin policy" |
| 519 | ); |
| 520 | let csrf = csrf_for(&http, &tahinli).await; |
| 521 | let res = http |
| 522 | .post_form( |
| 523 | "/org/new", |
| 524 | &[("csrf", &csrf), ("name", "globex"), ("description", "")], |
| 525 | Some(&tahinli), |
| 526 | ) |
| 527 | .await; |
| 528 | assert_eq!(res.status, 403, "the POST is refused too"); |
| 529 | |
| 530 | assert_eq!( |
| 531 | http.get("/org/new", Some(&root_session)).await.status, |
| 532 | 200, |
| 533 | "an admin may create orgs" |
| 534 | ); |
| 535 | let root_csrf = csrf_for(&http, &root_session).await; |
| 536 | let res = http |
| 537 | .post_form( |
| 538 | "/org/new", |
| 539 | &[ |
| 540 | ("csrf", &root_csrf), |
| 541 | ("name", "globex"), |
| 542 | ("description", "Globals"), |
| 543 | ], |
| 544 | Some(&root_session), |
| 545 | ) |
| 546 | .await; |
| 547 | assert_eq!(res.status, 303, "admin creates globex: {}", res.body); |
| 548 | assert_eq!(res.header(LOCATION).as_deref(), Some("/globex")); |
| 549 | let profile = http.get("/globex", None).await; |
| 550 | assert_eq!(profile.status, 200, "the org has a public page"); |
| 551 | assert!( |
| 552 | profile.body.contains("Globals"), |
| 553 | "description on the profile" |
| 554 | ); |
| 555 | assert!(profile.body.contains("Organization"), "marked as an org"); |
| 556 | } |
| 557 | |
| 558 | /// Git on the command line, with no credential helper and no prompts. |
| 559 | fn git(cwd: &Path, args: &[&str]) -> Output { |
| 560 | Command::new("git") |
| 561 | .current_dir(cwd) |
| 562 | .args(["-c", "credential.helper="]) |
| 563 | .args(args) |
| 564 | .env("GIT_TERMINAL_PROMPT", "0") |
| 565 | .output() |
| 566 | .expect("run git") |
| 567 | } |
| 568 | |
| 569 | /// Commits one file with a fixed identity. |
| 570 | fn commit_file(repo: &Path, name: &str, content: &str) { |
| 571 | fs::write(repo.join(name), content).expect("write file"); |
| 572 | let out = git(repo, &["add", name]); |
| 573 | assert!(out.status.success(), "git add: {}", describe(&out)); |
| 574 | let out = git( |
| 575 | repo, |
| 576 | &[ |
| 577 | "-c", |
| 578 | "user.name=forge", |
| 579 | "-c", |
| 580 | "user.email=forge@example.test", |
| 581 | "-c", |
| 582 | "commit.gpgsign=false", |
| 583 | "commit", |
| 584 | "-q", |
| 585 | "-m", |
| 586 | "push over https", |
| 587 | ], |
| 588 | ); |
| 589 | assert!(out.status.success(), "git commit: {}", describe(&out)); |
| 590 | } |
| 591 | |
| 592 | /// A new access token for the session, read from the page that shows it once. |
| 593 | async fn create_token(http: &Http, session: &str) -> String { |
| 594 | let csrf = csrf_for(http, session).await; |
| 595 | let res = http |
| 596 | .post_form( |
| 597 | "/settings/tokens", |
| 598 | &[("csrf", &csrf), ("name", "e2e"), ("expires", "30")], |
| 599 | Some(session), |
| 600 | ) |
| 601 | .await; |
| 602 | assert_eq!(res.status, 200, "create token: {}", res.body); |
| 603 | let doc = scraper::Html::parse_document(&res.body); |
| 604 | let selector = scraper::Selector::parse("pre.code code").expect("selector"); |
| 605 | doc.select(&selector) |
| 606 | .next() |
| 607 | .expect("token shown after creation") |
| 608 | .text() |
| 609 | .collect::<String>() |
| 610 | .trim() |
| 611 | .to_owned() |
| 612 | } |
| 613 | |
| 614 | /// The value of the first option of the `<select name="...">` on a page. |
| 615 | fn first_option(body: &str, name: &str) -> String { |
| 616 | let doc = scraper::Html::parse_document(body); |
| 617 | let selector = |
| 618 | scraper::Selector::parse(&format!("select[name=\"{name}\"] option")).expect("selector"); |
| 619 | doc.select(&selector) |
| 620 | .next() |
| 621 | .and_then(|el| el.value().attr("value")) |
| 622 | .unwrap_or_else(|| panic!("no option in select {name}")) |
| 623 | .to_owned() |
| 624 | } |
| 625 | |
| 626 | #[tokio::test] |
| 627 | async fn org_member_pushes_over_https_through_a_write_team() { |
| 628 | let _guard = SERIAL.lock().await; |
| 629 | if !tool_on_path("git") { |
| 630 | eprintln!("SKIP orgs_ui_e2e: `git` was not found on PATH"); |
| 631 | return; |
| 632 | } |
| 633 | let root = short_temp_dir(); |
| 634 | let dir = root.path().to_path_buf(); |
| 635 | let ports = pick_ports(); |
| 636 | let config = write_config(&dir, ports); |
| 637 | |
| 638 | admin(&config, &["create-user", "root", "--admin"]); |
| 639 | for name in ["tahinli", "bob", "dana"] { |
| 640 | admin(&config, &["create-user", name]); |
| 641 | } |
| 642 | { |
| 643 | let store = Store::open(&dir.join("forge.db")) |
| 644 | .await |
| 645 | .expect("open store"); |
| 646 | let tahinli = store |
| 647 | .user_by_username("tahinli") |
| 648 | .await |
| 649 | .unwrap() |
| 650 | .expect("tahinli"); |
| 651 | let bob = store.user_by_username("bob").await.unwrap().expect("bob"); |
| 652 | let acme = store.create_org("acme", &tahinli.id).await.expect("acme"); |
| 653 | store |
| 654 | .add_org_member(&acme.id, &bob.id, OrgMemberRole::Member) |
| 655 | .await |
| 656 | .expect("bob joins acme"); |
| 657 | store |
| 658 | .set_org_settings( |
| 659 | &acme.id, |
| 660 | OrgSettings { |
| 661 | default_repo_role: DefaultRepoRole::None, |
| 662 | members_can_create_repos: false, |
| 663 | }, |
| 664 | ) |
| 665 | .await |
| 666 | .expect("acme settings"); |
| 667 | } |
| 668 | |
| 669 | let (_server, ports) = launch(&dir, ports).await; |
| 670 | let http = Http::new(ports.http); |
| 671 | let tahinli = login(&http, "tahinli", PW).await; |
| 672 | let bob = login(&http, "bob", PW).await; |
| 673 | let dana = login(&http, "dana", PW).await; |
| 674 | |
| 675 | // Tahinli creates a private repo under acme through the new repo form. |
| 676 | let csrf = csrf_for(&http, &tahinli).await; |
| 677 | let res = http |
| 678 | .post_form( |
| 679 | "/new", |
| 680 | &[ |
| 681 | ("csrf", &csrf), |
| 682 | ("name", "pushme"), |
| 683 | ("visibility", "private"), |
| 684 | ("default_branch", "main"), |
| 685 | ("owner", "acme"), |
| 686 | ], |
| 687 | Some(&tahinli), |
| 688 | ) |
| 689 | .await; |
| 690 | assert_eq!(res.status, 303, "create acme/pushme: {}", res.body); |
| 691 | assert_eq!(res.header(LOCATION).as_deref(), Some("/acme/pushme")); |
| 692 | |
| 693 | // A write team takes the repo, and bob joins the team. |
| 694 | let res = http |
| 695 | .post_form( |
| 696 | "/org/acme/teams/new", |
| 697 | &[ |
| 698 | ("csrf", &csrf), |
| 699 | ("name", "writers"), |
| 700 | ("role", "write"), |
| 701 | ("description", ""), |
| 702 | ], |
| 703 | Some(&tahinli), |
| 704 | ) |
| 705 | .await; |
| 706 | assert_eq!(res.status, 303, "create writers: {}", res.body); |
| 707 | // The redirect carries the notice in its query. The team's own path is what follows. |
| 708 | let location = res.header(LOCATION).expect("team location"); |
| 709 | let team_url = location.split('?').next().expect("team path").to_owned(); |
| 710 | let team = http.get(&team_url, Some(&tahinli)).await; |
| 711 | assert_eq!(team.status, 200, "team page"); |
| 712 | let repo_id = first_option(&team.body, "repo_id"); |
| 713 | let res = http |
| 714 | .post_form( |
| 715 | &format!("{team_url}/repos/add"), |
| 716 | &[("csrf", &csrf), ("repo_id", &repo_id)], |
| 717 | Some(&tahinli), |
| 718 | ) |
| 719 | .await; |
| 720 | assert_eq!(res.status, 303, "link pushme to writers"); |
| 721 | let res = http |
| 722 | .post_form( |
| 723 | &format!("{team_url}/members/add"), |
| 724 | &[("csrf", &csrf), ("username", "bob")], |
| 725 | Some(&tahinli), |
| 726 | ) |
| 727 | .await; |
| 728 | assert_eq!(res.status, 303, "bob joins writers"); |
| 729 | |
| 730 | // Bob pushes over HTTPS with his token. Dana is not in the org, so her clone is refused. |
| 731 | let bob_token = create_token(&http, &bob).await; |
| 732 | let dana_token = create_token(&http, &dana).await; |
| 733 | let bob_url = format!( |
| 734 | "http://bob:{bob_token}@127.0.0.1:{}/acme/pushme.git", |
| 735 | ports.http |
| 736 | ); |
| 737 | let dana_url = format!( |
| 738 | "http://dana:{dana_token}@127.0.0.1:{}/acme/pushme.git", |
| 739 | ports.http |
| 740 | ); |
| 741 | |
| 742 | let refused = git(&dir, &["clone", &dana_url, "dana-work"]); |
| 743 | assert!(!refused.status.success(), "dana cannot clone acme/pushme"); |
| 744 | |
| 745 | let work = dir.join("bob-work"); |
| 746 | let work_str = work.to_str().expect("utf8 path"); |
| 747 | let clone = git(&dir, &["clone", &bob_url, work_str]); |
| 748 | assert!(clone.status.success(), "bob clones: {}", describe(&clone)); |
| 749 | commit_file(&work, "hello.txt", "hello over https\n"); |
| 750 | let push = git(&work, &["push", "origin", "HEAD:main"]); |
| 751 | assert!(push.status.success(), "bob pushes: {}", describe(&push)); |
| 752 | } |