From f23b28cdf6c1034b5debcbfd748fee004f9a2f12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 23 Oct 2026 06:36:51 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20test:=20pull=20page=20tests=20share?= =?UTF-8?q?=20their=20fixtures=20and=20merge=20into=20tables?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Counts before/after per file: - pull_view.rs: 20 -> 4 - pull_reviews.rs: 15 -> 5 - pull_files.rs: 11 -> 2 Scenarios that share one open pull run in order inside one test, so each scenario sees only the comments and state added before it. Every assert carries its scenario name. Pristine-instance tests are kept separate. --- crates/forge-web/src/pages/pull_files.rs | 279 ++++---- crates/forge-web/src/pages/pull_reviews.rs | 533 +++++++-------- crates/forge-web/src/pages/pull_view.rs | 716 ++++++++++----------- 3 files changed, 699 insertions(+), 829 deletions(-) diff --git a/crates/forge-web/src/pages/pull_files.rs b/crates/forge-web/src/pages/pull_files.rs index 08190ef..6026a2e 100644 --- a/crates/forge-web/src/pages/pull_files.rs +++ b/crates/forge-web/src/pages/pull_files.rs @@ -364,15 +364,23 @@ mod tests { format!("/{OWNER}/{REPO}/pulls/{number}/files{query}") } + /// Every Files tab scenario for one open pull request. The pull is opened once. Each + /// scenario adds only what it needs, and the scenarios that add comments run last in an + /// order where no later scenario is confused by an earlier comment. #[tokio::test] - async fn turkish_viewer_gets_turkish_review_bar() { + async fn files_tab_scenarios_on_one_open_pull() { let fx = Fixture::new(Visibility::Public).await; owner_session(&fx).await; + sign_in(&fx, "bob", "bob").await; let number = open_pull(&fx).await; + let (issue_id, head) = pull_ids(&fx, number).await; + let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap(); + let uri = files_uri(number, ""); + // Turkish viewers get the translated review bar. let req = Request::builder() .method("GET") - .uri(files_uri(number, "")) + .uri(uri.as_str()) .header("cookie", cookie("tahinli")) .header("accept-language", "tr") .body(Body::empty()) @@ -380,57 +388,47 @@ mod tests { let res = fx.router.handle(req).await; let bytes = to_bytes(res.into_body(), usize::MAX).await.unwrap(); let body = String::from_utf8_lossy(&bytes).into_owned(); - assert!(body.contains("İncelemeyi tamamla"), "{body}"); - assert!(body.contains("Bekleyen yorumları sil"), "{body}"); - } - - #[tokio::test] - async fn files_tab_has_line_anchors_and_plus_links_for_signed_in_viewers() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - let number = open_pull(&fx).await; - - let (status, body) = get(&fx.router, &files_uri(number, ""), Some("tahinli")).await; - assert_eq!(status, 200); - assert!(body.contains("id=\"L-0-new-1\""), "{body}"); - assert!(body.contains("?comment=0:new:1#L-0-new-1"), "{body}"); - assert!(body.contains("review-bar"), "{body}"); - } + assert!(body.contains("İncelemeyi tamamla"), "turkish bar: {body}"); + assert!( + body.contains("Bekleyen yorumları sil"), + "turkish bar: {body}" + ); - #[tokio::test] - async fn comment_param_opens_the_form_under_its_line() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - let number = open_pull(&fx).await; - let (_, head) = pull_ids(&fx, number).await; + // Signed-in viewer: line anchors and plus links. + let (status, body) = get(&fx.router, &uri, Some("tahinli")).await; + assert_eq!(status, 200, "anchors"); + assert!(body.contains("id=\"L-0-new-1\""), "anchors: {body}"); + assert!( + body.contains("?comment=0:new:1#L-0-new-1"), + "plus link: {body}" + ); + assert!(body.contains("review-bar"), "review bar: {body}"); + // A well-formed `comment` param opens the form under its line. let (status, body) = get( &fx.router, &files_uri(number, "?comment=0:new:1"), Some("tahinli"), ) .await; - assert_eq!(status, 200); - assert!(body.contains("name=\"file_index\" value=\"0\""), "{body}"); - assert!(body.contains("name=\"side\" value=\"new\""), "{body}"); - assert!(body.contains("name=\"line\" value=\"1\""), "{body}"); + assert_eq!(status, 200, "comment form"); + assert!( + body.contains("name=\"file_index\" value=\"0\""), + "form: {body}" + ); + assert!(body.contains("name=\"side\" value=\"new\""), "form: {body}"); + assert!(body.contains("name=\"line\" value=\"1\""), "form: {body}"); assert!( body.contains(&format!("name=\"head_oid\" value=\"{head}\"")), - "{body}" + "form head: {body}" ); - assert!(body.contains("value=\"single\""), "{body}"); - assert!(body.contains("value=\"review\""), "{body}"); + assert!(body.contains("value=\"single\""), "form: {body}"); + assert!(body.contains("value=\"review\""), "form: {body}"); let line = body.find("id=\"L-0-new-1\"").unwrap(); let form = body.find("name=\"file_index\"").unwrap(); assert!(form > line, "the form sits after its line"); - } - - #[tokio::test] - async fn garbage_comment_param_is_ignored() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - let number = open_pull(&fx).await; + // Malformed `comment` params are ignored. for query in [ "?comment=abc", "?comment=0:bogus:1", @@ -441,48 +439,59 @@ mod tests { "?comment=0:new:-1", ] { let (status, body) = get(&fx.router, &files_uri(number, query), Some("tahinli")).await; - assert_eq!(status, 200, "{query}"); - assert!(!body.contains("name=\"file_index\""), "{query}"); + assert_eq!(status, 200, "garbage {query}"); + assert!(!body.contains("name=\"file_index\""), "garbage {query}"); } - } - #[tokio::test] - async fn existing_thread_renders_under_its_line() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - let number = open_pull(&fx).await; - let (issue_id, head) = pull_ids(&fx, number).await; - let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap(); - fx.store - .add_review_comment( - &issue_id, - &owner.id, - false, - "feat.txt", - Side::New, - 1, - &head, - "+feature", - "Looks good", - ) - .await - .unwrap(); + // The review bar hides approve from the pull's author only. + let (_, author) = get(&fx.router, &uri, Some("tahinli")).await; + assert!(author.contains("value=\"comment\""), "author bar: {author}"); + assert!( + !author.contains("value=\"approve\""), + "author bar: {author}" + ); + assert!( + !author.contains("value=\"request_changes\""), + "author bar: {author}" + ); + let (_, other) = get(&fx.router, &uri, Some("bob")).await; + assert!(other.contains("value=\"approve\""), "reader bar: {other}"); + assert!( + other.contains("value=\"request_changes\""), + "reader bar: {other}" + ); - let (status, body) = get(&fx.router, &files_uri(number, ""), Some("tahinli")).await; - assert_eq!(status, 200); - assert!(body.contains("Looks good"), "{body}"); - let line = body.find("id=\"L-0-new-1\"").unwrap(); - let thread = body.find("class=\"review-thread\"").unwrap(); - assert!(thread > line, "the thread sits after its line"); - } + // Anonymous viewers get no comment controls at all. + let (status, body) = get(&fx.router, &files_uri(number, "?comment=0:new:1"), None).await; + assert_eq!(status, 200, "anonymous"); + assert!(!page_main(&body).contains("?comment="), "anonymous: {body}"); + assert!(!body.contains("id=\"L-"), "anonymous: {body}"); + assert!(!body.contains("name=\"file_index\""), "anonymous: {body}"); + assert!(!body.contains("review-bar"), "anonymous: {body}"); + assert!(!body.contains("class=\"diff-plus\""), "anonymous: {body}"); + + // A known review error shows its message, an unknown code shows nothing. + let (_, known) = get( + &fx.router, + &files_uri(number, "?review_error=own_pr"), + Some("tahinli"), + ) + .await; + assert!(known.contains("cannot approve"), "known error: {known}"); + let (status, unknown) = get( + &fx.router, + &files_uri(number, "?review_error=%3Cb%3Ehi%3C%2Fb%3E"), + Some("tahinli"), + ) + .await; + assert_eq!(status, 200, "unknown error"); + assert!( + !unknown.contains("role=\"alert\""), + "unknown error: {unknown}" + ); + assert!(!unknown.contains("hi"), "unknown error: {unknown}"); - #[tokio::test] - async fn outdated_thread_is_not_shown_on_the_files_tab() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - let number = open_pull(&fx).await; - let (issue_id, head) = pull_ids(&fx, number).await; - let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap(); + // An outdated thread is hidden. Only the outdated comment exists at this point. let comment = fx .store .add_review_comment( @@ -502,21 +511,15 @@ mod tests { .mark_outdated(&issue_id, std::slice::from_ref(&comment.id)) .await .unwrap(); + let (status, body) = get(&fx.router, &uri, Some("tahinli")).await; + assert_eq!(status, 200, "outdated"); + assert!(!body.contains("Stale remark"), "outdated: {body}"); + assert!( + !body.contains("class=\"review-thread\""), + "outdated: {body}" + ); - let (status, body) = get(&fx.router, &files_uri(number, ""), Some("tahinli")).await; - assert_eq!(status, 200); - assert!(!body.contains("Stale remark"), "{body}"); - assert!(!body.contains("class=\"review-thread\""), "{body}"); - } - - #[tokio::test] - async fn pending_comment_is_visible_only_to_its_author() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - sign_in(&fx, "bob", "bob").await; - let number = open_pull(&fx).await; - let (issue_id, head) = pull_ids(&fx, number).await; - let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap(); + // A pending comment is visible only to its author. fx.store .add_review_comment( &issue_id, @@ -531,71 +534,39 @@ mod tests { ) .await .unwrap(); + let (_, anonymous) = get(&fx.router, &uri, None).await; + assert!(!anonymous.contains("Draft secret"), "pending: {anonymous}"); + let (_, bob) = get(&fx.router, &uri, Some("bob")).await; + assert!(!bob.contains("Draft secret"), "pending: {bob}"); + let (_, author) = get(&fx.router, &uri, Some("tahinli")).await; + assert!(author.contains("Draft secret"), "pending author: {author}"); + assert!(author.contains("Pending"), "pending author: {author}"); + assert!( + author.contains("1 pending comment."), + "pending author: {author}" + ); - let (_, anonymous) = get(&fx.router, &files_uri(number, ""), None).await; - assert!(!anonymous.contains("Draft secret")); - let (_, bob) = get(&fx.router, &files_uri(number, ""), Some("bob")).await; - assert!(!bob.contains("Draft secret")); - let (_, author) = get(&fx.router, &files_uri(number, ""), Some("tahinli")).await; - assert!(author.contains("Draft secret"), "{author}"); - assert!(author.contains("Pending"), "{author}"); - assert!(author.contains("1 pending comment."), "{author}"); - } - - #[tokio::test] - async fn review_bar_hides_approve_for_the_pr_author() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - sign_in(&fx, "bob", "bob").await; - let number = open_pull(&fx).await; - - let (_, author) = get(&fx.router, &files_uri(number, ""), Some("tahinli")).await; - assert!(author.contains("value=\"comment\""), "{author}"); - assert!(!author.contains("value=\"approve\""), "{author}"); - assert!(!author.contains("value=\"request_changes\""), "{author}"); - - let (_, other) = get(&fx.router, &files_uri(number, ""), Some("bob")).await; - assert!(other.contains("value=\"approve\""), "{other}"); - assert!(other.contains("value=\"request_changes\""), "{other}"); - } - - #[tokio::test] - async fn anonymous_viewer_sees_no_comment_controls() { - let fx = Fixture::new(Visibility::Public).await; - let number = open_pull(&fx).await; - - let (status, body) = get(&fx.router, &files_uri(number, "?comment=0:new:1"), None).await; - assert_eq!(status, 200); - assert!(!page_main(&body).contains("?comment="), "{body}"); - assert!(!body.contains("id=\"L-"), "{body}"); - assert!(!body.contains("name=\"file_index\""), "{body}"); - assert!(!body.contains("review-bar"), "{body}"); - assert!(!body.contains("class=\"diff-plus\""), "{body}"); - } - - #[tokio::test] - async fn known_review_error_shows_its_message_and_unknown_code_shows_nothing() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - let number = open_pull(&fx).await; - - let (_, known) = get( - &fx.router, - &files_uri(number, "?review_error=own_pr"), - Some("tahinli"), - ) - .await; - assert!(known.contains("cannot approve"), "{known}"); - - let (status, unknown) = get( - &fx.router, - &files_uri(number, "?review_error=%3Cb%3Ehi%3C%2Fb%3E"), - Some("tahinli"), - ) - .await; - assert_eq!(status, 200); - assert!(!unknown.contains("role=\"alert\""), "{unknown}"); - assert!(!unknown.contains("hi"), "{unknown}"); + // A submitted thread renders under its line. + fx.store + .add_review_comment( + &issue_id, + &owner.id, + false, + "feat.txt", + Side::New, + 1, + &head, + "+feature", + "Looks good", + ) + .await + .unwrap(); + let (status, body) = get(&fx.router, &uri, Some("tahinli")).await; + assert_eq!(status, 200, "thread"); + assert!(body.contains("Looks good"), "thread: {body}"); + let line = body.find("id=\"L-0-new-1\"").unwrap(); + let thread = body.find("class=\"review-thread\"").unwrap(); + assert!(thread > line, "the thread sits after its line"); } #[tokio::test] diff --git a/crates/forge-web/src/pages/pull_reviews.rs b/crates/forge-web/src/pages/pull_reviews.rs index 3dd5069..6690fd0 100644 --- a/crates/forge-web/src/pages/pull_reviews.rs +++ b/crates/forge-web/src/pages/pull_reviews.rs @@ -997,64 +997,59 @@ mod tests { (status, location) } - /// The id of the first root comment on the pull request. - async fn root_id(fx: &Fixture, number: i64) -> String { + /// The id of the thread at `index` (in creation order) on the pull request. + async fn root_at(fx: &Fixture, number: i64, index: usize) -> String { let pr = pull(fx, number).await; - fx.store.review_threads(&pr.issue.id, None).await.unwrap()[0] + fx.store.review_threads(&pr.issue.id, None).await.unwrap()[index] .root .id .clone() } - #[tokio::test] - async fn single_comment_on_an_added_line_creates_a_thread_with_its_hunk() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - let number = open_pull(&fx).await; - let pr = pull(&fx, number).await; + /// The comment body that proposes replacing line 2 of `a.txt` with `text`. + fn suggestion(text: &str) -> String { + format!("Try this:\n```suggestion\n{text}\n```") + } - let (status, location) = comment_on(&fx, number, "tahinli", "2", "Looks good").await; - assert_eq!(status, 303); - assert_eq!( - location.as_deref(), - Some(format!("/tahinli/demo/pulls/{number}/files#L-0-new-2").as_str()) - ); - let threads = fx.store.review_threads(&pr.issue.id, None).await.unwrap(); - assert_eq!(threads.len(), 1); - let root = &threads[0].root; - assert_eq!( - (root.path.as_str(), root.side, root.line), - ("a.txt", Side::New, 2) - ); - assert_eq!(root.diff_hunk, " one\n-two\n+changed"); - assert!(!root.pending); + /// Posts the apply form for suggestion block 0 of comment `comment_id`. + async fn apply_as( + fx: &Fixture, + number: i64, + token: &str, + comment_id: &str, + ) -> (u16, Option) { + let form = csrf_form(token, &[("block", "0"), ("return", "files")]); + let uri = format!("/tahinli/demo/pulls/{number}/review_comments/{comment_id}/apply"); + let (status, location, _) = send(&fx.router, "POST", &uri, Some(token), Some(form)).await; + (status, location) } + /// Comment posting rules and a pending review, on one open pull. Scenarios run in order: + /// the rejected posts first (no thread), then the first thread, then the pending review. #[tokio::test] - async fn comment_on_a_line_outside_the_diff_is_rejected() { + async fn comment_rules_and_pending_review_on_one_pull() { let fx = Fixture::new(Visibility::Public).await; owner_session(&fx).await; let number = open_pull(&fx).await; let pr = pull(&fx, number).await; + // A line outside the diff is rejected and leaves no thread. let (status, location) = comment_on(&fx, number, "tahinli", "99", "Nope").await; - assert_eq!(status, 303); - assert!(location.unwrap().contains("review_error=bad_line")); + assert_eq!(status, 303, "outside diff"); + assert!( + location.unwrap().contains("review_error=bad_line"), + "outside diff" + ); assert!( fx.store .review_threads(&pr.issue.id, None) .await .unwrap() - .is_empty() + .is_empty(), + "outside diff left a thread" ); - } - #[tokio::test] - async fn stale_head_is_rejected() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - let number = open_pull(&fx).await; - let pr = pull(&fx, number).await; + // A head that is not the pull's current head is rejected. let zero = "0".repeat(40); let form = csrf_form( "tahinli", @@ -1075,26 +1070,41 @@ mod tests { Some(form), ) .await; - assert_eq!(status, 303); + assert_eq!(status, 303, "stale head"); assert_eq!( location.as_deref(), - Some(format!("/tahinli/demo/pulls/{number}/files?review_error=stale").as_str()) + Some(format!("/tahinli/demo/pulls/{number}/files?review_error=stale").as_str()), + "stale head" ); assert!( fx.store .review_threads(&pr.issue.id, None) .await .unwrap() - .is_empty() + .is_empty(), + "stale head left a thread" ); - } - #[tokio::test] - async fn start_review_keeps_the_comment_pending_until_submitted() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - let number = open_pull(&fx).await; - let pr = pull(&fx, number).await; + // A single comment on an added line creates a thread that carries its hunk. + let (status, location) = comment_on(&fx, number, "tahinli", "2", "Looks good").await; + assert_eq!(status, 303, "single comment"); + assert_eq!( + location.as_deref(), + Some(format!("/tahinli/demo/pulls/{number}/files#L-0-new-2").as_str()), + "single comment" + ); + let threads = fx.store.review_threads(&pr.issue.id, None).await.unwrap(); + assert_eq!(threads.len(), 1, "single comment"); + let root = &threads[0].root; + assert_eq!( + (root.path.as_str(), root.side, root.line), + ("a.txt", Side::New, 2), + "single comment" + ); + assert_eq!(root.diff_hunk, " one\n-two\n+changed", "single comment"); + assert!(!root.pending, "single comment"); + + // A pending review comment is hidden from everyone but its author until submitted. let tahinli = fx.store.user_by_username(OWNER).await.unwrap().unwrap(); let head = pr.head_oid.clone(); let form = csrf_form( @@ -1116,21 +1126,26 @@ mod tests { Some(form), ) .await; - assert_eq!(status, 303); - // Hidden from everyone but the author while pending. - assert!( + assert_eq!(status, 303, "pending start"); + assert_eq!( fx.store .review_threads(&pr.issue.id, None) .await .unwrap() - .is_empty() + .len(), + 1, + "pending thread hidden from everyone" ); let mine = fx .store .review_threads(&pr.issue.id, Some(&tahinli.id)) .await .unwrap(); - assert!(mine[0].root.pending); + assert_eq!(mine.len(), 2, "author sees the pending thread"); + assert!( + mine.iter().any(|t| t.root.pending), + "the author's draft is pending" + ); let form = csrf_form("tahinli", &[("state", "comment"), ("body", "Done")]); let (status, _, _) = send( @@ -1141,50 +1156,46 @@ mod tests { Some(form), ) .await; - assert_eq!(status, 303); + assert_eq!(status, 303, "submit review"); let threads = fx.store.review_threads(&pr.issue.id, None).await.unwrap(); - assert_eq!(threads.len(), 1); - assert!(!threads[0].root.pending); - } - - #[tokio::test] - async fn reader_approval_is_recorded_and_author_approval_is_refused() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - sign_in(&fx, "bob", "bob").await; - let number = open_pull(&fx).await; - let pr = pull(&fx, number).await; - let uri = format!("/tahinli/demo/pulls/{number}/reviews"); + assert_eq!(threads.len(), 2, "submitted review is public"); + assert!(threads.iter().all(|t| !t.root.pending), "submitted review"); - let form = csrf_form("bob", &[("state", "approve")]); - let (status, location, _) = send(&fx.router, "POST", &uri, Some("bob"), Some(form)).await; - assert_eq!(status, 303); - assert!(location.unwrap().contains("#review-")); - let reviews = fx.store.list_reviews(&pr.issue.id).await.unwrap(); - assert_eq!(reviews.len(), 1); - assert_eq!(reviews[0].state, ReviewState::Approved); - - let form = csrf_form("tahinli", &[("state", "approve")]); - let (status, location, _) = - send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await; - assert_eq!(status, 303); - assert_eq!( - location.as_deref(), - Some(format!("/tahinli/demo/pulls/{number}?review_error=own_pr").as_str()) + // Anonymous writes redirect to login, and a missing CSRF token is forbidden. + let uri = format!("/tahinli/demo/pulls/{number}/review_comments"); + let form = csrf_form( + "tahinli", + &[ + ("file_index", "0"), + ("side", "new"), + ("line", "2"), + ("head_oid", head.as_str()), + ("action", "single"), + ("body", "Anon"), + ], ); - assert_eq!(fx.store.list_reviews(&pr.issue.id).await.unwrap().len(), 1); + let (status, location, _) = send(&fx.router, "POST", &uri, None, Some(form)).await; + assert_eq!(status, 303, "anonymous write"); + assert!(location.unwrap().starts_with("/login"), "anonymous write"); + let form = + format!("file_index=0&side=new&line=2&head_oid={head}&action=single&body=NoCsrf"); + let (status, _, _) = send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await; + assert_eq!(status, 403, "missing csrf"); } + /// Reply, edit, resolve and delete on the threads of one pull. Each scenario acts on its + /// own thread (the first for reply, edit and resolve, the second for delete). #[tokio::test] - async fn reply_joins_the_thread_and_redirects_to_the_root() { + async fn thread_reply_edit_resolve_and_delete() { let fx = Fixture::new(Visibility::Public).await; owner_session(&fx).await; sign_in(&fx, "bob", "bob").await; let number = open_pull(&fx).await; - comment_on(&fx, number, "tahinli", "2", "Root").await; - let root = root_id(&fx, number).await; + comment_on(&fx, number, "tahinli", "2", "First").await; let pr = pull(&fx, number).await; + let root = root_at(&fx, number, 0).await; + // A reader replies into the thread and is sent back to its root. let form = csrf_form("bob", &[("body", "Reply")]); let (status, location, _) = send( &fx.router, @@ -1194,96 +1205,125 @@ mod tests { Some(form), ) .await; - assert_eq!(status, 303); + assert_eq!(status, 303, "reply"); assert_eq!( location.as_deref(), - Some(format!("/tahinli/demo/pulls/{number}#rc-{root}").as_str()) + Some(format!("/tahinli/demo/pulls/{number}#rc-{root}").as_str()), + "reply" ); let threads = fx.store.review_threads(&pr.issue.id, None).await.unwrap(); - assert_eq!(threads[0].replies.len(), 1); - assert_eq!(threads[0].replies[0].author_name, "bob"); - } + assert_eq!(threads[0].replies.len(), 1, "reply"); + assert_eq!(threads[0].replies[0].author_name, "bob", "reply"); - #[tokio::test] - async fn edit_is_for_the_author_only() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - sign_in(&fx, "bob", "bob").await; - let number = open_pull(&fx).await; - comment_on(&fx, number, "tahinli", "2", "First").await; - let root = root_id(&fx, number).await; + // Only the author may open the edit form, post an edit, or resolve. let edit = format!("/tahinli/demo/pulls/{number}/review_comments/{root}/edit"); - let (status, _, body) = send(&fx.router, "GET", &edit, Some("tahinli"), None).await; - assert_eq!(status, 200); - assert!(body.contains("Save comment")); + assert_eq!(status, 200, "edit form"); + assert!(body.contains("Save comment"), "edit form"); let (status, _, _) = send(&fx.router, "GET", &edit, Some("bob"), None).await; - assert_eq!(status, 403); - + assert_eq!(status, 403, "edit form for a non-author"); let form = csrf_form("bob", &[("body", "Hijacked")]); let (status, _, _) = send(&fx.router, "POST", &edit, Some("bob"), Some(form)).await; - assert_eq!(status, 403); - + assert_eq!(status, 403, "edit by a non-author"); let form = csrf_form("tahinli", &[("body", "Edited")]); let (status, location, _) = send(&fx.router, "POST", &edit, Some("tahinli"), Some(form)).await; - assert_eq!(status, 303); + assert_eq!(status, 303, "edit"); assert_eq!( location.as_deref(), - Some(format!("/tahinli/demo/pulls/{number}#rc-{root}").as_str()) + Some(format!("/tahinli/demo/pulls/{number}#rc-{root}").as_str()), + "edit" ); - let pr = pull(&fx, number).await; let threads = fx.store.review_threads(&pr.issue.id, None).await.unwrap(); - assert_eq!(threads[0].root.body, "Edited"); - } + assert_eq!(threads[0].root.body, "Edited", "edit"); - #[tokio::test] - async fn delete_removes_the_thread_for_its_author_and_refuses_others() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - sign_in(&fx, "bob", "bob").await; - let number = open_pull(&fx).await; - comment_on(&fx, number, "tahinli", "2", "Doomed").await; - let root = root_id(&fx, number).await; - let pr = pull(&fx, number).await; - let uri = format!("/tahinli/demo/pulls/{number}/review_comments/{root}/delete"); + let resolve = format!("/tahinli/demo/pulls/{number}/review_comments/{root}/resolve"); + let form = csrf_form("bob", &[("resolved", "1")]); + let (status, _, _) = send(&fx.router, "POST", &resolve, Some("bob"), Some(form)).await; + assert_eq!(status, 403, "resolve by a reader"); + let form = csrf_form("tahinli", &[("resolved", "1")]); + let (status, _, _) = send(&fx.router, "POST", &resolve, Some("tahinli"), Some(form)).await; + assert_eq!(status, 303, "resolve"); + let threads = fx.store.review_threads(&pr.issue.id, None).await.unwrap(); + assert!(threads[0].root.resolved, "resolve"); + // Delete removes only the second thread, and only for its author. + comment_on(&fx, number, "tahinli", "2", "Doomed").await; + let doomed = root_at(&fx, number, 1).await; + let uri = format!("/tahinli/demo/pulls/{number}/review_comments/{doomed}/delete"); let form = csrf_form("bob", &[]); let (status, _, _) = send(&fx.router, "POST", &uri, Some("bob"), Some(form)).await; - assert_eq!(status, 403); - + assert_eq!(status, 403, "delete by a non-author"); let form = csrf_form("tahinli", &[]); let (status, _, _) = send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await; - assert_eq!(status, 303); - assert!( - fx.store - .review_threads(&pr.issue.id, None) - .await - .unwrap() - .is_empty() - ); + assert_eq!(status, 303, "delete"); + let threads = fx.store.review_threads(&pr.issue.id, None).await.unwrap(); + assert_eq!(threads.len(), 1, "delete"); + assert_eq!(threads[0].root.id, root, "delete kept the other thread"); } + /// Reviews on one pull: a reader approves, the author cannot approve their own pull, and + /// after the pull closes it takes comments but no reviews. #[tokio::test] - async fn resolve_is_for_the_pr_author_or_write() { + async fn reviews_approve_then_close_the_pull() { let fx = Fixture::new(Visibility::Public).await; owner_session(&fx).await; sign_in(&fx, "bob", "bob").await; let number = open_pull(&fx).await; - comment_on(&fx, number, "tahinli", "2", "Thread").await; - let root = root_id(&fx, number).await; let pr = pull(&fx, number).await; - let uri = format!("/tahinli/demo/pulls/{number}/review_comments/{root}/resolve"); + let uri = format!("/tahinli/demo/pulls/{number}/reviews"); - let form = csrf_form("bob", &[("resolved", "1")]); - let (status, _, _) = send(&fx.router, "POST", &uri, Some("bob"), Some(form)).await; - assert_eq!(status, 403); + // A reader's approval is recorded. + let form = csrf_form("bob", &[("state", "approve")]); + let (status, location, _) = send(&fx.router, "POST", &uri, Some("bob"), Some(form)).await; + assert_eq!(status, 303, "reader approval"); + assert!(location.unwrap().contains("#review-"), "reader approval"); + let reviews = fx.store.list_reviews(&pr.issue.id).await.unwrap(); + assert_eq!(reviews.len(), 1, "reader approval"); + assert_eq!(reviews[0].state, ReviewState::Approved, "reader approval"); - let form = csrf_form("tahinli", &[("resolved", "1")]); - let (status, _, _) = send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await; - assert_eq!(status, 303); - let threads = fx.store.review_threads(&pr.issue.id, None).await.unwrap(); - assert!(threads[0].root.resolved); + // The author's approval is refused. + let form = csrf_form("tahinli", &[("state", "approve")]); + let (status, location, _) = + send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await; + assert_eq!(status, 303, "author approval"); + assert_eq!( + location.as_deref(), + Some(format!("/tahinli/demo/pulls/{number}?review_error=own_pr").as_str()), + "author approval" + ); + assert_eq!( + fx.store.list_reviews(&pr.issue.id).await.unwrap().len(), + 1, + "author approval" + ); + + // Once closed, comments still land but reviews are refused. + let tahinli = fx.store.user_by_username(OWNER).await.unwrap().unwrap(); + fx.store + .set_issue_state( + &pr.issue.id, + Some(&tahinli.id), + IssueState::Closed, + None, + serde_json::Value::Null, + ) + .await + .unwrap(); + let (status, location) = comment_on(&fx, number, "tahinli", "2", "After close").await; + assert_eq!(status, 303, "comment on a closed pull"); + assert!( + location.unwrap().contains("#L-0-new-2"), + "comment on a closed pull" + ); + let form = csrf_form("tahinli", &[("state", "approve")]); + let (status, location, _) = + send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await; + assert_eq!(status, 303, "review on a closed pull"); + assert!( + location.unwrap().ends_with("?review_error=closed"), + "review on a closed pull" + ); } #[tokio::test] @@ -1307,155 +1347,52 @@ mod tests { let form = csrf_form("bob", &[("reason", "mine")]); let (status, _, _) = send(&fx.router, "POST", &uri, Some("bob"), Some(form)).await; - assert_eq!(status, 403); + assert_eq!(status, 403, "dismiss by a reader"); let form = csrf_form("tahinli", &[("reason", " ")]); let (status, location, _) = send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await; - assert_eq!(status, 303); - assert!(location.unwrap().ends_with("?review_error=empty")); + assert_eq!(status, 303, "blank reason"); + assert!( + location.unwrap().ends_with("?review_error=empty"), + "blank reason" + ); let form = csrf_form("tahinli", &[("reason", "Addressed")]); let (status, _, _) = send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await; - assert_eq!(status, 303); + assert_eq!(status, 303, "dismiss"); let review = fx.store.list_reviews(&pr.issue.id).await.unwrap().remove(0); - assert_eq!(review.state, ReviewState::Dismissed); - } - - #[tokio::test] - async fn closed_pull_takes_comments_but_not_reviews() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - let number = open_pull(&fx).await; - let pr = pull(&fx, number).await; - let tahinli = fx.store.user_by_username(OWNER).await.unwrap().unwrap(); - fx.store - .set_issue_state( - &pr.issue.id, - Some(&tahinli.id), - IssueState::Closed, - None, - serde_json::Value::Null, - ) - .await - .unwrap(); - - let (status, location) = comment_on(&fx, number, "tahinli", "2", "After close").await; - assert_eq!(status, 303); - assert!(location.unwrap().contains("#L-0-new-2")); - - let form = csrf_form("tahinli", &[("state", "approve")]); - let (status, location, _) = send( - &fx.router, - "POST", - &format!("/tahinli/demo/pulls/{number}/reviews"), - Some("tahinli"), - Some(form), - ) - .await; - assert_eq!(status, 303); - assert!(location.unwrap().ends_with("?review_error=closed")); - } - - #[tokio::test] - async fn anonymous_writes_redirect_to_login_and_missing_csrf_is_forbidden() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - let number = open_pull(&fx).await; - let head = head_of(&fx, number).await; - let uri = format!("/tahinli/demo/pulls/{number}/review_comments"); - let form = csrf_form( - "tahinli", - &[ - ("file_index", "0"), - ("side", "new"), - ("line", "2"), - ("head_oid", head.as_str()), - ("action", "single"), - ("body", "Anon"), - ], - ); - let (status, location, _) = send(&fx.router, "POST", &uri, None, Some(form)).await; - assert_eq!(status, 303); - assert!(location.unwrap().starts_with("/login")); - - let form = - format!("file_index=0&side=new&line=2&head_oid={head}&action=single&body=NoCsrf"); - let (status, _, _) = send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await; - assert_eq!(status, 403); - } - - /// The comment body that proposes replacing line 2 of `a.txt` with `text`. - fn suggestion(text: &str) -> String { - format!("Try this:\n```suggestion\n{text}\n```") - } - - /// Posts the apply form for suggestion block 0 of comment `comment_id`. - async fn apply_as( - fx: &Fixture, - number: i64, - token: &str, - comment_id: &str, - ) -> (u16, Option) { - let form = csrf_form(token, &[("block", "0"), ("return", "files")]); - let uri = format!("/tahinli/demo/pulls/{number}/review_comments/{comment_id}/apply"); - let (status, location, _) = send(&fx.router, "POST", &uri, Some(token), Some(form)).await; - (status, location) + assert_eq!(review.state, ReviewState::Dismissed, "dismiss"); } + /// The apply-suggestion flow on one pull: a reader without push access sees no button, an + /// outdated suggestion is refused after the head moves, and a fresh suggestion applies. #[tokio::test] - async fn applying_a_suggestion_commits_it_to_the_head_and_resolves_the_thread() { + async fn suggestions_reader_outdated_and_apply() { let fx = Fixture::new(Visibility::Public).await; owner_session(&fx).await; + sign_in(&fx, "rita", "rita").await; let number = open_pull(&fx).await; - let before = pull(&fx, number).await.head_oid; - let (status, _) = comment_on(&fx, number, "tahinli", "2", &suggestion("two!")).await; - assert_eq!(status, 303); - let root = root_id(&fx, number).await; - let (status, location) = apply_as(&fx, number, "tahinli", &root).await; - assert_eq!(status, 303); - assert!(!location.unwrap().contains("review_error")); + assert_eq!(status, 303, "first suggestion"); + let first = root_at(&fx, number, 0).await; - let pr = pull(&fx, number).await; - assert_ne!( - pr.head_oid, before, - "the head moved to the suggestion commit" - ); - assert_eq!( - git(&fx.bare, &["show", &format!("{}:a.txt", pr.head_oid)]), - "one\ntwo!\nthree" - ); - assert_eq!( - git(&fx.bare, &["log", "-1", "--format=%an", &pr.head_oid]), - "tahinli" - ); - let message = git(&fx.bare, &["log", "-1", "--format=%B", &pr.head_oid]); + // A reader without push access sees the diff but no apply button, and is refused. + let page = get(&fx, number, "rita").await; + assert!(page.contains("Suggested change"), "the diff still shows"); assert!( - message.starts_with("Apply suggestion from review"), - "{message}" + !page.contains("Apply suggestion"), + "no apply button without push access" ); + let (status, location) = apply_as(&fx, number, "rita", &first).await; + assert_eq!(status, 303, "reader apply"); assert!( - message.contains("Co-authored-by: tahinli <"), - "commenter trailer: {message}" + location.unwrap().contains("review_error=no_push"), + "reader apply" ); - assert_eq!(git(&fx.bare, &["rev-parse", "feature"]), pr.head_oid); - let threads = fx.store.review_threads(&pr.issue.id, None).await.unwrap(); - assert!(threads[0].root.resolved, "applying resolves the thread"); - } - #[tokio::test] - async fn outdated_suggestion_is_shown_without_apply_and_refused() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - let number = open_pull(&fx).await; - let (status, _) = comment_on(&fx, number, "tahinli", "2", &suggestion("two!")).await; - assert_eq!(status, 303); - let root = root_id(&fx, number).await; - - // The head moves on: its line 2 no longer reads what the suggestion was written against. + // The head moves on: the first suggestion is outdated, shown without apply, and refused. let pr = pull(&fx, number).await; - git(&fx.work, &["checkout", "-q", "feature"]); std::fs::write(fx.work.join("a.txt"), "one\nmoved\nthree\n").unwrap(); git(&fx.work, &["commit", "-q", "-am", "Move line 2"]); let moved = git(&fx.work, &["rev-parse", "HEAD"]); @@ -1482,10 +1419,12 @@ mod tests { !page.contains("Apply suggestion"), "no apply button when outdated" ); - - let (status, location) = apply_as(&fx, number, "tahinli", &root).await; - assert_eq!(status, 303); - assert!(location.unwrap().contains("review_error=outdated")); + let (status, location) = apply_as(&fx, number, "tahinli", &first).await; + assert_eq!(status, 303, "outdated apply"); + assert!( + location.unwrap().contains("review_error=outdated"), + "outdated apply" + ); assert_eq!( git(&fx.bare, &["rev-parse", "feature"]), moved, @@ -1496,28 +1435,48 @@ mod tests { !threads[0].root.resolved, "a refused apply leaves the thread open" ); - } - #[tokio::test] - async fn a_reader_without_push_access_cannot_apply_and_sees_no_button() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - let number = open_pull(&fx).await; + // A fresh suggestion against the moved head applies as a commit by the commenter. + let before = pull(&fx, number).await.head_oid; + assert_eq!(before, moved, "the head is the moved commit"); let (status, _) = comment_on(&fx, number, "tahinli", "2", &suggestion("two!")).await; - assert_eq!(status, 303); - let root = root_id(&fx, number).await; - sign_in(&fx, "rita", "rita").await; + assert_eq!(status, 303, "fresh suggestion"); + let second = root_at(&fx, number, 1).await; + let (status, location) = apply_as(&fx, number, "tahinli", &second).await; + assert_eq!(status, 303, "apply"); + assert!(!location.unwrap().contains("review_error"), "apply"); - let page = get(&fx, number, "rita").await; - assert!(page.contains("Suggested change"), "the diff still shows"); + let pr = pull(&fx, number).await; + assert_ne!( + pr.head_oid, before, + "the head moved to the suggestion commit" + ); + assert_eq!( + git(&fx.bare, &["show", &format!("{}:a.txt", pr.head_oid)]), + "one\ntwo!\nthree", + "apply" + ); + assert_eq!( + git(&fx.bare, &["log", "-1", "--format=%an", &pr.head_oid]), + "tahinli", + "apply" + ); + let message = git(&fx.bare, &["log", "-1", "--format=%B", &pr.head_oid]); assert!( - !page.contains("Apply suggestion"), - "no apply button without push access" + message.starts_with("Apply suggestion from review"), + "{message}" ); - - let (status, location) = apply_as(&fx, number, "rita", &root).await; - assert_eq!(status, 303); - assert!(location.unwrap().contains("review_error=no_push")); + assert!( + message.contains("Co-authored-by: tahinli <"), + "commenter trailer: {message}" + ); + assert_eq!( + git(&fx.bare, &["rev-parse", "feature"]), + pr.head_oid, + "apply" + ); + let threads = fx.store.review_threads(&pr.issue.id, None).await.unwrap(); + assert!(threads[1].root.resolved, "applying resolves the thread"); } /// The Files tab of the pull request as `token` sees it. diff --git a/crates/forge-web/src/pages/pull_view.rs b/crates/forge-web/src/pages/pull_view.rs index 2d63e16..695119c 100644 --- a/crates/forge-web/src/pages/pull_view.rs +++ b/crates/forge-web/src/pages/pull_view.rs @@ -1206,12 +1206,18 @@ mod tests { pr.issue.number } + /// Conversation tab, merge box, merge and review error codes, and the read-only tabs, on one + /// open pull. Mergeability is set by each scenario right before it is checked. #[tokio::test] - async fn turkish_viewer_gets_turkish_pull_tabs() { + async fn conversation_merge_box_and_error_codes_on_one_pull() { let fx = Fixture::new(Visibility::Public).await; owner_session(&fx).await; + sign_in(&fx, "bob", "bob-token").await; let number = open_pull(&fx).await; + let pr = pull(&fx, number).await; let uri = format!("/tahinli/demo/pulls/{number}"); + + // Turkish viewers get the translated tabs. let req = Request::builder() .method("GET") .uri(uri.as_str()) @@ -1221,100 +1227,12 @@ mod tests { let res = fx.router.handle(req).await; let bytes = to_bytes(res.into_body(), usize::MAX).await.unwrap(); let body = String::from_utf8_lossy(&bytes).into_owned(); - assert!(body.contains("Tartışma"), "Turkish conversation tab"); - assert!(body.contains("Commitler "), "Turkish commits tab"); - assert!(body.contains("Değişen dosyalar "), "Turkish files tab"); + assert!(body.contains("Tartışma"), "turkish conversation tab"); + assert!(body.contains("Commitler "), "turkish commits tab"); + assert!(body.contains("Değişen dosyalar "), "turkish files tab"); assert!(!body.contains(">Conversation<"), "no English tab label"); - } - - #[tokio::test] - async fn conversation_renders_title_badge_and_merge_box() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - let number = open_pull(&fx).await; - let pr = pull(&fx, number).await; - fx.store - .set_mergeability( - &pr.issue.id, - &pr.base_oid, - Some(&fx.root), - Mergeable::Clean, - &[], - ) - .await - .unwrap(); - - let uri = format!("/tahinli/demo/pulls/{number}"); - let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await; - assert_eq!(status, 200); - assert!(body.contains("Add feature")); - assert!(body.contains("issue-badge issue-badge-open")); - assert!(body.contains("This branch has no conflicts with the base branch")); - assert!(body.contains(&format!("action=\"{uri}/merge\""))); - assert!(body.contains("name=\"style\"")); - assert!(body.contains(&format!("action=\"{uri}/comments\""))); - assert!(body.contains("Commits 1")); - assert!(body.contains("Files changed 1")); - } - - #[tokio::test] - async fn merge_error_code_shows_its_message_and_unknown_code_shows_nothing() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - let number = open_pull(&fx).await; - let uri = format!("/tahinli/demo/pulls/{number}"); - - let (status, _, body) = send( - &fx.router, - "GET", - &format!("{uri}?merge_error=conflict"), - Some("tahinli"), - None, - ) - .await; - assert_eq!(status, 200); - assert!(body.contains("class=\"alert merge-error\""), "{body}"); - assert!(body.contains("This branch has conflicts that must be resolved")); - - let (status, _, body) = send( - &fx.router, - "GET", - &format!("{uri}?merge_error=%3Cscript%3Ealert(1)%3C%2Fscript%3E"), - Some("tahinli"), - None, - ) - .await; - assert_eq!(status, 200); - assert!(!body.contains("merge-error"), "{body}"); - assert!(!page_main(&body).contains("alert(1)"), "{body}"); - } - #[tokio::test] - async fn commits_tab_lists_the_commit() { - let fx = Fixture::new(Visibility::Public).await; - let number = open_pull(&fx).await; - let uri = format!("/tahinli/demo/pulls/{number}/commits"); - let (status, _, body) = send(&fx.router, "GET", &uri, None, None).await; - assert_eq!(status, 200); - assert!(body.contains("Add feature file")); - } - - #[tokio::test] - async fn files_tab_shows_the_changed_file() { - let fx = Fixture::new(Visibility::Public).await; - let number = open_pull(&fx).await; - let uri = format!("/tahinli/demo/pulls/{number}/files"); - let (status, _, body) = send(&fx.router, "GET", &uri, None, None).await; - assert_eq!(status, 200); - assert!(body.contains("feat.txt")); - } - - #[tokio::test] - async fn conflict_lists_files_and_offers_no_merge() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - let number = open_pull(&fx).await; - let pr = pull(&fx, number).await; + // A conflict lists the files and offers no merge. fx.store .set_mergeability( &pr.issue.id, @@ -1325,22 +1243,20 @@ mod tests { ) .await .unwrap(); - - let uri = format!("/tahinli/demo/pulls/{number}"); let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await; - assert_eq!(status, 200); - assert!(body.contains("This branch has conflicts that must be resolved")); - assert!(body.contains("feat.txt")); - assert!(!body.contains(&format!("action=\"{uri}/merge\""))); - assert!(!body.contains("name=\"style\"")); - } + assert_eq!(status, 200, "conflict"); + assert!( + body.contains("This branch has conflicts that must be resolved"), + "conflict: {body}" + ); + assert!(body.contains("feat.txt"), "conflict: {body}"); + assert!( + !body.contains(&format!("action=\"{uri}/merge\"")), + "conflict: {body}" + ); + assert!(!body.contains("name=\"style\""), "conflict: {body}"); - #[tokio::test] - async fn read_only_viewer_sees_no_merge_form() { - let fx = Fixture::new(Visibility::Public).await; - sign_in(&fx, "bob", "bob").await; - let number = open_pull(&fx).await; - let pr = pull(&fx, number).await; + // A clean branch shows the title, badge, merge box and conversation form. fx.store .set_mergeability( &pr.issue.id, @@ -1351,167 +1267,142 @@ mod tests { ) .await .unwrap(); + let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await; + assert_eq!(status, 200, "clean"); + assert!(body.contains("Add feature"), "clean: {body}"); + assert!( + body.contains("issue-badge issue-badge-open"), + "clean: {body}" + ); + assert!( + body.contains("This branch has no conflicts with the base branch"), + "clean: {body}" + ); + assert!( + body.contains(&format!("action=\"{uri}/merge\"")), + "clean: {body}" + ); + assert!(body.contains("name=\"style\""), "clean: {body}"); + assert!( + body.contains(&format!("action=\"{uri}/comments\"")), + "clean: {body}" + ); + assert!(body.contains("Commits 1"), "clean: {body}"); + assert!(body.contains("Files changed 1"), "clean: {body}"); - let uri = format!("/tahinli/demo/pulls/{number}"); - let (status, _, body) = send(&fx.router, "GET", &uri, Some("bob"), None).await; - assert_eq!(status, 200); - assert!(body.contains("This branch has no conflicts with the base branch")); - assert!(!body.contains("name=\"style\"")); - assert!(!body.contains("Merge pull request")); - } + // A reader sees the clean status but no merge form. + let (status, _, body) = send(&fx.router, "GET", &uri, Some("bob-token"), None).await; + assert_eq!(status, 200, "reader"); + assert!( + body.contains("This branch has no conflicts with the base branch"), + "reader: {body}" + ); + assert!(!body.contains("name=\"style\""), "reader: {body}"); + assert!(!body.contains("Merge pull request"), "reader: {body}"); - #[tokio::test] - async fn issue_url_for_a_pull_redirects_to_the_pull() { - let fx = Fixture::new(Visibility::Public).await; - let number = open_pull(&fx).await; - let (status, location, _) = send( + // Known merge error codes show their message, unknown ones show nothing. + let (status, _, body) = send( &fx.router, "GET", - &format!("/tahinli/demo/issues/{number}"), - None, + &format!("{uri}?merge_error=conflict"), + Some("tahinli"), None, ) .await; - assert_eq!(status, 303); - assert_eq!( - location.as_deref(), - Some(format!("/tahinli/demo/pulls/{number}").as_str()) + assert_eq!(status, 200, "merge error"); + assert!( + body.contains("class=\"alert merge-error\""), + "merge error: {body}" ); - } - - #[tokio::test] - async fn comment_on_a_pull_is_saved() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - let number = open_pull(&fx).await; - let uri = format!("/tahinli/demo/pulls/{number}/comments"); - let form = csrf_form("tahinli", &[("body", "Looks good to me")]); - let (status, location, _) = - send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await; - assert_eq!(status, 303); assert!( - location - .unwrap() - .starts_with(&format!("/tahinli/demo/pulls/{number}#comment-")) + body.contains("This branch has conflicts that must be resolved"), + "merge error: {body}" ); - - let pr = pull(&fx, number).await; - let comments = fx.store.list_comments(&pr.issue.id).await.unwrap(); - assert_eq!(comments.len(), 1); - assert_eq!(comments[0].body, "Looks good to me"); - } - - #[tokio::test] - async fn state_route_closes_and_reopens_a_pull() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - let number = open_pull(&fx).await; - let uri = format!("/tahinli/demo/pulls/{number}/state"); - - let form = csrf_form("tahinli", &[("action", "close_not_planned")]); - let (status, _, _) = send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await; - assert_eq!(status, 303); - let pr = pull(&fx, number).await; - assert!(matches!(pr.issue.state, IssueState::Closed)); - - let form = csrf_form("tahinli", &[("action", "reopen")]); - let (status, _, _) = send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await; - assert_eq!(status, 303); - let pr = pull(&fx, number).await; - assert!(matches!(pr.issue.state, IssueState::Open)); - } - - #[tokio::test] - async fn author_edits_pull_title_and_body() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - let number = open_pull(&fx).await; - let uri = format!("/tahinli/demo/pulls/{number}"); - - let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await; - assert_eq!(status, 200); - assert!(body.contains(&format!("href=\"{uri}/edit\""))); - let (status, _, body) = send( &fx.router, "GET", - &format!("{uri}/edit"), + &format!("{uri}?merge_error=%3Cscript%3Ealert(1)%3C%2Fscript%3E"), Some("tahinli"), None, ) .await; - assert_eq!(status, 200); - assert!(body.contains("Edit pull request")); - - let form = csrf_form( - "tahinli", - &[("title", "Renamed pull"), ("body", "New text")], + assert_eq!(status, 200, "unknown merge error"); + assert!(!body.contains("merge-error"), "unknown merge error: {body}"); + assert!( + !page_main(&body).contains("alert(1)"), + "unknown merge error: {body}" ); - let (status, location, _) = send( + + // Known review error codes show their message, unknown ones show nothing. + let (status, _, body) = send( &fx.router, - "POST", - &format!("{uri}/edit"), + "GET", + &format!("{uri}?review_error=own_pr"), Some("tahinli"), - Some(form), + None, ) .await; - assert_eq!(status, 303); - assert_eq!(location.as_deref(), Some(uri.as_str())); - - let pr = pull(&fx, number).await; - assert_eq!(pr.issue.title, "Renamed pull"); - assert_eq!(pr.issue.body, "New text"); - let events = fx.store.list_events(&pr.issue.id).await.unwrap(); - assert!(events.iter().any(|e| e.kind == "renamed")); - } - - #[tokio::test] - async fn non_author_reader_cannot_edit_a_pull() { - let fx = Fixture::new(Visibility::Public).await; - owner_session(&fx).await; - sign_in(&fx, "bob", "bob-token").await; - let number = open_pull(&fx).await; - let uri = format!("/tahinli/demo/pulls/{number}"); - - let (_, _, body) = send(&fx.router, "GET", &uri, Some("bob-token"), None).await; - assert!(!body.contains(&format!("href=\"{uri}/edit\""))); - - let (status, _, _) = send( + assert_eq!(status, 200, "review error"); + assert!( + body.contains("You cannot approve or request changes on your own pull request."), + "review error: {body}" + ); + let (_, _, body) = send( &fx.router, "GET", - &format!("{uri}/edit"), - Some("bob-token"), + &format!("{uri}?review_error=%3Cscript%3E"), + Some("tahinli"), None, ) .await; - assert_eq!(status, 403); - let form = csrf_form("bob-token", &[("title", "Hijacked"), ("body", "x")]); - let (status, _, _) = send( + assert!(!body.contains("