Page tests share their fixtures and merge into tables
Browse files at this commitcbc0d0bc84da33d5d88e678e0446dedc44312fdaParent: f23b28c- test
- page tests share their fixtures and merge into tables
6 changed files +798 -590
- crates/forge-web/src/pages/profile.rs +287 -248
- crates/forge-web/src/pages/profile_pins.rs +75 -55
- crates/forge-web/src/pages/settings_appearance.rs +128 -54
- crates/forge-web/src/pages/settings_live.rs +69 -48
- crates/forge-web/src/pages/settings_notifications.rs +22 -23
- crates/forge-web/src/pages/settings_profile.rs +217 -162
crates/forge-web/src/pages/profile.rsmodified+287 -248
| @@ -686,103 +686,91 @@ mod tests { | |||
| 686 | site | 686 | site |
| 687 | } | 687 | } |
| 688 | 688 | ||
| 689 | + /// One seeded site covers the repo list for each kind of viewer, the about text, the | ||
| 690 | + /// edit button, the unknown-user 404 and the fixed routes. Each signed-in viewer gets its | ||
| 691 | + /// own session token. | ||
| 689 | #[tokio::test] | 692 | #[tokio::test] |
| 690 | - async fn about_is_rendered_and_sanitized() { | 693 | + async fn profile_visibility_routing_and_edit_button() { |
| 691 | let site = seeded().await; | 694 | let site = seeded().await; |
| 692 | - let (status, body) = get(&site.router, "/tahinli", None).await; | ||
| 693 | - assert_eq!(status, 200); | ||
| 694 | - assert!(body.contains("<h1"), "heading rendered: {body}"); | ||
| 695 | - assert!(body.contains("Hello heading")); | ||
| 696 | - assert!(body.contains("<strong>bold</strong>")); | ||
| 697 | - assert!(!body.contains("<script>"), "script stripped: {body}"); | ||
| 698 | - assert!(!body.contains("alert(1)")); | ||
| 699 | - } | ||
| 700 | - | ||
| 701 | - #[tokio::test] | ||
| 702 | - async fn org_sidebar_shows_settings_to_owners_only() { | ||
| 703 | - let site = site().await; | ||
| 704 | - let owner = site | ||
| 705 | - .store | ||
| 706 | - .create_user("tahinli", None, "not-a-real-hash", false) | ||
| 707 | - .await | ||
| 708 | - .unwrap(); | ||
| 709 | - let member = site | ||
| 710 | - .store | ||
| 711 | - .create_user("kaan", None, "not-a-real-hash", false) | ||
| 712 | - .await | ||
| 713 | - .unwrap(); | ||
| 714 | - let org = site.store.create_org("forge-org", &owner.id).await.unwrap(); | ||
| 715 | - site.store | ||
| 716 | - .add_org_member( | ||
| 717 | - &org.id, | ||
| 718 | - &member.id, | ||
| 719 | - forge_core::store::orgs::OrgMemberRole::Member, | ||
| 720 | - ) | ||
| 721 | - .await | ||
| 722 | - .unwrap(); | ||
| 723 | - let owner_cookie = sign_in(&site.store, &owner.id, "owner-token").await; | ||
| 724 | - let member_cookie = sign_in(&site.store, &member.id, "member-token").await; | ||
| 725 | - let href = "href=\"/org/forge-org/settings\""; | ||
| 726 | - let (_, as_owner) = get(&site.router, "/org/forge-org/members", Some(&owner_cookie)).await; | ||
| 727 | - assert!(as_owner.contains(href), "owner sees Settings"); | ||
| 728 | - let (_, as_member) = | ||
| 729 | - get(&site.router, "/org/forge-org/members", Some(&member_cookie)).await; | ||
| 730 | - assert!(as_member.contains("href=\"/org/forge-org/teams\"")); | ||
| 731 | - assert!(!as_member.contains(href), "member does not see Settings"); | ||
| 732 | - } | ||
| 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. | ||
| 736 | - #[tokio::test] | ||
| 737 | - async fn repo_list_follows_the_viewers_access() { | ||
| 738 | - let site = seeded().await; | ||
| 739 | - let (_, anon) = get(&site.router, "/tahinli", None).await; | ||
| 740 | - assert!(anon.contains("tahinli/open-repo"), "anonymous"); | ||
| 741 | - assert!(!anon.contains("tahinli/secret-repo"), "anonymous"); | ||
| 742 | - assert!(!anon.contains("tahinli/shared-repo"), "anonymous"); | ||
| 743 | 695 | ||
| 696 | + // About is rendered and sanitized. | ||
| 697 | + let (status, anon) = get(&site.router, "/tahinli", None).await; | ||
| 698 | + assert_eq!(status, 200, "about"); | ||
| 699 | + assert!(anon.contains("<h1"), "about: heading rendered: {anon}"); | ||
| 700 | + assert!(anon.contains("Hello heading"), "about: heading text"); | ||
| 701 | + assert!( | ||
| 702 | + anon.contains("<strong>bold</strong>"), | ||
| 703 | + "about: bold rendered" | ||
| 704 | + ); | ||
| 705 | + assert!(!anon.contains("<script>"), "about: script stripped: {anon}"); | ||
| 706 | + assert!(!anon.contains("alert(1)"), "about: script body stripped"); | ||
| 707 | + | ||
| 708 | + // Anonymous visitors see only public repos and no edit button. | ||
| 709 | + assert!(anon.contains("tahinli/open-repo"), "anonymous: public repo"); | ||
| 710 | + assert!( | ||
| 711 | + !anon.contains("tahinli/secret-repo"), | ||
| 712 | + "anonymous: private repo" | ||
| 713 | + ); | ||
| 714 | + assert!( | ||
| 715 | + !anon.contains("tahinli/shared-repo"), | ||
| 716 | + "anonymous: shared repo" | ||
| 717 | + ); | ||
| 718 | + assert!(!anon.contains("profile-edit"), "anonymous: edit button"); | ||
| 719 | + | ||
| 720 | + // Another user sees the same public-only list and no edit button. | ||
| 744 | let bob = site | 721 | let bob = site |
| 745 | .store | 722 | .store |
| 746 | .create_user("bob", None, "not-a-real-hash", false) | 723 | .create_user("bob", None, "not-a-real-hash", false) |
| 747 | .await | 724 | .await |
| 748 | .unwrap(); | 725 | .unwrap(); |
| 749 | - let cookie = sign_in(&site.store, &bob.id, "bob-token").await; | 726 | + let cookie = sign_in(&site.store, &bob.id, "visibility-bob-token").await; |
| 750 | let (_, other) = get(&site.router, "/tahinli", Some(&cookie)).await; | 727 | let (_, other) = get(&site.router, "/tahinli", Some(&cookie)).await; |
| 751 | - assert!(other.contains("tahinli/open-repo"), "other user"); | 728 | + assert!( |
| 752 | - assert!(!other.contains("tahinli/secret-repo"), "other user"); | 729 | + other.contains("tahinli/open-repo"), |
| 753 | - assert!(!other.contains("tahinli/shared-repo"), "other user"); | 730 | + "other user: public repo" |
| 754 | - | 731 | + ); |
| 755 | - let tahinli = site | 732 | + assert!( |
| 756 | - .store | 733 | + !other.contains("tahinli/secret-repo"), |
| 757 | - .user_by_username("tahinli") | 734 | + "other user: private repo" |
| 758 | - .await | 735 | + ); |
| 759 | - .unwrap() | 736 | + assert!( |
| 760 | - .unwrap(); | 737 | + !other.contains("tahinli/shared-repo"), |
| 761 | - let cookie = sign_in(&site.store, &tahinli.id, "tahinli-token").await; | 738 | + "other user: shared repo" |
| 762 | - let (_, body) = get(&site.router, "/tahinli", Some(&cookie)).await; | 739 | + ); |
| 763 | - assert!(body.contains("tahinli/secret-repo"), "owner"); | 740 | + assert!(!other.contains("profile-edit"), "other user: edit button"); |
| 764 | - assert!(body.contains("tahinli/shared-repo"), "owner"); | 741 | + |
| 765 | - | 742 | + // The owner sees every repo and the edit button. |
| 766 | - let carol = site.store.user_by_username("carol").await.unwrap().unwrap(); | 743 | + let tahinli = user_id(&site.store, "tahinli").await; |
| 767 | - let cookie = sign_in(&site.store, &carol.id, "carol-token").await; | 744 | + let cookie = sign_in(&site.store, &tahinli, "visibility-owner-token").await; |
| 768 | - let (_, body) = get(&site.router, "/tahinli", Some(&cookie)).await; | 745 | + let (_, owner) = get(&site.router, "/tahinli", Some(&cookie)).await; |
| 769 | - assert!(body.contains("tahinli/shared-repo"), "collaborator"); | 746 | + assert!(owner.contains("tahinli/secret-repo"), "owner: private repo"); |
| 770 | - assert!(!body.contains("tahinli/secret-repo"), "collaborator"); | 747 | + assert!(owner.contains("tahinli/shared-repo"), "owner: shared repo"); |
| 771 | - } | 748 | + assert!( |
| 772 | - | 749 | + owner.contains("class=\"button profile-edit\" href=\"/settings/profile\""), |
| 773 | - #[tokio::test] | 750 | + "owner: edit button" |
| 774 | - async fn unknown_user_is_404_and_lookup_is_case_insensitive() { | 751 | + ); |
| 775 | - let site = seeded().await; | 752 | + |
| 753 | + // A collaborator sees the repo they were added to, not the private one. | ||
| 754 | + let carol = user_id(&site.store, "carol").await; | ||
| 755 | + let cookie = sign_in(&site.store, &carol, "visibility-carol-token").await; | ||
| 756 | + let (_, collab) = get(&site.router, "/tahinli", Some(&cookie)).await; | ||
| 757 | + assert!( | ||
| 758 | + collab.contains("tahinli/shared-repo"), | ||
| 759 | + "collaborator: shared repo" | ||
| 760 | + ); | ||
| 761 | + assert!( | ||
| 762 | + !collab.contains("tahinli/secret-repo"), | ||
| 763 | + "collaborator: private repo" | ||
| 764 | + ); | ||
| 765 | + | ||
| 766 | + // Unknown users 404, and the lookup ignores case. | ||
| 776 | let (status, _) = get(&site.router, "/nobody-here", None).await; | 767 | let (status, _) = get(&site.router, "/nobody-here", None).await; |
| 777 | - assert_eq!(status, 404); | 768 | + assert_eq!(status, 404, "unknown user"); |
| 778 | let (status, body) = get(&site.router, "/TAHINLI", None).await; | 769 | let (status, body) = get(&site.router, "/TAHINLI", None).await; |
| 779 | - assert_eq!(status, 200); | 770 | + assert_eq!(status, 200, "case-insensitive lookup"); |
| 780 | - assert!(body.contains("tahinli")); | 771 | + assert!(body.contains("tahinli"), "case-insensitive lookup"); |
| 781 | - } | ||
| 782 | 772 | ||
| 783 | - #[tokio::test] | 773 | + // Fixed routes are not shadowed by the profile. |
| 784 | - async fn fixed_routes_are_not_shadowed_by_the_profile() { | ||
| 785 | - let site = seeded().await; | ||
| 786 | for uri in [ | 774 | for uri in [ |
| 787 | "/new", | 775 | "/new", |
| 788 | "/login", | 776 | "/login", |
| @@ -803,219 +791,270 @@ mod tests { | |||
| 803 | } | 791 | } |
| 804 | // The fixed login page still renders its form, not a profile. | 792 | // The fixed login page still renders its form, not a profile. |
| 805 | let (status, body) = get(&site.router, "/login", None).await; | 793 | let (status, body) = get(&site.router, "/login", None).await; |
| 806 | - assert_eq!(status, 200); | 794 | + assert_eq!(status, 200, "fixed login page"); |
| 807 | - assert!(body.contains("<form")); | 795 | + assert!(body.contains("<form"), "fixed login page renders its form"); |
| 808 | // Two-segment repo routes keep working and still 404 for a missing repo. | 796 | // Two-segment repo routes keep working and still 404 for a missing repo. |
| 809 | let (status, body) = get(&site.router, "/tahinli/missing", None).await; | 797 | let (status, body) = get(&site.router, "/tahinli/missing", None).await; |
| 810 | - assert_eq!(status, 404); | 798 | + assert_eq!(status, 404, "missing repo"); |
| 811 | - assert!(!body.contains("<article class=\"profile-page\"")); | 799 | + assert!( |
| 812 | - } | 800 | + !body.contains("<article class=\"profile-page\""), |
| 813 | - | 801 | + "missing repo" |
| 814 | - fn count(body: &str, needle: &str) -> usize { | 802 | + ); |
| 815 | - body.matches(needle).count() | ||
| 816 | - } | ||
| 817 | - | ||
| 818 | - /// A user with 35 public repos, `bulk-00` to `bulk-34`. | ||
| 819 | - async fn bulk_site() -> Site { | ||
| 820 | - let site = site().await; | ||
| 821 | - let tahinli = site | ||
| 822 | - .store | ||
| 823 | - .create_user("tahinli", None, "not-a-real-hash", false) | ||
| 824 | - .await | ||
| 825 | - .unwrap(); | ||
| 826 | - for i in 0..35 { | ||
| 827 | - site.store | ||
| 828 | - .create_repo( | ||
| 829 | - &tahinli.id, | ||
| 830 | - &format!("bulk-{i:02}"), | ||
| 831 | - "", | ||
| 832 | - Visibility::Public, | ||
| 833 | - "main", | ||
| 834 | - ) | ||
| 835 | - .await | ||
| 836 | - .unwrap(); | ||
| 837 | - } | ||
| 838 | - site | ||
| 839 | - } | ||
| 840 | - | ||
| 841 | - async fn user_id(store: &Store, name: &str) -> String { | ||
| 842 | - store.user_by_username(name).await.unwrap().unwrap().id | ||
| 843 | } | 803 | } |
| 844 | 804 | ||
| 805 | + /// Stars, the org sidebar and org pins, on one seeded site. The org scenarios use their | ||
| 806 | + /// own org names and session tokens. | ||
| 845 | #[tokio::test] | 807 | #[tokio::test] |
| 846 | - async fn repositories_tab_pages_thirty_at_a_time() { | 808 | + async fn stars_org_sidebar_and_org_pins() { |
| 847 | - let site = bulk_site().await; | ||
| 848 | - let (status, first) = get(&site.router, "/tahinli?tab=repositories&sort=name", None).await; | ||
| 849 | - assert_eq!(status, 200); | ||
| 850 | - assert_eq!(count(&first, "class=\"repo-name\""), 30); | ||
| 851 | - assert!(first.contains("page=2"), "next link: {first}"); | ||
| 852 | - | ||
| 853 | - let (_, second) = get( | ||
| 854 | - &site.router, | ||
| 855 | - "/tahinli?tab=repositories&sort=name&page=2", | ||
| 856 | - None, | ||
| 857 | - ) | ||
| 858 | - .await; | ||
| 859 | - assert_eq!(count(&second, "class=\"repo-name\""), 5); | ||
| 860 | - assert!(second.contains("tahinli/bulk-34")); | ||
| 861 | - assert!(!second.contains("tahinli/bulk-00")); | ||
| 862 | - } | ||
| 863 | - | ||
| 864 | - #[tokio::test] | ||
| 865 | - async fn name_filter_is_a_case_insensitive_substring() { | ||
| 866 | - let site = bulk_site().await; | ||
| 867 | - let (_, body) = get(&site.router, "/tahinli?tab=repositories&q=BULK-1", None).await; | ||
| 868 | - assert_eq!(count(&body, "class=\"repo-name\""), 10); | ||
| 869 | - assert!(body.contains("tahinli/bulk-12")); | ||
| 870 | - assert!(!body.contains("tahinli/bulk-22")); | ||
| 871 | - } | ||
| 872 | - | ||
| 873 | - #[tokio::test] | ||
| 874 | - async fn stars_tab_lists_only_starred_repos_the_viewer_may_read() { | ||
| 875 | let site = seeded().await; | 809 | let site = seeded().await; |
| 810 | + let tahinli = user_id(&site.store, "tahinli").await; | ||
| 811 | + | ||
| 812 | + // The stars tab lists only starred repos the viewer may read. | ||
| 876 | let mira = site | 813 | let mira = site |
| 877 | .store | 814 | .store |
| 878 | .create_user("mira", None, "not-a-real-hash", false) | 815 | .create_user("mira", None, "not-a-real-hash", false) |
| 879 | .await | 816 | .await |
| 880 | .unwrap(); | 817 | .unwrap(); |
| 881 | - let open = site | 818 | + let repos = site.store.list_repos_by_owner(&tahinli).await.unwrap(); |
| 882 | - .store | 819 | + for repo in &repos { |
| 883 | - .list_repos_by_owner(&user_id(&site.store, "tahinli").await) | ||
| 884 | - .await | ||
| 885 | - .unwrap(); | ||
| 886 | - for repo in &open { | ||
| 887 | if repo.name == "open-repo" || repo.name == "secret-repo" { | 820 | if repo.name == "open-repo" || repo.name == "secret-repo" { |
| 888 | site.store.star(&mira.id, &repo.id).await.unwrap(); | 821 | site.store.star(&mira.id, &repo.id).await.unwrap(); |
| 889 | } | 822 | } |
| 890 | } | 823 | } |
| 891 | let (_, anon) = get(&site.router, "/mira?tab=stars", None).await; | 824 | let (_, anon) = get(&site.router, "/mira?tab=stars", None).await; |
| 892 | - assert!(anon.contains("tahinli/open-repo")); | 825 | + assert!( |
| 893 | - assert!(!anon.contains("tahinli/secret-repo")); | 826 | + anon.contains("tahinli/open-repo"), |
| 827 | + "stars: public starred repo" | ||
| 828 | + ); | ||
| 829 | + assert!( | ||
| 830 | + !anon.contains("tahinli/secret-repo"), | ||
| 831 | + "stars: private starred repo" | ||
| 832 | + ); | ||
| 833 | + | ||
| 834 | + // The org sidebar shows Settings to owners only. | ||
| 835 | + let kaan = site | ||
| 836 | + .store | ||
| 837 | + .create_user("kaan", None, "not-a-real-hash", false) | ||
| 838 | + .await | ||
| 839 | + .unwrap(); | ||
| 840 | + let org = site.store.create_org("forge-org", &tahinli).await.unwrap(); | ||
| 841 | + site.store | ||
| 842 | + .add_org_member( | ||
| 843 | + &org.id, | ||
| 844 | + &kaan.id, | ||
| 845 | + forge_core::store::orgs::OrgMemberRole::Member, | ||
| 846 | + ) | ||
| 847 | + .await | ||
| 848 | + .unwrap(); | ||
| 849 | + let owner_cookie = sign_in(&site.store, &tahinli, "org-owner-token").await; | ||
| 850 | + let member_cookie = sign_in(&site.store, &kaan.id, "org-member-token").await; | ||
| 851 | + let href = "href=\"/org/forge-org/settings\""; | ||
| 852 | + let (_, as_owner) = get(&site.router, "/org/forge-org/members", Some(&owner_cookie)).await; | ||
| 853 | + assert!(as_owner.contains(href), "org sidebar: owner sees Settings"); | ||
| 854 | + let (_, as_member) = | ||
| 855 | + get(&site.router, "/org/forge-org/members", Some(&member_cookie)).await; | ||
| 856 | + assert!( | ||
| 857 | + as_member.contains("href=\"/org/forge-org/teams\""), | ||
| 858 | + "org sidebar: member sees Teams" | ||
| 859 | + ); | ||
| 860 | + assert!( | ||
| 861 | + !as_member.contains(href), | ||
| 862 | + "org sidebar: member does not see Settings" | ||
| 863 | + ); | ||
| 864 | + | ||
| 865 | + // Org pins show on the org profile, with an edit link for owners only. | ||
| 866 | + let acme = site.store.create_org("acme", &tahinli).await.unwrap(); | ||
| 867 | + let lib = site | ||
| 868 | + .store | ||
| 869 | + .create_repo(&acme.id, "lib", "", Visibility::Public, "main") | ||
| 870 | + .await | ||
| 871 | + .unwrap(); | ||
| 872 | + site.store | ||
| 873 | + .set_profile_pins(&acme.id, &[lib.id]) | ||
| 874 | + .await | ||
| 875 | + .unwrap(); | ||
| 876 | + let (_, owner) = get(&site.router, "/acme", Some(&owner_cookie)).await; | ||
| 877 | + assert!(owner.contains("acme/lib"), "org pins: owner sees pin"); | ||
| 878 | + assert!( | ||
| 879 | + owner.contains("href=\"/org/acme/settings/pins\""), | ||
| 880 | + "org pins: owner edit link" | ||
| 881 | + ); | ||
| 882 | + let (_, anon) = get(&site.router, "/acme", None).await; | ||
| 883 | + assert!(anon.contains("acme/lib"), "org pins: anonymous sees pin"); | ||
| 884 | + assert!( | ||
| 885 | + !anon.contains("Customize pins"), | ||
| 886 | + "org pins: anonymous has no edit link" | ||
| 887 | + ); | ||
| 894 | } | 888 | } |
| 895 | 889 | ||
| 890 | + /// Pins, the popular-repos fallback and the display name, on one seeded site. The | ||
| 891 | + /// scenarios run in order, and each one sets the pins or name the next one reads. | ||
| 896 | #[tokio::test] | 892 | #[tokio::test] |
| 897 | - async fn pins_show_on_the_overview_and_private_ones_only_to_the_owner() { | 893 | + async fn pins_fallback_and_display_name() { |
| 898 | let site = seeded().await; | 894 | let site = seeded().await; |
| 899 | let tahinli = user_id(&site.store, "tahinli").await; | 895 | let tahinli = user_id(&site.store, "tahinli").await; |
| 900 | let repos = site.store.list_repos_by_owner(&tahinli).await.unwrap(); | 896 | let repos = site.store.list_repos_by_owner(&tahinli).await.unwrap(); |
| 901 | - let open = repos | 897 | + let id_of = |name: &str| repos.iter().find(|r| r.name == name).unwrap().id.clone(); |
| 902 | - .iter() | 898 | + let open = id_of("open-repo"); |
| 903 | - .find(|r| r.name == "open-repo") | 899 | + let secret = id_of("secret-repo"); |
| 904 | - .unwrap() | 900 | + |
| 905 | - .id | 901 | + // Pins: anonymous visitors see the public pin only; the owner sees both. |
| 906 | - .clone(); | ||
| 907 | - let secret = repos | ||
| 908 | - .iter() | ||
| 909 | - .find(|r| r.name == "secret-repo") | ||
| 910 | - .unwrap() | ||
| 911 | - .id | ||
| 912 | - .clone(); | ||
| 913 | site.store | 902 | site.store |
| 914 | - .set_profile_pins(&tahinli, &[open, secret]) | 903 | + .set_profile_pins(&tahinli, &[open, secret.clone()]) |
| 915 | .await | 904 | .await |
| 916 | .unwrap(); | 905 | .unwrap(); |
| 917 | - | ||
| 918 | let (_, anon) = get(&site.router, "/tahinli", None).await; | 906 | let (_, anon) = get(&site.router, "/tahinli", None).await; |
| 919 | - assert!(anon.contains("<h2>Pinned</h2>")); | 907 | + assert!(anon.contains("<h2>Pinned</h2>"), "pins: section shown"); |
| 920 | - assert_eq!(count(&anon, "class=\"pin-card\""), 1); | 908 | + assert_eq!( |
| 921 | - assert!(anon.contains("tahinli/open-repo")); | 909 | + count(&anon, "class=\"pin-card\""), |
| 922 | - assert!(!anon.contains("tahinli/secret-repo")); | 910 | + 1, |
| 923 | - assert!(!anon.contains("Customize pins")); | 911 | + "pins: anonymous pin count" |
| 924 | - | 912 | + ); |
| 925 | - let cookie = sign_in(&site.store, &tahinli, "tahinli-token").await; | 913 | + assert!(anon.contains("tahinli/open-repo"), "pins: public pin"); |
| 914 | + assert!( | ||
| 915 | + !anon.contains("tahinli/secret-repo"), | ||
| 916 | + "pins: private pin hidden" | ||
| 917 | + ); | ||
| 918 | + assert!( | ||
| 919 | + !anon.contains("Customize pins"), | ||
| 920 | + "pins: anonymous has no customize link" | ||
| 921 | + ); | ||
| 922 | + | ||
| 923 | + let cookie = sign_in(&site.store, &tahinli, "pins-owner-token").await; | ||
| 926 | let (_, owner) = get(&site.router, "/tahinli", Some(&cookie)).await; | 924 | let (_, owner) = get(&site.router, "/tahinli", Some(&cookie)).await; |
| 927 | - assert_eq!(count(&owner, "class=\"pin-card\""), 2); | 925 | + assert_eq!( |
| 928 | - assert!(owner.contains("tahinli/secret-repo")); | 926 | + count(&owner, "class=\"pin-card\""), |
| 929 | - assert!(owner.contains("Customize pins")); | 927 | + 2, |
| 930 | - assert!(owner.contains("href=\"/settings/pins\"")); | 928 | + "pins: owner pin count" |
| 931 | - } | 929 | + ); |
| 932 | - | 930 | + assert!( |
| 933 | - #[tokio::test] | 931 | + owner.contains("tahinli/secret-repo"), |
| 934 | - async fn overview_falls_back_to_popular_repos_when_no_pin_is_visible() { | 932 | + "pins: owner sees private pin" |
| 935 | - let site = seeded().await; | 933 | + ); |
| 936 | - let tahinli = user_id(&site.store, "tahinli").await; | 934 | + assert!( |
| 937 | - let secret = site | 935 | + owner.contains("Customize pins"), |
| 938 | - .store | 936 | + "pins: owner customize link" |
| 939 | - .list_repos_by_owner(&tahinli) | 937 | + ); |
| 940 | - .await | 938 | + assert!( |
| 941 | - .unwrap() | 939 | + owner.contains("href=\"/settings/pins\""), |
| 942 | - .into_iter() | 940 | + "pins: owner settings link" |
| 943 | - .find(|r| r.name == "secret-repo") | 941 | + ); |
| 944 | - .unwrap() | 942 | + |
| 945 | - .id; | 943 | + // Fallback: with no visible pin, the overview shows popular repos instead. |
| 946 | site.store | 944 | site.store |
| 947 | .set_profile_pins(&tahinli, &[secret]) | 945 | .set_profile_pins(&tahinli, &[secret]) |
| 948 | .await | 946 | .await |
| 949 | .unwrap(); | 947 | .unwrap(); |
| 950 | - | ||
| 951 | let (_, anon) = get(&site.router, "/tahinli", None).await; | 948 | let (_, anon) = get(&site.router, "/tahinli", None).await; |
| 952 | - assert!(anon.contains("Popular repositories")); | 949 | + assert!(anon.contains("Popular repositories"), "fallback: heading"); |
| 953 | - assert!(anon.contains("tahinli/open-repo")); | 950 | + assert!(anon.contains("tahinli/open-repo"), "fallback: public repo"); |
| 954 | - assert!(!anon.contains("tahinli/secret-repo")); | 951 | + assert!( |
| 955 | - } | 952 | + !anon.contains("tahinli/secret-repo"), |
| 956 | - | 953 | + "fallback: private repo hidden" |
| 957 | - #[tokio::test] | 954 | + ); |
| 958 | - async fn display_name_is_the_heading_with_the_username_below() { | 955 | + |
| 959 | - let site = seeded().await; | 956 | + // Display name: the heading uses it with the username below; a blank name falls back. |
| 960 | - let tahinli = user_id(&site.store, "tahinli").await; | ||
| 961 | site.store | 957 | site.store |
| 962 | .set_display_name(&tahinli, "Ahmet Kaan Gümüş") | 958 | .set_display_name(&tahinli, "Ahmet Kaan Gümüş") |
| 963 | .await | 959 | .await |
| 964 | .unwrap(); | 960 | .unwrap(); |
| 965 | let (status, body) = get(&site.router, "/tahinli", None).await; | 961 | let (status, body) = get(&site.router, "/tahinli", None).await; |
| 966 | - assert_eq!(status, 200); | 962 | + assert_eq!(status, 200, "display name: page"); |
| 967 | - assert!(body.contains("<h1>Ahmet Kaan Gümüş</h1>"), "{body}"); | 963 | + assert!( |
| 968 | - assert!(body.contains("<p class=\"muted\">tahinli</p>"), "{body}"); | 964 | + body.contains("<h1>Ahmet Kaan Gümüş</h1>"), |
| 965 | + "display name: heading {body}" | ||
| 966 | + ); | ||
| 967 | + assert!( | ||
| 968 | + body.contains("<p class=\"muted\">tahinli</p>"), | ||
| 969 | + "display name: username below {body}" | ||
| 970 | + ); | ||
| 969 | 971 | ||
| 970 | site.store.set_display_name(&tahinli, " ").await.unwrap(); | 972 | site.store.set_display_name(&tahinli, " ").await.unwrap(); |
| 971 | let (_, body) = get(&site.router, "/tahinli", None).await; | 973 | let (_, body) = get(&site.router, "/tahinli", None).await; |
| 972 | - assert!(body.contains("<h1>tahinli</h1>"), "{body}"); | 974 | + assert!( |
| 973 | - assert!(!body.contains("<p class=\"muted\">tahinli</p>"), "{body}"); | 975 | + body.contains("<h1>tahinli</h1>"), |
| 976 | + "blank name: heading {body}" | ||
| 977 | + ); | ||
| 978 | + assert!( | ||
| 979 | + !body.contains("<p class=\"muted\">tahinli</p>"), | ||
| 980 | + "blank name: no username line {body}" | ||
| 981 | + ); | ||
| 974 | } | 982 | } |
| 975 | 983 | ||
| 976 | - #[tokio::test] | 984 | + fn count(body: &str, needle: &str) -> usize { |
| 977 | - async fn only_the_owner_sees_the_edit_profile_button() { | 985 | + body.matches(needle).count() |
| 978 | - let site = seeded().await; | 986 | + } |
| 979 | - let tahinli = user_id(&site.store, "tahinli").await; | ||
| 980 | - let cookie = sign_in(&site.store, &tahinli, "tahinli-token").await; | ||
| 981 | - let (_, owner) = get(&site.router, "/tahinli", Some(&cookie)).await; | ||
| 982 | - assert!(owner.contains("class=\"button profile-edit\" href=\"/settings/profile\"")); | ||
| 983 | - | ||
| 984 | - let (_, anon) = get(&site.router, "/tahinli", None).await; | ||
| 985 | - assert!(!anon.contains("profile-edit")); | ||
| 986 | 987 | ||
| 987 | - let bob = site | 988 | + /// A user with 35 public repos, `bulk-00` to `bulk-34`. |
| 989 | + async fn bulk_site() -> Site { | ||
| 990 | + let site = site().await; | ||
| 991 | + let tahinli = site | ||
| 988 | .store | 992 | .store |
| 989 | - .create_user("bob", None, "not-a-real-hash", false) | 993 | + .create_user("tahinli", None, "not-a-real-hash", false) |
| 990 | .await | 994 | .await |
| 991 | .unwrap(); | 995 | .unwrap(); |
| 992 | - let cookie = sign_in(&site.store, &bob.id, "bob-token").await; | 996 | + for i in 0..35 { |
| 993 | - let (_, other) = get(&site.router, "/tahinli", Some(&cookie)).await; | 997 | + site.store |
| 994 | - assert!(!other.contains("profile-edit")); | 998 | + .create_repo( |
| 999 | + &tahinli.id, | ||
| 1000 | + &format!("bulk-{i:02}"), | ||
| 1001 | + "", | ||
| 1002 | + Visibility::Public, | ||
| 1003 | + "main", | ||
| 1004 | + ) | ||
| 1005 | + .await | ||
| 1006 | + .unwrap(); | ||
| 1007 | + } | ||
| 1008 | + site | ||
| 1009 | + } | ||
| 1010 | + | ||
| 1011 | + async fn user_id(store: &Store, name: &str) -> String { | ||
| 1012 | + store.user_by_username(name).await.unwrap().unwrap().id | ||
| 995 | } | 1013 | } |
| 996 | 1014 | ||
| 1015 | + /// Pagination and the name filter on one 35-repo site. | ||
| 997 | #[tokio::test] | 1016 | #[tokio::test] |
| 998 | - async fn org_pins_show_on_the_org_profile_with_an_edit_link_for_owners() { | 1017 | + async fn repositories_tab_paging_and_name_filter() { |
| 999 | - let site = seeded().await; | 1018 | + let site = bulk_site().await; |
| 1000 | - let tahinli = user_id(&site.store, "tahinli").await; | ||
| 1001 | - let acme = site.store.create_org("acme", &tahinli).await.unwrap(); | ||
| 1002 | - let lib = site | ||
| 1003 | - .store | ||
| 1004 | - .create_repo(&acme.id, "lib", "", Visibility::Public, "main") | ||
| 1005 | - .await | ||
| 1006 | - .unwrap(); | ||
| 1007 | - site.store | ||
| 1008 | - .set_profile_pins(&acme.id, &[lib.id]) | ||
| 1009 | - .await | ||
| 1010 | - .unwrap(); | ||
| 1011 | 1019 | ||
| 1012 | - let cookie = sign_in(&site.store, &tahinli, "tahinli-token").await; | 1020 | + // Thirty repos per page, the rest on page two. |
| 1013 | - let (_, owner) = get(&site.router, "/acme", Some(&cookie)).await; | 1021 | + let (status, first) = get(&site.router, "/tahinli?tab=repositories&sort=name", None).await; |
| 1014 | - assert!(owner.contains("acme/lib")); | 1022 | + assert_eq!(status, 200, "paging: first page"); |
| 1015 | - assert!(owner.contains("href=\"/org/acme/settings/pins\"")); | 1023 | + assert_eq!( |
| 1024 | + count(&first, "class=\"repo-name\""), | ||
| 1025 | + 30, | ||
| 1026 | + "paging: first page size" | ||
| 1027 | + ); | ||
| 1028 | + assert!(first.contains("page=2"), "paging: next link: {first}"); | ||
| 1016 | 1029 | ||
| 1017 | - let (_, anon) = get(&site.router, "/acme", None).await; | 1030 | + let (_, second) = get( |
| 1018 | - assert!(anon.contains("acme/lib")); | 1031 | + &site.router, |
| 1019 | - assert!(!anon.contains("Customize pins")); | 1032 | + "/tahinli?tab=repositories&sort=name&page=2", |
| 1033 | + None, | ||
| 1034 | + ) | ||
| 1035 | + .await; | ||
| 1036 | + assert_eq!( | ||
| 1037 | + count(&second, "class=\"repo-name\""), | ||
| 1038 | + 5, | ||
| 1039 | + "paging: second page size" | ||
| 1040 | + ); | ||
| 1041 | + assert!( | ||
| 1042 | + second.contains("tahinli/bulk-34"), | ||
| 1043 | + "paging: last repo on page two" | ||
| 1044 | + ); | ||
| 1045 | + assert!( | ||
| 1046 | + !second.contains("tahinli/bulk-00"), | ||
| 1047 | + "paging: first repo not on page two" | ||
| 1048 | + ); | ||
| 1049 | + | ||
| 1050 | + // The name filter is a case-insensitive substring match. | ||
| 1051 | + let (_, body) = get(&site.router, "/tahinli?tab=repositories&q=BULK-1", None).await; | ||
| 1052 | + assert_eq!( | ||
| 1053 | + count(&body, "class=\"repo-name\""), | ||
| 1054 | + 10, | ||
| 1055 | + "filter: match count" | ||
| 1056 | + ); | ||
| 1057 | + assert!(body.contains("tahinli/bulk-12"), "filter: match"); | ||
| 1058 | + assert!(!body.contains("tahinli/bulk-22"), "filter: non-match"); | ||
| 1020 | } | 1059 | } |
| 1021 | } | 1060 | } |
crates/forge-web/src/pages/profile_pins.rsmodified+75 -55
| @@ -317,10 +317,24 @@ mod tests { | |||
| 317 | .collect() | 317 | .collect() |
| 318 | } | 318 | } |
| 319 | 319 | ||
| 320 | + /// The owner saves pins and the form reflects them. The bad submissions that follow | ||
| 321 | + /// (too many pins, a wrong CSRF token, a foreign repo, another user) must leave the saved | ||
| 322 | + /// pins alone. The scenarios run in order on one seed, and each reads the state before it. | ||
| 320 | #[tokio::test] | 323 | #[tokio::test] |
| 321 | - async fn owner_saves_pins_and_is_sent_back_to_the_profile() { | 324 | + async fn owner_pins_save_and_bad_submissions_leave_them_alone() { |
| 322 | let s = seeded().await; | 325 | let s = seeded().await; |
| 323 | - let cookie = sign_in(&s.site.store, &s.tahinli, "tahinli-token").await; | 326 | + let cookie = sign_in(&s.site.store, &s.tahinli, "pins-owner-token").await; |
| 327 | + let carol = s | ||
| 328 | + .site | ||
| 329 | + .store | ||
| 330 | + .user_by_username("carol") | ||
| 331 | + .await | ||
| 332 | + .unwrap() | ||
| 333 | + .unwrap(); | ||
| 334 | + let carol_cookie = sign_in(&s.site.store, &carol.id, "pins-carol-token").await; | ||
| 335 | + let saved = ["secret-repo", "open-repo"]; | ||
| 336 | + | ||
| 337 | + // Saving redirects to the profile and stores the pins in the chosen order. | ||
| 324 | let reply = send( | 338 | let reply = send( |
| 325 | &s.site, | 339 | &s.site, |
| 326 | "POST", | 340 | "POST", |
| @@ -329,44 +343,51 @@ mod tests { | |||
| 329 | &[("csrf", CSRF), ("repo", &s.secret), ("repo", &s.open)], | 343 | &[("csrf", CSRF), ("repo", &s.secret), ("repo", &s.open)], |
| 330 | ) | 344 | ) |
| 331 | .await; | 345 | .await; |
| 332 | - assert_eq!(reply.status, 303); | 346 | + assert_eq!(reply.status, 303, "save: redirect"); |
| 333 | - assert_eq!(reply.location.as_deref(), Some("/tahinli")); | 347 | + assert_eq!( |
| 348 | + reply.location.as_deref(), | ||
| 349 | + Some("/tahinli"), | ||
| 350 | + "save: location" | ||
| 351 | + ); | ||
| 334 | assert_eq!( | 352 | assert_eq!( |
| 335 | pinned_names(&s.site.store, &s.tahinli).await, | 353 | pinned_names(&s.site.store, &s.tahinli).await, |
| 336 | - ["secret-repo", "open-repo"] | 354 | + saved, |
| 355 | + "save: stored pins" | ||
| 337 | ); | 356 | ); |
| 338 | 357 | ||
| 358 | + // The form ticks the pinned repos. | ||
| 339 | let form = send(&s.site, "GET", "/settings/pins", Some(&cookie), &[]).await; | 359 | let form = send(&s.site, "GET", "/settings/pins", Some(&cookie), &[]).await; |
| 340 | - assert_eq!(form.status, 200); | 360 | + assert_eq!(form.status, 200, "form: status"); |
| 341 | - assert!(form.body.contains("checked"), "pinned repos are ticked"); | 361 | + assert!( |
| 342 | - assert!(form.body.contains("tahinli/open-repo")); | 362 | + form.body.contains("checked"), |
| 343 | - } | 363 | + "form: pinned repos are ticked" |
| 364 | + ); | ||
| 365 | + assert!(form.body.contains("tahinli/open-repo"), "form: lists repos"); | ||
| 344 | 366 | ||
| 345 | - #[tokio::test] | 367 | + // Seven pins is one too many: the form is shown again and the saved pins stay. |
| 346 | - async fn more_than_six_pins_shows_the_error_and_keeps_the_old_pins() { | ||
| 347 | - let s = seeded().await; | ||
| 348 | - let cookie = sign_in(&s.site.store, &s.tahinli, "tahinli-token").await; | ||
| 349 | let repos = s.site.store.list_repos_by_owner(&s.tahinli).await.unwrap(); | 368 | let repos = s.site.store.list_repos_by_owner(&s.tahinli).await.unwrap(); |
| 350 | assert!(repos.len() >= 7, "seed has enough repos"); | 369 | assert!(repos.len() >= 7, "seed has enough repos"); |
| 351 | let ids: Vec<String> = repos.iter().map(|r| r.id.clone()).collect(); | 370 | let ids: Vec<String> = repos.iter().map(|r| r.id.clone()).collect(); |
| 352 | let mut fields: Vec<(&str, &str)> = vec![("csrf", CSRF)]; | 371 | let mut fields: Vec<(&str, &str)> = vec![("csrf", CSRF)]; |
| 353 | fields.extend(ids.iter().map(|id| ("repo", id.as_str()))); | 372 | fields.extend(ids.iter().map(|id| ("repo", id.as_str()))); |
| 354 | - | 373 | + let six = send(&s.site, "POST", "/settings/pins", Some(&cookie), &fields).await; |
| 355 | - let reply = send(&s.site, "POST", "/settings/pins", Some(&cookie), &fields).await; | 374 | + assert_eq!( |
| 356 | - assert_eq!(reply.status, 200, "the form is shown again, not redirected"); | 375 | + six.status, 200, |
| 376 | + "six limit: form shown again, not redirected" | ||
| 377 | + ); | ||
| 357 | assert!( | 378 | assert!( |
| 358 | - reply.body.contains("at most six"), | 379 | + six.body.contains("at most six"), |
| 359 | - "friendly error: {}", | 380 | + "six limit: friendly error: {}", |
| 360 | - reply.body | 381 | + six.body |
| 382 | + ); | ||
| 383 | + assert_eq!( | ||
| 384 | + pinned_names(&s.site.store, &s.tahinli).await, | ||
| 385 | + saved, | ||
| 386 | + "six limit: saved pins kept" | ||
| 361 | ); | 387 | ); |
| 362 | - assert!(pinned_names(&s.site.store, &s.tahinli).await.is_empty()); | ||
| 363 | - } | ||
| 364 | 388 | ||
| 365 | - #[tokio::test] | 389 | + // A wrong CSRF token is refused and nothing changes. |
| 366 | - async fn a_missing_csrf_token_is_refused() { | 390 | + let bad_csrf = send( |
| 367 | - let s = seeded().await; | ||
| 368 | - let cookie = sign_in(&s.site.store, &s.tahinli, "tahinli-token").await; | ||
| 369 | - let reply = send( | ||
| 370 | &s.site, | 391 | &s.site, |
| 371 | "POST", | 392 | "POST", |
| 372 | "/settings/pins", | 393 | "/settings/pins", |
| @@ -374,15 +395,15 @@ mod tests { | |||
| 374 | &[("csrf", "wrong"), ("repo", &s.open)], | 395 | &[("csrf", "wrong"), ("repo", &s.open)], |
| 375 | ) | 396 | ) |
| 376 | .await; | 397 | .await; |
| 377 | - assert_eq!(reply.status, 403); | 398 | + assert_eq!(bad_csrf.status, 403, "csrf: refused"); |
| 378 | - assert!(pinned_names(&s.site.store, &s.tahinli).await.is_empty()); | 399 | + assert_eq!( |
| 379 | - } | 400 | + pinned_names(&s.site.store, &s.tahinli).await, |
| 401 | + saved, | ||
| 402 | + "csrf: saved pins kept" | ||
| 403 | + ); | ||
| 380 | 404 | ||
| 381 | - #[tokio::test] | 405 | + // A repo owned by someone else cannot be pinned, even alongside the owner's own. |
| 382 | - async fn another_users_repo_cannot_be_pinned() { | 406 | + let foreign = send( |
| 383 | - let s = seeded().await; | ||
| 384 | - let cookie = sign_in(&s.site.store, &s.tahinli, "tahinli-token").await; | ||
| 385 | - let reply = send( | ||
| 386 | &s.site, | 407 | &s.site, |
| 387 | "POST", | 408 | "POST", |
| 388 | "/settings/pins", | 409 | "/settings/pins", |
| @@ -390,35 +411,34 @@ mod tests { | |||
| 390 | &[("csrf", CSRF), ("repo", &s.open), ("repo", &s.carols)], | 411 | &[("csrf", CSRF), ("repo", &s.open), ("repo", &s.carols)], |
| 391 | ) | 412 | ) |
| 392 | .await; | 413 | .await; |
| 393 | - assert_ne!(reply.status, 303, "a foreign repo is refused"); | 414 | + assert_ne!(foreign.status, 303, "foreign repo: refused"); |
| 394 | - assert!(pinned_names(&s.site.store, &s.tahinli).await.is_empty()); | 415 | + assert_eq!( |
| 395 | - } | 416 | + pinned_names(&s.site.store, &s.tahinli).await, |
| 417 | + saved, | ||
| 418 | + "foreign repo: saved pins kept" | ||
| 419 | + ); | ||
| 396 | 420 | ||
| 397 | - #[tokio::test] | 421 | + // Carol's form cannot touch tahinli's pins; anonymous visitors are sent to log in. |
| 398 | - async fn only_the_signed_in_user_edits_their_own_pins() { | 422 | + let by_carol = send( |
| 399 | - let s = seeded().await; | ||
| 400 | - let carol = s | ||
| 401 | - .site | ||
| 402 | - .store | ||
| 403 | - .user_by_username("carol") | ||
| 404 | - .await | ||
| 405 | - .unwrap() | ||
| 406 | - .unwrap(); | ||
| 407 | - let cookie = sign_in(&s.site.store, &carol.id, "carol-token").await; | ||
| 408 | - // Carol's form lists her own repos; she cannot reach tahinli's pins through it. | ||
| 409 | - let reply = send( | ||
| 410 | &s.site, | 423 | &s.site, |
| 411 | "POST", | 424 | "POST", |
| 412 | "/settings/pins", | 425 | "/settings/pins", |
| 413 | - Some(&cookie), | 426 | + Some(&carol_cookie), |
| 414 | &[("csrf", CSRF), ("repo", &s.open)], | 427 | &[("csrf", CSRF), ("repo", &s.open)], |
| 415 | ) | 428 | ) |
| 416 | .await; | 429 | .await; |
| 417 | - assert_ne!(reply.status, 303); | 430 | + assert_ne!(by_carol.status, 303, "other user: refused"); |
| 418 | - assert!(pinned_names(&s.site.store, &s.tahinli).await.is_empty()); | 431 | + assert_eq!( |
| 432 | + pinned_names(&s.site.store, &s.tahinli).await, | ||
| 433 | + saved, | ||
| 434 | + "other user: saved pins kept" | ||
| 435 | + ); | ||
| 419 | let anon = send(&s.site, "POST", "/settings/pins", None, &[("csrf", CSRF)]).await; | 436 | let anon = send(&s.site, "POST", "/settings/pins", None, &[("csrf", CSRF)]).await; |
| 420 | - assert_eq!(anon.status, 303, "anonymous visitors are sent to log in"); | 437 | + assert_eq!(anon.status, 303, "anonymous: sent to log in"); |
| 421 | - assert!(anon.location.unwrap_or_default().starts_with("/login")); | 438 | + assert!( |
| 439 | + anon.location.unwrap_or_default().starts_with("/login"), | ||
| 440 | + "anonymous: login" | ||
| 441 | + ); | ||
| 422 | } | 442 | } |
| 423 | 443 | ||
| 424 | #[tokio::test] | 444 | #[tokio::test] |
crates/forge-web/src/pages/settings_appearance.rsmodified+128 -54
| @@ -506,32 +506,79 @@ mod tests { | |||
| 506 | use super::*; | 506 | use super::*; |
| 507 | 507 | ||
| 508 | #[test] | 508 | #[test] |
| 509 | - fn only_offered_choices_pass() { | 509 | + fn offered_choices_pass_and_others_are_refused() { |
| 510 | - assert!(check_choice(Lang::En, "dark", "tr").is_ok()); | 510 | + assert!( |
| 511 | - assert!(check_choice(Lang::Tr, "auto", "auto").is_ok()); | 511 | + check_choice(Lang::En, "dark", "tr").is_ok(), |
| 512 | - assert!(check_choice(Lang::En, "neon", "en").is_err()); | 512 | + "theme: offered" |
| 513 | - assert!(check_choice(Lang::En, "light", "de").is_err()); | 513 | + ); |
| 514 | - assert!(check_choice(Lang::Tr, "light", "").is_err()); | 514 | + assert!( |
| 515 | - assert!(accent_choice(Lang::En, Some("ocean"), "sage").is_ok()); | 515 | + check_choice(Lang::Tr, "auto", "auto").is_ok(), |
| 516 | - assert!(accent_choice(Lang::En, Some("neon"), "sage").is_err()); | 516 | + "theme: auto" |
| 517 | - assert!(accent_choice(Lang::En, Some("Ocean"), "sage").is_err()); | 517 | + ); |
| 518 | - assert!(accent_choice(Lang::En, Some(""), "sage").is_err()); | 518 | + assert!( |
| 519 | - assert!(base_choice(Lang::En, Some("ink"), "fern").is_ok()); | 519 | + check_choice(Lang::En, "neon", "en").is_err(), |
| 520 | - assert!(base_choice(Lang::En, Some("neon"), "fern").is_err()); | 520 | + "theme: unknown" |
| 521 | - assert!(base_choice(Lang::En, Some(""), "fern").is_err()); | 521 | + ); |
| 522 | - } | 522 | + assert!( |
| 523 | + check_choice(Lang::En, "light", "de").is_err(), | ||
| 524 | + "language: unknown" | ||
| 525 | + ); | ||
| 526 | + assert!( | ||
| 527 | + check_choice(Lang::Tr, "light", "").is_err(), | ||
| 528 | + "language: empty" | ||
| 529 | + ); | ||
| 530 | + assert!( | ||
| 531 | + accent_choice(Lang::En, Some("ocean"), "sage").is_ok(), | ||
| 532 | + "accent: offered" | ||
| 533 | + ); | ||
| 534 | + assert!( | ||
| 535 | + accent_choice(Lang::En, Some("neon"), "sage").is_err(), | ||
| 536 | + "accent: unknown" | ||
| 537 | + ); | ||
| 538 | + assert!( | ||
| 539 | + accent_choice(Lang::En, Some("Ocean"), "sage").is_err(), | ||
| 540 | + "accent: wrong case" | ||
| 541 | + ); | ||
| 542 | + assert!( | ||
| 543 | + accent_choice(Lang::En, Some(""), "sage").is_err(), | ||
| 544 | + "accent: empty" | ||
| 545 | + ); | ||
| 546 | + assert!( | ||
| 547 | + base_choice(Lang::En, Some("ink"), "fern").is_ok(), | ||
| 548 | + "base: offered" | ||
| 549 | + ); | ||
| 550 | + assert!( | ||
| 551 | + base_choice(Lang::En, Some("neon"), "fern").is_err(), | ||
| 552 | + "base: unknown" | ||
| 553 | + ); | ||
| 554 | + assert!( | ||
| 555 | + base_choice(Lang::En, Some(""), "fern").is_err(), | ||
| 556 | + "base: empty" | ||
| 557 | + ); | ||
| 523 | 558 | ||
| 524 | - #[test] | ||
| 525 | - fn the_newer_colours_pass_and_their_wrong_cases_do_not() { | ||
| 526 | for id in [ | 559 | for id in [ |
| 527 | "sage", "spark", "ocean", "plum", "rose", "gold", "hue-0", "hue-359", | 560 | "sage", "spark", "ocean", "plum", "rose", "gold", "hue-0", "hue-359", |
| 528 | ] { | 561 | ] { |
| 529 | - assert_eq!(accent_choice(Lang::En, Some(id), "sage").unwrap(), id); | 562 | + assert_eq!( |
| 530 | - assert!(accent_choice(Lang::Tr, Some(&id.to_uppercase()), "sage").is_err()); | 563 | + accent_choice(Lang::En, Some(id), "sage").unwrap(), |
| 564 | + id, | ||
| 565 | + "accent {id}" | ||
| 566 | + ); | ||
| 567 | + assert!( | ||
| 568 | + accent_choice(Lang::Tr, Some(&id.to_uppercase()), "sage").is_err(), | ||
| 569 | + "accent upper case {id}" | ||
| 570 | + ); | ||
| 531 | } | 571 | } |
| 532 | for id in ["fern", "spark", "graphite", "dusk", "ink", "hue-120"] { | 572 | for id in ["fern", "spark", "graphite", "dusk", "ink", "hue-120"] { |
| 533 | - assert_eq!(base_choice(Lang::Tr, Some(id), "fern").unwrap(), id); | 573 | + assert_eq!( |
| 534 | - assert!(base_choice(Lang::Tr, Some(&id.to_uppercase()), "fern").is_err()); | 574 | + base_choice(Lang::Tr, Some(id), "fern").unwrap(), |
| 575 | + id, | ||
| 576 | + "base {id}" | ||
| 577 | + ); | ||
| 578 | + assert!( | ||
| 579 | + base_choice(Lang::Tr, Some(&id.to_uppercase()), "fern").is_err(), | ||
| 580 | + "base upper case {id}" | ||
| 581 | + ); | ||
| 535 | } | 582 | } |
| 536 | // Removed presets, and hue values that are out of range or not canonical, are refused. | 583 | // Removed presets, and hue values that are out of range or not canonical, are refused. |
| 537 | for id in [ | 584 | for id in [ |
| @@ -544,7 +591,10 @@ mod tests { | |||
| 544 | "hue-", | 591 | "hue-", |
| 545 | "hue-+5", | 592 | "hue-+5", |
| 546 | ] { | 593 | ] { |
| 547 | - assert!(accent_choice(Lang::En, Some(id), "sage").is_err(), "{id}"); | 594 | + assert!( |
| 595 | + accent_choice(Lang::En, Some(id), "sage").is_err(), | ||
| 596 | + "accent refused {id}" | ||
| 597 | + ); | ||
| 548 | } | 598 | } |
| 549 | for id in [ | 599 | for id in [ |
| 550 | "dune", | 600 | "dune", |
| @@ -555,14 +605,23 @@ mod tests { | |||
| 555 | "hue-360", | 605 | "hue-360", |
| 556 | "hue-07", | 606 | "hue-07", |
| 557 | ] { | 607 | ] { |
| 558 | - assert!(base_choice(Lang::En, Some(id), "fern").is_err(), "{id}"); | 608 | + assert!( |
| 609 | + base_choice(Lang::En, Some(id), "fern").is_err(), | ||
| 610 | + "base refused {id}" | ||
| 611 | + ); | ||
| 559 | } | 612 | } |
| 560 | - } | ||
| 561 | 613 | ||
| 562 | - #[test] | 614 | + // An absent colour keeps the stored value. |
| 563 | - fn an_absent_colour_keeps_the_stored_value() { | 615 | + assert_eq!( |
| 564 | - assert_eq!(accent_choice(Lang::En, None, "plum").unwrap(), "plum"); | 616 | + accent_choice(Lang::En, None, "plum").unwrap(), |
| 565 | - assert_eq!(base_choice(Lang::En, None, "mist").unwrap(), "mist"); | 617 | + "plum", |
| 618 | + "accent: absent" | ||
| 619 | + ); | ||
| 620 | + assert_eq!( | ||
| 621 | + base_choice(Lang::En, None, "mist").unwrap(), | ||
| 622 | + "mist", | ||
| 623 | + "base: absent" | ||
| 624 | + ); | ||
| 566 | } | 625 | } |
| 567 | 626 | ||
| 568 | #[test] | 627 | #[test] |
| @@ -576,15 +635,17 @@ mod tests { | |||
| 576 | assert!(base_err.to_string().contains("background"), "{base_err}"); | 635 | assert!(base_err.to_string().contains("background"), "{base_err}"); |
| 577 | } | 636 | } |
| 578 | 637 | ||
| 638 | + /// The colour groups mark the stored theme, accent and base as selected. A custom hue | ||
| 639 | + /// fills its stripe and checks no preset; a preset moves the knob to its own hue. | ||
| 579 | #[test] | 640 | #[test] |
| 580 | - fn the_stored_theme_accent_and_base_are_selected() { | 641 | + fn stored_colours_are_selected_and_hues_drive_the_stripes() { |
| 581 | let options = choices(THEME_CODES, "dark", |code| theme_label(Lang::En, code)); | 642 | let options = choices(THEME_CODES, "dark", |code| theme_label(Lang::En, code)); |
| 582 | let selected: Vec<&str> = options | 643 | let selected: Vec<&str> = options |
| 583 | .iter() | 644 | .iter() |
| 584 | .filter(|c| c.selected) | 645 | .filter(|c| c.selected) |
| 585 | .map(|c| c.value) | 646 | .map(|c| c.value) |
| 586 | .collect(); | 647 | .collect(); |
| 587 | - assert_eq!(selected, ["dark"], "theme"); | 648 | + assert_eq!(selected, ["dark"], "theme: selected"); |
| 588 | 649 | ||
| 589 | let codes: Vec<&'static str> = accent::ids().collect(); | 650 | let codes: Vec<&'static str> = accent::ids().collect(); |
| 590 | let options = choices(&codes, "plum", |id| accent_name(Lang::En, id)); | 651 | let options = choices(&codes, "plum", |id| accent_name(Lang::En, id)); |
| @@ -593,8 +654,12 @@ mod tests { | |||
| 593 | .filter(|c| c.selected) | 654 | .filter(|c| c.selected) |
| 594 | .map(|c| c.value) | 655 | .map(|c| c.value) |
| 595 | .collect(); | 656 | .collect(); |
| 596 | - assert_eq!(selected, ["plum"]); | 657 | + assert_eq!(selected, ["plum"], "accent: selected"); |
| 597 | - assert_eq!(options[0].value, crate::accent_palette::DEFAULT); | 658 | + assert_eq!( |
| 659 | + options[0].value, | ||
| 660 | + crate::accent_palette::DEFAULT, | ||
| 661 | + "accent: default first" | ||
| 662 | + ); | ||
| 598 | 663 | ||
| 599 | let group = colour_group(Lang::En, true, "ink", "plum"); | 664 | let group = colour_group(Lang::En, true, "ink", "plum"); |
| 600 | let bases: Vec<&str> = group | 665 | let bases: Vec<&str> = group |
| @@ -603,36 +668,45 @@ mod tests { | |||
| 603 | .filter(|c| c.selected) | 668 | .filter(|c| c.selected) |
| 604 | .map(|c| c.value) | 669 | .map(|c| c.value) |
| 605 | .collect(); | 670 | .collect(); |
| 606 | - assert_eq!(bases, ["ink"]); | 671 | + assert_eq!(bases, ["ink"], "base: selected"); |
| 607 | - assert_eq!(group.bases[0].value, crate::accent_palette::BASE_DEFAULT); | 672 | + assert_eq!( |
| 608 | - } | 673 | + group.bases[0].value, |
| 674 | + crate::accent_palette::BASE_DEFAULT, | ||
| 675 | + "base: default first" | ||
| 676 | + ); | ||
| 609 | 677 | ||
| 610 | - #[test] | 678 | + // A custom hue fills the stripe and checks no preset. |
| 611 | - fn a_stored_hue_checks_custom_and_a_stored_preset_moves_the_knob() { | ||
| 612 | let group = colour_group(Lang::En, false, "hue-200", "hue-40"); | 679 | let group = colour_group(Lang::En, false, "hue-200", "hue-40"); |
| 613 | - assert!(group.base_stripe.custom); | 680 | + assert!(group.base_stripe.custom, "custom base hue"); |
| 614 | - assert_eq!(group.base_stripe.hue, 200); | 681 | + assert_eq!(group.base_stripe.hue, 200, "custom base hue value"); |
| 615 | - assert!(group.accent_stripe.custom); | 682 | + assert!(group.accent_stripe.custom, "custom accent hue"); |
| 616 | - assert_eq!(group.accent_stripe.hue, 40); | 683 | + assert_eq!(group.accent_stripe.hue, 40, "custom accent hue value"); |
| 617 | - assert!(group.bases.iter().all(|c| !c.selected)); | 684 | + assert!( |
| 618 | - assert!(group.accents.iter().all(|c| !c.selected)); | 685 | + group.bases.iter().all(|c| !c.selected), |
| 686 | + "custom hue: no base preset checked" | ||
| 687 | + ); | ||
| 688 | + assert!( | ||
| 689 | + group.accents.iter().all(|c| !c.selected), | ||
| 690 | + "custom hue: no accent preset checked" | ||
| 691 | + ); | ||
| 619 | 692 | ||
| 693 | + // A preset moves the knob to its own hue. | ||
| 620 | let group = colour_group(Lang::En, true, "ink", "plum"); | 694 | let group = colour_group(Lang::En, true, "ink", "plum"); |
| 621 | let plum_hue = preset_accent_hue("plum").unwrap(); | 695 | let plum_hue = preset_accent_hue("plum").unwrap(); |
| 622 | let ink_hue = preset_base_hue("ink").unwrap(); | 696 | let ink_hue = preset_base_hue("ink").unwrap(); |
| 623 | - assert!(!group.accent_stripe.custom); | 697 | + assert!(!group.accent_stripe.custom, "preset accent: not custom"); |
| 624 | - assert_eq!(group.accent_stripe.hue, plum_hue); | 698 | + assert_eq!(group.accent_stripe.hue, plum_hue, "preset accent: knob hue"); |
| 625 | - assert!(!group.base_stripe.custom); | 699 | + assert!(!group.base_stripe.custom, "preset base: not custom"); |
| 626 | - assert_eq!(group.base_stripe.hue, ink_hue); | 700 | + assert_eq!(group.base_stripe.hue, ink_hue, "preset base: knob hue"); |
| 627 | let plum = group.accents.iter().find(|c| c.value == "plum").unwrap(); | 701 | let plum = group.accents.iter().find(|c| c.value == "plum").unwrap(); |
| 628 | - assert!(plum.selected); | 702 | + assert!(plum.selected, "preset accent: checked"); |
| 629 | - assert_eq!(plum.hue, plum_hue.to_string()); | 703 | + assert_eq!(plum.hue, plum_hue.to_string(), "preset accent: hue value"); |
| 630 | - | 704 | + |
| 631 | - assert_eq!(hue_of("hue-359"), Some(359)); | 705 | + assert_eq!(hue_of("hue-359"), Some(359), "hue: max"); |
| 632 | - assert_eq!(hue_of("hue-360"), None); | 706 | + assert_eq!(hue_of("hue-360"), None, "hue: out of range"); |
| 633 | - assert_eq!(hue_of("hue-"), None); | 707 | + assert_eq!(hue_of("hue-"), None, "hue: empty"); |
| 634 | - assert_eq!(hue_of("hue-+5"), None); | 708 | + assert_eq!(hue_of("hue-+5"), None, "hue: sign"); |
| 635 | - assert_eq!(hue_of("sage"), None); | 709 | + assert_eq!(hue_of("sage"), None, "hue: preset id"); |
| 636 | } | 710 | } |
| 637 | 711 | ||
| 638 | #[test] | 712 | #[test] |
crates/forge-web/src/pages/settings_live.rsmodified+69 -48
| @@ -179,20 +179,10 @@ mod tests { | |||
| 179 | 179 | ||
| 180 | const CSRF: &str = "settings-live-csrf"; | 180 | const CSRF: &str = "settings-live-csrf"; |
| 181 | 181 | ||
| 182 | + /// The settings stream needs a session, and the settings pages name it (and not the old | ||
| 183 | + /// theme stream). | ||
| 182 | #[tokio::test] | 184 | #[tokio::test] |
| 183 | - async fn the_stream_needs_a_session() { | 185 | + async fn the_settings_stream_needs_a_session_and_the_sidebar_names_it() { |
| 184 | - let dir = tempfile::tempdir().expect("tempdir"); | ||
| 185 | - let router = app_router(test_state(dir.path()).await); | ||
| 186 | - let request = Request::builder() | ||
| 187 | - .method("GET") | ||
| 188 | - .uri("/settings/events") | ||
| 189 | - .body(Body::empty()) | ||
| 190 | - .expect("request"); | ||
| 191 | - assert_eq!(router.handle(request).await.status().as_u16(), 401); | ||
| 192 | - } | ||
| 193 | - | ||
| 194 | - #[tokio::test] | ||
| 195 | - async fn the_settings_sidebar_names_the_settings_stream() { | ||
| 196 | let dir = tempfile::tempdir().expect("tempdir"); | 186 | let dir = tempfile::tempdir().expect("tempdir"); |
| 197 | let state = test_state(dir.path()).await; | 187 | let state = test_state(dir.path()).await; |
| 198 | let store = state.store.clone(); | 188 | let store = state.store.clone(); |
| @@ -203,6 +193,17 @@ mod tests { | |||
| 203 | create_session(&store, &owner.id, "owner-token", CSRF).await; | 193 | create_session(&store, &owner.id, "owner-token", CSRF).await; |
| 204 | let router = app_router(state); | 194 | let router = app_router(state); |
| 205 | 195 | ||
| 196 | + let request = Request::builder() | ||
| 197 | + .method("GET") | ||
| 198 | + .uri("/settings/events") | ||
| 199 | + .body(Body::empty()) | ||
| 200 | + .expect("request"); | ||
| 201 | + assert_eq!( | ||
| 202 | + router.handle(request).await.status().as_u16(), | ||
| 203 | + 401, | ||
| 204 | + "signed out: stream refused" | ||
| 205 | + ); | ||
| 206 | + | ||
| 206 | let page = Request::builder() | 207 | let page = Request::builder() |
| 207 | .method("GET") | 208 | .method("GET") |
| 208 | .uri("/settings/profile") | 209 | .uri("/settings/profile") |
| @@ -213,8 +214,14 @@ mod tests { | |||
| 213 | let html = | 214 | let html = |
| 214 | String::from_utf8_lossy(&to_bytes(res.into_body(), usize::MAX).await.expect("body")) | 215 | String::from_utf8_lossy(&to_bytes(res.into_body(), usize::MAX).await.expect("body")) |
| 215 | .into_owned(); | 216 | .into_owned(); |
| 216 | - assert!(html.contains("data-live=\"/settings/events\"")); | 217 | + assert!( |
| 217 | - assert!(!html.contains("/settings/theme/events")); | 218 | + html.contains("data-live=\"/settings/events\""), |
| 219 | + "sidebar names the stream" | ||
| 220 | + ); | ||
| 221 | + assert!( | ||
| 222 | + !html.contains("/settings/theme/events"), | ||
| 223 | + "old theme stream is gone" | ||
| 224 | + ); | ||
| 218 | } | 225 | } |
| 219 | 226 | ||
| 220 | #[tokio::test] | 227 | #[tokio::test] |
| @@ -247,12 +254,22 @@ mod tests { | |||
| 247 | let res = router.handle(request).await; | 254 | let res = router.handle(request).await; |
| 248 | to_bytes(res.into_body(), usize::MAX).await.expect("body"); | 255 | to_bytes(res.into_body(), usize::MAX).await.expect("body"); |
| 249 | 256 | ||
| 250 | - assert!(owner_wake.has_changed().expect("open")); | 257 | + assert!( |
| 251 | - assert!(!other_wake.has_changed().expect("open")); | 258 | + owner_wake.has_changed().expect("open"), |
| 259 | + "owner's topic woken" | ||
| 260 | + ); | ||
| 261 | + assert!( | ||
| 262 | + !other_wake.has_changed().expect("open"), | ||
| 263 | + "other user's topic untouched" | ||
| 264 | + ); | ||
| 252 | } | 265 | } |
| 253 | 266 | ||
| 267 | + /// Two live streams on one app. The settings stream sends one changed event per write to | ||
| 268 | + /// the account. The alive stream sends the appearance only when it changes: a bump that | ||
| 269 | + /// changes nothing sends nothing, and a theme and language change sends the theme event | ||
| 270 | + /// first, then the language event. | ||
| 254 | #[tokio::test] | 271 | #[tokio::test] |
| 255 | - async fn the_settings_stream_sends_a_changed_event_per_write() { | 272 | + async fn the_settings_stream_sends_changes_and_the_alive_stream_sends_appearance() { |
| 256 | let dir = tempfile::tempdir().expect("tempdir"); | 273 | let dir = tempfile::tempdir().expect("tempdir"); |
| 257 | let state = test_state(dir.path()).await; | 274 | let state = test_state(dir.path()).await; |
| 258 | let store = state.store.clone(); | 275 | let store = state.store.clone(); |
| @@ -262,8 +279,13 @@ mod tests { | |||
| 262 | .await | 279 | .await |
| 263 | .expect("user"); | 280 | .expect("user"); |
| 264 | create_session(&store, &owner.id, "owner-token", CSRF).await; | 281 | create_session(&store, &owner.id, "owner-token", CSRF).await; |
| 282 | + store | ||
| 283 | + .set_ui_prefs(&owner.id, "light", "en") | ||
| 284 | + .await | ||
| 285 | + .expect("prefs"); | ||
| 265 | let router = app_router(state); | 286 | let router = app_router(state); |
| 266 | 287 | ||
| 288 | + // Settings stream: a retry hint first, then one changed event per write. | ||
| 267 | let request = Request::builder() | 289 | let request = Request::builder() |
| 268 | .method("GET") | 290 | .method("GET") |
| 269 | .uri("/settings/events") | 291 | .uri("/settings/events") |
| @@ -271,33 +293,23 @@ mod tests { | |||
| 271 | .body(Body::empty()) | 293 | .body(Body::empty()) |
| 272 | .expect("request"); | 294 | .expect("request"); |
| 273 | let res = router.handle(request).await; | 295 | let res = router.handle(request).await; |
| 274 | - assert_eq!(res.status().as_u16(), 200); | 296 | + assert_eq!(res.status().as_u16(), 200, "settings stream: status"); |
| 275 | let mut frames = res.into_body().into_data_stream(); | 297 | let mut frames = res.into_body().into_data_stream(); |
| 276 | let retry = frames.next().await.expect("a frame").expect("data"); | 298 | let retry = frames.next().await.expect("a frame").expect("data"); |
| 277 | - assert!(retry.starts_with(b"retry: 1000"), "{retry:?}"); | 299 | + assert!( |
| 300 | + retry.starts_with(b"retry: 1000"), | ||
| 301 | + "settings stream: {retry:?}" | ||
| 302 | + ); | ||
| 278 | 303 | ||
| 279 | live.bump(&Topic::Account(owner.id.clone())); | 304 | live.bump(&Topic::Account(owner.id.clone())); |
| 280 | let changed = frames.next().await.expect("a frame").expect("data"); | 305 | let changed = frames.next().await.expect("a frame").expect("data"); |
| 281 | - assert_eq!(&changed[..], b"event: changed\ndata: 1\n\n"); | 306 | + assert_eq!( |
| 282 | - } | 307 | + &changed[..], |
| 283 | - | 308 | + b"event: changed\ndata: 1\n\n", |
| 284 | - #[tokio::test] | 309 | + "settings stream: changed event" |
| 285 | - async fn the_alive_stream_sends_the_appearance_only_when_it_changes() { | 310 | + ); |
| 286 | - let dir = tempfile::tempdir().expect("tempdir"); | ||
| 287 | - let state = test_state(dir.path()).await; | ||
| 288 | - let store = state.store.clone(); | ||
| 289 | - let live = state.live.clone(); | ||
| 290 | - let owner = store | ||
| 291 | - .create_user("tahinli", None, "h", false) | ||
| 292 | - .await | ||
| 293 | - .expect("user"); | ||
| 294 | - create_session(&store, &owner.id, "owner-token", CSRF).await; | ||
| 295 | - store | ||
| 296 | - .set_ui_prefs(&owner.id, "light", "en") | ||
| 297 | - .await | ||
| 298 | - .expect("prefs"); | ||
| 299 | - let router = app_router(state); | ||
| 300 | 311 | ||
| 312 | + // Alive stream: no event for a bump that changes no appearance. | ||
| 301 | let request = Request::builder() | 313 | let request = Request::builder() |
| 302 | .method("GET") | 314 | .method("GET") |
| 303 | .uri("/-/alive") | 315 | .uri("/-/alive") |
| @@ -305,11 +317,10 @@ mod tests { | |||
| 305 | .body(Body::empty()) | 317 | .body(Body::empty()) |
| 306 | .expect("request"); | 318 | .expect("request"); |
| 307 | let res = router.handle(request).await; | 319 | let res = router.handle(request).await; |
| 308 | - let mut frames = res.into_body().into_data_stream(); | 320 | + let mut alive = res.into_body().into_data_stream(); |
| 309 | - let retry = frames.next().await.expect("a frame").expect("data"); | 321 | + let retry = alive.next().await.expect("a frame").expect("data"); |
| 310 | - assert!(retry.starts_with(b"retry: 1000"), "{retry:?}"); | 322 | + assert!(retry.starts_with(b"retry: 1000"), "alive stream: {retry:?}"); |
| 311 | 323 | ||
| 312 | - // A bump that changes no appearance sends nothing; the next change is the theme event. | ||
| 313 | live.bump(&Topic::Account(owner.id.clone())); | 324 | live.bump(&Topic::Account(owner.id.clone())); |
| 314 | // One change of theme and language: the theme event first, then the language event. | 325 | // One change of theme and language: the theme event first, then the language event. |
| 315 | store | 326 | store |
| @@ -317,11 +328,21 @@ mod tests { | |||
| 317 | .await | 328 | .await |
| 318 | .expect("prefs"); | 329 | .expect("prefs"); |
| 319 | live.bump(&Topic::Account(owner.id.clone())); | 330 | live.bump(&Topic::Account(owner.id.clone())); |
| 320 | - let event = frames.next().await.expect("a frame").expect("data"); | 331 | + let event = alive.next().await.expect("a frame").expect("data"); |
| 321 | let text = String::from_utf8_lossy(&event).into_owned(); | 332 | let text = String::from_utf8_lossy(&event).into_owned(); |
| 322 | - assert!(text.starts_with("event: theme\n"), "{text}"); | 333 | + assert!( |
| 323 | - assert!(text.contains("data-theme=dark;"), "{text}"); | 334 | + text.starts_with("event: theme\n"), |
| 324 | - let language = frames.next().await.expect("a frame").expect("data"); | 335 | + "alive stream: theme first {text}" |
| 325 | - assert_eq!(&language[..], b"event: language\ndata: 1\n\n"); | 336 | + ); |
| 337 | + assert!( | ||
| 338 | + text.contains("data-theme=dark;"), | ||
| 339 | + "alive stream: theme value {text}" | ||
| 340 | + ); | ||
| 341 | + let language = alive.next().await.expect("a frame").expect("data"); | ||
| 342 | + assert_eq!( | ||
| 343 | + &language[..], | ||
| 344 | + b"event: language\ndata: 1\n\n", | ||
| 345 | + "alive stream: language event" | ||
| 346 | + ); | ||
| 326 | } | 347 | } |
| 327 | } | 348 | } |
crates/forge-web/src/pages/settings_notifications.rsmodified+22 -23
| @@ -288,17 +288,18 @@ mod tests { | |||
| 288 | .email_notifications | 288 | .email_notifications |
| 289 | } | 289 | } |
| 290 | 290 | ||
| 291 | + /// The page needs a session, and once signed in it shows the box and its notes. The | ||
| 292 | + /// box saves itself but is locked, because this test server sends no email. | ||
| 291 | #[tokio::test] | 293 | #[tokio::test] |
| 292 | - async fn the_page_needs_a_session() { | 294 | + async fn the_page_needs_a_session_then_shows_the_checkbox_and_notes() { |
| 293 | let fx = fixture().await; | 295 | let fx = fixture().await; |
| 294 | let (status, body) = send(&fx.router, "GET", "/settings/notifications", None, None).await; | 296 | let (status, body) = send(&fx.router, "GET", "/settings/notifications", None, None).await; |
| 295 | - assert_ne!(status, 200, "{body}"); | 297 | + assert_ne!(status, 200, "signed out: {body}"); |
| 296 | - assert!(!body.contains("Email me about notifications")); | 298 | + assert!( |
| 297 | - } | 299 | + !body.contains("Email me about notifications"), |
| 300 | + "signed out: no box" | ||
| 301 | + ); | ||
| 298 | 302 | ||
| 299 | - #[tokio::test] | ||
| 300 | - async fn the_page_shows_the_checkbox_and_the_notes() { | ||
| 301 | - let fx = fixture().await; | ||
| 302 | let (status, body) = send( | 303 | let (status, body) = send( |
| 303 | &fx.router, | 304 | &fx.router, |
| 304 | "GET", | 305 | "GET", |
| @@ -324,48 +325,46 @@ mod tests { | |||
| 324 | ); | 325 | ); |
| 325 | } | 326 | } |
| 326 | 327 | ||
| 328 | + /// The flag starts on. A wrong CSRF token changes nothing; a valid form turns it off and | ||
| 329 | + /// back on. The scenarios run in order on one fixture. | ||
| 327 | #[tokio::test] | 330 | #[tokio::test] |
| 328 | - async fn the_checkbox_toggles_the_flag() { | 331 | + async fn the_checkbox_toggles_the_flag_and_a_wrong_csrf_token_changes_nothing() { |
| 329 | let fx = fixture().await; | 332 | let fx = fixture().await; |
| 330 | assert!(flag(&fx).await, "on by default"); | 333 | assert!(flag(&fx).await, "on by default"); |
| 331 | 334 | ||
| 332 | - let off = format!("csrf={CSRF}"); | ||
| 333 | let (status, _) = send( | 335 | let (status, _) = send( |
| 334 | &fx.router, | 336 | &fx.router, |
| 335 | "POST", | 337 | "POST", |
| 336 | "/settings/notifications", | 338 | "/settings/notifications", |
| 337 | Some(TOKEN), | 339 | Some(TOKEN), |
| 338 | - Some(off), | 340 | + Some("csrf=wrong".to_owned()), |
| 339 | ) | 341 | ) |
| 340 | .await; | 342 | .await; |
| 341 | - assert_eq!(status, 303); | 343 | + assert_ne!(status, 303, "wrong csrf: refused"); |
| 342 | - assert!(!flag(&fx).await, "an unchecked box turns it off"); | 344 | + assert!(flag(&fx).await, "wrong csrf: still on"); |
| 343 | 345 | ||
| 344 | - let on = format!("csrf={CSRF}&email_notifications=1"); | 346 | + let off = format!("csrf={CSRF}"); |
| 345 | let (status, _) = send( | 347 | let (status, _) = send( |
| 346 | &fx.router, | 348 | &fx.router, |
| 347 | "POST", | 349 | "POST", |
| 348 | "/settings/notifications", | 350 | "/settings/notifications", |
| 349 | Some(TOKEN), | 351 | Some(TOKEN), |
| 350 | - Some(on), | 352 | + Some(off), |
| 351 | ) | 353 | ) |
| 352 | .await; | 354 | .await; |
| 353 | - assert_eq!(status, 303); | 355 | + assert_eq!(status, 303, "unchecked: status"); |
| 354 | - assert!(flag(&fx).await); | 356 | + assert!(!flag(&fx).await, "an unchecked box turns it off"); |
| 355 | - } | ||
| 356 | 357 | ||
| 357 | - #[tokio::test] | 358 | + let on = format!("csrf={CSRF}&email_notifications=1"); |
| 358 | - async fn a_wrong_csrf_token_changes_nothing() { | ||
| 359 | - let fx = fixture().await; | ||
| 360 | let (status, _) = send( | 359 | let (status, _) = send( |
| 361 | &fx.router, | 360 | &fx.router, |
| 362 | "POST", | 361 | "POST", |
| 363 | "/settings/notifications", | 362 | "/settings/notifications", |
| 364 | Some(TOKEN), | 363 | Some(TOKEN), |
| 365 | - Some("csrf=wrong".to_owned()), | 364 | + Some(on), |
| 366 | ) | 365 | ) |
| 367 | .await; | 366 | .await; |
| 368 | - assert_ne!(status, 303); | 367 | + assert_eq!(status, 303, "checked: status"); |
| 369 | - assert!(flag(&fx).await, "still on"); | 368 | + assert!(flag(&fx).await, "a checked box turns it on"); |
| 370 | } | 369 | } |
| 371 | } | 370 | } |
crates/forge-web/src/pages/settings_profile.rsmodified+217 -162
| @@ -608,170 +608,159 @@ pub(crate) mod tests { | |||
| 608 | .await | 608 | .await |
| 609 | } | 609 | } |
| 610 | 610 | ||
| 611 | + /// The profile fields the form can change, read back from the store. | ||
| 612 | + async fn saved_fields(fx: &Fixture) -> (String, String, String, Option<String>) { | ||
| 613 | + let user = fx.store.user_by_id(&fx.user.id).await.unwrap().unwrap(); | ||
| 614 | + (user.username, user.about, user.timezone, user.display_name) | ||
| 615 | + } | ||
| 616 | + | ||
| 617 | + /// The profile form: round trips, the saved flash, and every rejection leaves the stored | ||
| 618 | + /// fields alone. The scenarios run in order on one fixture, and each rejection compares | ||
| 619 | + /// against the fields the step before it left behind. | ||
| 611 | #[tokio::test] | 620 | #[tokio::test] |
| 612 | - async fn one_save_changes_capitalization_about_and_timezone() { | 621 | + async fn profile_form_saves_and_rejects_bad_input() { |
| 613 | let fx = fixture().await; | 622 | let fx = fixture().await; |
| 623 | + | ||
| 624 | + // About and timezone round-trip through the form and show on the page. | ||
| 614 | let form = | 625 | let form = |
| 615 | - format!("csrf={CSRF}&username=Tahinli&about=Hello+*world*&timezone=Europe%2FIstanbul"); | 626 | + format!("csrf={CSRF}&username=tahinli&about=Hello+*world*&timezone=Europe%2FIstanbul"); |
| 616 | let (status, location, _) = | 627 | let (status, location, _) = |
| 617 | send(&fx.router, "POST", "/settings/profile", true, Some(&form)).await; | 628 | send(&fx.router, "POST", "/settings/profile", true, Some(&form)).await; |
| 618 | - assert_eq!(status, 303); | 629 | + assert_eq!(status, 303, "round trip: status"); |
| 619 | - assert_eq!(location.as_deref(), Some("/settings/profile?saved=1")); | 630 | + assert_eq!( |
| 620 | - | 631 | + location.as_deref(), |
| 621 | - let stored = fx.store.user_by_id(&fx.user.id).await.unwrap().unwrap(); | 632 | + Some("/settings/profile?saved=1"), |
| 622 | - assert_eq!(stored.username, "Tahinli"); | 633 | + "round trip: location" |
| 623 | - assert_eq!(stored.about, "Hello *world*"); | 634 | + ); |
| 624 | - assert_eq!(stored.timezone, "Europe/Istanbul"); | 635 | + let got = saved_fields(&fx).await; |
| 636 | + assert_eq!(got.0, "tahinli", "round trip: username"); | ||
| 637 | + assert_eq!(got.1, "Hello *world*", "round trip: about"); | ||
| 638 | + assert_eq!(got.2, "Europe/Istanbul", "round trip: timezone"); | ||
| 639 | + let (status, _, body) = send(&fx.router, "GET", "/settings/profile", true, None).await; | ||
| 640 | + assert_eq!(status, 200, "round trip: page"); | ||
| 641 | + assert!( | ||
| 642 | + body.contains("Hello *world*"), | ||
| 643 | + "round trip: about shown {body}" | ||
| 644 | + ); | ||
| 645 | + assert!( | ||
| 646 | + body.contains(r#"value="Europe/Istanbul" selected"#), | ||
| 647 | + "round trip: timezone shown {body}" | ||
| 648 | + ); | ||
| 625 | 649 | ||
| 650 | + // A capitalization-only change is saved, shows in the nav and gets the saved flash. | ||
| 651 | + let form = | ||
| 652 | + format!("csrf={CSRF}&username=Tahinli&about=Hello+*world*&timezone=Europe%2FIstanbul"); | ||
| 653 | + let (status, location, _) = | ||
| 654 | + send(&fx.router, "POST", "/settings/profile", true, Some(&form)).await; | ||
| 655 | + assert_eq!(status, 303, "capitalization: status"); | ||
| 656 | + assert_eq!( | ||
| 657 | + location.as_deref(), | ||
| 658 | + Some("/settings/profile?saved=1"), | ||
| 659 | + "capitalization: location" | ||
| 660 | + ); | ||
| 661 | + assert_eq!( | ||
| 662 | + saved_fields(&fx).await.0, | ||
| 663 | + "Tahinli", | ||
| 664 | + "capitalization: stored" | ||
| 665 | + ); | ||
| 626 | let (status, _, body) = send(&fx.router, "GET", "/", true, None).await; | 666 | let (status, _, body) = send(&fx.router, "GET", "/", true, None).await; |
| 627 | - assert_eq!(status, 200); | 667 | + assert_eq!(status, 200, "capitalization: nav"); |
| 628 | - assert!(body.contains("<summary aria-label=\"Tahinli\">"), "{body}"); | 668 | + assert!( |
| 629 | - | 669 | + body.contains("<summary aria-label=\"Tahinli\">"), |
| 670 | + "capitalization: nav {body}" | ||
| 671 | + ); | ||
| 630 | let (status, _, body) = | 672 | let (status, _, body) = |
| 631 | send(&fx.router, "GET", "/settings/profile?saved=1", true, None).await; | 673 | send(&fx.router, "GET", "/settings/profile?saved=1", true, None).await; |
| 632 | - assert_eq!(status, 200); | 674 | + assert_eq!(status, 200, "capitalization: flash page"); |
| 633 | - assert!(body.contains("Profile saved."), "{body}"); | 675 | + assert!( |
| 634 | - } | 676 | + body.contains("Profile saved."), |
| 677 | + "capitalization: flash {body}" | ||
| 678 | + ); | ||
| 635 | 679 | ||
| 636 | - #[tokio::test] | 680 | + // A different name is rejected and nothing is saved. |
| 637 | - async fn a_different_name_is_rejected_and_nothing_is_saved() { | 681 | + let before = saved_fields(&fx).await; |
| 638 | - let fx = fixture().await; | ||
| 639 | let form = format!("csrf={CSRF}&username=other&about=kept&timezone=UTC"); | 682 | let form = format!("csrf={CSRF}&username=other&about=kept&timezone=UTC"); |
| 640 | let (status, _, body) = | 683 | let (status, _, body) = |
| 641 | send(&fx.router, "POST", "/settings/profile", true, Some(&form)).await; | 684 | send(&fx.router, "POST", "/settings/profile", true, Some(&form)).await; |
| 642 | - assert_eq!(status, 200); | 685 | + assert_eq!(status, 200, "different name: status"); |
| 643 | assert!( | 686 | assert!( |
| 644 | body.contains("Only the capitalization can be changed."), | 687 | body.contains("Only the capitalization can be changed."), |
| 645 | - "{body}" | 688 | + "different name: {body}" |
| 646 | ); | 689 | ); |
| 647 | - let stored = fx.store.user_by_id(&fx.user.id).await.unwrap().unwrap(); | 690 | + assert_eq!( |
| 648 | - assert_eq!(stored.username, "tahinli"); | 691 | + saved_fields(&fx).await, |
| 649 | - assert_eq!(stored.about, ""); | 692 | + before, |
| 650 | - } | 693 | + "different name: nothing saved" |
| 651 | - | ||
| 652 | - #[tokio::test] | ||
| 653 | - async fn post_without_a_valid_csrf_token_is_rejected() { | ||
| 654 | - let fx = fixture().await; | ||
| 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 | - } | ||
| 668 | - let stored = fx.store.user_by_id(&fx.user.id).await.unwrap().unwrap(); | ||
| 669 | - assert_eq!(stored.username, "tahinli"); | ||
| 670 | - assert_eq!(stored.about, ""); | ||
| 671 | - } | ||
| 672 | - | ||
| 673 | - #[tokio::test] | ||
| 674 | - async fn anonymous_requests_are_sent_to_login() { | ||
| 675 | - let fx = fixture().await; | ||
| 676 | - let (status, location, _) = send(&fx.router, "GET", "/settings/profile", false, None).await; | ||
| 677 | - assert_eq!(status, 303); | ||
| 678 | - assert!(location.unwrap_or_default().starts_with("/login?next=")); | ||
| 679 | - | ||
| 680 | - let form = format!("csrf={CSRF}&username=Tahinli"); | ||
| 681 | - let (status, location, _) = | ||
| 682 | - send(&fx.router, "POST", "/settings/profile", false, Some(&form)).await; | ||
| 683 | - assert_eq!(status, 303); | ||
| 684 | - assert!(location.unwrap_or_default().starts_with("/login?next=")); | ||
| 685 | - let stored = fx.store.user_by_id(&fx.user.id).await.unwrap().unwrap(); | ||
| 686 | - assert_eq!(stored.username, "tahinli"); | ||
| 687 | - } | ||
| 688 | - | ||
| 689 | - #[tokio::test] | ||
| 690 | - async fn about_and_timezone_round_trip() { | ||
| 691 | - let fx = fixture().await; | ||
| 692 | - let form = | ||
| 693 | - format!("csrf={CSRF}&username=tahinli&about=Hello+*world*&timezone=Europe%2FIstanbul"); | ||
| 694 | - let (status, location, _) = | ||
| 695 | - send(&fx.router, "POST", "/settings/profile", true, Some(&form)).await; | ||
| 696 | - assert_eq!(status, 303); | ||
| 697 | - assert_eq!(location.as_deref(), Some("/settings/profile?saved=1")); | ||
| 698 | - | ||
| 699 | - let stored = fx.store.user_by_id(&fx.user.id).await.unwrap().unwrap(); | ||
| 700 | - assert_eq!(stored.username, "tahinli"); | ||
| 701 | - assert_eq!(stored.about, "Hello *world*"); | ||
| 702 | - assert_eq!(stored.timezone, "Europe/Istanbul"); | ||
| 703 | - | ||
| 704 | - let (status, _, body) = send(&fx.router, "GET", "/settings/profile", true, None).await; | ||
| 705 | - assert_eq!(status, 200); | ||
| 706 | - assert!(body.contains("Hello *world*"), "{body}"); | ||
| 707 | - assert!( | ||
| 708 | - body.contains(r#"value="Europe/Istanbul" selected"#), | ||
| 709 | - "{body}" | ||
| 710 | ); | 694 | ); |
| 711 | - } | ||
| 712 | 695 | ||
| 713 | - #[tokio::test] | 696 | + // A bad timezone is rejected and nothing is saved. |
| 714 | - async fn a_bad_timezone_is_rejected_and_nothing_is_saved() { | 697 | + let before = saved_fields(&fx).await; |
| 715 | - let fx = fixture().await; | ||
| 716 | let form = format!("csrf={CSRF}&username=Tahinli&about=kept&timezone=Mars%2FOlympus"); | 698 | let form = format!("csrf={CSRF}&username=Tahinli&about=kept&timezone=Mars%2FOlympus"); |
| 717 | let (status, location, body) = | 699 | let (status, location, body) = |
| 718 | send(&fx.router, "POST", "/settings/profile", true, Some(&form)).await; | 700 | send(&fx.router, "POST", "/settings/profile", true, Some(&form)).await; |
| 719 | - assert_eq!(status, 200); | 701 | + assert_eq!(status, 200, "bad timezone: status"); |
| 720 | - assert!(location.is_none()); | 702 | + assert!(location.is_none(), "bad timezone: no redirect"); |
| 721 | - assert!(body.contains(r#"role="alert""#), "{body}"); | 703 | + assert!( |
| 722 | - // The valid username and about text wait for the timezone to be fixed too. | 704 | + body.contains(r#"role="alert""#), |
| 723 | - let stored = fx.store.user_by_id(&fx.user.id).await.unwrap().unwrap(); | 705 | + "bad timezone: alert {body}" |
| 724 | - assert_eq!(stored.username, "tahinli"); | 706 | + ); |
| 725 | - assert_eq!(stored.timezone, "UTC"); | 707 | + assert_eq!( |
| 726 | - assert_eq!(stored.about, ""); | 708 | + saved_fields(&fx).await, |
| 727 | - } | 709 | + before, |
| 710 | + "bad timezone: nothing saved" | ||
| 711 | + ); | ||
| 728 | 712 | ||
| 729 | - #[tokio::test] | 713 | + // A display name saves, shows in the field, and a blank one clears it. |
| 730 | - async fn display_name_saves_shows_in_the_field_and_blank_clears_it() { | ||
| 731 | - let fx = fixture().await; | ||
| 732 | let form = format!( | 714 | let form = format!( |
| 733 | "csrf={CSRF}&display_name=+Ahmet+Kaan+G%C3%BCm%C3%BC%C5%9F+&username=tahinli&about=&timezone=UTC" | 715 | "csrf={CSRF}&display_name=+Ahmet+Kaan+G%C3%BCm%C3%BC%C5%9F+&username=tahinli&about=&timezone=UTC" |
| 734 | ); | 716 | ); |
| 735 | let (status, location, _) = | 717 | let (status, location, _) = |
| 736 | send(&fx.router, "POST", "/settings/profile", true, Some(&form)).await; | 718 | send(&fx.router, "POST", "/settings/profile", true, Some(&form)).await; |
| 737 | - assert_eq!(status, 303); | 719 | + assert_eq!(status, 303, "display name: status"); |
| 738 | - assert_eq!(location.as_deref(), Some("/settings/profile?saved=1")); | 720 | + assert_eq!( |
| 739 | - let stored = fx.store.user_by_id(&fx.user.id).await.unwrap().unwrap(); | 721 | + location.as_deref(), |
| 740 | - assert_eq!(stored.display_name.as_deref(), Some("Ahmet Kaan Gümüş")); | 722 | + Some("/settings/profile?saved=1"), |
| 741 | - | 723 | + "display name: location" |
| 724 | + ); | ||
| 725 | + assert_eq!( | ||
| 726 | + saved_fields(&fx).await.3.as_deref(), | ||
| 727 | + Some("Ahmet Kaan Gümüş"), | ||
| 728 | + "display name: stored" | ||
| 729 | + ); | ||
| 742 | let (status, _, body) = send(&fx.router, "GET", "/settings/profile", true, None).await; | 730 | let (status, _, body) = send(&fx.router, "GET", "/settings/profile", true, None).await; |
| 743 | - assert_eq!(status, 200); | 731 | + assert_eq!(status, 200, "display name: page"); |
| 744 | assert!( | 732 | assert!( |
| 745 | body.contains(r#"name="display_name""#) && body.contains("Ahmet Kaan Gümüş"), | 733 | body.contains(r#"name="display_name""#) && body.contains("Ahmet Kaan Gümüş"), |
| 746 | - "{body}" | 734 | + "display name: field shows it {body}" |
| 747 | ); | 735 | ); |
| 748 | - | ||
| 749 | let blank = format!("csrf={CSRF}&display_name=+++&username=tahinli&about=&timezone=UTC"); | 736 | let blank = format!("csrf={CSRF}&display_name=+++&username=tahinli&about=&timezone=UTC"); |
| 750 | let (status, _, _) = | 737 | let (status, _, _) = |
| 751 | send(&fx.router, "POST", "/settings/profile", true, Some(&blank)).await; | 738 | send(&fx.router, "POST", "/settings/profile", true, Some(&blank)).await; |
| 752 | - assert_eq!(status, 303); | 739 | + assert_eq!(status, 303, "blank display name: status"); |
| 753 | - let stored = fx.store.user_by_id(&fx.user.id).await.unwrap().unwrap(); | 740 | + assert_eq!( |
| 754 | - assert_eq!(stored.display_name, None); | 741 | + saved_fields(&fx).await.3, |
| 755 | - } | 742 | + None, |
| 743 | + "blank display name: cleared" | ||
| 744 | + ); | ||
| 756 | 745 | ||
| 757 | - #[tokio::test] | 746 | + // A name that is too long, or has a line break, is rejected and nothing is saved. |
| 758 | - async fn a_bad_display_name_is_rejected_and_nothing_is_saved() { | 747 | + let before = saved_fields(&fx).await; |
| 759 | - let fx = fixture().await; | ||
| 760 | let long = "x".repeat(81); | 748 | let long = "x".repeat(81); |
| 761 | let form = | 749 | let form = |
| 762 | format!("csrf={CSRF}&display_name={long}&username=tahinli&about=kept&timezone=UTC"); | 750 | format!("csrf={CSRF}&display_name={long}&username=tahinli&about=kept&timezone=UTC"); |
| 763 | let (status, location, body) = | 751 | let (status, location, body) = |
| 764 | send(&fx.router, "POST", "/settings/profile", true, Some(&form)).await; | 752 | send(&fx.router, "POST", "/settings/profile", true, Some(&form)).await; |
| 765 | - assert_eq!(status, 200); | 753 | + assert_eq!(status, 200, "long display name: status"); |
| 766 | - assert!(location.is_none()); | 754 | + assert!(location.is_none(), "long display name: no redirect"); |
| 767 | assert!( | 755 | assert!( |
| 768 | body.contains("The name must be at most 80 characters."), | 756 | body.contains("The name must be at most 80 characters."), |
| 769 | - "{body}" | 757 | + "long display name: {body}" |
| 758 | + ); | ||
| 759 | + assert_eq!( | ||
| 760 | + saved_fields(&fx).await, | ||
| 761 | + before, | ||
| 762 | + "long display name: nothing saved" | ||
| 770 | ); | 763 | ); |
| 771 | - // A bad name holds back the valid "about" text too. | ||
| 772 | - let stored = fx.store.user_by_id(&fx.user.id).await.unwrap().unwrap(); | ||
| 773 | - assert_eq!(stored.display_name, None); | ||
| 774 | - assert_eq!(stored.about, ""); | ||
| 775 | 764 | ||
| 776 | let control = | 765 | let control = |
| 777 | format!("csrf={CSRF}&display_name=two%0Alines&username=tahinli&about=&timezone=UTC"); | 766 | format!("csrf={CSRF}&display_name=two%0Alines&username=tahinli&about=&timezone=UTC"); |
| @@ -783,17 +772,55 @@ pub(crate) mod tests { | |||
| 783 | Some(&control), | 772 | Some(&control), |
| 784 | ) | 773 | ) |
| 785 | .await; | 774 | .await; |
| 786 | - assert_eq!(status, 200); | 775 | + assert_eq!(status, 200, "line break: status"); |
| 787 | assert!( | 776 | assert!( |
| 788 | body.contains("The name must not contain line breaks or control characters."), | 777 | body.contains("The name must not contain line breaks or control characters."), |
| 789 | - "{body}" | 778 | + "line break: {body}" |
| 779 | + ); | ||
| 780 | + assert_eq!(saved_fields(&fx).await, before, "line break: nothing saved"); | ||
| 781 | + | ||
| 782 | + // A missing or forged CSRF token is refused. | ||
| 783 | + let cases = [ | ||
| 784 | + ("forged", "csrf=wrong&username=Tahinli&about=&timezone=UTC"), | ||
| 785 | + ("missing", "username=Tahinli&about=&timezone=UTC"), | ||
| 786 | + ( | ||
| 787 | + "missing with about", | ||
| 788 | + "username=Tahinli&about=x&timezone=UTC", | ||
| 789 | + ), | ||
| 790 | + ]; | ||
| 791 | + for (name, form) in cases { | ||
| 792 | + let (status, _, _) = | ||
| 793 | + send(&fx.router, "POST", "/settings/profile", true, Some(form)).await; | ||
| 794 | + assert_eq!(status, 403, "csrf case {name}"); | ||
| 795 | + } | ||
| 796 | + assert_eq!(saved_fields(&fx).await, before, "csrf: nothing saved"); | ||
| 797 | + | ||
| 798 | + // Anonymous requests are sent to log in and change nothing. | ||
| 799 | + let (status, location, _) = send(&fx.router, "GET", "/settings/profile", false, None).await; | ||
| 800 | + assert_eq!(status, 303, "anonymous GET: status"); | ||
| 801 | + assert!( | ||
| 802 | + location.unwrap_or_default().starts_with("/login?next="), | ||
| 803 | + "anonymous GET: login" | ||
| 804 | + ); | ||
| 805 | + let form = format!("csrf={CSRF}&username=Tahinli"); | ||
| 806 | + let (status, location, _) = | ||
| 807 | + send(&fx.router, "POST", "/settings/profile", false, Some(&form)).await; | ||
| 808 | + assert_eq!(status, 303, "anonymous POST: status"); | ||
| 809 | + assert!( | ||
| 810 | + location.unwrap_or_default().starts_with("/login?next="), | ||
| 811 | + "anonymous POST: login" | ||
| 790 | ); | 812 | ); |
| 813 | + assert_eq!(saved_fields(&fx).await, before, "anonymous: nothing saved"); | ||
| 791 | } | 814 | } |
| 792 | 815 | ||
| 816 | + /// Avatars: a missing token and an SVG are rejected, then a PNG is uploaded, served, | ||
| 817 | + /// shown and removed. The scenarios run in order on one fixture. | ||
| 793 | #[tokio::test] | 818 | #[tokio::test] |
| 794 | - async fn avatar_writes_without_csrf_are_rejected() { | 819 | + async fn avatar_rejections_then_upload_and_removal() { |
| 795 | let fx = fixture().await; | 820 | let fx = fixture().await; |
| 796 | let png = png_bytes(); | 821 | let png = png_bytes(); |
| 822 | + | ||
| 823 | + // Uploads and removals without a CSRF token are refused. | ||
| 797 | let (content_type, body) = multipart(&[("avatar", Some(("a.png", "image/png")), &png[..])]); | 824 | let (content_type, body) = multipart(&[("avatar", Some(("a.png", "image/png")), &png[..])]); |
| 798 | let reply = send_full( | 825 | let reply = send_full( |
| 799 | &fx.router, | 826 | &fx.router, |
| @@ -804,8 +831,11 @@ pub(crate) mod tests { | |||
| 804 | body, | 831 | body, |
| 805 | ) | 832 | ) |
| 806 | .await; | 833 | .await; |
| 807 | - assert_eq!(reply.status, 403, "upload"); | 834 | + assert_eq!(reply.status, 403, "no csrf: upload"); |
| 808 | - assert!(fx.store.avatar_hash(&fx.user.id).await.unwrap().is_none()); | 835 | + assert!( |
| 836 | + fx.store.avatar_hash(&fx.user.id).await.unwrap().is_none(), | ||
| 837 | + "no csrf: nothing stored" | ||
| 838 | + ); | ||
| 809 | 839 | ||
| 810 | let (status, _, _) = send( | 840 | let (status, _, _) = send( |
| 811 | &fx.router, | 841 | &fx.router, |
| @@ -815,12 +845,9 @@ pub(crate) mod tests { | |||
| 815 | Some("csrf=nope"), | 845 | Some("csrf=nope"), |
| 816 | ) | 846 | ) |
| 817 | .await; | 847 | .await; |
| 818 | - assert_eq!(status, 403, "remove"); | 848 | + assert_eq!(status, 403, "no csrf: remove"); |
| 819 | - } | ||
| 820 | 849 | ||
| 821 | - #[tokio::test] | 850 | + // An SVG upload is rejected and nothing is stored. |
| 822 | - async fn svg_upload_is_rejected_and_nothing_is_stored() { | ||
| 823 | - let fx = fixture().await; | ||
| 824 | let svg = br#"<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>"#; | 851 | let svg = br#"<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>"#; |
| 825 | let (content_type, body) = multipart(&[ | 852 | let (content_type, body) = multipart(&[ |
| 826 | ("csrf", None, CSRF.as_bytes()), | 853 | ("csrf", None, CSRF.as_bytes()), |
| @@ -835,34 +862,36 @@ pub(crate) mod tests { | |||
| 835 | body, | 862 | body, |
| 836 | ) | 863 | ) |
| 837 | .await; | 864 | .await; |
| 838 | - assert_eq!(reply.status, 200); | 865 | + assert_eq!(reply.status, 200, "svg: status"); |
| 839 | - assert!(reply.text().contains(r#"role="alert""#)); | 866 | + assert!(reply.text().contains(r#"role="alert""#), "svg: alert"); |
| 840 | - assert!(fx.store.avatar_hash(&fx.user.id).await.unwrap().is_none()); | 867 | + assert!( |
| 841 | - } | 868 | + fx.store.avatar_hash(&fx.user.id).await.unwrap().is_none(), |
| 842 | - | 869 | + "svg: nothing stored" |
| 843 | - #[tokio::test] | 870 | + ); |
| 844 | - async fn avatar_upload_then_remove_is_served_correctly() { | ||
| 845 | - let fx = fixture().await; | ||
| 846 | - let png = png_bytes(); | ||
| 847 | 871 | ||
| 872 | + // A PNG upload is stored, shown and served with its hash as the version. | ||
| 848 | let reply = upload_avatar(&fx.router, CSRF, &png).await; | 873 | let reply = upload_avatar(&fx.router, CSRF, &png).await; |
| 849 | - assert_eq!(reply.status, 303); | 874 | + assert_eq!(reply.status, 303, "upload: status"); |
| 850 | assert_eq!( | 875 | assert_eq!( |
| 851 | reply.header("location").as_deref(), | 876 | reply.header("location").as_deref(), |
| 852 | - Some("/settings/profile?saved=avatar") | 877 | + Some("/settings/profile?saved=avatar"), |
| 878 | + "upload: location" | ||
| 853 | ); | 879 | ); |
| 854 | let stored_hash = fx.store.avatar_hash(&fx.user.id).await.unwrap().unwrap(); | 880 | let stored_hash = fx.store.avatar_hash(&fx.user.id).await.unwrap().unwrap(); |
| 855 | - assert_eq!(stored_hash.len(), 16); | 881 | + assert_eq!(stored_hash.len(), 16, "upload: hash length"); |
| 856 | 882 | ||
| 857 | // The page shows the new avatar with the hash as its version. | 883 | // The page shows the new avatar with the hash as its version. |
| 858 | let (_, _, body) = send(&fx.router, "GET", "/settings/profile", true, None).await; | 884 | let (_, _, body) = send(&fx.router, "GET", "/settings/profile", true, None).await; |
| 859 | - assert!(body.contains(&format!("?v={stored_hash}")), "{body}"); | 885 | + assert!( |
| 860 | - assert!(body.contains("Remove photo"), "{body}"); | 886 | + body.contains(&format!("?v={stored_hash}")), |
| 887 | + "page: version {body}" | ||
| 888 | + ); | ||
| 889 | + assert!(body.contains("Remove photo"), "page: remove item {body}"); | ||
| 861 | 890 | ||
| 862 | // The top-nav avatar uses the real hash too, not the default URL. | 891 | // The top-nav avatar uses the real hash too, not the default URL. |
| 863 | let (_, _, body) = send(&fx.router, "GET", "/", true, None).await; | 892 | let (_, _, body) = send(&fx.router, "GET", "/", true, None).await; |
| 864 | let nav_src = format!(r#"src="/avatars/tahinli?v={stored_hash}""#); | 893 | let nav_src = format!(r#"src="/avatars/tahinli?v={stored_hash}""#); |
| 865 | - assert!(body.contains(&nav_src), "{body}"); | 894 | + assert!(body.contains(&nav_src), "nav: avatar url {body}"); |
| 866 | 895 | ||
| 867 | // Served as a stored PNG with a hash ETag, and 304 on a match. | 896 | // Served as a stored PNG with a hash ETag, and 304 on a match. |
| 868 | let served = send_full( | 897 | let served = send_full( |
| @@ -874,26 +903,39 @@ pub(crate) mod tests { | |||
| 874 | Vec::new(), | 903 | Vec::new(), |
| 875 | ) | 904 | ) |
| 876 | .await; | 905 | .await; |
| 877 | - assert_eq!(served.status, 200); | 906 | + assert_eq!(served.status, 200, "served: status"); |
| 878 | - assert_eq!(served.header("content-type").as_deref(), Some("image/png")); | 907 | + assert_eq!( |
| 879 | - assert_eq!(served.header("etag"), Some(format!("\"{stored_hash}\""))); | 908 | + served.header("content-type").as_deref(), |
| 909 | + Some("image/png"), | ||
| 910 | + "served: type" | ||
| 911 | + ); | ||
| 912 | + assert_eq!( | ||
| 913 | + served.header("etag"), | ||
| 914 | + Some(format!("\"{stored_hash}\"")), | ||
| 915 | + "served: etag" | ||
| 916 | + ); | ||
| 880 | assert_eq!( | 917 | assert_eq!( |
| 881 | served.header("x-content-type-options").as_deref(), | 918 | served.header("x-content-type-options").as_deref(), |
| 882 | - Some("nosniff") | 919 | + Some("nosniff"), |
| 920 | + "served: nosniff" | ||
| 921 | + ); | ||
| 922 | + assert!( | ||
| 923 | + served.body.starts_with(&[0x89, b'P', b'N', b'G']), | ||
| 924 | + "served: png bytes" | ||
| 883 | ); | 925 | ); |
| 884 | - assert!(served.body.starts_with(&[0x89, b'P', b'N', b'G'])); | ||
| 885 | 926 | ||
| 886 | let uri = format!("/avatars/tahinli?v={stored_hash}"); | 927 | let uri = format!("/avatars/tahinli?v={stored_hash}"); |
| 887 | let immutable = send_full(&fx.router, "GET", &uri, false, None, Vec::new()).await; | 928 | let immutable = send_full(&fx.router, "GET", &uri, false, None, Vec::new()).await; |
| 888 | assert_eq!( | 929 | assert_eq!( |
| 889 | immutable.header("cache-control").as_deref(), | 930 | immutable.header("cache-control").as_deref(), |
| 890 | - Some("public, max-age=31536000, immutable") | 931 | + Some("public, max-age=31536000, immutable"), |
| 932 | + "versioned: cache-control" | ||
| 891 | ); | 933 | ); |
| 892 | let etag = format!("\"{stored_hash}\""); | 934 | let etag = format!("\"{stored_hash}\""); |
| 893 | let mut request = Request::builder().method("GET").uri(&uri); | 935 | let mut request = Request::builder().method("GET").uri(&uri); |
| 894 | request = request.header("if-none-match", etag.as_str()); | 936 | request = request.header("if-none-match", etag.as_str()); |
| 895 | let not_modified = fx.router.handle(request.body(Body::empty()).unwrap()).await; | 937 | let not_modified = fx.router.handle(request.body(Body::empty()).unwrap()).await; |
| 896 | - assert_eq!(not_modified.status().as_u16(), 304); | 938 | + assert_eq!(not_modified.status().as_u16(), 304, "versioned: 304"); |
| 897 | 939 | ||
| 898 | // Removing gives the generated default SVG. | 940 | // Removing gives the generated default SVG. |
| 899 | let form = format!("csrf={CSRF}"); | 941 | let form = format!("csrf={CSRF}"); |
| @@ -905,20 +947,27 @@ pub(crate) mod tests { | |||
| 905 | Some(&form), | 947 | Some(&form), |
| 906 | ) | 948 | ) |
| 907 | .await; | 949 | .await; |
| 908 | - assert_eq!(status, 303); | 950 | + assert_eq!(status, 303, "remove: status"); |
| 909 | assert_eq!( | 951 | assert_eq!( |
| 910 | location.as_deref(), | 952 | location.as_deref(), |
| 911 | - Some("/settings/profile?saved=avatar-removed") | 953 | + Some("/settings/profile?saved=avatar-removed"), |
| 954 | + "remove: location" | ||
| 955 | + ); | ||
| 956 | + assert!( | ||
| 957 | + fx.store.avatar_hash(&fx.user.id).await.unwrap().is_none(), | ||
| 958 | + "remove: cleared" | ||
| 912 | ); | 959 | ); |
| 913 | - assert!(fx.store.avatar_hash(&fx.user.id).await.unwrap().is_none()); | ||
| 914 | let (_, _, body) = send(&fx.router, "GET", "/", true, None).await; | 960 | let (_, _, body) = send(&fx.router, "GET", "/", true, None).await; |
| 915 | assert!( | 961 | assert!( |
| 916 | body.contains(r#"src="/avatars/tahinli?v=default""#), | 962 | body.contains(r#"src="/avatars/tahinli?v=default""#), |
| 917 | - "{body}" | 963 | + "remove: nav default {body}" |
| 918 | ); | 964 | ); |
| 919 | // With no photo there is nothing to remove, so the menu leaves that item out. | 965 | // With no photo there is nothing to remove, so the menu leaves that item out. |
| 920 | let (_, _, body) = send(&fx.router, "GET", "/settings/profile", true, None).await; | 966 | let (_, _, body) = send(&fx.router, "GET", "/settings/profile", true, None).await; |
| 921 | - assert!(!body.contains("Remove photo"), "{body}"); | 967 | + assert!( |
| 968 | + !body.contains("Remove photo"), | ||
| 969 | + "remove: no remove item {body}" | ||
| 970 | + ); | ||
| 922 | 971 | ||
| 923 | let default = send_full( | 972 | let default = send_full( |
| 924 | &fx.router, | 973 | &fx.router, |
| @@ -929,15 +978,21 @@ pub(crate) mod tests { | |||
| 929 | Vec::new(), | 978 | Vec::new(), |
| 930 | ) | 979 | ) |
| 931 | .await; | 980 | .await; |
| 932 | - assert_eq!(default.status, 200); | 981 | + assert_eq!(default.status, 200, "default: status"); |
| 933 | assert_eq!( | 982 | assert_eq!( |
| 934 | default.header("content-type").as_deref(), | 983 | default.header("content-type").as_deref(), |
| 935 | - Some("image/svg+xml") | 984 | + Some("image/svg+xml"), |
| 985 | + "default: type" | ||
| 936 | ); | 986 | ); |
| 937 | assert_eq!( | 987 | assert_eq!( |
| 938 | default.header("x-content-type-options").as_deref(), | 988 | default.header("x-content-type-options").as_deref(), |
| 939 | - Some("nosniff") | 989 | + Some("nosniff"), |
| 990 | + "default: nosniff" | ||
| 991 | + ); | ||
| 992 | + assert!( | ||
| 993 | + default.text().starts_with("<svg"), | ||
| 994 | + "default: svg {}", | ||
| 995 | + default.text() | ||
| 940 | ); | 996 | ); |
| 941 | - assert!(default.text().starts_with("<svg"), "{}", default.text()); | ||
| 942 | } | 997 | } |
| 943 | } | 998 | } |