diff --git a/crates/forge-web/src/pages/notifications.rs b/crates/forge-web/src/pages/notifications.rs index ec23464..bf024fd 100644 --- a/crates/forge-web/src/pages/notifications.rs +++ b/crates/forge-web/src/pages/notifications.rs @@ -1311,25 +1311,14 @@ mod tests { assert!(page.body.contains("Mark all as read")); } + /// Another user cannot open or mark read a row that is not theirs. The owner's open redirects + /// to the issue and marks the row read. #[tokio::test] - async fn open_marks_read_and_redirects_to_the_issue() { - let site = site().await; - let w = world(&site, Visibility::Public).await; - let cookie = sign_in(&site.store, &w.bob.id, "bob-token").await; - notify(&site, &w, Reason::Comment).await; - let id = first_note_id(&site, &w.bob.id).await; - - let res = get(&site, &format!("/notifications/{id}/open"), Some(&cookie)).await; - assert_eq!(res.status, 303); - assert_eq!(res.location.as_deref(), Some("/tahinli/demo/issues/1")); - assert_eq!(site.store.unread_count(&w.bob.id).await.expect("count"), 0); - } - - #[tokio::test] - async fn another_users_notification_is_404() { + async fn another_users_notification_is_404_and_the_owner_opens_it() { let site = site().await; let w = world(&site, Visibility::Public).await; let tahinli_cookie = sign_in(&site.store, &w.tahinli.id, "tahinli-token").await; + let bob_cookie = sign_in(&site.store, &w.bob.id, "bob-token").await; notify(&site, &w, Reason::Comment).await; let id = first_note_id(&site, &w.bob.id).await; @@ -1351,6 +1340,12 @@ mod tests { .await; assert_eq!(read.status, 303); assert_eq!(site.store.unread_count(&w.bob.id).await.expect("count"), 1); + + // Bob opens his own row: it redirects to the issue and marks the row read. + let res = get(&site, &format!("/notifications/{id}/open"), Some(&bob_cookie)).await; + assert_eq!(res.status, 303); + assert_eq!(res.location.as_deref(), Some("/tahinli/demo/issues/1")); + assert_eq!(site.store.unread_count(&w.bob.id).await.expect("count"), 0); } #[tokio::test] @@ -1365,13 +1360,6 @@ mod tests { assert_eq!(site.store.unread_count(&w.bob.id).await.expect("count"), 0); } - #[tokio::test] - async fn the_stream_needs_a_session() { - let site = site().await; - let res = get(&site, "/notifications/stream", None).await; - assert_eq!(res.status, 401); - } - #[tokio::test] async fn the_stream_sends_the_count_then_each_change() { let site = site().await; @@ -1423,7 +1411,7 @@ mod tests { } #[tokio::test] - async fn subscription_toggle_on_an_issue() { + async fn subscription_toggles_on_an_issue_and_unknown_pull_is_404() { let site = site().await; let w = world(&site, Visibility::Public).await; let cookie = sign_in(&site.store, &w.bob.id, "bob-token").await; @@ -1461,13 +1449,8 @@ mod tests { .await .expect("sub"); assert_eq!(sub, Some((SubState::Ignored, Reason::Subscribed))); - } - #[tokio::test] - async fn pull_subscription_404_for_unknown_number() { - let site = site().await; - let w = world(&site, Visibility::Public).await; - let cookie = sign_in(&site.store, &w.bob.id, "bob-token").await; + // A pull request number that does not exist is a 404 on the pulls subscription. let res = post( &site, "/tahinli/demo/pulls/1/subscription", @@ -1515,12 +1498,14 @@ mod tests { } #[tokio::test] - async fn anonymous_viewer_is_redirected_to_login() { + async fn anonymous_viewers_are_sent_to_login_and_the_stream_needs_a_session() { let site = site().await; let _w = world(&site, Visibility::Public).await; let res = get(&site, "/notifications", None).await; assert_eq!(res.status, 303); assert!(res.location.unwrap_or_default().starts_with("/login")); + let stream = get(&site, "/notifications/stream", None).await; + assert_eq!(stream.status, 401); } #[tokio::test] @@ -1546,7 +1531,7 @@ mod tests { } #[tokio::test] - async fn discussion_reply_shows_in_inbox_and_open_marks_read() { + async fn discussion_replies_show_in_the_inbox_and_clear_by_open_or_mark_all() { let site = site().await; let w = world(&site, Visibility::Public).await; // The world's issue takes number 1, so the discussion is #2. @@ -1589,12 +1574,8 @@ mod tests { site.store.unread_count(&w.tahinli.id).await.expect("count"), 0 ); - } - #[tokio::test] - async fn discussion_rows_are_marked_read_by_mark_all() { - let site = site().await; - let w = world(&site, Visibility::Public).await; + // A second discussion, cleared by mark all read. let discussion = site .store .create_discussion( @@ -1610,7 +1591,6 @@ mod tests { .add_discussion_comment(&discussion.id, &w.bob.id, None, "+1", false) .await .expect("reply"); - let tahinli = sign_in(&site.store, &w.tahinli.id, "tahinli-token").await; assert_eq!( site.store.unread_count(&w.tahinli.id).await.expect("count"), 1 diff --git a/crates/forge-web/src/pages/repo_mirrors.rs b/crates/forge-web/src/pages/repo_mirrors.rs index 9325b01..b40b782 100644 --- a/crates/forge-web/src/pages/repo_mirrors.rs +++ b/crates/forge-web/src/pages/repo_mirrors.rs @@ -1149,11 +1149,14 @@ mod tests { assert_eq!(redacted_url("not a url"), "(unreadable URL)"); } + /// One seeded push mirror with a secret. Non-admins get 404 on every mirror route. The owner's + /// pages never render the secret or the query string, and the list follows the stored + /// Turkish language once that preference is set last. #[tokio::test] - async fn non_admins_get_not_found() { + async fn mirror_pages_hide_from_non_admins_and_redact_for_the_owner() { let dir = tempfile::tempdir().expect("tempdir"); let (store, app, repo) = fixture(dir.path()).await; - let id = push_mirror(&store, &repo, None).await; + let id = push_mirror(&store, &repo, Some(b"hunter2-secret")).await; for token in ["bob-token", "carol-token"] { let (status, _, _) = send( &app, @@ -1192,41 +1195,8 @@ mod tests { ) .await; assert_eq!(status, 200); - } - - #[tokio::test] - async fn list_page_follows_the_stored_turkish_language() { - let dir = tempfile::tempdir().expect("tempdir"); - let (store, app, _repo) = fixture(dir.path()).await; - let tahinli = store - .user_by_username("tahinli") - .await - .expect("lookup") - .expect("tahinli"); - store - .set_ui_prefs(&tahinli.id, "auto", "tr") - .await - .expect("set language"); - let (status, _, body) = send( - &app, - "GET", - "/tahinli/proj/settings/mirrors", - &cookie_for("tahinli-token"), - &[], - ) - .await; - assert_eq!(status, 200, "{body}"); - assert!( - body.contains("

Yansılar

"), - "Turkish heading missing" - ); - } - #[tokio::test] - async fn secret_is_never_rendered_and_urls_are_redacted() { - let dir = tempfile::tempdir().expect("tempdir"); - let (store, app, repo) = fixture(dir.path()).await; - let id = push_mirror(&store, &repo, Some(b"hunter2-secret")).await; + // The owner's list and edit pages hide the secret and the query string. let cookie = cookie_for("tahinli-token"); for uri in [ "/tahinli/proj/settings/mirrors".to_owned(), @@ -1249,30 +1219,50 @@ mod tests { .expect("mirror") .has_secret ); + + // Last, so the Turkish preference does not change the English checks above. + let tahinli = store + .user_by_username("tahinli") + .await + .expect("lookup") + .expect("tahinli"); + store + .set_ui_prefs(&tahinli.id, "auto", "tr") + .await + .expect("set language"); + let (status, _, body) = send( + &app, + "GET", + "/tahinli/proj/settings/mirrors", + &cookie, + &[], + ) + .await; + assert_eq!(status, 200, "{body}"); + assert!(body.contains("

Yansılar

"), "Turkish heading missing"); } + /// The push form refuses a loopback destination and stores nothing, then adds a good mirror + /// that a sync-now makes due. #[tokio::test] - async fn loopback_url_is_rejected_by_the_form() { + async fn push_form_refuses_loopback_then_adds_and_syncs() { let dir = tempfile::tempdir().expect("tempdir"); let (store, app, repo) = fixture(dir.path()).await; + let cookie = cookie_for("tahinli-token"); + + // A loopback destination is shown as an error and nothing is stored. let (status, _, body) = send( &app, "POST", "/tahinli/proj/settings/mirrors", - &cookie_for("tahinli-token"), + &cookie, &[("url", "https://127.0.0.1/remote.git"), ("interval", "480")], ) .await; - assert_eq!(status, 200); + assert_eq!(status, 200, "loopback form"); assert!(body.contains("destination not allowed"), "error not shown"); assert!(store.list_mirrors(&repo.id).await.expect("list").is_empty()); - } - #[tokio::test] - async fn add_push_mirror_then_sync_now_schedules_it() { - let dir = tempfile::tempdir().expect("tempdir"); - let (store, app, repo) = fixture(dir.path()).await; - let cookie = cookie_for("tahinli-token"); let (status, location, _) = send( &app, "POST", @@ -1315,8 +1305,10 @@ mod tests { ); } + /// One seeded mirror with a secret. An edit can clear the password and keep the stored URL. + /// Then delete needs the confirm box before it removes the mirror. #[tokio::test] - async fn edit_clears_the_password_on_request() { + async fn edit_clears_the_password_and_delete_needs_the_confirm_box() { let dir = tempfile::tempdir().expect("tempdir"); let (store, app, repo) = fixture(dir.path()).await; let id = push_mirror(&store, &repo, Some(b"hunter2-secret")).await; @@ -1341,13 +1333,7 @@ mod tests { mirror.url, "https://example.com/remote.git?token=abc123", "a blank URL keeps the stored one" ); - } - #[tokio::test] - async fn delete_needs_the_confirm_box() { - let dir = tempfile::tempdir().expect("tempdir"); - let (store, app, repo) = fixture(dir.path()).await; - let id = push_mirror(&store, &repo, None).await; let cookie = cookie_for("tahinli-token"); let uri = format!("/tahinli/proj/settings/mirrors/{id}/delete"); let (status, _, _) = send(&app, "POST", &uri, &cookie, &[]).await; @@ -1382,10 +1368,25 @@ mod tests { run(&["update-ref", "refs/heads/main", &commit]); } + /// On an empty repo a pull mirror can be added, and only the owner may add one: non-admins + /// get 404 first and store nothing. #[tokio::test] - async fn pull_mirror_can_be_added_to_an_empty_repo() { + async fn pull_mirror_can_be_added_to_an_empty_repo_by_the_owner_only() { let dir = tempfile::tempdir().expect("tempdir"); let (store, app, repo) = fixture(dir.path()).await; + for token in ["bob-token", "carol-token"] { + let (status, _, _) = send( + &app, + "POST", + "/tahinli/proj/settings/mirrors", + &cookie_for(token), + &[("direction", "pull"), ("url", REMOTE), ("interval", "60")], + ) + .await; + assert_eq!(status, 404, "pull add as {token}"); + } + assert!(store.list_mirrors(&repo.id).await.expect("list").is_empty()); + let cookie = cookie_for("tahinli-token"); let (status, _, body) = send(&app, "GET", "/tahinli/proj/settings/mirrors", &cookie, &[]).await; @@ -1468,21 +1469,4 @@ mod tests { assert!(store.list_mirrors(&repo.id).await.expect("list").is_empty()); } - #[tokio::test] - async fn pull_mirror_add_is_404_for_non_admins() { - let dir = tempfile::tempdir().expect("tempdir"); - let (store, app, repo) = fixture(dir.path()).await; - for token in ["bob-token", "carol-token"] { - let (status, _, _) = send( - &app, - "POST", - "/tahinli/proj/settings/mirrors", - &cookie_for(token), - &[("direction", "pull"), ("url", REMOTE), ("interval", "60")], - ) - .await; - assert_eq!(status, 404, "pull add as {token}"); - } - assert!(store.list_mirrors(&repo.id).await.expect("list").is_empty()); - } } diff --git a/crates/forge-web/src/pages/repo_settings_live_tests.rs b/crates/forge-web/src/pages/repo_settings_live_tests.rs index c64dbe0..0aba66e 100644 --- a/crates/forge-web/src/pages/repo_settings_live_tests.rs +++ b/crates/forge-web/src/pages/repo_settings_live_tests.rs @@ -83,23 +83,31 @@ async fn fixture(site: &Site) -> (String, String) { (owner_cookie, reader_cookie) } +/// Who may open each live stream. The settings stream is admin-only. The security and transfer +/// streams take the same page check as their pages: a reader gets in, anonymous does not. #[tokio::test] -async fn the_settings_stream_is_for_admins_only() { +async fn the_streams_check_who_may_open_them() { let site = site().await; let (owner_cookie, reader_cookie) = fixture(&site).await; let owner = get(&site, "/owner/proj/settings/live", Some(&owner_cookie)).await; - assert_eq!(owner.status().as_u16(), 200); + assert_eq!(owner.status().as_u16(), 200, "settings: owner"); assert_eq!( owner.headers().get("content-type").unwrap(), - "text/event-stream" + "text/event-stream", + "settings: owner content type" ); - let reader = get(&site, "/owner/proj/settings/live", Some(&reader_cookie)).await; - assert_eq!(reader.status().as_u16(), 403); - + assert_eq!(reader.status().as_u16(), 403, "settings: reader"); let anonymous = get(&site, "/owner/proj/settings/live", None).await; - assert_ne!(anonymous.status().as_u16(), 200); + assert_ne!(anonymous.status().as_u16(), 200, "settings: anonymous"); + + for path in ["/owner/proj/security/live", "/owner/proj/transfer/live"] { + let reader = get(&site, path, Some(&reader_cookie)).await; + assert_eq!(reader.status().as_u16(), 200, "{path}: reader"); + let anonymous = get(&site, path, None).await; + assert_ne!(anonymous.status().as_u16(), 200, "{path}: anonymous"); + } } #[tokio::test] @@ -126,28 +134,6 @@ async fn a_settings_write_wakes_the_open_settings_stream() { assert_eq!(&changed[..], b"event: changed\ndata: 1\n\n"); } -#[tokio::test] -async fn the_security_stream_takes_the_security_page_check() { - let site = site().await; - let (_, reader_cookie) = fixture(&site).await; - - let reader = get(&site, "/owner/proj/security/live", Some(&reader_cookie)).await; - assert_eq!(reader.status().as_u16(), 200); - let anonymous = get(&site, "/owner/proj/security/live", None).await; - assert_ne!(anonymous.status().as_u16(), 200); -} - -#[tokio::test] -async fn the_transfer_stream_takes_the_transfer_page_check() { - let site = site().await; - let (_, reader_cookie) = fixture(&site).await; - - let reader = get(&site, "/owner/proj/transfer/live", Some(&reader_cookie)).await; - assert_eq!(reader.status().as_u16(), 200); - let anonymous = get(&site, "/owner/proj/transfer/live", None).await; - assert_ne!(anonymous.status().as_u16(), 200); -} - #[tokio::test] async fn a_visibility_change_wakes_the_repo_owner_explore_and_the_repo() { let site = site().await; diff --git a/crates/forge-web/src/pages/repo_webhooks.rs b/crates/forge-web/src/pages/repo_webhooks.rs index 7694d36..114e8cd 100644 --- a/crates/forge-web/src/pages/repo_webhooks.rs +++ b/crates/forge-web/src/pages/repo_webhooks.rs @@ -1246,7 +1246,7 @@ mod tests { } #[tokio::test] - async fn invalid_urls_and_empty_event_list_show_errors() { + async fn invalid_hook_forms_show_errors_and_store_nothing() { let tmp = tempfile::tempdir().expect("tempdir"); let fx = fixture(tmp.path()).await; let cookie = cookie_for("tahinli-token"); @@ -1283,13 +1283,8 @@ mod tests { .expect("hooks") .is_empty() ); - } - #[tokio::test] - async fn long_secret_is_refused() { - let tmp = tempfile::tempdir().expect("tempdir"); - let fx = fixture(tmp.path()).await; - let cookie = cookie_for("tahinli-token"); + // A secret over 256 characters is refused too, and stores nothing. let long = "x".repeat(257); let mut fields = hook_fields(URL, &["ev_push"]); fields.push(("secret", long)); @@ -1346,7 +1341,7 @@ mod tests { } #[tokio::test] - async fn hook_of_another_repo_is_404() { + async fn hook_pages_are_repo_scoped_and_edit_and_delete_keep_their_rules() { let tmp = tempfile::tempdir().expect("tempdir"); let fx = fixture(tmp.path()).await; let other = fx @@ -1381,12 +1376,8 @@ mod tests { .await; assert_eq!(status, 404); assert!(fx.store.webhook(&hook.id).await.expect("lookup").is_some()); - } - #[tokio::test] - async fn edit_with_blank_secret_keeps_the_secret() { - let tmp = tempfile::tempdir().expect("tempdir"); - let fx = fixture(tmp.path()).await; + // An edit with a blank secret keeps the stored one, and a typed one replaces it. let sealed = secret::seal(&KEY, b"old-secret").expect("seal"); let hook = fx .store @@ -1438,12 +1429,8 @@ mod tests { .expect("secret"), "new-secret" ); - } - #[tokio::test] - async fn delete_needs_confirmation_then_removes_the_hook() { - let tmp = tempfile::tempdir().expect("tempdir"); - let fx = fixture(tmp.path()).await; + // Delete needs the confirm box, then removes the hook. let sealed = secret::seal(&KEY, b"s").expect("seal"); let hook = fx .store @@ -1473,7 +1460,7 @@ mod tests { } #[tokio::test] - async fn delivery_body_is_escaped_on_the_hook_page() { + async fn delivery_bodies_are_escaped_and_redeliver_queues_one_more() { let tmp = tempfile::tempdir().expect("tempdir"); let fx = fixture(tmp.path()).await; let sealed = secret::seal(&KEY, b"s").expect("seal"); @@ -1506,12 +1493,8 @@ mod tests { assert!(body.contains("<script>alert(1)"), "{body}"); assert!(!body.contains("