Page tests merge into tables and drop duplicate cases
Browse files at this commit79f48c9d787b4bdedb03367c23aa8f64e650cf0cParent: f12f76a- test
- page tests merge into tables and drop duplicate cases
5 changed files +83 -141
- crates/forge-web/src/pages/profile.rs +13 -15
- crates/forge-web/src/pages/pulls_merge.rs +21 -44
- crates/forge-web/src/pages/releases_tests.rs +21 -38
- crates/forge-web/src/pages/settings_appearance.rs +2 -10
- crates/forge-web/src/pages/settings_profile.rs +26 -34
crates/forge-web/src/pages/profile.rsmodified+13 -15
| @@ -731,13 +731,15 @@ mod tests { | |||
| 731 | assert!(!as_member.contains(href), "member does not see Settings"); | 731 | assert!(!as_member.contains(href), "member does not see Settings"); |
| 732 | } | 732 | } |
| 733 | 733 | ||
| 734 | + /// Anonymous and other users see only public repos; the owner sees all, and a collaborator | ||
| 735 | + /// sees the repo they were added to. | ||
| 734 | #[tokio::test] | 736 | #[tokio::test] |
| 735 | - async fn anonymous_and_other_users_see_only_public_repos() { | 737 | + async fn repo_list_follows_the_viewers_access() { |
| 736 | let site = seeded().await; | 738 | let site = seeded().await; |
| 737 | let (_, anon) = get(&site.router, "/tahinli", None).await; | 739 | let (_, anon) = get(&site.router, "/tahinli", None).await; |
| 738 | - assert!(anon.contains("tahinli/open-repo")); | 740 | + assert!(anon.contains("tahinli/open-repo"), "anonymous"); |
| 739 | - assert!(!anon.contains("tahinli/secret-repo")); | 741 | + assert!(!anon.contains("tahinli/secret-repo"), "anonymous"); |
| 740 | - assert!(!anon.contains("tahinli/shared-repo")); | 742 | + assert!(!anon.contains("tahinli/shared-repo"), "anonymous"); |
| 741 | 743 | ||
| 742 | let bob = site | 744 | let bob = site |
| 743 | .store | 745 | .store |
| @@ -746,14 +748,10 @@ mod tests { | |||
| 746 | .unwrap(); | 748 | .unwrap(); |
| 747 | let cookie = sign_in(&site.store, &bob.id, "bob-token").await; | 749 | let cookie = sign_in(&site.store, &bob.id, "bob-token").await; |
| 748 | let (_, other) = get(&site.router, "/tahinli", Some(&cookie)).await; | 750 | let (_, other) = get(&site.router, "/tahinli", Some(&cookie)).await; |
| 749 | - assert!(other.contains("tahinli/open-repo")); | 751 | + assert!(other.contains("tahinli/open-repo"), "other user"); |
| 750 | - assert!(!other.contains("tahinli/secret-repo")); | 752 | + assert!(!other.contains("tahinli/secret-repo"), "other user"); |
| 751 | - assert!(!other.contains("tahinli/shared-repo")); | 753 | + assert!(!other.contains("tahinli/shared-repo"), "other user"); |
| 752 | - } | ||
| 753 | 754 | ||
| 754 | - #[tokio::test] | ||
| 755 | - async fn owner_sees_private_and_collaborator_sees_their_repo() { | ||
| 756 | - let site = seeded().await; | ||
| 757 | let tahinli = site | 755 | let tahinli = site |
| 758 | .store | 756 | .store |
| 759 | .user_by_username("tahinli") | 757 | .user_by_username("tahinli") |
| @@ -762,14 +760,14 @@ mod tests { | |||
| 762 | .unwrap(); | 760 | .unwrap(); |
| 763 | let cookie = sign_in(&site.store, &tahinli.id, "tahinli-token").await; | 761 | let cookie = sign_in(&site.store, &tahinli.id, "tahinli-token").await; |
| 764 | let (_, body) = get(&site.router, "/tahinli", Some(&cookie)).await; | 762 | let (_, body) = get(&site.router, "/tahinli", Some(&cookie)).await; |
| 765 | - assert!(body.contains("tahinli/secret-repo")); | 763 | + assert!(body.contains("tahinli/secret-repo"), "owner"); |
| 766 | - assert!(body.contains("tahinli/shared-repo")); | 764 | + assert!(body.contains("tahinli/shared-repo"), "owner"); |
| 767 | 765 | ||
| 768 | let carol = site.store.user_by_username("carol").await.unwrap().unwrap(); | 766 | let carol = site.store.user_by_username("carol").await.unwrap().unwrap(); |
| 769 | let cookie = sign_in(&site.store, &carol.id, "carol-token").await; | 767 | let cookie = sign_in(&site.store, &carol.id, "carol-token").await; |
| 770 | let (_, body) = get(&site.router, "/tahinli", Some(&cookie)).await; | 768 | let (_, body) = get(&site.router, "/tahinli", Some(&cookie)).await; |
| 771 | - assert!(body.contains("tahinli/shared-repo")); | 769 | + assert!(body.contains("tahinli/shared-repo"), "collaborator"); |
| 772 | - assert!(!body.contains("tahinli/secret-repo")); | 770 | + assert!(!body.contains("tahinli/secret-repo"), "collaborator"); |
| 773 | } | 771 | } |
| 774 | 772 | ||
| 775 | #[tokio::test] | 773 | #[tokio::test] |
crates/forge-web/src/pages/pulls_merge.rsmodified+21 -44
| @@ -1184,7 +1184,7 @@ mod tests { | |||
| 1184 | } | 1184 | } |
| 1185 | 1185 | ||
| 1186 | #[tokio::test] | 1186 | #[tokio::test] |
| 1187 | - async fn read_only_collaborator_gets_403() { | 1187 | + async fn unauthorised_merge_requests_are_refused_and_base_unchanged() { |
| 1188 | let fx = fixture().await; | 1188 | let fx = fixture().await; |
| 1189 | let number = fx | 1189 | let number = fx |
| 1190 | .open_feature("feature", "feature.txt", "f1", &fx.bob) | 1190 | .open_feature("feature", "feature.txt", "f1", &fx.bob) |
| @@ -1196,52 +1196,29 @@ mod tests { | |||
| 1196 | .expect("collaborator"); | 1196 | .expect("collaborator"); |
| 1197 | let bob_cookie = fx.sign_in(&fx.bob, "bob-token").await; | 1197 | let bob_cookie = fx.sign_in(&fx.bob, "bob-token").await; |
| 1198 | let before = fx.main_tip(); | 1198 | let before = fx.main_tip(); |
| 1199 | + let path = format!("/tahinli/proj/pulls/{number}/merge"); | ||
| 1199 | 1200 | ||
| 1200 | - let reply = fx | 1201 | + // A read-only collaborator gets 403, a stranger to the private repo gets 404. |
| 1201 | - .post( | 1202 | + let cases = [ |
| 1202 | - &format!("/tahinli/proj/pulls/{number}/merge"), | 1203 | + ( |
| 1203 | - Some(&bob_cookie), | 1204 | + "read only", |
| 1204 | - merge_form("merge", None), | 1205 | + Some(bob_cookie.as_str()), |
| 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, | ||
| 1222 | merge_form("merge", None), | 1206 | merge_form("merge", None), |
| 1223 | - ) | 1207 | + 403, |
| 1224 | - .await; | 1208 | + ), |
| 1225 | - assert_eq!(reply.status, 404); | 1209 | + ("anonymous", None, merge_form("merge", None), 404), |
| 1226 | - } | 1210 | + ( |
| 1227 | - | 1211 | + "missing csrf", |
| 1228 | - #[tokio::test] | 1212 | + Some(fx.tahinli_cookie.as_str()), |
| 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), | ||
| 1240 | "style=merge".to_owned(), | 1213 | "style=merge".to_owned(), |
| 1241 | - ) | 1214 | + 403, |
| 1242 | - .await; | 1215 | + ), |
| 1243 | - assert_eq!(reply.status, 403); | 1216 | + ]; |
| 1244 | - assert_eq!(fx.main_tip(), before); | 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 | + } | ||
| 1245 | } | 1222 | } |
| 1246 | 1223 | ||
| 1247 | #[tokio::test] | 1224 | #[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() { | |||
| 830 | ); | 830 | ); |
| 831 | } | 831 | } |
| 832 | 832 | ||
| 833 | +/// Create refuses a bad tag or a missing target before touching git, and takes a commit id as a | ||
| 834 | +/// target. | ||
| 833 | #[tokio::test] | 835 | #[tokio::test] |
| 834 | -async fn create_refuses_a_bad_tag_before_touching_git() { | 836 | +async fn create_validates_its_tag_and_target() { |
| 835 | let fx = fixture(None).await; | 837 | let fx = fixture(None).await; |
| 836 | - let input = CreateRelease { | 838 | + let input = |tag: &str, target: &str| CreateRelease { |
| 837 | - tag: "a/b".to_owned(), | 839 | + tag: tag.to_owned(), |
| 838 | - target: Some("main".to_owned()), | 840 | + target: Some(target.to_owned()), |
| 839 | title: "x".to_owned(), | 841 | title: "x".to_owned(), |
| 840 | body: String::new(), | 842 | body: String::new(), |
| 841 | draft: false, | 843 | draft: false, |
| 842 | prerelease: false, | 844 | prerelease: false, |
| 843 | }; | 845 | }; |
| 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; |
| 845 | assert!( | 847 | assert!( |
| 846 | matches!(refused, Err(ReleaseError::Invalid(_))), | 848 | matches!(refused, Err(ReleaseError::Invalid(_))), |
| 847 | - "{refused:?}" | 849 | + "bad tag: {refused:?}" |
| 848 | ); | 850 | ); |
| 849 | - assert!(fx.tag_target("a/b").is_none()); | 851 | + assert!(fx.tag_target("a/b").is_none(), "bad tag"); |
| 850 | -} | ||
| 851 | 852 | ||
| 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; | ||
| 855 | let tip = fx.main_tip(); | 853 | let tip = fx.main_tip(); |
| 856 | - let made = release_ops::create( | 854 | + let made = release_ops::create(&fx.state, &fx.repo, &fx.tahinli, input("v7", &tip)) |
| 857 | - &fx.state, | 855 | + .await |
| 858 | - &fx.repo, | 856 | + .expect("a commit id works as a target"); |
| 859 | - &fx.tahinli, | 857 | + assert_eq!(made.target_oid, tip, "commit id target"); |
| 860 | - CreateRelease { | 858 | + assert_eq!( |
| 861 | - tag: "v7".to_owned(), | 859 | + fx.tag_target("v7").as_deref(), |
| 862 | - target: Some(tip.clone()), | 860 | + Some(tip.as_str()), |
| 863 | - title: "by sha".to_owned(), | 861 | + "commit id target" |
| 864 | - body: String::new(), | 862 | + ); |
| 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())); | ||
| 873 | 863 | ||
| 874 | let missing = release_ops::create( | 864 | let missing = release_ops::create( |
| 875 | &fx.state, | 865 | &fx.state, |
| 876 | &fx.repo, | 866 | &fx.repo, |
| 877 | &fx.tahinli, | 867 | &fx.tahinli, |
| 878 | - CreateRelease { | 868 | + input("v8", "1111111111111111111111111111111111111111"), |
| 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 | - }, | ||
| 886 | ) | 869 | ) |
| 887 | .await; | 870 | .await; |
| 888 | assert!( | 871 | assert!( |
| 889 | matches!(missing, Err(ReleaseError::Invalid(_))), | 872 | matches!(missing, Err(ReleaseError::Invalid(_))), |
| 890 | - "{missing:?}" | 873 | + "missing target: {missing:?}" |
| 891 | ); | 874 | ); |
| 892 | - assert!(fx.tag_target("v8").is_none()); | 875 | + assert!(fx.tag_target("v8").is_none(), "missing target"); |
| 893 | } | 876 | } |
| 894 | 877 | ||
| 895 | #[tokio::test] | 878 | #[tokio::test] |
crates/forge-web/src/pages/settings_appearance.rsmodified+2 -10
| @@ -543,7 +543,6 @@ mod tests { | |||
| 543 | "hue-07", | 543 | "hue-07", |
| 544 | "hue-", | 544 | "hue-", |
| 545 | "hue-+5", | 545 | "hue-+5", |
| 546 | - "teal", | ||
| 547 | ] { | 546 | ] { |
| 548 | assert!(accent_choice(Lang::En, Some(id), "sage").is_err(), "{id}"); | 547 | assert!(accent_choice(Lang::En, Some(id), "sage").is_err(), "{id}"); |
| 549 | } | 548 | } |
| @@ -578,18 +577,15 @@ mod tests { | |||
| 578 | } | 577 | } |
| 579 | 578 | ||
| 580 | #[test] | 579 | #[test] |
| 581 | - fn the_stored_value_is_selected() { | 580 | + fn the_stored_theme_accent_and_base_are_selected() { |
| 582 | let options = choices(THEME_CODES, "dark", |code| theme_label(Lang::En, code)); | 581 | let options = choices(THEME_CODES, "dark", |code| theme_label(Lang::En, code)); |
| 583 | let selected: Vec<&str> = options | 582 | let selected: Vec<&str> = options |
| 584 | .iter() | 583 | .iter() |
| 585 | .filter(|c| c.selected) | 584 | .filter(|c| c.selected) |
| 586 | .map(|c| c.value) | 585 | .map(|c| c.value) |
| 587 | .collect(); | 586 | .collect(); |
| 588 | - assert_eq!(selected, ["dark"]); | 587 | + assert_eq!(selected, ["dark"], "theme"); |
| 589 | - } | ||
| 590 | 588 | ||
| 591 | - #[test] | ||
| 592 | - fn the_stored_accent_and_base_are_selected() { | ||
| 593 | let codes: Vec<&'static str> = accent::ids().collect(); | 589 | let codes: Vec<&'static str> = accent::ids().collect(); |
| 594 | let options = choices(&codes, "plum", |id| accent_name(Lang::En, id)); | 590 | let options = choices(&codes, "plum", |id| accent_name(Lang::En, id)); |
| 595 | let selected: Vec<&str> = options | 591 | let selected: Vec<&str> = options |
| @@ -644,10 +640,6 @@ mod tests { | |||
| 644 | assert_eq!(theme_label(Lang::En, "dark"), "Dark"); | 640 | assert_eq!(theme_label(Lang::En, "dark"), "Dark"); |
| 645 | assert_eq!(theme_label(Lang::Tr, "dark"), "Koyu"); | 641 | assert_eq!(theme_label(Lang::Tr, "dark"), "Koyu"); |
| 646 | assert_eq!(base_name(Lang::Tr, "ink"), "M\u{fc}rekkep"); | 642 | assert_eq!(base_name(Lang::Tr, "ink"), "M\u{fc}rekkep"); |
| 647 | - } | ||
| 648 | - | ||
| 649 | - #[test] | ||
| 650 | - fn light_is_marked_as_the_default() { | ||
| 651 | assert_eq!(theme_label(Lang::En, "light"), "Light (default)"); | 643 | assert_eq!(theme_label(Lang::En, "light"), "Light (default)"); |
| 652 | assert_eq!( | 644 | assert_eq!( |
| 653 | theme_label(Lang::Tr, "light"), | 645 | theme_label(Lang::Tr, "light"), |
crates/forge-web/src/pages/settings_profile.rsmodified+26 -34
| @@ -652,16 +652,22 @@ pub(crate) mod tests { | |||
| 652 | #[tokio::test] | 652 | #[tokio::test] |
| 653 | async fn post_without_a_valid_csrf_token_is_rejected() { | 653 | async fn post_without_a_valid_csrf_token_is_rejected() { |
| 654 | let fx = fixture().await; | 654 | let fx = fixture().await; |
| 655 | - let forged = "csrf=wrong&username=Tahinli&about=&timezone=UTC"; | 655 | + let cases = [ |
| 656 | - let (status, _, _) = | 656 | + ("forged", "csrf=wrong&username=Tahinli&about=&timezone=UTC"), |
| 657 | - send(&fx.router, "POST", "/settings/profile", true, Some(forged)).await; | 657 | + ("missing", "username=Tahinli&about=&timezone=UTC"), |
| 658 | - assert_eq!(status, 403); | 658 | + ( |
| 659 | - let missing = "username=Tahinli&about=&timezone=UTC"; | 659 | + "missing with about", |
| 660 | - let (status, _, _) = | 660 | + "username=Tahinli&about=x&timezone=UTC", |
| 661 | - send(&fx.router, "POST", "/settings/profile", true, Some(missing)).await; | 661 | + ), |
| 662 | - assert_eq!(status, 403); | 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 | + } | ||
| 663 | let stored = fx.store.user_by_id(&fx.user.id).await.unwrap().unwrap(); | 668 | let stored = fx.store.user_by_id(&fx.user.id).await.unwrap().unwrap(); |
| 664 | assert_eq!(stored.username, "tahinli"); | 669 | assert_eq!(stored.username, "tahinli"); |
| 670 | + assert_eq!(stored.about, ""); | ||
| 665 | } | 671 | } |
| 666 | 672 | ||
| 667 | #[tokio::test] | 673 | #[tokio::test] |
| @@ -785,17 +791,7 @@ pub(crate) mod tests { | |||
| 785 | } | 791 | } |
| 786 | 792 | ||
| 787 | #[tokio::test] | 793 | #[tokio::test] |
| 788 | - async fn profile_post_without_csrf_is_rejected() { | 794 | + async fn avatar_writes_without_csrf_are_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() { | ||
| 799 | let fx = fixture().await; | 795 | let fx = fixture().await; |
| 800 | let png = png_bytes(); | 796 | let png = png_bytes(); |
| 801 | let (content_type, body) = multipart(&[("avatar", Some(("a.png", "image/png")), &png[..])]); | 797 | let (content_type, body) = multipart(&[("avatar", Some(("a.png", "image/png")), &png[..])]); |
| @@ -808,8 +804,18 @@ pub(crate) mod tests { | |||
| 808 | body, | 804 | body, |
| 809 | ) | 805 | ) |
| 810 | .await; | 806 | .await; |
| 811 | - assert_eq!(reply.status, 403); | 807 | + assert_eq!(reply.status, 403, "upload"); |
| 812 | assert!(fx.store.avatar_hash(&fx.user.id).await.unwrap().is_none()); | 808 | 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"); | ||
| 813 | } | 819 | } |
| 814 | 820 | ||
| 815 | #[tokio::test] | 821 | #[tokio::test] |
| @@ -934,18 +940,4 @@ pub(crate) mod tests { | |||
| 934 | ); | 940 | ); |
| 935 | assert!(default.text().starts_with("<svg"), "{}", default.text()); | 941 | assert!(default.text().starts_with("<svg"), "{}", default.text()); |
| 936 | } | 942 | } |
| 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 | - } | ||
| 951 | } | 943 | } |