forge
Explore
Log in

Don't you dare go Hollow

Page tests merge into tables and drop duplicate cases

Browse files at this commit

79f48c9d787b4bdedb03367c23aa8f64e650cf0cAhmet Kaan GümüşParent: f12f76a
  • test
    • page tests merge into tables and drop duplicate cases
Show full message
✅ test: page tests merge into tables and drop duplicate cases

Counts for the m-z page files: 332 #[test] before, 324 after.

Merged: three unauthorised merge requests (read only, anonymous,
missing csrf) share one fixture; the profile repo-visibility checks
share one seeded site; the create tag/target checks share one fixture;
three csrf-refusal tests in settings_profile become table or combined
cases; two settings_appearance label tests and two selection tests
become one test each.

Removed: a duplicate "teal" case in settings_appearance.
crates/forge-web/src/pages/profile.rsmodified+13 -15
@@ -731,13 +731,15 @@ mod tests {
731731 assert!(!as_member.contains(href), "member does not see Settings");
732732 }
733733
734+ /// Anonymous and other users see only public repos; the owner sees all, and a collaborator
735+ /// sees the repo they were added to.
734736 #[tokio::test]
735- async fn anonymous_and_other_users_see_only_public_repos() {
737+ async fn repo_list_follows_the_viewers_access() {
736738 let site = seeded().await;
737739 let (_, anon) = get(&site.router, "/tahinli", None).await;
738- assert!(anon.contains("tahinli/open-repo"));
739- assert!(!anon.contains("tahinli/secret-repo"));
740- assert!(!anon.contains("tahinli/shared-repo"));
740+ assert!(anon.contains("tahinli/open-repo"), "anonymous");
741+ assert!(!anon.contains("tahinli/secret-repo"), "anonymous");
742+ assert!(!anon.contains("tahinli/shared-repo"), "anonymous");
741743
742744 let bob = site
743745 .store
@@ -746,14 +748,10 @@ mod tests {
746748 .unwrap();
747749 let cookie = sign_in(&site.store, &bob.id, "bob-token").await;
748750 let (_, other) = get(&site.router, "/tahinli", Some(&cookie)).await;
749- assert!(other.contains("tahinli/open-repo"));
750- assert!(!other.contains("tahinli/secret-repo"));
751- assert!(!other.contains("tahinli/shared-repo"));
752- }
751+ assert!(other.contains("tahinli/open-repo"), "other user");
752+ assert!(!other.contains("tahinli/secret-repo"), "other user");
753+ assert!(!other.contains("tahinli/shared-repo"), "other user");
753754
754- #[tokio::test]
755- async fn owner_sees_private_and_collaborator_sees_their_repo() {
756- let site = seeded().await;
757755 let tahinli = site
758756 .store
759757 .user_by_username("tahinli")
@@ -762,14 +760,14 @@ mod tests {
762760 .unwrap();
763761 let cookie = sign_in(&site.store, &tahinli.id, "tahinli-token").await;
764762 let (_, body) = get(&site.router, "/tahinli", Some(&cookie)).await;
765- assert!(body.contains("tahinli/secret-repo"));
766- assert!(body.contains("tahinli/shared-repo"));
763+ assert!(body.contains("tahinli/secret-repo"), "owner");
764+ assert!(body.contains("tahinli/shared-repo"), "owner");
767765
768766 let carol = site.store.user_by_username("carol").await.unwrap().unwrap();
769767 let cookie = sign_in(&site.store, &carol.id, "carol-token").await;
770768 let (_, body) = get(&site.router, "/tahinli", Some(&cookie)).await;
771- assert!(body.contains("tahinli/shared-repo"));
772- assert!(!body.contains("tahinli/secret-repo"));
769+ assert!(body.contains("tahinli/shared-repo"), "collaborator");
770+ assert!(!body.contains("tahinli/secret-repo"), "collaborator");
773771 }
774772
775773 #[tokio::test]
crates/forge-web/src/pages/pulls_merge.rsmodified+21 -44
@@ -1184,7 +1184,7 @@ mod tests {
11841184 }
11851185
11861186 #[tokio::test]
1187- async fn read_only_collaborator_gets_403() {
1187+ async fn unauthorised_merge_requests_are_refused_and_base_unchanged() {
11881188 let fx = fixture().await;
11891189 let number = fx
11901190 .open_feature("feature", "feature.txt", "f1", &fx.bob)
@@ -1196,52 +1196,29 @@ mod tests {
11961196 .expect("collaborator");
11971197 let bob_cookie = fx.sign_in(&fx.bob, "bob-token").await;
11981198 let before = fx.main_tip();
1199+ let path = format!("/tahinli/proj/pulls/{number}/merge");
11991200
1200- let reply = fx
1201- .post(
1202- &format!("/tahinli/proj/pulls/{number}/merge"),
1203- Some(&bob_cookie),
1204- merge_form("merge", None),
1205- )
1206- .await;
1207- assert_eq!(reply.status, 403);
1208- assert_eq!(fx.main_tip(), before);
1209- }
1210-
1211- #[tokio::test]
1212- async fn anonymous_viewer_of_private_repo_gets_404() {
1213- let fx = fixture().await;
1214- let number = fx
1215- .open_feature("feature", "feature.txt", "f1", &fx.bob)
1216- .await;
1217-
1218- let reply = fx
1219- .post(
1220- &format!("/tahinli/proj/pulls/{number}/merge"),
1221- None,
1201+ // A read-only collaborator gets 403, a stranger to the private repo gets 404.
1202+ let cases = [
1203+ (
1204+ "read only",
1205+ Some(bob_cookie.as_str()),
12221206 merge_form("merge", None),
1223- )
1224- .await;
1225- assert_eq!(reply.status, 404);
1226- }
1227-
1228- #[tokio::test]
1229- async fn missing_csrf_is_rejected() {
1230- let fx = fixture().await;
1231- let number = fx
1232- .open_feature("feature", "feature.txt", "f1", &fx.bob)
1233- .await;
1234- let before = fx.main_tip();
1235-
1236- let reply = fx
1237- .post(
1238- &format!("/tahinli/proj/pulls/{number}/merge"),
1239- Some(&fx.tahinli_cookie),
1207+ 403,
1208+ ),
1209+ ("anonymous", None, merge_form("merge", None), 404),
1210+ (
1211+ "missing csrf",
1212+ Some(fx.tahinli_cookie.as_str()),
12401213 "style=merge".to_owned(),
1241- )
1242- .await;
1243- assert_eq!(reply.status, 403);
1244- assert_eq!(fx.main_tip(), before);
1214+ 403,
1215+ ),
1216+ ];
1217+ for (name, cookie, body, status) in cases {
1218+ let reply = fx.post(&path, cookie, body).await;
1219+ assert_eq!(reply.status, status, "case {name}");
1220+ assert_eq!(fx.main_tip(), before, "case {name}");
1221+ }
12451222 }
12461223
12471224 #[tokio::test]
crates/forge-web/src/pages/releases_tests.rsmodified+21 -38
@@ -830,66 +830,49 @@ async fn retagging_adopts_an_existing_tag_and_refuses_clashes() {
830830 );
831831 }
832832
833+/// Create refuses a bad tag or a missing target before touching git, and takes a commit id as a
834+/// target.
833835 #[tokio::test]
834-async fn create_refuses_a_bad_tag_before_touching_git() {
836+async fn create_validates_its_tag_and_target() {
835837 let fx = fixture(None).await;
836- let input = CreateRelease {
837- tag: "a/b".to_owned(),
838- target: Some("main".to_owned()),
838+ let input = |tag: &str, target: &str| CreateRelease {
839+ tag: tag.to_owned(),
840+ target: Some(target.to_owned()),
839841 title: "x".to_owned(),
840842 body: String::new(),
841843 draft: false,
842844 prerelease: false,
843845 };
844- let refused = release_ops::create(&fx.state, &fx.repo, &fx.tahinli, input).await;
846+ let refused = release_ops::create(&fx.state, &fx.repo, &fx.tahinli, input("a/b", "main")).await;
845847 assert!(
846848 matches!(refused, Err(ReleaseError::Invalid(_))),
847- "{refused:?}"
849+ "bad tag: {refused:?}"
848850 );
849- assert!(fx.tag_target("a/b").is_none());
850-}
851+ assert!(fx.tag_target("a/b").is_none(), "bad tag");
851852
852-#[tokio::test]
853-async fn create_takes_a_commit_id_as_its_target_and_refuses_a_missing_one() {
854- let fx = fixture(None).await;
855853 let tip = fx.main_tip();
856- let made = release_ops::create(
857- &fx.state,
858- &fx.repo,
859- &fx.tahinli,
860- CreateRelease {
861- tag: "v7".to_owned(),
862- target: Some(tip.clone()),
863- title: "by sha".to_owned(),
864- body: String::new(),
865- draft: false,
866- prerelease: false,
867- },
868- )
869- .await
870- .expect("a commit id works as a target");
871- assert_eq!(made.target_oid, tip);
872- assert_eq!(fx.tag_target("v7").as_deref(), Some(tip.as_str()));
854+ let made = release_ops::create(&fx.state, &fx.repo, &fx.tahinli, input("v7", &tip))
855+ .await
856+ .expect("a commit id works as a target");
857+ assert_eq!(made.target_oid, tip, "commit id target");
858+ assert_eq!(
859+ fx.tag_target("v7").as_deref(),
860+ Some(tip.as_str()),
861+ "commit id target"
862+ );
873863
874864 let missing = release_ops::create(
875865 &fx.state,
876866 &fx.repo,
877867 &fx.tahinli,
878- CreateRelease {
879- tag: "v8".to_owned(),
880- target: Some("1111111111111111111111111111111111111111".to_owned()),
881- title: "missing".to_owned(),
882- body: String::new(),
883- draft: false,
884- prerelease: false,
885- },
868+ input("v8", "1111111111111111111111111111111111111111"),
886869 )
887870 .await;
888871 assert!(
889872 matches!(missing, Err(ReleaseError::Invalid(_))),
890- "{missing:?}"
873+ "missing target: {missing:?}"
891874 );
892- assert!(fx.tag_target("v8").is_none());
875+ assert!(fx.tag_target("v8").is_none(), "missing target");
893876 }
894877
895878 #[tokio::test]
crates/forge-web/src/pages/settings_appearance.rsmodified+2 -10
@@ -543,7 +543,6 @@ mod tests {
543543 "hue-07",
544544 "hue-",
545545 "hue-+5",
546- "teal",
547546 ] {
548547 assert!(accent_choice(Lang::En, Some(id), "sage").is_err(), "{id}");
549548 }
@@ -578,18 +577,15 @@ mod tests {
578577 }
579578
580579 #[test]
581- fn the_stored_value_is_selected() {
580+ fn the_stored_theme_accent_and_base_are_selected() {
582581 let options = choices(THEME_CODES, "dark", |code| theme_label(Lang::En, code));
583582 let selected: Vec<&str> = options
584583 .iter()
585584 .filter(|c| c.selected)
586585 .map(|c| c.value)
587586 .collect();
588- assert_eq!(selected, ["dark"]);
589- }
587+ assert_eq!(selected, ["dark"], "theme");
590588
591- #[test]
592- fn the_stored_accent_and_base_are_selected() {
593589 let codes: Vec<&'static str> = accent::ids().collect();
594590 let options = choices(&codes, "plum", |id| accent_name(Lang::En, id));
595591 let selected: Vec<&str> = options
@@ -644,10 +640,6 @@ mod tests {
644640 assert_eq!(theme_label(Lang::En, "dark"), "Dark");
645641 assert_eq!(theme_label(Lang::Tr, "dark"), "Koyu");
646642 assert_eq!(base_name(Lang::Tr, "ink"), "M\u{fc}rekkep");
647- }
648-
649- #[test]
650- fn light_is_marked_as_the_default() {
651643 assert_eq!(theme_label(Lang::En, "light"), "Light (default)");
652644 assert_eq!(
653645 theme_label(Lang::Tr, "light"),
crates/forge-web/src/pages/settings_profile.rsmodified+26 -34
@@ -652,16 +652,22 @@ pub(crate) mod tests {
652652 #[tokio::test]
653653 async fn post_without_a_valid_csrf_token_is_rejected() {
654654 let fx = fixture().await;
655- let forged = "csrf=wrong&username=Tahinli&about=&timezone=UTC";
656- let (status, _, _) =
657- send(&fx.router, "POST", "/settings/profile", true, Some(forged)).await;
658- assert_eq!(status, 403);
659- let missing = "username=Tahinli&about=&timezone=UTC";
660- let (status, _, _) =
661- send(&fx.router, "POST", "/settings/profile", true, Some(missing)).await;
662- assert_eq!(status, 403);
655+ let cases = [
656+ ("forged", "csrf=wrong&username=Tahinli&about=&timezone=UTC"),
657+ ("missing", "username=Tahinli&about=&timezone=UTC"),
658+ (
659+ "missing with about",
660+ "username=Tahinli&about=x&timezone=UTC",
661+ ),
662+ ];
663+ for (name, form) in cases {
664+ let (status, _, _) =
665+ send(&fx.router, "POST", "/settings/profile", true, Some(form)).await;
666+ assert_eq!(status, 403, "case {name}");
667+ }
663668 let stored = fx.store.user_by_id(&fx.user.id).await.unwrap().unwrap();
664669 assert_eq!(stored.username, "tahinli");
670+ assert_eq!(stored.about, "");
665671 }
666672
667673 #[tokio::test]
@@ -785,17 +791,7 @@ pub(crate) mod tests {
785791 }
786792
787793 #[tokio::test]
788- async fn profile_post_without_csrf_is_rejected() {
789- let fx = fixture().await;
790- let form = "username=tahinli&about=x&timezone=UTC";
791- let (status, _, _) = send(&fx.router, "POST", "/settings/profile", true, Some(form)).await;
792- assert_eq!(status, 403);
793- let stored = fx.store.user_by_id(&fx.user.id).await.unwrap().unwrap();
794- assert_eq!(stored.about, "");
795- }
796-
797- #[tokio::test]
798- async fn avatar_upload_without_csrf_is_rejected() {
794+ async fn avatar_writes_without_csrf_are_rejected() {
799795 let fx = fixture().await;
800796 let png = png_bytes();
801797 let (content_type, body) = multipart(&[("avatar", Some(("a.png", "image/png")), &png[..])]);
@@ -808,8 +804,18 @@ pub(crate) mod tests {
808804 body,
809805 )
810806 .await;
811- assert_eq!(reply.status, 403);
807+ assert_eq!(reply.status, 403, "upload");
812808 assert!(fx.store.avatar_hash(&fx.user.id).await.unwrap().is_none());
809+
810+ let (status, _, _) = send(
811+ &fx.router,
812+ "POST",
813+ "/settings/profile/avatar/delete",
814+ true,
815+ Some("csrf=nope"),
816+ )
817+ .await;
818+ assert_eq!(status, 403, "remove");
813819 }
814820
815821 #[tokio::test]
@@ -934,18 +940,4 @@ pub(crate) mod tests {
934940 );
935941 assert!(default.text().starts_with("<svg"), "{}", default.text());
936942 }
937-
938- #[tokio::test]
939- async fn avatar_remove_without_csrf_is_rejected() {
940- let fx = fixture().await;
941- let (status, _, _) = send(
942- &fx.router,
943- "POST",
944- "/settings/profile/avatar/delete",
945- true,
946- Some("csrf=nope"),
947- )
948- .await;
949- assert_eq!(status, 403);
950- }
951943 }