diff --git a/crates/forge-web/src/pages/issue_meta_tests.rs b/crates/forge-web/src/pages/issue_meta_tests.rs index 8952021..25f1fb6 100644 --- a/crates/forge-web/src/pages/issue_meta_tests.rs +++ b/crates/forge-web/src/pages/issue_meta_tests.rs @@ -150,15 +150,63 @@ async fn seeded() -> Seed { const FORGE_LABELS: &str = "/tahinli/forge/labels"; +/// Label and milestone writes on forge, in the order the cycle needs: private repos 404 for +/// anonymous viewers, CSRF and role refusals first, then the create, duplicate, edit and delete +/// cycle (the list ends empty), then the archived refusal last. #[tokio::test] -async fn owner_creates_edits_and_deletes_a_label() { +async fn label_writes_follow_csrf_roles_and_archive_rules() { let s = seeded().await; - let cookie = Some(s.tahinli_cookie.as_str()); + let store = &s.site.store; + let owner = Some(s.tahinli_cookie.as_str()); + + // [anonymous] private repo pages are 404, the public milestones page renders. + for uri in ["/tahinli/secret/labels", "/tahinli/secret/milestones"] { + let res = call(&s.site, "GET", uri, None, None).await; + assert_eq!(res.status, 404, "private {uri}"); + } + let res = call(&s.site, "GET", "/tahinli/forge/milestones", None, None).await; + assert_eq!(res.status, 200, "public milestones"); + + // [csrf] label and milestone writes without the token are refused. + let no_token = Some( + form_urlencoded::Serializer::new(String::new()) + .extend_pairs([("name", "bug"), ("color", "#ff0000")]) + .finish(), + ); + let res = call(&s.site, "POST", FORGE_LABELS, owner, no_token).await; + assert_eq!(res.status, 403, "csrf label"); + let res = call( + &s.site, + "POST", + "/tahinli/forge/milestones", + owner, + Some( + form_urlencoded::Serializer::new(String::new()) + .extend_pairs([("title", "v1")]) + .finish(), + ), + ) + .await; + assert_eq!(res.status, 403, "csrf milestone"); + + // [role] a read-only collaborator cannot create labels. + let res = call( + &s.site, + "POST", + FORGE_LABELS, + Some(s.bob_cookie.as_str()), + form(&[("name", "bug"), ("color", "#ff0000")]), + ) + .await; + assert_eq!(res.status, 403, "read-only collaborator"); + assert!(store.list_labels(&s.repo_id).await.expect("labels").is_empty()); + + // [create] the owner creates a label; a case-insensitive duplicate is refused. let created = call( &s.site, "POST", FORGE_LABELS, - cookie, + owner, form(&[ ("name", "bug"), ("color", "#FF0000"), @@ -166,25 +214,40 @@ async fn owner_creates_edits_and_deletes_a_label() { ]), ) .await; - assert_eq!(created.status, 303); + assert_eq!(created.status, 303, "create"); assert_eq!(created.location.as_deref(), Some(FORGE_LABELS)); - let page = call(&s.site, "GET", FORGE_LABELS, None, None).await; - assert_eq!(page.status, 200); - assert!(page.body.contains("label-chip label-")); - assert!(page.body.contains("bug")); + assert_eq!(page.status, 200, "labels page"); + assert!(page.body.contains("label-chip label-"), "labels page chip"); + assert!(page.body.contains("bug"), "labels page name"); assert!(page.body.contains("/tahinli/forge/issues?label=bug")); - - let labels = s.site.store.list_labels(&s.repo_id).await.expect("labels"); - assert_eq!(labels.len(), 1); - assert_eq!(labels[0].color, "#ff0000"); + let labels = store.list_labels(&s.repo_id).await.expect("labels"); + assert_eq!(labels.len(), 1, "one label after create"); + assert_eq!(labels[0].color, "#ff0000", "color is lowercased"); let id = labels[0].id.clone(); + let dup = call( + &s.site, + "POST", + FORGE_LABELS, + owner, + form(&[("name", "BUG"), ("color", "#00ff00")]), + ) + .await; + assert_eq!(dup.status, 200, "duplicate"); + assert!(dup.body.contains("already exists"), "duplicate message"); + assert_eq!( + store.list_labels(&s.repo_id).await.expect("labels").len(), + 1, + "duplicate not stored" + ); + + // [edit, delete] the label is renamed, then removed; the list ends empty. let edited = call( &s.site, "POST", &format!("{FORGE_LABELS}/{id}"), - cookie, + owner, form(&[ ("name", "defect"), ("color", "#00ff00"), @@ -192,90 +255,23 @@ async fn owner_creates_edits_and_deletes_a_label() { ]), ) .await; - assert_eq!(edited.status, 303); - let labels = s.site.store.list_labels(&s.repo_id).await.expect("labels"); - assert_eq!(labels[0].name, "defect"); - assert_eq!(labels[0].color, "#00ff00"); - + assert_eq!(edited.status, 303, "edit"); + let labels = store.list_labels(&s.repo_id).await.expect("labels"); + assert_eq!(labels[0].name, "defect", "edited name"); + assert_eq!(labels[0].color, "#00ff00", "edited color"); let deleted = call( &s.site, "POST", &format!("{FORGE_LABELS}/{id}/delete"), - cookie, + owner, form(&[]), ) .await; - assert_eq!(deleted.status, 303); - assert!( - s.site - .store - .list_labels(&s.repo_id) - .await - .expect("labels") - .is_empty() - ); -} - -#[tokio::test] -async fn duplicate_label_name_shows_an_error() { - let s = seeded().await; - let cookie = Some(s.tahinli_cookie.as_str()); - call( - &s.site, - "POST", - FORGE_LABELS, - cookie, - form(&[("name", "bug"), ("color", "#ff0000")]), - ) - .await; - let dup = call( - &s.site, - "POST", - FORGE_LABELS, - cookie, - form(&[("name", "BUG"), ("color", "#00ff00")]), - ) - .await; - assert_eq!(dup.status, 200); - assert!(dup.body.contains("already exists")); - assert_eq!( - s.site - .store - .list_labels(&s.repo_id) - .await - .expect("labels") - .len(), - 1 - ); -} + assert_eq!(deleted.status, 303, "delete"); + assert!(store.list_labels(&s.repo_id).await.expect("labels").is_empty()); -#[tokio::test] -async fn read_only_collaborator_cannot_write_labels() { - let s = seeded().await; - let res = call( - &s.site, - "POST", - FORGE_LABELS, - Some(s.bob_cookie.as_str()), - form(&[("name", "bug"), ("color", "#ff0000")]), - ) - .await; - assert_eq!(res.status, 403); - assert!( - s.site - .store - .list_labels(&s.repo_id) - .await - .expect("labels") - .is_empty() - ); -} - -#[tokio::test] -async fn archived_repo_refuses_label_writes() { - let s = seeded().await; - s.site - .store + // [archive] an archived repo refuses label writes (last, it changes the repo). + store .set_repo_archived(&s.repo_id, true) .await .expect("archive"); @@ -283,21 +279,27 @@ async fn archived_repo_refuses_label_writes() { &s.site, "POST", FORGE_LABELS, - Some(s.tahinli_cookie.as_str()), + owner, form(&[("name", "bug"), ("color", "#ff0000")]), ) .await; - assert_eq!(res.status, 403); + assert_eq!(res.status, 403, "archived repo"); } +/// Issue metadata on forge: labels (a label from another repo rejects the whole write), +/// assignees (an unknown user is rejected), and milestone set and clear. Then milestone progress, +/// close and delete on `other`, which has no milestones before this test. #[tokio::test] -async fn labels_on_an_issue_are_recorded_and_cross_repo_ids_are_rejected() { +async fn issue_metadata_and_milestone_progress() { let s = seeded().await; let store = &s.site.store; + let carol = Some(s.carol_cookie.as_str()); + let owner = Some(s.tahinli_cookie.as_str()); let issue = store .create_issue(&s.repo_id, &s.tahinli, "Crash", "") .await .expect("issue"); + let uri = format!("/tahinli/forge/issues/{}", issue.number); let mine = store .create_label(&s.repo_id, "bug", "#ff0000", "") .await @@ -306,54 +308,43 @@ async fn labels_on_an_issue_are_recorded_and_cross_repo_ids_are_rejected() { .create_label(&s.other_repo_id, "elsewhere", "#00ff00", "") .await .expect("other label"); - let uri = "/tahinli/forge/issues/1/labels"; + // [labels] a valid label is recorded; a batch with a foreign label id is rejected. let ok = call( &s.site, "POST", - uri, - Some(s.carol_cookie.as_str()), + &format!("{uri}/labels"), + carol, form(&[("label", mine.id.as_str())]), ) .await; - assert_eq!(ok.status, 303); - assert_eq!(ok.location.as_deref(), Some("/tahinli/forge/issues/1")); + assert_eq!(ok.status, 303, "label add"); + assert_eq!(ok.location.as_deref(), Some(uri.as_str())); let events = store.list_events(&issue.id).await.expect("events"); - assert!(events.iter().any(|e| e.kind == "labeled")); - + assert!(events.iter().any(|e| e.kind == "labeled"), "labeled event"); let rejected = call( &s.site, "POST", - uri, - Some(s.carol_cookie.as_str()), + &format!("{uri}/labels"), + carol, form(&[("label", mine.id.as_str()), ("label", theirs.id.as_str())]), ) .await; - assert_eq!(rejected.status, 200); + assert_eq!(rejected.status, 200, "foreign label"); let current = store.issue_labels(&issue.id).await.expect("labels"); - assert_eq!(current.len(), 1); + assert_eq!(current.len(), 1, "foreign label not stored"); assert_eq!(current[0].id, mine.id); -} - -#[tokio::test] -async fn assignees_accept_valid_users_and_reject_unknown_ones() { - let s = seeded().await; - let store = &s.site.store; - let issue = store - .create_issue(&s.repo_id, &s.tahinli, "Crash", "") - .await - .expect("issue"); - let uri = "/tahinli/forge/issues/1/assignees"; + // [assignees] known users are set; an unknown user leaves the two in place. let ok = call( &s.site, "POST", - uri, - Some(s.carol_cookie.as_str()), + &format!("{uri}/assignees"), + carol, form(&[("assignees", "carol, bob")]), ) .await; - assert_eq!(ok.status, 303); + assert_eq!(ok.status, 303, "assign"); let names: Vec = store .issue_assignees(&issue.id) .await @@ -361,42 +352,34 @@ async fn assignees_accept_valid_users_and_reject_unknown_ones() { .into_iter() .map(|(_, name)| name) .collect(); - assert_eq!(names.len(), 2); + assert_eq!(names.len(), 2, "two assignees"); assert!(names.iter().any(|n| n == "carol")); assert!(names.iter().any(|n| n == "bob")); - let unknown = call( &s.site, "POST", - uri, - Some(s.carol_cookie.as_str()), + &format!("{uri}/assignees"), + carol, form(&[("assignees", "ghost")]), ) .await; - assert_eq!(unknown.status, 200); + assert_eq!(unknown.status, 200, "unknown assignee"); assert_eq!( store .issue_assignees(&issue.id) .await .expect("assignees") .len(), - 2 + 2, + "unknown assignee not stored" ); -} -#[tokio::test] -async fn milestone_is_set_and_cleared_on_an_issue() { - let s = seeded().await; - let store = &s.site.store; - store - .create_issue(&s.repo_id, &s.tahinli, "Crash", "") - .await - .expect("issue"); + // [milestone] created on forge, set on the issue by a triage user, then cleared. let created = call( &s.site, "POST", "/tahinli/forge/milestones", - Some(s.tahinli_cookie.as_str()), + owner, form(&[ ("title", "v1"), ("description", "**Ship**"), @@ -404,25 +387,24 @@ async fn milestone_is_set_and_cleared_on_an_issue() { ]), ) .await; - assert_eq!(created.status, 303); + assert_eq!(created.status, 303, "milestone create"); let milestone = store .list_milestones(&s.repo_id, None) .await .expect("milestones") .remove(0); assert_eq!(milestone.due_on.as_deref(), Some("2030-01-02")); - let set = call( &s.site, "POST", - "/tahinli/forge/issues/1/milestone", - Some(s.carol_cookie.as_str()), + &format!("{uri}/milestone"), + carol, form(&[("milestone_id", milestone.id.as_str())]), ) .await; - assert_eq!(set.status, 303); + assert_eq!(set.status, 303, "milestone set"); let issue_now = store - .issue_by_number(&s.repo_id, 1) + .issue_by_number(&s.repo_id, issue.number) .await .expect("issue") .expect("exists"); @@ -430,46 +412,42 @@ async fn milestone_is_set_and_cleared_on_an_issue() { issue_now.milestone_id.as_deref(), Some(milestone.id.as_str()) ); - let cleared = call( &s.site, "POST", - "/tahinli/forge/issues/1/milestone", - Some(s.carol_cookie.as_str()), + &format!("{uri}/milestone"), + carol, form(&[("milestone_id", "")]), ) .await; - assert_eq!(cleared.status, 303); + assert_eq!(cleared.status, 303, "milestone clear"); let issue_now = store - .issue_by_number(&s.repo_id, 1) + .issue_by_number(&s.repo_id, issue.number) .await .expect("issue") .expect("exists"); - assert!(issue_now.milestone_id.is_none()); -} + assert!(issue_now.milestone_id.is_none(), "milestone cleared"); -#[tokio::test] -async fn milestone_progress_close_and_delete_keeps_issues() { - let s = seeded().await; - let store = &s.site.store; + // [progress] on `other`: one open and one closed issue under one milestone. + let other = &s.other_repo_id; let open_issue = store - .create_issue(&s.repo_id, &s.tahinli, "Open one", "") + .create_issue(other, &s.tahinli, "Open one", "") .await .expect("issue"); let done_issue = store - .create_issue(&s.repo_id, &s.tahinli, "Done one", "") + .create_issue(other, &s.tahinli, "Done one", "") .await .expect("issue"); call( &s.site, "POST", - "/tahinli/forge/milestones", - Some(s.tahinli_cookie.as_str()), + "/tahinli/other/milestones", + owner, form(&[("title", "v1"), ("due_on", "2030-01-02")]), ) .await; - let milestone = store - .list_milestones(&s.repo_id, None) + let other_ms = store + .list_milestones(other, None) .await .expect("milestones") .remove(0); @@ -477,13 +455,13 @@ async fn milestone_progress_close_and_delete_keeps_issues() { store .set_issue_milestone( &store - .issue_by_number(&s.repo_id, number) + .issue_by_number(other, number) .await .expect("issue") .expect("exists") .id, &s.tahinli, - Some(milestone.id.as_str()), + Some(other_ms.id.as_str()), ) .await .expect("set milestone"); @@ -498,29 +476,27 @@ async fn milestone_progress_close_and_delete_keeps_issues() { ) .await .expect("close"); - - let list = call(&s.site, "GET", "/tahinli/forge/milestones", None, None).await; - assert_eq!(list.status, 200); + let list = call(&s.site, "GET", "/tahinli/other/milestones", None, None).await; + assert_eq!(list.status, 200, "progress list"); assert!(list.body.contains(r#""#)); assert!(list.body.contains("1 open")); assert!(list.body.contains("1 closed")); - let closed_list = call( &s.site, "GET", - "/tahinli/forge/milestones?state=closed", + "/tahinli/other/milestones?state=closed", None, None, ) .await; assert!(closed_list.body.contains("No closed milestones.")); - let edit_uri = format!("/tahinli/forge/milestones/{}/edit", milestone.id); + // [close, delete] closing via edit, then deleting keeps the issues. let edit = call( &s.site, "POST", - &edit_uri, - Some(s.tahinli_cookie.as_str()), + &format!("/tahinli/other/milestones/{}/edit", other_ms.id), + owner, form(&[ ("title", "v1"), ("description", ""), @@ -529,110 +505,65 @@ async fn milestone_progress_close_and_delete_keeps_issues() { ]), ) .await; - assert_eq!(edit.status, 303); + assert_eq!(edit.status, 303, "milestone edit"); assert_eq!( store - .milestone_by_id(&s.repo_id, &milestone.id) + .milestone_by_id(other, &other_ms.id) .await .expect("milestone") .expect("exists") .state, forge_core::IssueState::Closed ); - let deleted = call( &s.site, "POST", - &format!("/tahinli/forge/milestones/{}/delete", milestone.id), - Some(s.tahinli_cookie.as_str()), + &format!("/tahinli/other/milestones/{}/delete", other_ms.id), + owner, form(&[]), ) .await; - assert_eq!(deleted.status, 303); + assert_eq!(deleted.status, 303, "milestone delete"); assert!( store - .list_milestones(&s.repo_id, None) + .list_milestones(other, None) .await .expect("milestones") .is_empty() ); let survivor = store - .issue_by_number(&s.repo_id, open_issue.number) + .issue_by_number(other, open_issue.number) .await .expect("issue") .expect("issues keep existing"); - assert!(survivor.milestone_id.is_none()); + assert!(survivor.milestone_id.is_none(), "issue kept, milestone gone"); } +/// Locking on forge: only triage holders may lock, CSRF is required, and a locked issue refuses +/// comments and reactions from read-only users while triage can still comment. #[tokio::test] -async fn write_without_csrf_is_rejected() { +async fn issue_lock_requires_triage_and_csrf_and_refuses_read_only_replies() { let s = seeded().await; - let no_token = Some( - form_urlencoded::Serializer::new(String::new()) - .extend_pairs([("name", "bug"), ("color", "#ff0000")]) - .finish(), - ); - let res = call( - &s.site, - "POST", - FORGE_LABELS, - Some(s.tahinli_cookie.as_str()), - no_token, - ) - .await; - assert_eq!(res.status, 403); - let res = call( - &s.site, - "POST", - "/tahinli/forge/milestones", - Some(s.tahinli_cookie.as_str()), - Some( - form_urlencoded::Serializer::new(String::new()) - .extend_pairs([("title", "v1")]) - .finish(), - ), - ) - .await; - assert_eq!(res.status, 403); -} - -#[tokio::test] -async fn anonymous_viewer_of_a_private_repo_gets_404() { - let s = seeded().await; - for uri in ["/tahinli/secret/labels", "/tahinli/secret/milestones"] { - let res = call(&s.site, "GET", uri, None, None).await; - assert_eq!(res.status, 404, "{uri}"); - } - let res = call(&s.site, "GET", "/tahinli/forge/milestones", None, None).await; - assert_eq!(res.status, 200); -} - -const FORGE_ISSUE: &str = "/tahinli/forge/issues/1"; - -/// Tahinli opens issue #1 in the seeded repo and returns its id. -async fn open_issue(s: &Seed) -> String { - s.site - .store + let store = &s.site.store; + let issue = store .create_issue(&s.repo_id, &s.tahinli, "Lock me", "body") .await - .expect("issue") - .id -} + .expect("issue"); + let base = format!("/tahinli/forge/issues/{}", issue.number); + let lock_uri = format!("{base}/lock"); -#[tokio::test] -async fn lock_requires_triage_and_csrf() { - let s = seeded().await; - open_issue(&s).await; - let uri = "/tahinli/forge/issues/1/lock"; + // [role] a read-only collaborator cannot lock. let res = call( &s.site, "POST", - uri, + &lock_uri, Some(s.bob_cookie.as_str()), form(&[("locked", "1"), ("reason", "spam")]), ) .await; - assert_eq!(res.status, 403); + assert_eq!(res.status, 403, "read-only lock"); + + // [csrf] a triage user without the token is refused. let no_token = Some( form_urlencoded::Serializer::new(String::new()) .extend_pairs([("locked", "1"), ("reason", "spam")]) @@ -641,126 +572,99 @@ async fn lock_requires_triage_and_csrf() { let res = call( &s.site, "POST", - uri, + &lock_uri, Some(s.carol_cookie.as_str()), no_token, ) .await; - assert_eq!(res.status, 403); - let issue = s - .site - .store - .issue_by_number(&s.repo_id, 1) + assert_eq!(res.status, 403, "lock csrf"); + let current = store + .issue_by_number(&s.repo_id, issue.number) .await .expect("lookup") .expect("issue"); - assert!(!issue.locked); -} + assert!(!current.locked, "not locked after refusals"); -#[tokio::test] -async fn triage_locks_and_unlocks_with_reason() { - let s = seeded().await; - open_issue(&s).await; - let uri = "/tahinli/forge/issues/1/lock"; + // [lock, unlock] triage locks with a reason, then unlocks and clears it. let res = call( &s.site, "POST", - uri, + &lock_uri, Some(s.carol_cookie.as_str()), form(&[("locked", "1"), ("reason", "too_heated")]), ) .await; - assert_eq!(res.status, 303); - let issue = s - .site - .store - .issue_by_number(&s.repo_id, 1) + assert_eq!(res.status, 303, "triage lock"); + let current = store + .issue_by_number(&s.repo_id, issue.number) .await .expect("lookup") .expect("issue"); - assert!(issue.locked); - assert_eq!(issue.lock_reason.as_deref(), Some("too_heated")); - + assert!(current.locked); + assert_eq!(current.lock_reason.as_deref(), Some("too_heated")); let res = call( &s.site, "POST", - uri, + &lock_uri, Some(s.carol_cookie.as_str()), form(&[("locked", "0")]), ) .await; - assert_eq!(res.status, 303); - let issue = s - .site - .store - .issue_by_number(&s.repo_id, 1) + assert_eq!(res.status, 303, "triage unlock"); + let current = store + .issue_by_number(&s.repo_id, issue.number) .await .expect("lookup") .expect("issue"); - assert!(!issue.locked); - assert_eq!(issue.lock_reason, None); -} + assert!(!current.locked); + assert_eq!(current.lock_reason, None); -#[tokio::test] -async fn locked_issue_refuses_comments_and_reactions_from_read_users() { - let s = seeded().await; - let issue_id = open_issue(&s).await; - s.site - .store - .set_issue_locked(&issue_id, Some(&s.tahinli), true, Some("spam")) + // [locked replies] lock via the store; read-only replies and reactions are refused. + store + .set_issue_locked(&issue.id, Some(&s.tahinli), true, Some("spam")) .await .expect("lock"); - let res = call( &s.site, "POST", - &format!("{FORGE_ISSUE}/comment"), + &format!("{base}/comment"), Some(s.bob_cookie.as_str()), form(&[("body", "hello"), ("action", "comment")]), ) .await; - assert_eq!(res.status, 403); + assert_eq!(res.status, 403, "read-only comment on locked"); let res = call( &s.site, "POST", - &format!("{FORGE_ISSUE}/reactions"), + &format!("{base}/reactions"), Some(s.bob_cookie.as_str()), form(&[ ("target_kind", "issue"), - ("target_id", issue_id.as_str()), + ("target_id", issue.id.as_str()), ("reaction", "heart"), ]), ) .await; - assert_eq!(res.status, 403); - let issue = s - .site - .store - .issue_by_number(&s.repo_id, 1) + assert_eq!(res.status, 403, "read-only reaction on locked"); + let current = store + .issue_by_number(&s.repo_id, issue.number) .await .expect("lookup") .expect("issue"); - assert_eq!(issue.comment_count, 0); + assert_eq!(current.comment_count, 0, "refused comment not counted"); // Triage holders still comment, and the page tells Bob why he cannot. let res = call( &s.site, "POST", - &format!("{FORGE_ISSUE}/comment"), + &format!("{base}/comment"), Some(s.carol_cookie.as_str()), form(&[("body", "moderating"), ("action", "comment")]), ) .await; - assert_eq!(res.status, 303); - let res = call( - &s.site, - "GET", - FORGE_ISSUE, - Some(s.bob_cookie.as_str()), - None, - ) - .await; - assert_eq!(res.status, 200); + assert_eq!(res.status, 303, "triage comment on locked"); + let res = call(&s.site, "GET", &base, Some(s.bob_cookie.as_str()), None).await; + assert_eq!(res.status, 200, "locked page"); assert!( res.body .contains("This conversation has been locked (spam) and limited to collaborators.") diff --git a/crates/forge-web/src/pages/issue_view.rs b/crates/forge-web/src/pages/issue_view.rs index 0ac0d65..341e9c6 100644 --- a/crates/forge-web/src/pages/issue_view.rs +++ b/crates/forge-web/src/pages/issue_view.rs @@ -638,8 +638,11 @@ mod tests { const ISSUE: &str = "/tahinli/demo/issues/1"; + /// The issue page on a fresh public fixture (issue 1), the markdown and reference rendering, + /// bad numbers, form gating for anonymous and owner viewers, and a private repo's 404 on + /// its own fixture. #[tokio::test] - async fn renders_body_markdown_safely_and_links_refs() { + async fn issue_page_renders_and_gates_forms() { let fx = Fixture::new(Visibility::Public).await; let owner = tahinli(&fx).await; let id = repo_id(&fx).await; @@ -652,18 +655,17 @@ mod tests { ) .await .unwrap(); + + // [markdown] the body renders, script tags are stripped, and #1 links to the issue. let (status, _, body) = get(&fx.router, ISSUE).await; - assert_eq!(status, 200); + assert_eq!(status, 200, "issue page"); let html = String::from_utf8(body).unwrap(); assert!(html.contains(">Heading")); assert!(html.contains("class=\"issue-ref\"")); assert!(html.contains("href=\"/tahinli/demo/issues/1\"")); - } - #[tokio::test] - async fn bad_numbers_and_missing_issues_are_404() { - let fx = Fixture::new(Visibility::Public).await; + // [numbers] zero, non-numeric and missing issues are 404. for uri in [ "/tahinli/demo/issues/0", "/tahinli/demo/issues/abc", @@ -672,23 +674,20 @@ mod tests { let (status, _, _) = get(&fx.router, uri).await; assert_eq!(status, 404, "{uri}"); } - } - #[tokio::test] - async fn anonymous_sees_no_forms_and_private_is_404() { - let fx = Fixture::new(Visibility::Public).await; - let owner = tahinli(&fx).await; - let id = repo_id(&fx).await; - fx.store - .create_issue(&id, &owner.id, "First", "body") - .await - .unwrap(); + // [forms] anonymous viewers get no forms; the owner gets the triage forms. let (status, _, body) = get(&fx.router, ISSUE).await; - assert_eq!(status, 200); + assert_eq!(status, 200, "anonymous page"); let html = String::from_utf8(body).unwrap(); - assert!(!html.contains("name=\"csrf\"")); - assert!(!html.contains("/issues/1/labels")); + assert!(!html.contains("name=\"csrf\""), "anonymous csrf"); + assert!(!html.contains("/issues/1/labels"), "anonymous labels"); + let (status, _, body) = send(&fx.router, "GET", ISSUE, Some("tahinli"), None).await; + assert_eq!(status, 200, "owner page"); + assert!(body.contains("/issues/1/labels")); + assert!(body.contains("name=\"assignees\"")); + assert!(body.contains("name=\"milestone_id\"")); + // [private] anonymous viewers of a private repo get 404. let private = Fixture::new(Visibility::Private).await; let owner = tahinli(&private).await; let id = repo_id(&private).await; @@ -698,39 +697,29 @@ mod tests { .await .unwrap(); let (status, _, _) = get(&private.router, ISSUE).await; - assert_eq!(status, 404); + assert_eq!(status, 404, "private issue"); } + /// Comments, closing, editing, deleting and archiving on one public fixture. Each scenario + /// opens its own issue so the numbers and counts do not interfere. Archiving is last. #[tokio::test] - async fn owner_gets_triage_forms_in_sidebar() { + async fn comment_close_edit_and_archive_rules() { let fx = Fixture::new(Visibility::Public).await; let owner = tahinli(&fx).await; let id = repo_id(&fx).await; + let bob = signed_in(&fx, "bob", "bob-token").await; + let carol = signed_in(&fx, "carol", "carol-token").await; fx.store - .create_issue(&id, &owner.id, "First", "body") + .set_collaborator(&id, &carol.id, Role::Triage) .await .unwrap(); - let (status, _, body) = send(&fx.router, "GET", ISSUE, Some("tahinli"), None).await; - assert_eq!(status, 200); - assert!(body.contains("/issues/1/labels")); - assert!(body.contains("name=\"assignees\"")); - assert!(body.contains("name=\"milestone_id\"")); - let (_, _, body) = send(&fx.router, "GET", ISSUE, None, None).await; - assert!(!body.contains("/issues/1/labels")); - assert!(!body.contains("name=\"assignees\"")); - assert!(!body.contains("name=\"milestone_id\"")); - } - #[tokio::test] - async fn comment_as_another_user_redirects_to_the_comment() { - let fx = Fixture::new(Visibility::Public).await; - let owner = tahinli(&fx).await; - let id = repo_id(&fx).await; - fx.store - .create_issue(&id, &owner.id, "First", "body") + // [comment] a read user's comment redirects to the new comment. + let a = fx + .store + .create_issue(&id, &owner.id, "A", "body") .await .unwrap(); - signed_in(&fx, "bob", "bob-token").await; let form = csrf_form( "bob-token", &[("body", "Looks good"), ("action", "comment")], @@ -738,177 +727,138 @@ mod tests { let (status, location, _) = send( &fx.router, "POST", - "/tahinli/demo/issues/1/comment", + &format!("/tahinli/demo/issues/{}/comment", a.number), Some("bob-token"), Some(form), ) .await; - assert_eq!(status, 303); - let issue = fx.store.issue_by_number(&id, 1).await.unwrap().unwrap(); - let comments = fx.store.list_comments(&issue.id).await.unwrap(); - assert_eq!(comments.len(), 1); + assert_eq!(status, 303, "bob comment"); + let comments = fx.store.list_comments(&a.id).await.unwrap(); + assert_eq!(comments.len(), 1, "one comment on A"); assert_eq!( location.unwrap(), - format!("/tahinli/demo/issues/1#comment-{}", comments[0].id) + format!( + "/tahinli/demo/issues/{}#comment-{}", + a.number, comments[0].id + ) ); - let (_, _, body) = get(&fx.router, ISSUE).await; + let (_, _, body) = get(&fx.router, &format!("/tahinli/demo/issues/{}", a.number)).await; assert!(String::from_utf8(body).unwrap().contains("Looks good")); - } - #[tokio::test] - async fn empty_comment_is_a_validation_error() { - let fx = Fixture::new(Visibility::Public).await; - let owner = tahinli(&fx).await; - let id = repo_id(&fx).await; - let issue = fx + // [empty] a blank comment is a validation error and stores nothing. + let b = fx .store - .create_issue(&id, &owner.id, "First", "body") + .create_issue(&id, &owner.id, "B", "body") .await .unwrap(); let form = csrf_form("tahinli", &[("body", " "), ("action", "comment")]); let (status, _, body) = send( &fx.router, "POST", - "/tahinli/demo/issues/1/comment", + &format!("/tahinli/demo/issues/{}/comment", b.number), Some("tahinli"), Some(form), ) .await; - assert_eq!(status, 200); + assert_eq!(status, 200, "empty comment"); assert!(body.contains("Comment cannot be empty.")); - assert!(fx.store.list_comments(&issue.id).await.unwrap().is_empty()); - } + assert!(fx.store.list_comments(&b.id).await.unwrap().is_empty()); - #[tokio::test] - async fn author_closes_as_not_planned() { - let fx = Fixture::new(Visibility::Public).await; - let owner = tahinli(&fx).await; - let id = repo_id(&fx).await; - let issue = fx + // [author close] the author closes as not planned; the event and the page show it. + let c = fx .store - .create_issue(&id, &owner.id, "First", "body") + .create_issue(&id, &owner.id, "C", "body") .await .unwrap(); let form = csrf_form("tahinli", &[("action", "close_not_planned")]); let (status, location, _) = send( &fx.router, "POST", - "/tahinli/demo/issues/1/comment", + &format!("/tahinli/demo/issues/{}/comment", c.number), Some("tahinli"), Some(form), ) .await; - assert_eq!(status, 303); - assert_eq!(location.unwrap(), "/tahinli/demo/issues/1#issue-1"); + assert_eq!(status, 303, "author close"); + assert_eq!( + location.unwrap(), + format!("/tahinli/demo/issues/{}#issue-{}", c.number, c.number) + ); assert_eq!( - issue_state(&fx, 1).await, + issue_state(&fx, c.number).await, (false, Some("not_planned".to_owned())) ); - let events = fx.store.list_events(&issue.id).await.unwrap(); + let events = fx.store.list_events(&c.id).await.unwrap(); assert!(events.iter().any(|e| e.kind == "closed")); - let (_, _, body) = get(&fx.router, ISSUE).await; + let (_, _, body) = get(&fx.router, &format!("/tahinli/demo/issues/{}", c.number)).await; assert!( String::from_utf8(body) .unwrap() .contains("Closed as not planned") ); - } - #[tokio::test] - async fn non_author_cannot_close_but_can_comment() { - let fx = Fixture::new(Visibility::Public).await; - let owner = tahinli(&fx).await; - let id = repo_id(&fx).await; - fx.store - .create_issue(&id, &owner.id, "First", "body") + // [non-author] a read user cannot close, but can still comment. + let d = fx + .store + .create_issue(&id, &owner.id, "D", "body") .await .unwrap(); - signed_in(&fx, "bob", "bob-token").await; + let d_uri = format!("/tahinli/demo/issues/{}/comment", d.number); let form = csrf_form("bob-token", &[("action", "close_completed")]); - let (status, _, _) = send( - &fx.router, - "POST", - "/tahinli/demo/issues/1/comment", - Some("bob-token"), - Some(form), - ) - .await; - assert_eq!(status, 403); - assert_eq!(issue_state(&fx, 1).await, (true, None)); - + let (status, _, _) = send(&fx.router, "POST", &d_uri, Some("bob-token"), Some(form)).await; + assert_eq!(status, 403, "non-author close"); + assert_eq!(issue_state(&fx, d.number).await, (true, None)); let form = csrf_form("bob-token", &[("body", "Me too"), ("action", "comment")]); - let (status, _, _) = send( - &fx.router, - "POST", - "/tahinli/demo/issues/1/comment", - Some("bob-token"), - Some(form), - ) - .await; - assert_eq!(status, 303); - } + let (status, _, _) = send(&fx.router, "POST", &d_uri, Some("bob-token"), Some(form)).await; + assert_eq!(status, 303, "non-author comment"); - #[tokio::test] - async fn triage_collaborator_closes_and_edits_others_comments() { - let fx = Fixture::new(Visibility::Public).await; - let owner = tahinli(&fx).await; - let id = repo_id(&fx).await; - let issue = fx + // [triage] a triage user closes the issue and edits another user's comment. + let e = fx .store - .create_issue(&id, &owner.id, "First", "body") - .await - .unwrap(); - let carol = signed_in(&fx, "carol", "carol-token").await; - fx.store - .set_collaborator(&id, &carol.id, Role::Triage) + .create_issue(&id, &owner.id, "E", "body") .await .unwrap(); - let bob = signed_in(&fx, "bob", "bob-token").await; let comment = fx .store - .add_comment(&issue.id, &bob.id, "original") + .add_comment(&e.id, &bob.id, "original") .await .unwrap(); - let form = csrf_form("carol-token", &[("action", "close_completed")]); let (status, _, _) = send( &fx.router, "POST", - "/tahinli/demo/issues/1/comment", + &format!("/tahinli/demo/issues/{}/comment", e.number), Some("carol-token"), Some(form), ) .await; - assert_eq!(status, 303); + assert_eq!(status, 303, "triage close"); assert_eq!( - issue_state(&fx, 1).await, + issue_state(&fx, e.number).await, (false, Some("completed".to_owned())) ); - - let uri = format!("/tahinli/demo/issues/1/comments/{}/edit", comment.id); + let uri = format!( + "/tahinli/demo/issues/{}/comments/{}/edit", + e.number, comment.id + ); let (status, _, _) = get(&fx.router, &uri).await; - assert_eq!(status, 303, "anonymous goes to login"); + assert_eq!(status, 303, "anonymous edit goes to login"); let (status, _, _) = send(&fx.router, "GET", &uri, Some("carol-token"), None).await; - assert_eq!(status, 200); + assert_eq!(status, 200, "triage edit form"); let form = csrf_form("carol-token", &[("body", "edited by triage")]); let (status, _, _) = send(&fx.router, "POST", &uri, Some("carol-token"), Some(form)).await; - assert_eq!(status, 303); + assert_eq!(status, 303, "triage edit"); let saved = fx .store - .comment_by_id(&issue.id, &comment.id) + .comment_by_id(&e.id, &comment.id) .await .unwrap() .unwrap(); assert_eq!(saved.body, "edited by triage"); assert!(saved.edited); - } - #[tokio::test] - async fn edit_issue_updates_title_and_records_rename() { - let fx = Fixture::new(Visibility::Public).await; - let owner = tahinli(&fx).await; - let id = repo_id(&fx).await; - let issue = fx + // [edit issue] title and body change, and the rename is recorded. + let f = fx .store .create_issue(&id, &owner.id, "old", "old body") .await @@ -917,104 +867,108 @@ mod tests { let (status, location, _) = send( &fx.router, "POST", - "/tahinli/demo/issues/1/edit", + &format!("/tahinli/demo/issues/{}/edit", f.number), Some("tahinli"), Some(form), ) .await; - assert_eq!(status, 303); - assert_eq!(location.unwrap(), "/tahinli/demo/issues/1"); - let saved = fx.store.issue_by_number(&id, 1).await.unwrap().unwrap(); + assert_eq!(status, 303, "issue edit"); + assert_eq!( + location.unwrap(), + format!("/tahinli/demo/issues/{}", f.number) + ); + let saved = fx + .store + .issue_by_number(&id, f.number) + .await + .unwrap() + .unwrap(); assert_eq!( (saved.title.as_str(), saved.body.as_str()), ("new", "new body") ); // The store writes the previous version to edit_history on every edit; the rename event // is the visible part of that write. - let events = fx.store.list_events(&issue.id).await.unwrap(); + let events = fx.store.list_events(&f.id).await.unwrap(); assert!(events.iter().any(|e| e.kind == "renamed")); - } - #[tokio::test] - async fn deleting_a_comment_decrements_the_count() { - let fx = Fixture::new(Visibility::Public).await; - let owner = tahinli(&fx).await; - let id = repo_id(&fx).await; - let issue = fx + // [delete comment] deleting one of two comments drops the count to one. + let g = fx .store - .create_issue(&id, &owner.id, "First", "body") + .create_issue(&id, &owner.id, "G", "body") .await .unwrap(); let first = fx .store - .add_comment(&issue.id, &owner.id, "one") + .add_comment(&g.id, &owner.id, "one") .await .unwrap(); fx.store - .add_comment(&issue.id, &owner.id, "two") + .add_comment(&g.id, &owner.id, "two") .await .unwrap(); assert_eq!( fx.store - .issue_by_number(&id, 1) + .issue_by_number(&id, g.number) .await .unwrap() .unwrap() .comment_count, - 2 + 2, + "two comments before delete" + ); + let uri = format!( + "/tahinli/demo/issues/{}/comments/{}/delete", + g.number, first.id ); - - let uri = format!("/tahinli/demo/issues/1/comments/{}/delete", first.id); let form = csrf_form("tahinli", &[]); let (status, _, _) = send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await; - assert_eq!(status, 303); + assert_eq!(status, 303, "delete comment"); assert_eq!( fx.store - .issue_by_number(&id, 1) + .issue_by_number(&id, g.number) .await .unwrap() .unwrap() .comment_count, - 1 + 1, + "count after delete" ); - } - #[tokio::test] - async fn comment_id_from_another_issue_is_404() { - let fx = Fixture::new(Visibility::Public).await; - let owner = tahinli(&fx).await; - let id = repo_id(&fx).await; - fx.store - .create_issue(&id, &owner.id, "First", "body") + // [foreign comment] a comment id from another issue is 404 on edit and delete. + let h1 = fx + .store + .create_issue(&id, &owner.id, "H1", "body") .await .unwrap(); - let other = fx + let h2 = fx .store - .create_issue(&id, &owner.id, "Second", "body") + .create_issue(&id, &owner.id, "H2", "body") .await .unwrap(); let foreign = fx .store - .add_comment(&other.id, &owner.id, "elsewhere") + .add_comment(&h2.id, &owner.id, "elsewhere") .await .unwrap(); - - let edit = format!("/tahinli/demo/issues/1/comments/{}/edit", foreign.id); + let edit = format!( + "/tahinli/demo/issues/{}/comments/{}/edit", + h1.number, foreign.id + ); let (status, _, _) = send(&fx.router, "GET", &edit, Some("tahinli"), None).await; - assert_eq!(status, 404); - let delete = format!("/tahinli/demo/issues/1/comments/{}/delete", foreign.id); + assert_eq!(status, 404, "foreign edit"); + let delete = format!( + "/tahinli/demo/issues/{}/comments/{}/delete", + h1.number, foreign.id + ); let form = csrf_form("tahinli", &[]); let (status, _, _) = send(&fx.router, "POST", &delete, Some("tahinli"), Some(form)).await; - assert_eq!(status, 404); - } + assert_eq!(status, 404, "foreign delete"); - #[tokio::test] - async fn archived_repo_rejects_writes() { - let fx = Fixture::new(Visibility::Public).await; - let owner = tahinli(&fx).await; - let id = repo_id(&fx).await; - fx.store - .create_issue(&id, &owner.id, "First", "body") + // [archived] last, because it changes the repo: comments are 403 and the form is hidden. + let i = fx + .store + .create_issue(&id, &owner.id, "I", "body") .await .unwrap(); fx.store.set_repo_archived(&id, true).await.unwrap(); @@ -1022,13 +976,20 @@ mod tests { let (status, _, _) = send( &fx.router, "POST", - "/tahinli/demo/issues/1/comment", + &format!("/tahinli/demo/issues/{}/comment", i.number), Some("tahinli"), Some(form), ) .await; - assert_eq!(status, 403); - let (_, _, body) = send(&fx.router, "GET", ISSUE, Some("tahinli"), None).await; - assert!(!body.contains("name=\"body\"")); + assert_eq!(status, 403, "archived comment"); + let (_, _, body) = send( + &fx.router, + "GET", + &format!("/tahinli/demo/issues/{}", i.number), + Some("tahinli"), + None, + ) + .await; + assert!(!body.contains("name=\"body\""), "archived form hidden"); } } diff --git a/crates/forge-web/src/pages/issues_list.rs b/crates/forge-web/src/pages/issues_list.rs index 4b0cbc5..591dbca 100644 --- a/crates/forge-web/src/pages/issues_list.rs +++ b/crates/forge-web/src/pages/issues_list.rs @@ -997,11 +997,16 @@ mod tests { html.matches("class=\"issue-row\"").count() } + /// One pristine public fixture, in order: empty list and toolbar links, Turkish empty state, + /// a refused draft and bad CSRF, the new-issue form, a valid create, newest-first order, the + /// Turkish relative times, and archived refusals last. #[tokio::test] - async fn toolbar_links_to_labels_and_milestones() { + async fn empty_create_and_locale_flow() { let fx = fixture(Visibility::Public).await; let labels = format!("href=\"/{OWNER}/{REPO}/labels\""); let milestones = format!("href=\"/{OWNER}/{REPO}/milestones\""); + + // [empty] toolbar links for signed-in and anonymous viewers; no rows yet. for signed_in in [true, false] { let html = send(&fx.router, "GET", &base(), signed_in, None, &[]) .await @@ -1009,21 +1014,61 @@ mod tests { assert!(html.contains(&labels), "signed_in={signed_in}"); assert!(html.contains(&milestones), "signed_in={signed_in}"); } - } + let empty = send(&fx.router, "GET", &base(), true, None, &[]).await; + assert_eq!(empty.status, 200, "empty list"); + assert!(empty.text().contains("No issues match these filters.")); + assert_eq!(rows(&empty.text()), 0); + let tr = send( + &fx.router, + "GET", + &base(), + true, + None, + &[("accept-language", "tr")], + ) + .await + .text(); + assert!(tr.contains(">Yeni sorun"), "{tr}"); + assert!(tr.contains("Bu filtrelerle eşleşen sorun yok."), "{tr}"); - #[tokio::test] - async fn empty_list_says_so() { - let fx = fixture(Visibility::Public).await; - let reply = send(&fx.router, "GET", &base(), true, None, &[]).await; - assert_eq!(reply.status, 200); - assert!(reply.text().contains("No issues match these filters.")); - assert_eq!(rows(&reply.text()), 0); - } + // [refusals] an empty-title draft keeps its body and creates nothing; bad CSRF is 403. + let draft = send( + &fx.router, + "POST", + &format!("{}/new", base()), + true, + Some("csrf=issues-test-csrf&title=+++&body=kept+body"), + &[], + ) + .await; + assert_eq!(draft.status, 422, "invalid draft"); + assert!(draft.text().contains("kept body")); + let bad = send( + &fx.router, + "POST", + &format!("{}/new", base()), + true, + Some("csrf=wrong&title=hi"), + &[], + ) + .await; + assert_eq!(bad.status, 403, "bad csrf"); + let (total, _) = fx.store.issue_counts(&fx.repo.id).await.unwrap(); + assert_eq!(total, 0, "refusals create nothing"); - #[tokio::test] - async fn newest_first_and_post_creates_an_issue() { - let fx = fixture(Visibility::Public).await; - let reply = send( + // [create] the owner sees the form, and a valid create redirects to issue 1. + let form = send( + &fx.router, + "GET", + &format!("{}/new", base()), + true, + None, + &[], + ) + .await; + assert_eq!(form.status, 200, "new form"); + assert!(form.text().contains(&format!("action=\"{}/new\"", base()))); + let created = send( &fx.router, "POST", &format!("{}/new", base()), @@ -1032,35 +1077,77 @@ mod tests { &[], ) .await; - assert_eq!(reply.status, 303); + assert_eq!(created.status, 303, "create"); assert_eq!( - reply.header("location").as_deref(), + created.header("location").as_deref(), Some(format!("{}/1", base()).as_str()) ); - let repo_id = fx.repo.id.clone(); - fx.store - .create_issue(&repo_id, &fx.owner.id, "second issue", "") - .await - .unwrap(); - fx.store - .create_issue(&repo_id, &fx.owner.id, "third issue", "") - .await - .unwrap(); + // [order] two more issues; the list is newest first. + let repo_id = fx.repo.id.clone(); + for title in ["second issue", "third issue"] { + fx.store + .create_issue(&repo_id, &fx.owner.id, title, "") + .await + .unwrap(); + } let html = send(&fx.router, "GET", &base(), true, None, &[]) .await .text(); - assert_eq!(rows(&html), 3); + assert_eq!(rows(&html), 3, "three rows"); let third = html.find("third issue").unwrap(); let second = html.find("second issue").unwrap(); let first = html.find("first issue").unwrap(); assert!(third < second && second < first, "{html}"); + + // [locale] Turkish relative times for fresh issues. + let html = send( + &fx.router, + "GET", + &base(), + true, + None, + &[("accept-language", "tr")], + ) + .await + .text(); + assert!(html.contains("az önce"), "{html}"); + assert!(html.contains("açıldı"), "{html}"); + assert!(!html.contains("just now"), "{html}"); + + // [archived] last, because it changes the repo: creates and the form are 403. + fx.store.set_repo_archived(&fx.repo.id, true).await.unwrap(); + let archived = send( + &fx.router, + "POST", + &format!("{}/new", base()), + true, + Some("csrf=issues-test-csrf&title=hi"), + &[], + ) + .await; + assert_eq!(archived.status, 403, "archived create"); + let form = send( + &fx.router, + "GET", + &format!("{}/new", base()), + true, + None, + &[], + ) + .await; + assert_eq!(form.status, 403, "archived form"); } + /// One populated public fixture, in order: 30 bulk issues for pagination (checked before + /// anything else is added), then the state filter, the label filter, search, and the + /// anonymous view. Anonymous writes are compared against the total taken just before them. #[tokio::test] - async fn pagination_with_thirty_issues() { + async fn pagination_state_label_search_and_anonymous_reads() { let fx = fixture(Visibility::Public).await; let repo_id = fx.repo.id.clone(); + + // [pagination] 30 issues: page 1 holds 25, page 2 the oldest 5. for n in 1..=30 { fx.store .create_issue(&repo_id, &fx.owner.id, &format!("bulk {n:02}"), "") @@ -1070,10 +1157,9 @@ mod tests { let page1 = send(&fx.router, "GET", &base(), true, None, &[]) .await .text(); - assert_eq!(rows(&page1), 25); + assert_eq!(rows(&page1), 25, "page 1 rows"); assert!(page1.contains(">Older<")); assert!(!page1.contains(">Newer<")); - let page2 = send( &fx.router, "GET", @@ -1084,18 +1170,13 @@ mod tests { ) .await .text(); - assert_eq!(rows(&page2), 5); + assert_eq!(rows(&page2), 5, "page 2 rows"); assert!(page2.contains(">Newer<")); assert!(!page2.contains(">Older<")); assert!(page2.contains("bulk 01")); - } - #[tokio::test] - async fn state_filter_and_counts() { - let fx = fixture(Visibility::Public).await; - let repo_id = fx.repo.id.clone(); - let open = fx - .store + // [state] one open and one closed issue; the filters split them. + fx.store .create_issue(&repo_id, &fx.owner.id, "still open", "") .await .unwrap(); @@ -1114,15 +1195,12 @@ mod tests { ) .await .unwrap(); - assert_eq!(open.number, 1); - let default = send(&fx.router, "GET", &base(), true, None, &[]) .await .text(); assert!(default.contains("still open")); assert!(!default.contains("already done")); assert!(default.contains("Closed 1"), "{default}"); - let closed = send( &fx.router, "GET", @@ -1135,7 +1213,6 @@ mod tests { .text(); assert!(closed.contains("already done")); assert!(!closed.contains("still open")); - let all = send( &fx.router, "GET", @@ -1147,7 +1224,6 @@ mod tests { .await .text(); assert!(all.contains("already done") && all.contains("still open")); - let bogus = send( &fx.router, "GET", @@ -1157,14 +1233,10 @@ mod tests { &[], ) .await; - assert_eq!(bogus.status, 200); + assert_eq!(bogus.status, 200, "bogus state"); assert!(bogus.text().contains("still open")); - } - #[tokio::test] - async fn label_filter_uses_store_labels() { - let fx = fixture(Visibility::Public).await; - let repo_id = fx.repo.id.clone(); + // [label] the label filter shows only the tagged issue. let bug = fx .store .create_label(&repo_id, "bug", "#d73a4a", "") @@ -1183,7 +1255,6 @@ mod tests { .set_issue_labels(&tagged.id, &fx.owner.id, std::slice::from_ref(&bug.id)) .await .unwrap(); - let filtered = send( &fx.router, "GET", @@ -1198,12 +1269,8 @@ mod tests { assert!(!filtered.contains("plain issue")); assert!(filtered.contains(&format!("label-chip label-{}", bug.id))); assert!(filtered.contains(&format!("/{OWNER}/{REPO}/labels.css"))); - } - #[tokio::test] - async fn search_treats_percent_as_a_literal() { - let fx = fixture(Visibility::Public).await; - let repo_id = fx.repo.id.clone(); + // [search] % is a literal character in the search term. for title in ["100% done", "100 done", "other thing"] { fx.store .create_issue(&repo_id, &fx.owner.id, title, "") @@ -1223,7 +1290,6 @@ mod tests { assert!(html.contains(">100% done"), "{html}"); assert!(!html.contains(">100 done"), "{html}"); assert!(!html.contains(">other thing"), "{html}"); - let raw = send( &fx.router, "GET", @@ -1233,22 +1299,18 @@ mod tests { &[], ) .await; - assert_eq!(raw.status, 200); - } + assert_eq!(raw.status, 200, "raw percent"); - #[tokio::test] - async fn anonymous_reads_public_but_cannot_create() { - let fx = fixture(Visibility::Public).await; - let repo_id = fx.repo.id.clone(); + // [anonymous] reads are public; creates bounce to login and store nothing. fx.store .create_issue(&repo_id, &fx.owner.id, "public issue", "") .await .unwrap(); + let (before, _) = fx.store.issue_counts(&repo_id).await.unwrap(); let list = send(&fx.router, "GET", &base(), false, None, &[]).await; - assert_eq!(list.status, 200); + assert_eq!(list.status, 200, "anonymous list"); assert!(list.text().contains("public issue")); assert!(!list.text().contains("New issue")); - let form = send( &fx.router, "GET", @@ -1258,9 +1320,8 @@ mod tests { &[], ) .await; - assert_eq!(form.status, 303); + assert_eq!(form.status, 303, "anonymous form"); assert!(form.header("location").unwrap().starts_with("/login")); - let post = send( &fx.router, "POST", @@ -1270,24 +1331,21 @@ mod tests { &[], ) .await; - assert_eq!(post.status, 303); + assert_eq!(post.status, 303, "anonymous create"); assert!(post.header("location").unwrap().starts_with("/login")); let (total, _) = fx.store.issue_counts(&repo_id).await.unwrap(); - assert_eq!(total, 1); + assert_eq!(total, before, "anonymous create stores nothing"); } + /// A private repo is 404 for anonymous viewers and visible to the owner, whose invalid + /// query values fall back to the defaults. #[tokio::test] - async fn private_repo_is_404_for_anonymous() { + async fn private_repo_is_404_for_anonymous_and_bad_queries_fall_back() { let fx = fixture(Visibility::Private).await; let list = send(&fx.router, "GET", &base(), false, None, &[]).await; - assert_eq!(list.status, 404); + assert_eq!(list.status, 404, "anonymous private"); let owner = send(&fx.router, "GET", &base(), true, None, &[]).await; - assert_eq!(owner.status, 200); - } - - #[tokio::test] - async fn invalid_query_values_fall_back_to_defaults() { - let fx = fixture(Visibility::Public).await; + assert_eq!(owner.status, 200, "owner private"); for query in [ "page=abc", "page=0", @@ -1307,115 +1365,4 @@ mod tests { assert!(reply.text().contains("Page 1"), "{query}"); } } - - #[tokio::test] - async fn new_issue_route_wins_over_issue_number() { - let fx = fixture(Visibility::Public).await; - let reply = send( - &fx.router, - "GET", - &format!("{}/new", base()), - true, - None, - &[], - ) - .await; - assert_eq!(reply.status, 200); - assert!(reply.text().contains(&format!("action=\"{}/new\"", base()))); - } - - #[tokio::test] - async fn invalid_draft_keeps_values_and_creates_nothing() { - let fx = fixture(Visibility::Public).await; - let reply = send( - &fx.router, - "POST", - &format!("{}/new", base()), - true, - Some("csrf=issues-test-csrf&title=+++&body=kept+body"), - &[], - ) - .await; - assert_eq!(reply.status, 422); - assert!(reply.text().contains("kept body")); - let (total, _) = fx.store.issue_counts(&fx.repo.id).await.unwrap(); - assert_eq!(total, 0); - } - - #[tokio::test] - async fn bad_csrf_is_403_and_archived_repo_is_403() { - let fx = fixture(Visibility::Public).await; - let bad = send( - &fx.router, - "POST", - &format!("{}/new", base()), - true, - Some("csrf=wrong&title=hi"), - &[], - ) - .await; - assert_eq!(bad.status, 403); - - fx.store.set_repo_archived(&fx.repo.id, true).await.unwrap(); - let archived = send( - &fx.router, - "POST", - &format!("{}/new", base()), - true, - Some("csrf=issues-test-csrf&title=hi"), - &[], - ) - .await; - assert_eq!(archived.status, 403); - let form = send( - &fx.router, - "GET", - &format!("{}/new", base()), - true, - None, - &[], - ) - .await; - assert_eq!(form.status, 403); - } - - #[tokio::test] - async fn turkish_viewer_sees_turkish_list() { - let fx = fixture(Visibility::Public).await; - let reply = send( - &fx.router, - "GET", - &base(), - true, - None, - &[("accept-language", "tr")], - ) - .await; - assert_eq!(reply.status, 200); - let html = reply.text(); - assert!(html.contains(">Yeni sorun"), "{html}"); - assert!(html.contains("Bu filtrelerle eşleşen sorun yok."), "{html}"); - } - - #[tokio::test] - async fn turkish_viewer_sees_relative_times_in_turkish() { - let fx = fixture(Visibility::Public).await; - fx.store - .create_issue(&fx.repo.id, &fx.owner.id, "fresh issue", "") - .await - .unwrap(); - let html = send( - &fx.router, - "GET", - &base(), - true, - None, - &[("accept-language", "tr")], - ) - .await - .text(); - assert!(html.contains("az önce"), "{html}"); - assert!(html.contains("açıldı"), "{html}"); - assert!(!html.contains("just now"), "{html}"); - } }