diff --git a/crates/forge-web/src/pages/authoring_tests.rs b/crates/forge-web/src/pages/authoring_tests.rs index bc2623e..ecb4040 100644 --- a/crates/forge-web/src/pages/authoring_tests.rs +++ b/crates/forge-web/src/pages/authoring_tests.rs @@ -6,7 +6,7 @@ use topcoat::router::{Body, to_bytes}; use crate::auth::SESSION_COOKIE; use crate::pages::authoring::clean_name; -use crate::pages::issues_list::test_util::{CSRF, OWNER, REPO, TOKEN, fixture, send}; +use crate::pages::issues_list::test_util::{CSRF, Fixture, OWNER, REPO, TOKEN, fixture, send}; const OID_A: &str = "1111111111111111111111111111111111111111"; const OID_B: &str = "2222222222222222222222222222222222222222"; @@ -107,9 +107,8 @@ async fn attachment_round_trip_and_visibility() { ); } -#[tokio::test] -async fn attachment_rejects_bad_csrf_and_types() { - let fx = fixture(Visibility::Public).await; +/// Uploads with a bad token or a bad type are refused, and a text file downloads. +async fn scenario_upload_refusals(fx: &Fixture) { let path = format!("{}/attachments", base()); assert_eq!( upload(&fx.router, &path, "wrong", "a.png", b"x").await.0, @@ -137,9 +136,9 @@ async fn attachment_rejects_bad_csrf_and_types() { assert_eq!(got.body, "hello"); } -#[tokio::test] -async fn suggest_lists_users_and_issues() { - let fx = fixture(Visibility::Public).await; +/// Mention suggestions list users and issues, and anonymous viewers get nothing. This creates +/// issue 1 and the user tanya, which the later scenarios rely on. +async fn scenario_suggestions(fx: &Fixture) { fx.store .create_issue(&fx.repo.id, &fx.owner.id, "Parser crash", "") .await @@ -184,9 +183,8 @@ async fn suggest_lists_users_and_issues() { assert_eq!(anon.status, 403); } -#[tokio::test] -async fn ticking_a_task_saves_it_with_history() { - let fx = fixture(Visibility::Public).await; +/// Ticking a task saves it with history, and bad, missing or anonymous requests change nothing. +async fn scenario_task_toggles(fx: &Fixture) { let issue = fx .store .create_issue(&fx.repo.id, &fx.owner.id, "Plan", "- [ ] one\n- [ ] two\n") @@ -257,9 +255,9 @@ async fn ticking_a_task_saves_it_with_history() { assert_eq!(c.body, "- [ ] done\n- [ ] todo"); } -#[tokio::test] -async fn requesting_a_review_notifies_and_a_review_clears_it() { - let fx = fixture(Visibility::Public).await; +/// A review request notifies the reviewer, and a review or a withdrawal clears it. This creates +/// bob, whom the global review-requests scenario reuses. +async fn scenario_review_requests(fx: &Fixture) { let bob = fx .store .create_user("bob", None, "hash", false) @@ -439,14 +437,10 @@ async fn requesting_a_review_notifies_and_a_review_clears_it() { ); } -#[tokio::test] -async fn global_review_requests_page_lists_waiting_pulls() { - let fx = fixture(Visibility::Public).await; - let bob = fx - .store - .create_user("bob", None, "hash", false) - .await - .unwrap(); +/// The global review-requests page lists the pulls waiting on a reviewer. Runs after the review +/// scenario, which counts bob's notifications. +async fn scenario_review_page(fx: &Fixture) { + let bob = fx.store.user_by_username("bob").await.unwrap().unwrap(); let pr = fx .store .create_pull( @@ -456,7 +450,7 @@ async fn global_review_requests_page_lists_waiting_pulls() { "", "main", &fx.repo.id, - "feature", + "bobs-change", OID_A, OID_B, ) @@ -475,3 +469,15 @@ async fn global_review_requests_page_lists_waiting_pulls() { ); assert!(page.body.contains("requested by bob")); } + +/// The public-repo flows on one fixture. Each scenario builds on the ones before it (issue +/// numbers, the notifications bob gets), so keep this order. +#[tokio::test] +async fn public_repo_authoring_flows() { + let fx = fixture(Visibility::Public).await; + scenario_upload_refusals(&fx).await; + scenario_suggestions(&fx).await; + scenario_task_toggles(&fx).await; + scenario_review_requests(&fx).await; + scenario_review_page(&fx).await; +} diff --git a/crates/forge-web/src/pages/blame.rs b/crates/forge-web/src/pages/blame.rs index de2fac2..34a7ca1 100644 --- a/crates/forge-web/src/pages/blame.rs +++ b/crates/forge-web/src/pages/blame.rs @@ -252,9 +252,8 @@ mod tests { use crate::pages::commits::test_support::{Fixture, OWNER, REPO, get}; - #[tokio::test] - async fn blame_groups_lines_by_commit() { - let fx = Fixture::new(Visibility::Public).await; + /// Blame groups each line under the commit that wrote it. + async fn scenario_blame_lines(fx: &Fixture) { let (status, _, body) = get(&fx.router, &format!("/{OWNER}/{REPO}/blame/main/a.txt")).await; assert_eq!(status, 200); let html = String::from_utf8(body).unwrap(); @@ -268,13 +267,20 @@ mod tests { assert!(html.contains("id=\"L4\"")); } - #[tokio::test] - async fn blame_of_a_missing_file_is_404() { - let fx = Fixture::new(Visibility::Public).await; + /// A file that is not in the tree is 404. + async fn scenario_missing_file(fx: &Fixture) { let (status, _, _) = get(&fx.router, &format!("/{OWNER}/{REPO}/blame/main/nope.txt")).await; assert_eq!(status, 404); } + /// Both public blame scenarios on one fixture. + #[tokio::test] + async fn public_repo_blame() { + let fx = Fixture::new(Visibility::Public).await; + scenario_blame_lines(&fx).await; + scenario_missing_file(&fx).await; + } + #[tokio::test] async fn private_repo_blame_is_404_for_anonymous() { let fx = Fixture::new(Visibility::Private).await; diff --git a/crates/forge-web/src/pages/browse_tests.rs b/crates/forge-web/src/pages/browse_tests.rs index a67cc74..e54936e 100644 --- a/crates/forge-web/src/pages/browse_tests.rs +++ b/crates/forge-web/src/pages/browse_tests.rs @@ -71,9 +71,8 @@ fn whitespace_commit(fx: &Fixture) -> String { oid } -#[tokio::test] -async fn commit_page_has_toolbar_and_ignores_whitespace() { - let fx = Fixture::new(Visibility::Public).await; +/// The commit toolbar, and the whitespace toggle that hides whitespace-only files. +async fn scenario_whitespace_toolbar(fx: &Fixture) { let oid = whitespace_commit(&fx); let base = format!("/{OWNER}/{REPO}/commit/{oid}"); @@ -99,9 +98,8 @@ async fn commit_page_has_toolbar_and_ignores_whitespace() { assert!(main.contains("aria-pressed=\"true\"")); } -#[tokio::test] -async fn split_view_renders_two_sides_and_is_remembered() { - let fx = Fixture::new(Visibility::Public).await; +/// The split view renders two sides and is remembered in a cookie. +async fn scenario_split_view(fx: &Fixture) { let uri = format!("/{OWNER}/{REPO}/commit/{}?view=split", fx.modified); let (status, headers, body) = get(&fx.router, &uri).await; @@ -138,9 +136,8 @@ async fn split_view_renders_two_sides_and_is_remembered() { assert!(!String::from_utf8(body).unwrap().contains("diff-split-row")); } -#[tokio::test] -async fn patch_and_diff_downloads() { - let fx = Fixture::new(Visibility::Public).await; +/// Patch and diff downloads for a commit and a root commit, and 404s for bad or private ones. +async fn scenario_patch_and_diff(fx: &Fixture) { let base = format!("/{OWNER}/{REPO}/commit/{}", fx.modified); let (status, headers, body) = get(&fx.router, &format!("{base}.patch")).await; @@ -162,7 +159,7 @@ async fn patch_and_diff_downloads() { assert!(text.starts_with("diff --git a/a.txt b/a.txt")); assert!(!text.contains("Subject:")); - // Root commits work, and a private repo hides both downloads. + // Root commits work. let (status, _, _) = get( &fx.router, &format!("/{OWNER}/{REPO}/commit/{}.patch", fx.root), @@ -171,18 +168,10 @@ async fn patch_and_diff_downloads() { assert_eq!(status, 200); let (status, _, _) = get(&fx.router, &format!("/{OWNER}/{REPO}/commit/zzzz.patch")).await; assert_eq!(status, 404); - let private = Fixture::new(Visibility::Private).await; - let (status, _, _) = get( - &private.router, - &format!("/{OWNER}/{REPO}/commit/{}.patch", private.root), - ) - .await; - assert_eq!(status, 404); } -#[tokio::test] -async fn compare_patch_lists_commits_of_the_head_side() { - let fx = Fixture::new(Visibility::Public).await; +/// Compare lists the commits on the head side, as a patch and as a diff. +async fn scenario_compare(fx: &Fixture) { git(&fx.work, &["checkout", "-q", "-b", "topic", &fx.root]); std::fs::write(fx.work.join("topic.txt"), "topic\n").unwrap(); git(&fx.work, &["add", "-A"]); @@ -215,9 +204,8 @@ async fn compare_patch_lists_commits_of_the_head_side() { assert!(text.contains("+++ b/topic.txt")); } -#[tokio::test] -async fn blob_page_offers_blame_permalink_and_finder() { - let fx = Fixture::new(Visibility::Public).await; +/// The blob page offers blame, a commit-pinned permalink and the finder. +async fn scenario_blob_page(fx: &Fixture) { let (status, _, body) = get(&fx.router, &format!("/{OWNER}/{REPO}/blob/main/a.txt")).await; assert_eq!(status, 200); let html = String::from_utf8(body).unwrap(); @@ -243,9 +231,8 @@ async fn blob_page_offers_blame_permalink_and_finder() { assert!(String::from_utf8(body).unwrap().contains("data-finder")); } -#[tokio::test] -async fn paths_endpoint_lists_every_file() { - let fx = Fixture::new(Visibility::Public).await; +/// The paths endpoint lists every file on the ref, and refuses unknown refs and subpaths. +async fn scenario_paths(fx: &Fixture) { let (status, headers, body) = get(&fx.router, &format!("/{OWNER}/{REPO}/paths/main")).await; assert_eq!(status, 200); assert!( @@ -269,14 +256,10 @@ async fn paths_endpoint_lists_every_file() { assert_eq!(status, 404); let (status, _, _) = get(&fx.router, &format!("/{OWNER}/{REPO}/paths/main/dir")).await; assert_eq!(status, 404); - let private = Fixture::new(Visibility::Private).await; - let (status, _, _) = get(&private.router, &format!("/{OWNER}/{REPO}/paths/main")).await; - assert_eq!(status, 404); } -#[tokio::test] -async fn branches_page_shows_counts_and_creates_and_deletes() { - let fx = Fixture::new(Visibility::Public).await; +/// The branches page shows counts, creates and deletes branches, and refuses bad input. +async fn scenario_branches(fx: &Fixture, cookie: &str) { git(&fx.work, &["checkout", "-q", "-b", "feature", &fx.root]); std::fs::write(fx.work.join("f.txt"), "f\n").unwrap(); git(&fx.work, &["add", "-A"]); @@ -290,7 +273,6 @@ async fn branches_page_shows_counts_and_creates_and_deletes() { "HEAD:refs/heads/feature", ], ); - let cookie = sign_in_owner(&fx).await; let page = format!("/{OWNER}/{REPO}/branches"); let (status, _, html) = send(&fx.router, "GET", &page, Some(&cookie), &[]).await; @@ -399,9 +381,8 @@ async fn branches_page_shows_counts_and_creates_and_deletes() { git(&fx.bare, &["rev-parse", "--verify", "refs/heads/main"]); } -#[tokio::test] -async fn protection_rules_block_branch_deletion() { - let fx = Fixture::new(Visibility::Public).await; +/// A branch rule blocks deleting the branches it matches. +async fn scenario_protection(fx: &Fixture, cookie: &str) { git(&fx.bare, &["branch", "release/1", "main"]); git(&fx.bare, &["branch", "scratch", "main"]); let repo = fx @@ -414,7 +395,6 @@ async fn protection_rules_block_branch_deletion() { .upsert_branch_rule(&repo.id, "release/*", RuleFlags::basic(false, false)) .await .unwrap(); - let cookie = sign_in_owner(&fx).await; let delete = format!("/{OWNER}/{REPO}/branches/delete"); let tip = git(&fx.bare, &["rev-parse", "refs/heads/main"]); @@ -439,11 +419,9 @@ async fn protection_rules_block_branch_deletion() { assert!(git(&fx.bare, &["branch", "--list", "scratch"]).is_empty()); } -#[tokio::test] -async fn default_branch_setting_updates_head_and_the_record() { - let fx = Fixture::new(Visibility::Public).await; +/// Changing the default branch moves HEAD and the record; a missing branch changes nothing. +async fn scenario_default_branch(fx: &Fixture, cookie: &str) { git(&fx.bare, &["branch", "trunk", "main"]); - let cookie = sign_in_owner(&fx).await; let settings = format!("/{OWNER}/{REPO}/settings/general"); let (status, _, _) = send( @@ -502,3 +480,48 @@ async fn default_branch_setting_updates_head_and_the_record() { .await; assert!(html.contains("name=\"default_branch\"")); } + +/// Paths, patch and diff, split view and blob page, on one public repo. These pin the file set +/// and the commit count, so they run before any commit is added. +#[tokio::test] +async fn public_repo_pages_and_downloads() { + let fx = Fixture::new(Visibility::Public).await; + scenario_paths(&fx).await; + scenario_patch_and_diff(&fx).await; + scenario_split_view(&fx).await; + scenario_blob_page(&fx).await; +} + +/// The commit toolbar, then compare. The whitespace scenario adds commits to main, and compare +/// moves the working checkout to a topic branch, so compare runs last. +#[tokio::test] +async fn public_repo_commit_views_and_compare() { + let fx = Fixture::new(Visibility::Public).await; + scenario_whitespace_toolbar(&fx).await; + scenario_compare(&fx).await; +} + +/// Branches, branch rules and the default branch on one fixture. The branch counts assume main +/// has not moved, and the default-branch change runs last. +#[tokio::test] +async fn public_repo_branches_rules_and_default_branch() { + let fx = Fixture::new(Visibility::Public).await; + let cookie = sign_in_owner(&fx).await; + scenario_branches(&fx, &cookie).await; + scenario_protection(&fx, &cookie).await; + scenario_default_branch(&fx, &cookie).await; +} + +/// A private repo hides its patch download and its file list from anonymous viewers. +#[tokio::test] +async fn private_repo_hides_downloads_and_paths() { + let private = Fixture::new(Visibility::Private).await; + let (status, _, _) = get( + &private.router, + &format!("/{OWNER}/{REPO}/commit/{}.patch", private.root), + ) + .await; + assert_eq!(status, 404); + let (status, _, _) = get(&private.router, &format!("/{OWNER}/{REPO}/paths/main")).await; + assert_eq!(status, 404); +} diff --git a/crates/forge-web/src/pages/commit.rs b/crates/forge-web/src/pages/commit.rs index 9d63b7f..569f1f6 100644 --- a/crates/forge-web/src/pages/commit.rs +++ b/crates/forge-web/src/pages/commit.rs @@ -228,7 +228,10 @@ mod tests { use scraper::{Html, Selector}; use super::is_hex_prefix; + use crate::auth::SESSION_COOKIE; use crate::pages::commits::test_support::{Fixture, OWNER, REPO, get}; + use topcoat::router::request::Request; + use topcoat::router::{Body, to_bytes}; fn doc(body: &[u8]) -> Html { Html::parse_document(std::str::from_utf8(body).unwrap()) @@ -256,9 +259,8 @@ mod tests { assert!(!is_hex_prefix("zzzz")); } - #[tokio::test] - async fn modified_and_added_files_have_line_numbers() { - let fx = Fixture::new(Visibility::Public).await; + /// The modified and added files show line numbers, with their body and file headers. + async fn scenario_modified_and_added(fx: &Fixture) { let (status, _, body) = get( &fx.router, &format!("/{OWNER}/{REPO}/commit/{}", fx.modified), @@ -302,9 +304,8 @@ mod tests { assert!(!text.contains("sig-badge"), "unsigned commit has no badge"); } - #[tokio::test] - async fn root_commit_page_renders() { - let fx = Fixture::new(Visibility::Public).await; + /// The root commit has no parent, and its one added line is shown. + async fn scenario_root_commit(fx: &Fixture) { let (status, _, body) = get(&fx.router, &format!("/{OWNER}/{REPO}/commit/{}", fx.root)).await; assert_eq!(status, 200); @@ -317,14 +318,8 @@ mod tests { ); } - #[tokio::test] - async fn commit_title_uses_viewer_timezone() { - use topcoat::router::request::Request; - use topcoat::router::{Body, to_bytes}; - - use crate::auth::SESSION_COOKIE; - - let fx = Fixture::new(Visibility::Public).await; + /// Titles follow the viewer's timezone; anonymous viewers see UTC. + async fn scenario_title_timezone(fx: &Fixture) { let tokyo = fx .store .create_user("tokyo", None, "not-a-real-hash", false) @@ -378,9 +373,8 @@ mod tests { assert!(!anon.contains("+09\""), "anonymous view is not Tokyo time"); } - #[tokio::test] - async fn bad_oid_is_404() { - let fx = Fixture::new(Visibility::Public).await; + /// Bad and unknown oids are 404. + async fn scenario_bad_oid(fx: &Fixture) { let (status, _, _) = get(&fx.router, &format!("/{OWNER}/{REPO}/commit/zzzz")).await; assert_eq!(status, 404); let (status, _, _) = get(&fx.router, &format!("/{OWNER}/{REPO}/commit/abc")).await; @@ -390,6 +384,16 @@ mod tests { assert_eq!(status, 404); } + /// Every page of one public commit, on one fixture. The scenarios only read, so order is free. + #[tokio::test] + async fn public_commit_pages() { + let fx = Fixture::new(Visibility::Public).await; + scenario_modified_and_added(&fx).await; + scenario_root_commit(&fx).await; + scenario_title_timezone(&fx).await; + scenario_bad_oid(&fx).await; + } + #[tokio::test] async fn private_repo_is_404_for_anonymous() { let fx = Fixture::new(Visibility::Private).await; diff --git a/crates/forge-web/src/pages/commits.rs b/crates/forge-web/src/pages/commits.rs index 41299df..13e1444 100644 --- a/crates/forge-web/src/pages/commits.rs +++ b/crates/forge-web/src/pages/commits.rs @@ -512,13 +512,9 @@ mod tests { use crate::views::relative_time; #[test] - fn formats_dates() { + fn dates_and_relative_units_format() { assert_eq!(iso_time(0), "1970-01-01T00:00:00Z"); assert_eq!(iso_time(1_700_000_000), "2023-11-14T22:13:20Z"); - } - - #[test] - fn relative_units() { assert_eq!(relative_time(Lang::En, 990, 1000), "just now"); assert_eq!( relative_time(Lang::En, 10_000 - 3 * 86_400, 10_000), @@ -531,9 +527,8 @@ mod tests { assert_eq!(url_path("a b/c#d"), "a%20b/c%23d"); } - #[tokio::test] - async fn commits_list_and_pagination() { - let fx = Fixture::new(Visibility::Public).await; + /// Pages through history (50, then 5), and refuses bad pages and unknown refs. + async fn scenario_pagination(fx: &Fixture) { let base = format!("/{OWNER}/{REPO}/commits/main"); let (status, _, body) = get(&fx.router, &base).await; @@ -565,9 +560,8 @@ mod tests { assert_eq!(status, 404); } - #[tokio::test] - async fn path_filter_limits_history() { - let fx = Fixture::new(Visibility::Public).await; + /// The path filter keeps only the commits that touch the file. + async fn scenario_path_filter(fx: &Fixture) { let (status, _, body) = get(&fx.router, &format!("/{OWNER}/{REPO}/commits/main/a.txt")).await; assert_eq!(status, 200); @@ -579,11 +573,10 @@ mod tests { assert!(html.contains("Clear filter")); } - #[tokio::test] - async fn commit_list_shows_status_icon_and_contexts() { + /// Commit status icons and contexts show on the list. + async fn scenario_status_icons(fx: &Fixture) { use forge_core::store::commit_status::StatusState; - let fx = Fixture::new(Visibility::Public).await; let repo = fx .store .repo_by_owner_and_name(OWNER, REPO) @@ -636,9 +629,8 @@ mod tests { assert!(html.contains("rel=\"nofollow noopener noreferrer\"")); } - #[tokio::test] - async fn gitmoji_shortcodes_show_as_emoji() { - let fx = Fixture::new(Visibility::Public).await; + /// Gitmoji shortcodes show as emoji. This pushes one more commit, so it runs last. + async fn scenario_gitmoji(fx: &Fixture) { git( &fx.work, &[ @@ -676,6 +668,17 @@ mod tests { assert!(html.contains("Body with 🎉 and 12:30:45"), "{html}"); } + /// Every list scenario on one public repo. Order matters: the gitmoji scenario adds a commit on + /// top, which would shift the pagination counts. + #[tokio::test] + async fn public_repo_commit_lists() { + let fx = Fixture::new(Visibility::Public).await; + scenario_pagination(&fx).await; + scenario_path_filter(&fx).await; + scenario_status_icons(&fx).await; + scenario_gitmoji(&fx).await; + } + #[tokio::test] async fn private_repo_is_404_for_anonymous() { let fx = Fixture::new(Visibility::Private).await; diff --git a/crates/forge-web/src/pages/forgot.rs b/crates/forge-web/src/pages/forgot.rs index 7774c75..b8adde9 100644 --- a/crates/forge-web/src/pages/forgot.rs +++ b/crates/forge-web/src/pages/forgot.rs @@ -260,6 +260,17 @@ mod tests { (crate::test_support::app_router(state.clone()), state) } + /// The three account flows share one app. The unknown-address scenario reads the whole outbox + /// for reset mail, so it runs before the reset flow queues any. + #[tokio::test] + async fn forgot_and_reset_flows() { + let tmp = tempfile::tempdir().expect("tempdir"); + let (router, state) = app(tmp.path()).await; + scenario_same_answer(&router, &state).await; + scenario_reset_once(&router, &state).await; + scenario_weak_password(&router, &state).await; + } + /// Waits until a mail with `marker` in its body is in the outbox, and returns that body. async fn wait_for_mail(state: &AppState, marker: &str) -> String { for _ in 0..100 { @@ -272,10 +283,8 @@ mod tests { panic!("no mail with {marker}"); } - #[tokio::test] - async fn forgot_answers_the_same_for_unknown_and_unverified_addresses() { - let tmp = tempfile::tempdir().expect("tempdir"); - let (router, state) = app(tmp.path()).await; + /// Unknown and unverified addresses get the same answer, and no reset mail is queued. + async fn scenario_same_answer(router: &topcoat::router::Router, state: &AppState) { state .store .create_user("frank", Some("frank@example.com"), "hash", false) @@ -296,10 +305,8 @@ mod tests { assert!(mail.iter().all(|m| !m.body.contains("/reset/"))); } - #[tokio::test] - async fn reset_link_works_once_and_sets_the_password() { - let tmp = tempfile::tempdir().expect("tempdir"); - let (router, state) = app(tmp.path()).await; + /// A reset link sets the password once; the spent link is refused. + async fn scenario_reset_once(router: &topcoat::router::Router, state: &AppState) { let user = state .store .create_user("gina", Some("gina@example.com"), "old-hash", false) @@ -353,10 +360,8 @@ mod tests { assert!(html.contains("This reset link is invalid")); } - #[tokio::test] - async fn a_weak_password_keeps_the_link_live() { - let tmp = tempfile::tempdir().expect("tempdir"); - let (router, state) = app(tmp.path()).await; + /// A weak password is refused and the link stays live. + async fn scenario_weak_password(router: &topcoat::router::Router, state: &AppState) { let user = state .store .create_user("hal", Some("hal@example.com"), "old-hash", false) diff --git a/crates/forge-web/src/pages/fork.rs b/crates/forge-web/src/pages/fork.rs index 92eb230..bfdb113 100644 --- a/crates/forge-web/src/pages/fork.rs +++ b/crates/forge-web/src/pages/fork.rs @@ -286,9 +286,99 @@ mod tests { .await } - #[tokio::test] - async fn fork_copies_source_labels_and_adds_no_defaults() { - let (site, [_, bob, _]) = site().await; + /// Refusals and the repo header, before anyone forks. A bad or missing token starts nothing, + /// anonymous viewers go to login, a private repo is 404 to outsiders, the owner's own fork is + /// a no-op, and the header offers the fork button only to a signed-in non-owner. + async fn scenario_refusals(site: &Site, tahinli: &str, bob: &str, carol: &str) { + let reply = send( + site, + "POST", + "/tahinli/demo/fork", + Some(bob), + Some(String::new()), + ) + .await; + assert_eq!(reply.status, 403); + let reply = send( + site, + "POST", + "/tahinli/demo/fork", + Some(bob), + Some("csrf=wrong".into()), + ) + .await; + assert_eq!(reply.status, 403); + assert!( + site.store + .repo_by_owner_and_name("bob", "demo") + .await + .expect("q") + .is_none(), + "nothing was created" + ); + + let reply = fork(site, None).await; + assert_eq!(reply.status, 303); + assert!( + reply + .location + .as_deref() + .is_some_and(|l| l.starts_with("/login")), + "{:?}", + reply.location + ); + + let reply = send( + site, + "POST", + "/tahinli/secret/fork", + Some(carol), + Some(format!("csrf={CSRF}")), + ) + .await; + assert_eq!(reply.status, 404); + let reply = send( + site, + "POST", + "/tahinli/secret/fork", + None, + Some(format!("csrf={CSRF}")), + ) + .await; + assert_eq!(reply.status, 404, "anonymous viewers also get 404"); + + let reply = fork(site, Some(tahinli)).await; + assert_eq!(reply.status, 303); + assert_eq!(reply.location.as_deref(), Some("/tahinli/demo")); + assert!( + site.store + .repo_by_owner_and_name("tahinli", "demo-1") + .await + .expect("q") + .is_none() + ); + + let reply = send(site, "GET", "/tahinli/demo", Some(carol), None).await; + assert_eq!(reply.status, 200); + assert!( + reply.body.contains("href=\"/tahinli/demo/pulls\""), + "pulls tab" + ); + assert!(reply.body.contains("Pull requests")); + assert!( + reply.body.contains("action=\"/tahinli/demo/fork\""), + "fork button" + ); + let reply = send(site, "GET", "/tahinli/demo", None, None).await; + assert!( + !reply.body.contains("action=\"/tahinli/demo/fork\""), + "no fork button when anonymous" + ); + } + + /// Bob's fork copies the source's labels and branches and nothing else, records one audit + /// entry, and shows its source on the page. + async fn scenario_fork_copies(site: &Site, bob: &str) { let demo = site .store .repo_by_owner_and_name("tahinli", "demo") @@ -299,14 +389,26 @@ mod tests { .create_label(&demo.id, "triage-me", "#123456", "only here") .await .expect("label"); - let reply = fork(&site, Some(&bob)).await; + let reply = fork(site, Some(bob)).await; assert_eq!(reply.status, 303); + assert_eq!(reply.location.as_deref(), Some("/bob/demo")); + + let bob_id = site + .store + .user_by_username("bob") + .await + .expect("q") + .expect("bob") + .id; let fork = site .store .repo_by_owner_and_name("bob", "demo") .await - .expect("query") - .expect("fork"); + .expect("q") + .expect("fork row"); + assert_eq!(fork.fork_of.as_deref(), Some(demo.id.as_str())); + assert_eq!(fork.visibility, Visibility::Public); + assert_eq!(fork.owner_id, bob_id); let labels: Vec<(String, String, String)> = site .store .list_labels(&fork.id) @@ -323,37 +425,6 @@ mod tests { "only here".to_string() )] ); - } - - #[tokio::test] - async fn fork_copies_branches_and_shows_the_source() { - let (site, [_, bob, _]) = site().await; - let reply = fork(&site, Some(&bob)).await; - assert_eq!(reply.status, 303); - assert_eq!(reply.location.as_deref(), Some("/bob/demo")); - - let bob_id = site - .store - .user_by_username("bob") - .await - .expect("q") - .expect("bob") - .id; - let source = site - .store - .repo_by_owner_and_name("tahinli", "demo") - .await - .expect("q") - .expect("source"); - let fork = site - .store - .repo_by_owner_and_name("bob", "demo") - .await - .expect("q") - .expect("fork row"); - assert_eq!(fork.fork_of.as_deref(), Some(source.id.as_str())); - assert_eq!(fork.visibility, Visibility::Public); - assert_eq!(fork.owner_id, bob_id); let forked = site .store @@ -387,7 +458,7 @@ mod tests { vec!["refs/heads/main", "refs/tags/v1"] ); - let page = send(&site, "GET", "/bob/demo", Some(&bob), None).await; + let page = send(site, "GET", "/bob/demo", Some(bob), None).await; assert_eq!(page.status, 200); assert!(page.body.contains("forked from"), "fork line shown"); assert!( @@ -396,11 +467,10 @@ mod tests { ); } - #[tokio::test] - async fn forking_twice_redirects_to_the_existing_fork() { - let (site, [_, bob, _]) = site().await; - assert_eq!(fork(&site, Some(&bob)).await.status, 303); - let again = fork(&site, Some(&bob)).await; + /// Forking again goes to the existing fork and makes no `demo-1`. + async fn scenario_fork_again(site: &Site, bob: &str) { + assert_eq!(fork(site, Some(bob)).await.status, 303); + let again = fork(site, Some(bob)).await; assert_eq!(again.status, 303); assert_eq!(again.location.as_deref(), Some("/bob/demo")); assert!( @@ -413,125 +483,30 @@ mod tests { ); } - #[tokio::test] - async fn fork_name_gets_a_suffix_when_taken() { - let (site, [_, bob, _]) = site().await; - let bob_user = site + /// Erin already has a `demo`, so her fork of the source takes the `-1` suffix. + async fn scenario_suffix(site: &Site) { + let erin = site .store - .user_by_username("bob") + .create_user("erin", None, "x", false) .await - .expect("q") - .expect("bob"); + .expect("erin"); + let erin_cookie = sign_in(&site.store, &erin.id, "erin-token").await; site.store - .create_repo(&bob_user.id, "demo", "", Visibility::Private, "main") + .create_repo(&erin.id, "demo", "", Visibility::Private, "main") .await - .expect("bob's own demo"); - let reply = fork(&site, Some(&bob)).await; - assert_eq!(reply.location.as_deref(), Some("/bob/demo-1")); - } - - #[tokio::test] - async fn owner_forking_own_repo_is_a_no_op() { - let (site, [tahinli, _, _]) = site().await; - let reply = fork(&site, Some(&tahinli)).await; - assert_eq!(reply.status, 303); - assert_eq!(reply.location.as_deref(), Some("/tahinli/demo")); - assert!( - site.store - .repo_by_owner_and_name("tahinli", "demo-1") - .await - .expect("q") - .is_none() - ); - } - - #[tokio::test] - async fn hidden_repo_is_404_for_a_non_collaborator() { - let (site, [_, _, carol]) = site().await; - let reply = send( - &site, - "POST", - "/tahinli/secret/fork", - Some(&carol), - Some(format!("csrf={CSRF}")), - ) - .await; - assert_eq!(reply.status, 404); - let reply = send( - &site, - "POST", - "/tahinli/secret/fork", - None, - Some(format!("csrf={CSRF}")), - ) - .await; - assert_eq!(reply.status, 404, "anonymous viewers also get 404"); + .expect("erin's own demo"); + let reply = fork(site, Some(&erin_cookie)).await; + assert_eq!(reply.location.as_deref(), Some("/erin/demo-1")); } + /// Every fork scenario on one site. The refusals run first, since they assert that bob has no + /// fork yet, and the fork scenarios then run in order. #[tokio::test] - async fn anonymous_viewer_is_sent_to_login() { - let (site, _) = site().await; - let reply = fork(&site, None).await; - assert_eq!(reply.status, 303); - assert!( - reply - .location - .as_deref() - .is_some_and(|l| l.starts_with("/login")), - "{:?}", - reply.location - ); - } - - #[tokio::test] - async fn missing_or_wrong_csrf_is_rejected() { - let (site, [_, bob, _]) = site().await; - let reply = send( - &site, - "POST", - "/tahinli/demo/fork", - Some(&bob), - Some(String::new()), - ) - .await; - assert_eq!(reply.status, 403); - let reply = send( - &site, - "POST", - "/tahinli/demo/fork", - Some(&bob), - Some("csrf=wrong".into()), - ) - .await; - assert_eq!(reply.status, 403); - assert!( - site.store - .repo_by_owner_and_name("bob", "demo") - .await - .expect("q") - .is_none(), - "nothing was created" - ); - } - - #[tokio::test] - async fn repo_header_shows_pull_requests_tab_and_fork_button() { - let (site, [_, bob, _]) = site().await; - let reply = send(&site, "GET", "/tahinli/demo", Some(&bob), None).await; - assert_eq!(reply.status, 200); - assert!( - reply.body.contains("href=\"/tahinli/demo/pulls\""), - "pulls tab" - ); - assert!(reply.body.contains("Pull requests")); - assert!( - reply.body.contains("action=\"/tahinli/demo/fork\""), - "fork button" - ); - let reply = send(&site, "GET", "/tahinli/demo", None, None).await; - assert!( - !reply.body.contains("action=\"/tahinli/demo/fork\""), - "no fork button when anonymous" - ); + async fn fork_flows() { + let (site, [tahinli, bob, carol]) = site().await; + scenario_refusals(&site, &tahinli, &bob, &carol).await; + scenario_fork_copies(&site, &bob).await; + scenario_fork_again(&site, &bob).await; + scenario_suffix(&site).await; } } diff --git a/crates/forge-web/src/pages/import_tests.rs b/crates/forge-web/src/pages/import_tests.rs index bb4d59f..6d243d3 100644 --- a/crates/forge-web/src/pages/import_tests.rs +++ b/crates/forge-web/src/pages/import_tests.rs @@ -68,9 +68,8 @@ async fn send( (status, String::from_utf8_lossy(&bytes).into_owned()) } -#[tokio::test] -async fn the_import_pages_render_for_a_signed_in_user() { - let fx = fixture().await; +/// The import and export pages render for a signed-in user. +async fn scenario_pages_render(fx: &Fixture) { for (uri, needle) in [ ("/new/import?source=git", "Import a repository"), ("/new/import?source=github", "Import a repository"), @@ -88,9 +87,8 @@ async fn the_import_pages_render_for_a_signed_in_user() { } } -#[tokio::test] -async fn the_import_and_export_pages_need_a_session() { - let fx = fixture().await; +/// Without a session, the import and export pages do not render. +async fn scenario_need_session(fx: &Fixture) { for uri in [ "/new/import", "/new/import/bulk", @@ -102,9 +100,8 @@ async fn the_import_and_export_pages_need_a_session() { } } -#[tokio::test] -async fn a_post_without_the_csrf_token_starts_nothing() { - let fx = fixture().await; +/// A POST with a bad CSRF token starts no import and no export. +async fn scenario_csrf(fx: &Fixture) { let form = "source=git&remote=https%3A%2F%2Fexample.org%2Fa.git&csrf=wrong".to_owned(); let (status, _) = send(&fx.router, "POST", "/new/import", Some(TOKEN), Some(form)).await; assert_ne!(status, 303, "a bad token must not redirect to a job"); @@ -133,9 +130,8 @@ async fn a_post_without_the_csrf_token_starts_nothing() { ); } -#[tokio::test] -async fn a_private_address_is_refused_before_any_job_exists() { - let fx = fixture().await; +/// A private address is refused before any job exists. +async fn scenario_private_address(fx: &Fixture) { let form = format!( "csrf={CSRF}&source=git&remote=https%3A%2F%2F127.0.0.1%2Fa.git&owner=tahinli&visibility=private" ); @@ -151,9 +147,8 @@ async fn a_private_address_is_refused_before_any_job_exists() { ); } -#[tokio::test] -async fn another_users_archive_cannot_be_downloaded() { - let fx = fixture().await; +/// Another user's archive is not found. This creates an export for the owner, so it runs last. +async fn scenario_archive(fx: &Fixture) { let export = fx .store .create_export(&fx.user_id, std::time::Duration::from_secs(3600)) @@ -169,3 +164,21 @@ async fn another_users_archive_cannot_be_downloaded() { let (status, _) = send(&fx.router, "GET", &uri, None, None).await; assert_ne!(status, 200); } + +/// The read-only pages, on one fixture. +#[tokio::test] +async fn import_and_export_pages_render_and_need_a_session() { + let fx = fixture().await; + scenario_pages_render(&fx).await; + scenario_need_session(&fx).await; +} + +/// Writes and downloads on one fixture. The CSRF and private-address checks assert that no job +/// exists, so they run before the archive scenario creates an export. +#[tokio::test] +async fn import_and_export_writes_are_refused_or_scoped() { + let fx = fixture().await; + scenario_csrf(&fx).await; + scenario_private_address(&fx).await; + scenario_archive(&fx).await; +}