Forge-web page tests share their fixtures and merge into tables
Browse files at this commit5137546bd223b1ff41369645c24f80daf79f7202Parent: 8a535cb- test
- forge-web page tests share their fixtures and merge into tables
8 changed files +318 -327
- crates/forge-web/src/git_auth.rs +44 -76
- crates/forge-web/src/git_http.rs +21 -15
- crates/forge-web/src/internal.rs +19 -17
- crates/forge-web/src/lfs/route_tests.rs +2 -5
- crates/forge-web/src/notify.rs +20 -20
- crates/forge-web/src/raw.rs +137 -142
- crates/forge-web/src/signatures.rs +57 -35
- crates/forge-web/src/webhooks/client.rs +18 -17
crates/forge-web/src/git_auth.rsmodified+44 -76
| @@ -99,81 +99,49 @@ mod tests { | |||
| 99 | } | 99 | } |
| 100 | 100 | ||
| 101 | #[test] | 101 | #[test] |
| 102 | - fn parses_user_and_token() { | 102 | + fn parse_basic_auth_cases() { |
| 103 | - let headers = headers_with(&basic("tahinli:forge_abc")); | 103 | + // One labelled table: each case keeps its own input and expected result. |
| 104 | - assert_eq!( | 104 | + let t = BASE64_STANDARD.encode("tahinli:t"); |
| 105 | - parse_basic_auth(&headers), | 105 | + let cases: Vec<(&str, String, Option<(&str, &str)>)> = vec![ |
| 106 | - Some(("tahinli".to_owned(), "forge_abc".to_owned())) | 106 | + ( |
| 107 | - ); | 107 | + "user and token", |
| 108 | - } | 108 | + basic("tahinli:forge_abc"), |
| 109 | - | 109 | + Some(("tahinli", "forge_abc")), |
| 110 | - #[test] | 110 | + ), |
| 111 | - fn scheme_is_case_insensitive() { | 111 | + ( |
| 112 | - let value = format!("bAsIc {}", BASE64_STANDARD.encode("tahinli:t")); | 112 | + "scheme is case insensitive", |
| 113 | - let headers = headers_with(&value); | 113 | + format!("bAsIc {t}"), |
| 114 | - assert_eq!( | 114 | + Some(("tahinli", "t")), |
| 115 | - parse_basic_auth(&headers), | 115 | + ), |
| 116 | - Some(("tahinli".into(), "t".into())) | 116 | + ( |
| 117 | - ); | 117 | + "password may contain colons", |
| 118 | - } | 118 | + basic("tahinli:a:b:c"), |
| 119 | - | 119 | + Some(("tahinli", "a:b:c")), |
| 120 | - #[test] | 120 | + ), |
| 121 | - fn password_may_contain_colons() { | 121 | + ( |
| 122 | - let headers = headers_with(&basic("tahinli:a:b:c")); | 122 | + "empty password is parsed", |
| 123 | - assert_eq!( | 123 | + basic("tahinli:"), |
| 124 | - parse_basic_auth(&headers), | 124 | + Some(("tahinli", "")), |
| 125 | - Some(("tahinli".into(), "a:b:c".into())) | 125 | + ), |
| 126 | - ); | 126 | + ("wrong scheme", format!("Bearer {t}"), None), |
| 127 | - } | 127 | + ("missing colon", basic("tahinlitoken"), None), |
| 128 | - | 128 | + ("bad base64", "Basic !!!not-base64!!!".to_owned(), None), |
| 129 | - #[test] | 129 | + ("extra space", format!("Basic {t}"), None), |
| 130 | - fn empty_password_is_parsed_but_user_is_not_resolved_here() { | 130 | + ("empty value", "Basic ".to_owned(), None), |
| 131 | - let headers = headers_with(&basic("tahinli:")); | 131 | + ( |
| 132 | - assert_eq!( | 132 | + "non utf8 credentials", |
| 133 | - parse_basic_auth(&headers), | 133 | + format!( |
| 134 | - Some(("tahinli".into(), String::new())) | 134 | + "Basic {}", |
| 135 | - ); | 135 | + BASE64_STANDARD.encode([0xff_u8, 0xfe, b':', b'x']) |
| 136 | - } | 136 | + ), |
| 137 | - | 137 | + None, |
| 138 | - #[test] | 138 | + ), |
| 139 | - fn rejects_wrong_scheme() { | 139 | + ]; |
| 140 | - let value = format!("Bearer {}", BASE64_STANDARD.encode("tahinli:t")); | 140 | + for (name, value, want) in cases { |
| 141 | - assert_eq!(parse_basic_auth(&headers_with(&value)), None); | 141 | + let got = parse_basic_auth(&headers_with(&value)); |
| 142 | - } | 142 | + let want = want.map(|(user, pass)| (user.to_owned(), pass.to_owned())); |
| 143 | - | 143 | + assert_eq!(got, want, "case {name}"); |
| 144 | - #[test] | 144 | + } |
| 145 | - fn rejects_missing_colon() { | 145 | + assert_eq!(parse_basic_auth(&HeaderMap::new()), None, "case no header"); |
| 146 | - assert_eq!( | ||
| 147 | - parse_basic_auth(&headers_with(&basic("tahinlitoken"))), | ||
| 148 | - None | ||
| 149 | - ); | ||
| 150 | - } | ||
| 151 | - | ||
| 152 | - #[test] | ||
| 153 | - fn rejects_bad_base64() { | ||
| 154 | - assert_eq!( | ||
| 155 | - parse_basic_auth(&headers_with("Basic !!!not-base64!!!")), | ||
| 156 | - None | ||
| 157 | - ); | ||
| 158 | - } | ||
| 159 | - | ||
| 160 | - #[test] | ||
| 161 | - fn rejects_extra_spaces_and_empty_value() { | ||
| 162 | - let encoded = BASE64_STANDARD.encode("tahinli:t"); | ||
| 163 | - assert_eq!( | ||
| 164 | - parse_basic_auth(&headers_with(&format!("Basic {encoded}"))), | ||
| 165 | - None | ||
| 166 | - ); | ||
| 167 | - assert_eq!(parse_basic_auth(&headers_with("Basic ")), None); | ||
| 168 | - assert_eq!(parse_basic_auth(&HeaderMap::new()), None); | ||
| 169 | - } | ||
| 170 | - | ||
| 171 | - #[test] | ||
| 172 | - fn rejects_non_utf8_credentials() { | ||
| 173 | - let encoded = BASE64_STANDARD.encode([0xff_u8, 0xfe, b':', b'x']); | ||
| 174 | - assert_eq!( | ||
| 175 | - parse_basic_auth(&headers_with(&format!("Basic {encoded}"))), | ||
| 176 | - None | ||
| 177 | - ); | ||
| 178 | } | 146 | } |
| 179 | } | 147 | } |
crates/forge-web/src/git_http.rsmodified+21 -15
| @@ -457,21 +457,27 @@ mod tests { | |||
| 457 | use super::*; | 457 | use super::*; |
| 458 | 458 | ||
| 459 | #[test] | 459 | #[test] |
| 460 | - fn git_protocol_accepts_documented_charset() { | 460 | + fn git_protocol_value_charset_and_length() { |
| 461 | - assert!(valid_git_protocol("version=2")); | 461 | + // (name, value, expected validity); accepted charset first, then rejects and the length limit. |
| 462 | - assert!(valid_git_protocol("version=2:object-format=sha1")); | 462 | + let cases: Vec<(&str, String, bool)> = vec![ |
| 463 | - assert!(valid_git_protocol("a.b,c_d-e:f")); | 463 | + ("documented version", "version=2".into(), true), |
| 464 | - } | 464 | + ( |
| 465 | - | 465 | + "object format pair", |
| 466 | - #[test] | 466 | + "version=2:object-format=sha1".into(), |
| 467 | - fn git_protocol_rejects_bad_values() { | 467 | + true, |
| 468 | - assert!(!valid_git_protocol("")); | 468 | + ), |
| 469 | - assert!(!valid_git_protocol("Version=2")); | 469 | + ("punctuation charset", "a.b,c_d-e:f".into(), true), |
| 470 | - assert!(!valid_git_protocol("version 2")); | 470 | + ("empty", String::new(), false), |
| 471 | - assert!(!valid_git_protocol("version=2;x")); | 471 | + ("uppercase key", "Version=2".into(), false), |
| 472 | - assert!(!valid_git_protocol("version=2\n")); | 472 | + ("space", "version 2".into(), false), |
| 473 | - assert!(!valid_git_protocol(&"a".repeat(65))); | 473 | + ("semicolon", "version=2;x".into(), false), |
| 474 | - assert!(valid_git_protocol(&"a".repeat(64))); | 474 | + ("newline", "version=2\n".into(), false), |
| 475 | + ("65 chars", "a".repeat(65), false), | ||
| 476 | + ("64 chars", "a".repeat(64), true), | ||
| 477 | + ]; | ||
| 478 | + for (name, value, want) in cases { | ||
| 479 | + assert_eq!(valid_git_protocol(&value), want, "case {name}"); | ||
| 480 | + } | ||
| 475 | } | 481 | } |
| 476 | 482 | ||
| 477 | #[test] | 483 | #[test] |
crates/forge-web/src/internal.rsmodified+19 -17
| @@ -679,23 +679,25 @@ mod tests { | |||
| 679 | use topcoat::router::RouterBuilderDiscoverExt; | 679 | use topcoat::router::RouterBuilderDiscoverExt; |
| 680 | 680 | ||
| 681 | #[test] | 681 | #[test] |
| 682 | - fn ref_names_must_live_under_refs() { | 682 | + fn ref_name_validity_cases() { |
| 683 | - assert!(is_valid_ref_name("refs/heads/main")); | 683 | + // (ref name, expected validity): must live under refs/, no dotdot or leading dash, no control chars. |
| 684 | - assert!(is_valid_ref_name("refs/tags/v1.0")); | 684 | + let cases: [(&str, bool); 12] = [ |
| 685 | - assert!(!is_valid_ref_name("HEAD")); | 685 | + ("refs/heads/main", true), |
| 686 | - assert!(!is_valid_ref_name("heads/main")); | 686 | + ("refs/tags/v1.0", true), |
| 687 | - assert!(!is_valid_ref_name("refs/")); | 687 | + ("HEAD", false), |
| 688 | - } | 688 | + ("heads/main", false), |
| 689 | - | 689 | + ("refs/", false), |
| 690 | - #[test] | 690 | + ("refs/heads/a..b", false), |
| 691 | - fn ref_names_reject_dotdot_dash_components_and_control_chars() { | 691 | + ("refs/heads/-evil", false), |
| 692 | - assert!(!is_valid_ref_name("refs/heads/a..b")); | 692 | + ("refs/-evil/main", false), |
| 693 | - assert!(!is_valid_ref_name("refs/heads/-evil")); | 693 | + ("refs/heads/a-b", true), |
| 694 | - assert!(!is_valid_ref_name("refs/-evil/main")); | 694 | + ("refs/heads/a\nb", false), |
| 695 | - assert!(is_valid_ref_name("refs/heads/a-b")); | 695 | + ("refs/heads/a\0b", false), |
| 696 | - assert!(!is_valid_ref_name("refs/heads/a\nb")); | 696 | + ("refs/heads/a\x7fb", false), |
| 697 | - assert!(!is_valid_ref_name("refs/heads/a\0b")); | 697 | + ]; |
| 698 | - assert!(!is_valid_ref_name("refs/heads/a\x7fb")); | 698 | + for (name, want) in cases { |
| 699 | + assert_eq!(is_valid_ref_name(name), want, "case {name:?}"); | ||
| 700 | + } | ||
| 699 | } | 701 | } |
| 700 | 702 | ||
| 701 | #[test] | 703 | #[test] |
crates/forge-web/src/lfs/route_tests.rsmodified+2 -5
| @@ -195,11 +195,8 @@ async fn batch_download_returns_action_for_stored_object() { | |||
| 195 | now(), | 195 | now(), |
| 196 | )); | 196 | )); |
| 197 | assert_eq!(body["objects"][1]["error"]["code"], 404); | 197 | assert_eq!(body["objects"][1]["error"]["code"], 404); |
| 198 | -} | ||
| 199 | 198 | ||
| 200 | -#[tokio::test] | 199 | + // Same router: the batch path without the `.git` suffix also answers (second object, same repo). |
| 201 | -async fn batch_accepts_the_path_without_dot_git() { | ||
| 202 | - let fx = fixture(Visibility::Public).await; | ||
| 203 | let oid = upload_with_pat(&fx, b"bare path\n").await; | 200 | let oid = upload_with_pat(&fx, b"bare path\n").await; |
| 204 | let reply = send( | 201 | let reply = send( |
| 205 | &fx.router, | 202 | &fx.router, |
| @@ -209,7 +206,7 @@ async fn batch_accepts_the_path_without_dot_git() { | |||
| 209 | batch_body("download", &[(oid.clone(), 9)]), | 206 | batch_body("download", &[(oid.clone(), 9)]), |
| 210 | ) | 207 | ) |
| 211 | .await; | 208 | .await; |
| 212 | - assert_eq!(reply.status, 200); | 209 | + assert_eq!(reply.status, 200, "path without .git"); |
| 213 | assert!(reply.json()["objects"][0]["actions"]["download"].is_object()); | 210 | assert!(reply.json()["objects"][0]["actions"]["download"].is_object()); |
| 214 | } | 211 | } |
| 215 | 212 | ||
crates/forge-web/src/notify.rsmodified+20 -20
| @@ -754,26 +754,26 @@ mod tests { | |||
| 754 | } | 754 | } |
| 755 | 755 | ||
| 756 | #[test] | 756 | #[test] |
| 757 | - fn mentions_skip_code_links_and_emails() { | 757 | + fn mentioned_usernames_cases() { |
| 758 | - let text = "hi @Tahinli and @bob. mail a@b.com `@code`\n\n\ | 758 | + // (name, markdown, expected usernames): code, links, emails, HTML, dedupe and no-mention cases. |
| 759 | - ```\n@blocked\n```\n[@linked](https://x.test) @tahinli again"; | 759 | + let cases: Vec<(&str, &str, Vec<&str>)> = vec![ |
| 760 | - assert_eq!(mentioned_usernames(text), vec!["tahinli", "bob"]); | 760 | + ( |
| 761 | - } | 761 | + "skips code, links and emails", |
| 762 | - | 762 | + "hi @Tahinli and @bob. mail a@b.com `@code`\n\n\ |
| 763 | - #[test] | 763 | + ```\n@blocked\n```\n[@linked](https://x.test) @tahinli again", |
| 764 | - fn mentions_in_html_blocks_are_found() { | 764 | + vec!["tahinli", "bob"], |
| 765 | - let text = "<div>\n@carol\n</div>\n"; | 765 | + ), |
| 766 | - assert_eq!(mentioned_usernames(text), vec!["carol"]); | 766 | + ( |
| 767 | - } | 767 | + "found inside html blocks", |
| 768 | - | 768 | + "<div>\n@carol\n</div>\n", |
| 769 | - #[test] | 769 | + vec!["carol"], |
| 770 | - fn mentions_are_deduped_case_insensitively() { | 770 | + ), |
| 771 | - assert_eq!( | 771 | + ("deduped case-insensitively", "@Bob @bob @BOB", vec!["bob"]), |
| 772 | - mentioned_usernames("@Bob @bob @BOB"), | 772 | + ("no mentions", "no mentions here", vec![]), |
| 773 | - vec!["bob"], | 773 | + ]; |
| 774 | - "one name however it is cased" | 774 | + for (name, text, want) in cases { |
| 775 | - ); | 775 | + assert_eq!(mentioned_usernames(text), want, "case {name}"); |
| 776 | - assert!(mentioned_usernames("no mentions here").is_empty()); | 776 | + } |
| 777 | } | 777 | } |
| 778 | 778 | ||
| 779 | #[tokio::test] | 779 | #[tokio::test] |
crates/forge-web/src/raw.rsmodified+137 -142
| @@ -647,162 +647,157 @@ mod tests { | |||
| 647 | assert_eq!(header_str(res, "referrer-policy"), "no-referrer"); | 647 | assert_eq!(header_str(res, "referrer-policy"), "no-referrer"); |
| 648 | } | 648 | } |
| 649 | 649 | ||
| 650 | + /// One fixture (one 9 MB repo, private repo and signer) serves every read-only scenario below. | ||
| 650 | #[tokio::test] | 651 | #[tokio::test] |
| 651 | - async fn public_text_file_is_plain_and_sandboxed() { | 652 | + async fn raw_serves_every_scenario_from_one_fixture() { |
| 652 | let fx = fixture().await; | 653 | let fx = fixture().await; |
| 653 | - let res = get(&fx.router, "GET", "/tahinli/pub/main/hello.txt", None).await; | 654 | + // Scenario: a public text file is plain and sandboxed. |
| 654 | - assert_eq!(res.status(), 200); | 655 | + { |
| 655 | - assert_eq!(header_str(&res, "content-type"), TEXT); | 656 | + let res = get(&fx.router, "GET", "/tahinli/pub/main/hello.txt", None).await; |
| 656 | - assert_eq!(header_str(&res, "content-disposition"), ""); | 657 | + assert_eq!(res.status(), 200); |
| 657 | - assert_eq!(header_str(&res, "cache-control"), CACHE_PUBLIC); | 658 | + assert_eq!(header_str(&res, "content-type"), TEXT); |
| 658 | - assert_security_headers(&res); | 659 | + assert_eq!(header_str(&res, "content-disposition"), ""); |
| 659 | - assert_eq!(body_bytes(res).await, b"hello raw\n"); | 660 | + assert_eq!(header_str(&res, "cache-control"), CACHE_PUBLIC); |
| 660 | - } | 661 | + assert_security_headers(&res); |
| 662 | + assert_eq!(body_bytes(res).await, b"hello raw\n"); | ||
| 663 | + } | ||
| 661 | 664 | ||
| 662 | - #[tokio::test] | 665 | + // Scenario: a nested path resolves. |
| 663 | - async fn nested_path_resolves() { | 666 | + { |
| 664 | - let fx = fixture().await; | 667 | + let res = get(&fx.router, "GET", "/tahinli/pub/main/sub/a.txt", None).await; |
| 665 | - let res = get(&fx.router, "GET", "/tahinli/pub/main/sub/a.txt", None).await; | 668 | + assert_eq!(res.status(), 200); |
| 666 | - assert_eq!(res.status(), 200); | 669 | + assert_eq!(body_bytes(res).await, b"nested\n"); |
| 667 | - assert_eq!(body_bytes(res).await, b"nested\n"); | 670 | + } |
| 668 | - } | ||
| 669 | 671 | ||
| 670 | - #[tokio::test] | 672 | + // Scenario: html is forced to an attachment and png is an image. |
| 671 | - async fn html_is_forced_to_attachment_and_png_is_an_image() { | 673 | + { |
| 672 | - let fx = fixture().await; | 674 | + // (path, want content-type, want content-disposition) |
| 673 | - // (path, want content-type, want content-disposition) | 675 | + let cases = [ |
| 674 | - let cases = [ | 676 | + ("/tahinli/pub/main/page.html", TEXT, "attachment"), |
| 675 | - ("/tahinli/pub/main/page.html", TEXT, "attachment"), | 677 | + ("/tahinli/pub/main/pic.png", "image/png", ""), |
| 676 | - ("/tahinli/pub/main/pic.png", "image/png", ""), | 678 | + ]; |
| 677 | - ]; | 679 | + for (uri, content_type, disposition) in cases { |
| 678 | - for (uri, content_type, disposition) in cases { | 680 | + let res = get(&fx.router, "GET", uri, None).await; |
| 679 | - let res = get(&fx.router, "GET", uri, None).await; | 681 | + assert_eq!(res.status(), 200, "case {uri}"); |
| 680 | - assert_eq!(res.status(), 200, "case {uri}"); | 682 | + assert_eq!(header_str(&res, "content-type"), content_type, "case {uri}"); |
| 681 | - assert_eq!(header_str(&res, "content-type"), content_type, "case {uri}"); | 683 | + assert_eq!( |
| 682 | - assert_eq!( | 684 | + header_str(&res, "content-disposition"), |
| 683 | - header_str(&res, "content-disposition"), | 685 | + disposition, |
| 684 | - disposition, | 686 | + "case {uri}" |
| 685 | - "case {uri}" | 687 | + ); |
| 686 | - ); | 688 | + } |
| 687 | } | 689 | } |
| 688 | - } | ||
| 689 | 690 | ||
| 690 | - #[tokio::test] | 691 | + // Scenario: a symlink is served as its target text. |
| 691 | - async fn symlink_is_served_as_its_target_text() { | 692 | + { |
| 692 | - let fx = fixture().await; | 693 | + let res = get(&fx.router, "GET", "/tahinli/pub/main/link", None).await; |
| 693 | - let res = get(&fx.router, "GET", "/tahinli/pub/main/link", None).await; | 694 | + assert_eq!(res.status(), 200); |
| 694 | - assert_eq!(res.status(), 200); | 695 | + assert_eq!(header_str(&res, "content-type"), TEXT); |
| 695 | - assert_eq!(header_str(&res, "content-type"), TEXT); | 696 | + assert_eq!(body_bytes(res).await, b"hello.txt"); |
| 696 | - assert_eq!(body_bytes(res).await, b"hello.txt"); | 697 | + } |
| 697 | - } | ||
| 698 | 698 | ||
| 699 | - #[tokio::test] | 699 | + // Scenario: a private repo requires a valid, unexpired token for that repo. |
| 700 | - async fn private_repo_requires_a_valid_token() { | 700 | + { |
| 701 | - let fx = fixture().await; | 701 | + let path = "/tahinli/priv/main/hello.txt"; |
| 702 | - let path = "/tahinli/priv/main/hello.txt"; | 702 | + let now = now_unix(); |
| 703 | - let now = now_unix(); | ||
| 704 | - | ||
| 705 | - let none = get(&fx.router, "GET", path, None).await; | ||
| 706 | - assert_eq!(none.status(), 404); | ||
| 707 | - assert_eq!(header_str(&none, "cache-control"), CACHE_NONE); | ||
| 708 | - | ||
| 709 | - let valid = sign_raw(&KEY, &fx.private.id, now + 600); | ||
| 710 | - let res = get(&fx.router, "GET", &format!("{path}?token={valid}"), None).await; | ||
| 711 | - assert_eq!(res.status(), 200); | ||
| 712 | - assert_eq!(header_str(&res, "cache-control"), CACHE_PRIVATE); | ||
| 713 | - assert_eq!(body_bytes(res).await, b"secret\n"); | ||
| 714 | - | ||
| 715 | - let expired = sign_raw(&KEY, &fx.private.id, now - 1); | ||
| 716 | - let res = get(&fx.router, "GET", &format!("{path}?token={expired}"), None).await; | ||
| 717 | - assert_eq!(res.status(), 404); | ||
| 718 | - | ||
| 719 | - let other = sign_raw(&KEY, &fx.public.id, now + 600); | ||
| 720 | - let res = get(&fx.router, "GET", &format!("{path}?token={other}"), None).await; | ||
| 721 | - assert_eq!(res.status(), 404); | ||
| 722 | - } | ||
| 723 | 703 | ||
| 724 | - #[tokio::test] | 704 | + let none = get(&fx.router, "GET", path, None).await; |
| 725 | - async fn missing_repos_and_directories_are_404() { | 705 | + assert_eq!(none.status(), 404); |
| 726 | - let fx = fixture().await; | 706 | + assert_eq!(header_str(&none, "cache-control"), CACHE_NONE); |
| 727 | - for uri in [ | 707 | + |
| 728 | - "/tahinli/nope/main/hello.txt", | 708 | + let valid = sign_raw(&KEY, &fx.private.id, now + 600); |
| 729 | - "/tahinli/pub/main/sub", | 709 | + let res = get(&fx.router, "GET", &format!("{path}?token={valid}"), None).await; |
| 730 | - "/tahinli/pub/main", | 710 | + assert_eq!(res.status(), 200); |
| 731 | - ] { | 711 | + assert_eq!(header_str(&res, "cache-control"), CACHE_PRIVATE); |
| 732 | - let res = get(&fx.router, "GET", uri, None).await; | 712 | + assert_eq!(body_bytes(res).await, b"secret\n"); |
| 733 | - assert_eq!(res.status(), 404, "case {uri}"); | 713 | + |
| 714 | + let expired = sign_raw(&KEY, &fx.private.id, now - 1); | ||
| 715 | + let res = get(&fx.router, "GET", &format!("{path}?token={expired}"), None).await; | ||
| 716 | + assert_eq!(res.status(), 404); | ||
| 717 | + | ||
| 718 | + let other = sign_raw(&KEY, &fx.public.id, now + 600); | ||
| 719 | + let res = get(&fx.router, "GET", &format!("{path}?token={other}"), None).await; | ||
| 720 | + assert_eq!(res.status(), 404); | ||
| 734 | } | 721 | } |
| 735 | - } | ||
| 736 | 722 | ||
| 737 | - #[tokio::test] | 723 | + // Scenario: missing repos and directories are 404. |
| 738 | - async fn etag_gives_304() { | 724 | + { |
| 739 | - let fx = fixture().await; | 725 | + for uri in [ |
| 740 | - let first = get(&fx.router, "GET", "/tahinli/pub/main/hello.txt", None).await; | 726 | + "/tahinli/nope/main/hello.txt", |
| 741 | - let etag = header_str(&first, "etag"); | 727 | + "/tahinli/pub/main/sub", |
| 742 | - assert!(etag.starts_with('"') && etag.ends_with('"'), "etag {etag}"); | 728 | + "/tahinli/pub/main", |
| 743 | - let again = get( | 729 | + ] { |
| 744 | - &fx.router, | 730 | + let res = get(&fx.router, "GET", uri, None).await; |
| 745 | - "GET", | 731 | + assert_eq!(res.status(), 404, "case {uri}"); |
| 746 | - "/tahinli/pub/main/hello.txt", | 732 | + } |
| 747 | - Some(&etag), | 733 | + } |
| 748 | - ) | ||
| 749 | - .await; | ||
| 750 | - assert_eq!(again.status(), 304); | ||
| 751 | - assert_eq!(header_str(&again, "etag"), etag); | ||
| 752 | - } | ||
| 753 | 734 | ||
| 754 | - #[tokio::test] | 735 | + // Scenario: a matching etag gives 304. |
| 755 | - async fn head_has_headers_and_no_body() { | 736 | + { |
| 756 | - let fx = fixture().await; | 737 | + let first = get(&fx.router, "GET", "/tahinli/pub/main/hello.txt", None).await; |
| 757 | - let res = get(&fx.router, "HEAD", "/tahinli/pub/main/hello.txt", None).await; | 738 | + let etag = header_str(&first, "etag"); |
| 758 | - assert_eq!(res.status(), 200); | 739 | + assert!(etag.starts_with('"') && etag.ends_with('"'), "etag {etag}"); |
| 759 | - assert_eq!(header_str(&res, "content-type"), TEXT); | 740 | + let again = get( |
| 760 | - assert_eq!(header_str(&res, "content-length"), "10"); | 741 | + &fx.router, |
| 761 | - assert!(body_bytes(res).await.is_empty()); | 742 | + "GET", |
| 762 | - } | 743 | + "/tahinli/pub/main/hello.txt", |
| 744 | + Some(&etag), | ||
| 745 | + ) | ||
| 746 | + .await; | ||
| 747 | + assert_eq!(again.status(), 304); | ||
| 748 | + assert_eq!(header_str(&again, "etag"), etag); | ||
| 749 | + } | ||
| 763 | 750 | ||
| 764 | - #[tokio::test] | 751 | + // Scenario: HEAD has headers and no body. |
| 765 | - async fn large_file_streams_intact() { | 752 | + { |
| 766 | - let fx = fixture().await; | 753 | + let res = get(&fx.router, "HEAD", "/tahinli/pub/main/hello.txt", None).await; |
| 767 | - let res = get(&fx.router, "GET", "/tahinli/pub/main/big.txt", None).await; | 754 | + assert_eq!(res.status(), 200); |
| 768 | - assert_eq!(res.status(), 200); | 755 | + assert_eq!(header_str(&res, "content-type"), TEXT); |
| 769 | - assert_eq!(header_str(&res, "content-length"), "9437184"); | 756 | + assert_eq!(header_str(&res, "content-length"), "10"); |
| 770 | - let expected: Vec<u8> = b"0123456789abcdef\n" | 757 | + assert!(body_bytes(res).await.is_empty()); |
| 771 | - .iter() | 758 | + } |
| 772 | - .cycle() | ||
| 773 | - .take(9 * 1024 * 1024) | ||
| 774 | - .copied() | ||
| 775 | - .collect(); | ||
| 776 | - let body = body_bytes(res).await; | ||
| 777 | - assert!(body == expected, "streamed body differs"); | ||
| 778 | - } | ||
| 779 | 759 | ||
| 780 | - #[tokio::test] | 760 | + // Scenario: a large file streams intact. |
| 781 | - async fn app_routes_are_not_on_the_raw_origin() { | 761 | + { |
| 782 | - let fx = fixture().await; | 762 | + let res = get(&fx.router, "GET", "/tahinli/pub/main/big.txt", None).await; |
| 783 | - for path in ["/login", "/settings/keys", "/tahinli/pub/tree/main"] { | 763 | + assert_eq!(res.status(), 200); |
| 784 | - let res = get(&fx.router, "GET", path, None).await; | 764 | + assert_eq!(header_str(&res, "content-length"), "9437184"); |
| 785 | - assert_eq!(res.status(), 404, "{path} is reachable"); | 765 | + let expected: Vec<u8> = b"0123456789abcdef\n" |
| 766 | + .iter() | ||
| 767 | + .cycle() | ||
| 768 | + .take(9 * 1024 * 1024) | ||
| 769 | + .copied() | ||
| 770 | + .collect(); | ||
| 771 | + let body = body_bytes(res).await; | ||
| 772 | + assert!(body == expected, "streamed body differs"); | ||
| 786 | } | 773 | } |
| 787 | - let health = get(&fx.router, "GET", "/healthz", None).await; | ||
| 788 | - assert_eq!(health.status(), 200); | ||
| 789 | - } | ||
| 790 | 774 | ||
| 791 | - #[tokio::test] | 775 | + // Scenario: app routes are not on the raw origin, but healthz is. |
| 792 | - async fn raw_url_encodes_and_signs() { | 776 | + { |
| 793 | - let fx = fixture().await; | 777 | + for path in ["/login", "/settings/keys", "/tahinli/pub/tree/main"] { |
| 794 | - let url = raw_url(&fx.state, &fx.public, "main", "my file#1.txt"); | 778 | + let res = get(&fx.router, "GET", path, None).await; |
| 795 | - assert_eq!( | 779 | + assert_eq!(res.status(), 404, "{path} is reachable"); |
| 796 | - url, | 780 | + } |
| 797 | - "http://127.0.0.1:7661/tahinli/pub/main/my%20file%231.txt" | 781 | + let health = get(&fx.router, "GET", "/healthz", None).await; |
| 798 | - ); | 782 | + assert_eq!(health.status(), 200); |
| 799 | - let private = raw_url(&fx.state, &fx.private, "feature/x", "a b/c#d.txt"); | 783 | + } |
| 800 | - assert!( | 784 | + |
| 801 | - private | 785 | + // Scenario: raw URLs are encoded, and private ones are signed. |
| 802 | - .starts_with("http://127.0.0.1:7661/tahinli/priv/feature/x/a%20b/c%23d.txt?token="), | 786 | + { |
| 803 | - "{private}" | 787 | + let url = raw_url(&fx.state, &fx.public, "main", "my file#1.txt"); |
| 804 | - ); | 788 | + assert_eq!( |
| 805 | - let token = private.rsplit_once("?token=").map(|(_, t)| t).unwrap_or(""); | 789 | + url, |
| 806 | - assert!(verify_raw(&KEY, token, &fx.private.id, now_unix())); | 790 | + "http://127.0.0.1:7661/tahinli/pub/main/my%20file%231.txt" |
| 791 | + ); | ||
| 792 | + let private = raw_url(&fx.state, &fx.private, "feature/x", "a b/c#d.txt"); | ||
| 793 | + assert!( | ||
| 794 | + private.starts_with( | ||
| 795 | + "http://127.0.0.1:7661/tahinli/priv/feature/x/a%20b/c%23d.txt?token=" | ||
| 796 | + ), | ||
| 797 | + "{private}" | ||
| 798 | + ); | ||
| 799 | + let token = private.rsplit_once("?token=").map(|(_, t)| t).unwrap_or(""); | ||
| 800 | + assert!(verify_raw(&KEY, token, &fx.private.id, now_unix())); | ||
| 801 | + } | ||
| 807 | } | 802 | } |
| 808 | } | 803 | } |
crates/forge-web/src/signatures.rsmodified+57 -35
| @@ -697,45 +697,47 @@ mod tests { | |||
| 697 | } | 697 | } |
| 698 | 698 | ||
| 699 | #[tokio::test] | 699 | #[tokio::test] |
| 700 | - async fn ssh_signature_by_a_user_with_matching_email_is_verified() { | 700 | + async fn ssh_badges_per_commit_scenarios() { |
| 701 | + // One fixture (one repo and one signer) for three commits: each scenario pushes its own | ||
| 702 | + // commit and checks only that commit's badge. The cache test below stays separate because | ||
| 703 | + // it deletes the signer's key. | ||
| 701 | let f = fixture().await; | 704 | let f = fixture().await; |
| 702 | - let oid = signed_commit(&f, "tahinli@example.com"); | ||
| 703 | 705 | ||
| 704 | - let badges = badges(&f.state, &f.repo, &[f.base_oid.clone(), oid.clone()]).await; | 706 | + // Scenario: a signature by a user whose email matches the committer is verified. |
| 705 | - assert_eq!(badges.len(), 1, "the unsigned base commit has no badge"); | 707 | + let oid = signed_commit(&f, "tahinli@example.com"); |
| 708 | + let got = badges(&f.state, &f.repo, &[f.base_oid.clone(), oid.clone()]).await; | ||
| 709 | + assert_eq!(got.len(), 1, "the unsigned base commit has no badge"); | ||
| 706 | assert_eq!( | 710 | assert_eq!( |
| 707 | - badges.get(&oid), | 711 | + got.get(&oid), |
| 708 | Some(&SigBadge::Verified { | 712 | Some(&SigBadge::Verified { |
| 709 | signer: "tahinli".into(), | 713 | signer: "tahinli".into(), |
| 710 | key: ssh_fingerprint(&f.pub_path), | 714 | key: ssh_fingerprint(&f.pub_path), |
| 711 | - }) | 715 | + }), |
| 716 | + "matching email" | ||
| 712 | ); | 717 | ); |
| 713 | - } | ||
| 714 | 718 | ||
| 715 | - #[tokio::test] | 719 | + // Scenario: a committer email that differs from the signer is unverified. |
| 716 | - async fn committer_email_mismatch_is_unverified() { | ||
| 717 | - let f = fixture().await; | ||
| 718 | let oid = signed_commit(&f, "bob@example.com"); | 720 | let oid = signed_commit(&f, "bob@example.com"); |
| 719 | - | 721 | + let got = badges(&f.state, &f.repo, std::slice::from_ref(&oid)).await; |
| 720 | - let badges = badges(&f.state, &f.repo, std::slice::from_ref(&oid)).await; | ||
| 721 | assert!( | 722 | assert!( |
| 722 | - matches!(badges.get(&oid), Some(SigBadge::Unverified { .. })), | 723 | + matches!(got.get(&oid), Some(SigBadge::Unverified { .. })), |
| 723 | - "{badges:?}" | 724 | + "mismatched email: {got:?}" |
| 724 | ); | 725 | ); |
| 725 | - } | ||
| 726 | 726 | ||
| 727 | - #[tokio::test] | 727 | + // Scenario: an unsigned commit gets no badge and is cached as unsigned. |
| 728 | - async fn unsigned_commits_are_omitted() { | 728 | + let got = badges(&f.state, &f.repo, std::slice::from_ref(&f.base_oid)).await; |
| 729 | - let f = fixture().await; | 729 | + assert!(got.is_empty(), "unsigned commit"); |
| 730 | - let badges = badges(&f.state, &f.repo, std::slice::from_ref(&f.base_oid)).await; | ||
| 731 | - assert!(badges.is_empty()); | ||
| 732 | let cached = f | 730 | let cached = f |
| 733 | .state | 731 | .state |
| 734 | .store | 732 | .store |
| 735 | .cached_signatures(&f.repo.id, std::slice::from_ref(&f.base_oid)) | 733 | .cached_signatures(&f.repo.id, std::slice::from_ref(&f.base_oid)) |
| 736 | .await | 734 | .await |
| 737 | .unwrap(); | 735 | .unwrap(); |
| 738 | - assert_eq!(cached[&f.base_oid].status, SigStatus::Unsigned); | 736 | + assert_eq!( |
| 737 | + cached[&f.base_oid].status, | ||
| 738 | + SigStatus::Unsigned, | ||
| 739 | + "unsigned cache" | ||
| 740 | + ); | ||
| 739 | } | 741 | } |
| 740 | 742 | ||
| 741 | #[tokio::test] | 743 | #[tokio::test] |
| @@ -789,24 +791,44 @@ mod tests { | |||
| 789 | } | 791 | } |
| 790 | 792 | ||
| 791 | #[test] | 793 | #[test] |
| 792 | - fn key_listing_yields_primary_fingerprint_and_emails() { | 794 | + fn key_listing_parses_one_key_or_rejects() { |
| 793 | - let listing = "pub:u:255:22:ABCDEF0123456789ABCDEF0123456789ABCDEF01:1700000000::::::23::0:\n\ | 795 | + // (name, colon listing, expected Some((primary fingerprint, emails)) or None for rejection) |
| 796 | + let cases: Vec<(&str, &str, Option<(&str, Vec<&str>)>)> = vec![ | ||
| 797 | + ( | ||
| 798 | + "primary fingerprint and emails", | ||
| 799 | + "pub:u:255:22:ABCDEF0123456789ABCDEF0123456789ABCDEF01:1700000000::::::23::0:\n\ | ||
| 794 | fpr:::::::::ABCDEF0123456789ABCDEF0123456789ABCDEF01:\n\ | 800 | fpr:::::::::ABCDEF0123456789ABCDEF0123456789ABCDEF01:\n\ |
| 795 | uid:u::::1700000000::HASH::Tahinli <Tahinli@Example.com>::::::::::0:\n\ | 801 | uid:u::::1700000000::HASH::Tahinli <Tahinli@Example.com>::::::::::0:\n\ |
| 796 | uid:u::::1700000000::HASH::Tahinli Work <work@example.org>::::::::::0:\n\ | 802 | uid:u::::1700000000::HASH::Tahinli Work <work@example.org>::::::::::0:\n\ |
| 797 | sub:u:255:18:1111111111111111111111111111111111111111:1700000000:::::e::::::23:\n\ | 803 | sub:u:255:18:1111111111111111111111111111111111111111:1700000000:::::e::::::23:\n\ |
| 798 | -fpr:::::::::1111111111111111111111111111111111111111:\n"; | 804 | +fpr:::::::::1111111111111111111111111111111111111111:\n", |
| 799 | - let (fp, emails) = parse_key_listing(listing).unwrap(); | 805 | + Some(( |
| 800 | - assert_eq!(fp, "ABCDEF0123456789ABCDEF0123456789ABCDEF01"); | 806 | + "ABCDEF0123456789ABCDEF0123456789ABCDEF01", |
| 801 | - assert_eq!(emails, vec!["tahinli@example.com", "work@example.org"]); | 807 | + vec!["tahinli@example.com", "work@example.org"], |
| 802 | - } | 808 | + )), |
| 803 | - | 809 | + ), |
| 804 | - #[test] | 810 | + ( |
| 805 | - fn key_listing_rejects_two_keys_and_missing_email() { | 811 | + "two keys", |
| 806 | - let two = "pub:u:255:22:AAAA:1::::::23::0:\nfpr:::::::::AAAA:\npub:u:255:22:BBBB:1::::::23::0:\nfpr:::::::::BBBB:\n"; | 812 | + "pub:u:255:22:AAAA:1::::::23::0:\nfpr:::::::::AAAA:\npub:u:255:22:BBBB:1::::::23::0:\nfpr:::::::::BBBB:\n", |
| 807 | - assert!(parse_key_listing(two).is_err()); | 813 | + None, |
| 808 | - let no_mail = "pub:u:255:22:AAAA:1::::::23::0:\nfpr:::::::::AAAA:\nuid:u::::1::HASH::Nobody::::::::::0:\n"; | 814 | + ), |
| 809 | - assert!(parse_key_listing(no_mail).is_err()); | 815 | + ( |
| 816 | + "missing email", | ||
| 817 | + "pub:u:255:22:AAAA:1::::::23::0:\nfpr:::::::::AAAA:\nuid:u::::1::HASH::Nobody::::::::::0:\n", | ||
| 818 | + None, | ||
| 819 | + ), | ||
| 820 | + ]; | ||
| 821 | + for (name, listing, want) in cases { | ||
| 822 | + let got = parse_key_listing(listing); | ||
| 823 | + match want { | ||
| 824 | + Some((fp, emails)) => { | ||
| 825 | + let (got_fp, got_emails) = got.unwrap_or_else(|e| panic!("case {name}: {e}")); | ||
| 826 | + assert_eq!(got_fp, fp, "case {name}"); | ||
| 827 | + assert_eq!(got_emails, emails, "case {name}"); | ||
| 828 | + } | ||
| 829 | + None => assert!(got.is_err(), "case {name}"), | ||
| 830 | + } | ||
| 831 | + } | ||
| 810 | } | 832 | } |
| 811 | 833 | ||
| 812 | /// Runs `gpg` against `home` with `--batch`, asserting success. Returns stdout. | 834 | /// Runs `gpg` against `home` with `--batch`, asserting success. Returns stdout. |
crates/forge-web/src/webhooks/client.rsmodified+18 -17
| @@ -606,23 +606,24 @@ mod tests { | |||
| 606 | } | 606 | } |
| 607 | 607 | ||
| 608 | #[tokio::test] | 608 | #[tokio::test] |
| 609 | - async fn send_accepts_allowed_loopback_cidr() { | 609 | + async fn send_accepts_allowed_cidr_and_hostname() { |
| 610 | - let addr = serve(ok_response()).await; | 610 | + // (name, allowed entry, host used in the URL); both must deliver the same response. |
| 611 | - let client = HookClient::new(policy(true, &["127.0.0.1/32"])).expect("client"); | 611 | + let cases = [ |
| 612 | - let outcome = post(&client, &format!("http://127.0.0.1:{}/hook", addr.port())).await; | 612 | + ("loopback cidr", "127.0.0.1/32", "127.0.0.1"), |
| 613 | - assert_eq!(outcome.error, None); | 613 | + ("hostname", "localhost", "localhost"), |
| 614 | - assert_eq!(outcome.status, Some(200)); | 614 | + ]; |
| 615 | - assert_eq!(outcome.body, "ok"); | 615 | + for (name, allowed, host) in cases { |
| 616 | - assert!(outcome.headers.iter().any(|(k, _)| k == "content-type")); | 616 | + let addr = serve(ok_response()).await; |
| 617 | - } | 617 | + let client = HookClient::new(policy(true, &[allowed])).expect("client"); |
| 618 | - | 618 | + let outcome = post(&client, &format!("http://{host}:{}/hook", addr.port())).await; |
| 619 | - #[tokio::test] | 619 | + assert_eq!(outcome.error, None, "case {name}"); |
| 620 | - async fn send_accepts_allowed_hostname() { | 620 | + assert_eq!(outcome.status, Some(200), "case {name}"); |
| 621 | - let addr = serve(ok_response()).await; | 621 | + assert_eq!(outcome.body, "ok", "case {name}"); |
| 622 | - let client = HookClient::new(policy(true, &["localhost"])).expect("client"); | 622 | + assert!( |
| 623 | - let outcome = post(&client, &format!("http://localhost:{}/hook", addr.port())).await; | 623 | + outcome.headers.iter().any(|(k, _)| k == "content-type"), |
| 624 | - assert_eq!(outcome.error, None); | 624 | + "case {name}" |
| 625 | - assert_eq!(outcome.status, Some(200)); | 625 | + ); |
| 626 | + } | ||
| 626 | } | 627 | } |
| 627 | 628 | ||
| 628 | #[tokio::test] | 629 | #[tokio::test] |