forge
Explore
Log in

Don't you dare go Hollow

Pull page tests share their fixtures and merge into tables

Browse files at this commit

f23b28cdf6c1034b5debcbfd748fee004f9a2f12Ahmet Kaan GümüşParent: eb7e617
  • test
    • pull page tests share their fixtures and merge into tables
Show full message
✅ test: pull page tests share their fixtures and merge into tables

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.rsmodified+125 -154
@@ -364,15 +364,23 @@ mod tests {
364364 format!("/{OWNER}/{REPO}/pulls/{number}/files{query}")
365365 }
366366
367+ /// Every Files tab scenario for one open pull request. The pull is opened once. Each
368+ /// scenario adds only what it needs, and the scenarios that add comments run last in an
369+ /// order where no later scenario is confused by an earlier comment.
367370 #[tokio::test]
368- async fn turkish_viewer_gets_turkish_review_bar() {
371+ async fn files_tab_scenarios_on_one_open_pull() {
369372 let fx = Fixture::new(Visibility::Public).await;
370373 owner_session(&fx).await;
374+ sign_in(&fx, "bob", "bob").await;
371375 let number = open_pull(&fx).await;
376+ let (issue_id, head) = pull_ids(&fx, number).await;
377+ let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap();
378+ let uri = files_uri(number, "");
372379
380+ // Turkish viewers get the translated review bar.
373381 let req = Request::builder()
374382 .method("GET")
375- .uri(files_uri(number, ""))
383+ .uri(uri.as_str())
376384 .header("cookie", cookie("tahinli"))
377385 .header("accept-language", "tr")
378386 .body(Body::empty())
@@ -380,57 +388,47 @@ mod tests {
380388 let res = fx.router.handle(req).await;
381389 let bytes = to_bytes(res.into_body(), usize::MAX).await.unwrap();
382390 let body = String::from_utf8_lossy(&bytes).into_owned();
383- assert!(body.contains("İncelemeyi tamamla"), "{body}");
384- assert!(body.contains("Bekleyen yorumları sil"), "{body}");
385- }
386-
387- #[tokio::test]
388- async fn files_tab_has_line_anchors_and_plus_links_for_signed_in_viewers() {
389- let fx = Fixture::new(Visibility::Public).await;
390- owner_session(&fx).await;
391- let number = open_pull(&fx).await;
392-
393- let (status, body) = get(&fx.router, &files_uri(number, ""), Some("tahinli")).await;
394- assert_eq!(status, 200);
395- assert!(body.contains("id=\"L-0-new-1\""), "{body}");
396- assert!(body.contains("?comment=0:new:1#L-0-new-1"), "{body}");
397- assert!(body.contains("review-bar"), "{body}");
398- }
391+ assert!(body.contains("İncelemeyi tamamla"), "turkish bar: {body}");
392+ assert!(
393+ body.contains("Bekleyen yorumları sil"),
394+ "turkish bar: {body}"
395+ );
399396
400- #[tokio::test]
401- async fn comment_param_opens_the_form_under_its_line() {
402- let fx = Fixture::new(Visibility::Public).await;
403- owner_session(&fx).await;
404- let number = open_pull(&fx).await;
405- let (_, head) = pull_ids(&fx, number).await;
397+ // Signed-in viewer: line anchors and plus links.
398+ let (status, body) = get(&fx.router, &uri, Some("tahinli")).await;
399+ assert_eq!(status, 200, "anchors");
400+ assert!(body.contains("id=\"L-0-new-1\""), "anchors: {body}");
401+ assert!(
402+ body.contains("?comment=0:new:1#L-0-new-1"),
403+ "plus link: {body}"
404+ );
405+ assert!(body.contains("review-bar"), "review bar: {body}");
406406
407+ // A well-formed `comment` param opens the form under its line.
407408 let (status, body) = get(
408409 &fx.router,
409410 &files_uri(number, "?comment=0:new:1"),
410411 Some("tahinli"),
411412 )
412413 .await;
413- assert_eq!(status, 200);
414- assert!(body.contains("name=\"file_index\" value=\"0\""), "{body}");
415- assert!(body.contains("name=\"side\" value=\"new\""), "{body}");
416- assert!(body.contains("name=\"line\" value=\"1\""), "{body}");
414+ assert_eq!(status, 200, "comment form");
415+ assert!(
416+ body.contains("name=\"file_index\" value=\"0\""),
417+ "form: {body}"
418+ );
419+ assert!(body.contains("name=\"side\" value=\"new\""), "form: {body}");
420+ assert!(body.contains("name=\"line\" value=\"1\""), "form: {body}");
417421 assert!(
418422 body.contains(&format!("name=\"head_oid\" value=\"{head}\"")),
419- "{body}"
423+ "form head: {body}"
420424 );
421- assert!(body.contains("value=\"single\""), "{body}");
422- assert!(body.contains("value=\"review\""), "{body}");
425+ assert!(body.contains("value=\"single\""), "form: {body}");
426+ assert!(body.contains("value=\"review\""), "form: {body}");
423427 let line = body.find("id=\"L-0-new-1\"").unwrap();
424428 let form = body.find("name=\"file_index\"").unwrap();
425429 assert!(form > line, "the form sits after its line");
426- }
427-
428- #[tokio::test]
429- async fn garbage_comment_param_is_ignored() {
430- let fx = Fixture::new(Visibility::Public).await;
431- owner_session(&fx).await;
432- let number = open_pull(&fx).await;
433430
431+ // Malformed `comment` params are ignored.
434432 for query in [
435433 "?comment=abc",
436434 "?comment=0:bogus:1",
@@ -441,48 +439,59 @@ mod tests {
441439 "?comment=0:new:-1",
442440 ] {
443441 let (status, body) = get(&fx.router, &files_uri(number, query), Some("tahinli")).await;
444- assert_eq!(status, 200, "{query}");
445- assert!(!body.contains("name=\"file_index\""), "{query}");
442+ assert_eq!(status, 200, "garbage {query}");
443+ assert!(!body.contains("name=\"file_index\""), "garbage {query}");
446444 }
447- }
448445
449- #[tokio::test]
450- async fn existing_thread_renders_under_its_line() {
451- let fx = Fixture::new(Visibility::Public).await;
452- owner_session(&fx).await;
453- let number = open_pull(&fx).await;
454- let (issue_id, head) = pull_ids(&fx, number).await;
455- let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap();
456- fx.store
457- .add_review_comment(
458- &issue_id,
459- &owner.id,
460- false,
461- "feat.txt",
462- Side::New,
463- 1,
464- &head,
465- "+feature",
466- "Looks good",
467- )
468- .await
469- .unwrap();
446+ // The review bar hides approve from the pull's author only.
447+ let (_, author) = get(&fx.router, &uri, Some("tahinli")).await;
448+ assert!(author.contains("value=\"comment\""), "author bar: {author}");
449+ assert!(
450+ !author.contains("value=\"approve\""),
451+ "author bar: {author}"
452+ );
453+ assert!(
454+ !author.contains("value=\"request_changes\""),
455+ "author bar: {author}"
456+ );
457+ let (_, other) = get(&fx.router, &uri, Some("bob")).await;
458+ assert!(other.contains("value=\"approve\""), "reader bar: {other}");
459+ assert!(
460+ other.contains("value=\"request_changes\""),
461+ "reader bar: {other}"
462+ );
470463
471- let (status, body) = get(&fx.router, &files_uri(number, ""), Some("tahinli")).await;
472- assert_eq!(status, 200);
473- assert!(body.contains("Looks good"), "{body}");
474- let line = body.find("id=\"L-0-new-1\"").unwrap();
475- let thread = body.find("class=\"review-thread\"").unwrap();
476- assert!(thread > line, "the thread sits after its line");
477- }
464+ // Anonymous viewers get no comment controls at all.
465+ let (status, body) = get(&fx.router, &files_uri(number, "?comment=0:new:1"), None).await;
466+ assert_eq!(status, 200, "anonymous");
467+ assert!(!page_main(&body).contains("?comment="), "anonymous: {body}");
468+ assert!(!body.contains("id=\"L-"), "anonymous: {body}");
469+ assert!(!body.contains("name=\"file_index\""), "anonymous: {body}");
470+ assert!(!body.contains("review-bar"), "anonymous: {body}");
471+ assert!(!body.contains("class=\"diff-plus\""), "anonymous: {body}");
472+
473+ // A known review error shows its message, an unknown code shows nothing.
474+ let (_, known) = get(
475+ &fx.router,
476+ &files_uri(number, "?review_error=own_pr"),
477+ Some("tahinli"),
478+ )
479+ .await;
480+ assert!(known.contains("cannot approve"), "known error: {known}");
481+ let (status, unknown) = get(
482+ &fx.router,
483+ &files_uri(number, "?review_error=%3Cb%3Ehi%3C%2Fb%3E"),
484+ Some("tahinli"),
485+ )
486+ .await;
487+ assert_eq!(status, 200, "unknown error");
488+ assert!(
489+ !unknown.contains("role=\"alert\""),
490+ "unknown error: {unknown}"
491+ );
492+ assert!(!unknown.contains("<b>hi</b>"), "unknown error: {unknown}");
478493
479- #[tokio::test]
480- async fn outdated_thread_is_not_shown_on_the_files_tab() {
481- let fx = Fixture::new(Visibility::Public).await;
482- owner_session(&fx).await;
483- let number = open_pull(&fx).await;
484- let (issue_id, head) = pull_ids(&fx, number).await;
485- let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap();
494+ // An outdated thread is hidden. Only the outdated comment exists at this point.
486495 let comment = fx
487496 .store
488497 .add_review_comment(
@@ -502,21 +511,15 @@ mod tests {
502511 .mark_outdated(&issue_id, std::slice::from_ref(&comment.id))
503512 .await
504513 .unwrap();
514+ let (status, body) = get(&fx.router, &uri, Some("tahinli")).await;
515+ assert_eq!(status, 200, "outdated");
516+ assert!(!body.contains("Stale remark"), "outdated: {body}");
517+ assert!(
518+ !body.contains("class=\"review-thread\""),
519+ "outdated: {body}"
520+ );
505521
506- let (status, body) = get(&fx.router, &files_uri(number, ""), Some("tahinli")).await;
507- assert_eq!(status, 200);
508- assert!(!body.contains("Stale remark"), "{body}");
509- assert!(!body.contains("class=\"review-thread\""), "{body}");
510- }
511-
512- #[tokio::test]
513- async fn pending_comment_is_visible_only_to_its_author() {
514- let fx = Fixture::new(Visibility::Public).await;
515- owner_session(&fx).await;
516- sign_in(&fx, "bob", "bob").await;
517- let number = open_pull(&fx).await;
518- let (issue_id, head) = pull_ids(&fx, number).await;
519- let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap();
522+ // A pending comment is visible only to its author.
520523 fx.store
521524 .add_review_comment(
522525 &issue_id,
@@ -531,71 +534,39 @@ mod tests {
531534 )
532535 .await
533536 .unwrap();
537+ let (_, anonymous) = get(&fx.router, &uri, None).await;
538+ assert!(!anonymous.contains("Draft secret"), "pending: {anonymous}");
539+ let (_, bob) = get(&fx.router, &uri, Some("bob")).await;
540+ assert!(!bob.contains("Draft secret"), "pending: {bob}");
541+ let (_, author) = get(&fx.router, &uri, Some("tahinli")).await;
542+ assert!(author.contains("Draft secret"), "pending author: {author}");
543+ assert!(author.contains("Pending"), "pending author: {author}");
544+ assert!(
545+ author.contains("1 pending comment."),
546+ "pending author: {author}"
547+ );
534548
535- let (_, anonymous) = get(&fx.router, &files_uri(number, ""), None).await;
536- assert!(!anonymous.contains("Draft secret"));
537- let (_, bob) = get(&fx.router, &files_uri(number, ""), Some("bob")).await;
538- assert!(!bob.contains("Draft secret"));
539- let (_, author) = get(&fx.router, &files_uri(number, ""), Some("tahinli")).await;
540- assert!(author.contains("Draft secret"), "{author}");
541- assert!(author.contains("Pending"), "{author}");
542- assert!(author.contains("1 pending comment."), "{author}");
543- }
544-
545- #[tokio::test]
546- async fn review_bar_hides_approve_for_the_pr_author() {
547- let fx = Fixture::new(Visibility::Public).await;
548- owner_session(&fx).await;
549- sign_in(&fx, "bob", "bob").await;
550- let number = open_pull(&fx).await;
551-
552- let (_, author) = get(&fx.router, &files_uri(number, ""), Some("tahinli")).await;
553- assert!(author.contains("value=\"comment\""), "{author}");
554- assert!(!author.contains("value=\"approve\""), "{author}");
555- assert!(!author.contains("value=\"request_changes\""), "{author}");
556-
557- let (_, other) = get(&fx.router, &files_uri(number, ""), Some("bob")).await;
558- assert!(other.contains("value=\"approve\""), "{other}");
559- assert!(other.contains("value=\"request_changes\""), "{other}");
560- }
561-
562- #[tokio::test]
563- async fn anonymous_viewer_sees_no_comment_controls() {
564- let fx = Fixture::new(Visibility::Public).await;
565- let number = open_pull(&fx).await;
566-
567- let (status, body) = get(&fx.router, &files_uri(number, "?comment=0:new:1"), None).await;
568- assert_eq!(status, 200);
569- assert!(!page_main(&body).contains("?comment="), "{body}");
570- assert!(!body.contains("id=\"L-"), "{body}");
571- assert!(!body.contains("name=\"file_index\""), "{body}");
572- assert!(!body.contains("review-bar"), "{body}");
573- assert!(!body.contains("class=\"diff-plus\""), "{body}");
574- }
575-
576- #[tokio::test]
577- async fn known_review_error_shows_its_message_and_unknown_code_shows_nothing() {
578- let fx = Fixture::new(Visibility::Public).await;
579- owner_session(&fx).await;
580- let number = open_pull(&fx).await;
581-
582- let (_, known) = get(
583- &fx.router,
584- &files_uri(number, "?review_error=own_pr"),
585- Some("tahinli"),
586- )
587- .await;
588- assert!(known.contains("cannot approve"), "{known}");
589-
590- let (status, unknown) = get(
591- &fx.router,
592- &files_uri(number, "?review_error=%3Cb%3Ehi%3C%2Fb%3E"),
593- Some("tahinli"),
594- )
595- .await;
596- assert_eq!(status, 200);
597- assert!(!unknown.contains("role=\"alert\""), "{unknown}");
598- assert!(!unknown.contains("<b>hi</b>"), "{unknown}");
549+ // A submitted thread renders under its line.
550+ fx.store
551+ .add_review_comment(
552+ &issue_id,
553+ &owner.id,
554+ false,
555+ "feat.txt",
556+ Side::New,
557+ 1,
558+ &head,
559+ "+feature",
560+ "Looks good",
561+ )
562+ .await
563+ .unwrap();
564+ let (status, body) = get(&fx.router, &uri, Some("tahinli")).await;
565+ assert_eq!(status, 200, "thread");
566+ assert!(body.contains("Looks good"), "thread: {body}");
567+ let line = body.find("id=\"L-0-new-1\"").unwrap();
568+ let thread = body.find("class=\"review-thread\"").unwrap();
569+ assert!(thread > line, "the thread sits after its line");
599570 }
600571
601572 #[tokio::test]
crates/forge-web/src/pages/pull_reviews.rsmodified+246 -287
@@ -997,64 +997,59 @@ mod tests {
997997 (status, location)
998998 }
999999
1000- /// The id of the first root comment on the pull request.
1001- async fn root_id(fx: &Fixture, number: i64) -> String {
1000+ /// The id of the thread at `index` (in creation order) on the pull request.
1001+ async fn root_at(fx: &Fixture, number: i64, index: usize) -> String {
10021002 let pr = pull(fx, number).await;
1003- fx.store.review_threads(&pr.issue.id, None).await.unwrap()[0]
1003+ fx.store.review_threads(&pr.issue.id, None).await.unwrap()[index]
10041004 .root
10051005 .id
10061006 .clone()
10071007 }
10081008
1009- #[tokio::test]
1010- async fn single_comment_on_an_added_line_creates_a_thread_with_its_hunk() {
1011- let fx = Fixture::new(Visibility::Public).await;
1012- owner_session(&fx).await;
1013- let number = open_pull(&fx).await;
1014- let pr = pull(&fx, number).await;
1009+ /// The comment body that proposes replacing line 2 of `a.txt` with `text`.
1010+ fn suggestion(text: &str) -> String {
1011+ format!("Try this:\n```suggestion\n{text}\n```")
1012+ }
10151013
1016- let (status, location) = comment_on(&fx, number, "tahinli", "2", "Looks good").await;
1017- assert_eq!(status, 303);
1018- assert_eq!(
1019- location.as_deref(),
1020- Some(format!("/tahinli/demo/pulls/{number}/files#L-0-new-2").as_str())
1021- );
1022- let threads = fx.store.review_threads(&pr.issue.id, None).await.unwrap();
1023- assert_eq!(threads.len(), 1);
1024- let root = &threads[0].root;
1025- assert_eq!(
1026- (root.path.as_str(), root.side, root.line),
1027- ("a.txt", Side::New, 2)
1028- );
1029- assert_eq!(root.diff_hunk, " one\n-two\n+changed");
1030- assert!(!root.pending);
1014+ /// Posts the apply form for suggestion block 0 of comment `comment_id`.
1015+ async fn apply_as(
1016+ fx: &Fixture,
1017+ number: i64,
1018+ token: &str,
1019+ comment_id: &str,
1020+ ) -> (u16, Option<String>) {
1021+ let form = csrf_form(token, &[("block", "0"), ("return", "files")]);
1022+ let uri = format!("/tahinli/demo/pulls/{number}/review_comments/{comment_id}/apply");
1023+ let (status, location, _) = send(&fx.router, "POST", &uri, Some(token), Some(form)).await;
1024+ (status, location)
10311025 }
10321026
1027+ /// Comment posting rules and a pending review, on one open pull. Scenarios run in order:
1028+ /// the rejected posts first (no thread), then the first thread, then the pending review.
10331029 #[tokio::test]
1034- async fn comment_on_a_line_outside_the_diff_is_rejected() {
1030+ async fn comment_rules_and_pending_review_on_one_pull() {
10351031 let fx = Fixture::new(Visibility::Public).await;
10361032 owner_session(&fx).await;
10371033 let number = open_pull(&fx).await;
10381034 let pr = pull(&fx, number).await;
10391035
1036+ // A line outside the diff is rejected and leaves no thread.
10401037 let (status, location) = comment_on(&fx, number, "tahinli", "99", "Nope").await;
1041- assert_eq!(status, 303);
1042- assert!(location.unwrap().contains("review_error=bad_line"));
1038+ assert_eq!(status, 303, "outside diff");
1039+ assert!(
1040+ location.unwrap().contains("review_error=bad_line"),
1041+ "outside diff"
1042+ );
10431043 assert!(
10441044 fx.store
10451045 .review_threads(&pr.issue.id, None)
10461046 .await
10471047 .unwrap()
1048- .is_empty()
1048+ .is_empty(),
1049+ "outside diff left a thread"
10491050 );
1050- }
10511051
1052- #[tokio::test]
1053- async fn stale_head_is_rejected() {
1054- let fx = Fixture::new(Visibility::Public).await;
1055- owner_session(&fx).await;
1056- let number = open_pull(&fx).await;
1057- let pr = pull(&fx, number).await;
1052+ // A head that is not the pull's current head is rejected.
10581053 let zero = "0".repeat(40);
10591054 let form = csrf_form(
10601055 "tahinli",
@@ -1075,26 +1070,41 @@ mod tests {
10751070 Some(form),
10761071 )
10771072 .await;
1078- assert_eq!(status, 303);
1073+ assert_eq!(status, 303, "stale head");
10791074 assert_eq!(
10801075 location.as_deref(),
1081- Some(format!("/tahinli/demo/pulls/{number}/files?review_error=stale").as_str())
1076+ Some(format!("/tahinli/demo/pulls/{number}/files?review_error=stale").as_str()),
1077+ "stale head"
10821078 );
10831079 assert!(
10841080 fx.store
10851081 .review_threads(&pr.issue.id, None)
10861082 .await
10871083 .unwrap()
1088- .is_empty()
1084+ .is_empty(),
1085+ "stale head left a thread"
10891086 );
1090- }
10911087
1092- #[tokio::test]
1093- async fn start_review_keeps_the_comment_pending_until_submitted() {
1094- let fx = Fixture::new(Visibility::Public).await;
1095- owner_session(&fx).await;
1096- let number = open_pull(&fx).await;
1097- let pr = pull(&fx, number).await;
1088+ // A single comment on an added line creates a thread that carries its hunk.
1089+ let (status, location) = comment_on(&fx, number, "tahinli", "2", "Looks good").await;
1090+ assert_eq!(status, 303, "single comment");
1091+ assert_eq!(
1092+ location.as_deref(),
1093+ Some(format!("/tahinli/demo/pulls/{number}/files#L-0-new-2").as_str()),
1094+ "single comment"
1095+ );
1096+ let threads = fx.store.review_threads(&pr.issue.id, None).await.unwrap();
1097+ assert_eq!(threads.len(), 1, "single comment");
1098+ let root = &threads[0].root;
1099+ assert_eq!(
1100+ (root.path.as_str(), root.side, root.line),
1101+ ("a.txt", Side::New, 2),
1102+ "single comment"
1103+ );
1104+ assert_eq!(root.diff_hunk, " one\n-two\n+changed", "single comment");
1105+ assert!(!root.pending, "single comment");
1106+
1107+ // A pending review comment is hidden from everyone but its author until submitted.
10981108 let tahinli = fx.store.user_by_username(OWNER).await.unwrap().unwrap();
10991109 let head = pr.head_oid.clone();
11001110 let form = csrf_form(
@@ -1116,21 +1126,26 @@ mod tests {
11161126 Some(form),
11171127 )
11181128 .await;
1119- assert_eq!(status, 303);
1120- // Hidden from everyone but the author while pending.
1121- assert!(
1129+ assert_eq!(status, 303, "pending start");
1130+ assert_eq!(
11221131 fx.store
11231132 .review_threads(&pr.issue.id, None)
11241133 .await
11251134 .unwrap()
1126- .is_empty()
1135+ .len(),
1136+ 1,
1137+ "pending thread hidden from everyone"
11271138 );
11281139 let mine = fx
11291140 .store
11301141 .review_threads(&pr.issue.id, Some(&tahinli.id))
11311142 .await
11321143 .unwrap();
1133- assert!(mine[0].root.pending);
1144+ assert_eq!(mine.len(), 2, "author sees the pending thread");
1145+ assert!(
1146+ mine.iter().any(|t| t.root.pending),
1147+ "the author's draft is pending"
1148+ );
11341149
11351150 let form = csrf_form("tahinli", &[("state", "comment"), ("body", "Done")]);
11361151 let (status, _, _) = send(
@@ -1141,50 +1156,46 @@ mod tests {
11411156 Some(form),
11421157 )
11431158 .await;
1144- assert_eq!(status, 303);
1159+ assert_eq!(status, 303, "submit review");
11451160 let threads = fx.store.review_threads(&pr.issue.id, None).await.unwrap();
1146- assert_eq!(threads.len(), 1);
1147- assert!(!threads[0].root.pending);
1148- }
1149-
1150- #[tokio::test]
1151- async fn reader_approval_is_recorded_and_author_approval_is_refused() {
1152- let fx = Fixture::new(Visibility::Public).await;
1153- owner_session(&fx).await;
1154- sign_in(&fx, "bob", "bob").await;
1155- let number = open_pull(&fx).await;
1156- let pr = pull(&fx, number).await;
1157- let uri = format!("/tahinli/demo/pulls/{number}/reviews");
1161+ assert_eq!(threads.len(), 2, "submitted review is public");
1162+ assert!(threads.iter().all(|t| !t.root.pending), "submitted review");
11581163
1159- let form = csrf_form("bob", &[("state", "approve")]);
1160- let (status, location, _) = send(&fx.router, "POST", &uri, Some("bob"), Some(form)).await;
1161- assert_eq!(status, 303);
1162- assert!(location.unwrap().contains("#review-"));
1163- let reviews = fx.store.list_reviews(&pr.issue.id).await.unwrap();
1164- assert_eq!(reviews.len(), 1);
1165- assert_eq!(reviews[0].state, ReviewState::Approved);
1166-
1167- let form = csrf_form("tahinli", &[("state", "approve")]);
1168- let (status, location, _) =
1169- send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await;
1170- assert_eq!(status, 303);
1171- assert_eq!(
1172- location.as_deref(),
1173- Some(format!("/tahinli/demo/pulls/{number}?review_error=own_pr").as_str())
1164+ // Anonymous writes redirect to login, and a missing CSRF token is forbidden.
1165+ let uri = format!("/tahinli/demo/pulls/{number}/review_comments");
1166+ let form = csrf_form(
1167+ "tahinli",
1168+ &[
1169+ ("file_index", "0"),
1170+ ("side", "new"),
1171+ ("line", "2"),
1172+ ("head_oid", head.as_str()),
1173+ ("action", "single"),
1174+ ("body", "Anon"),
1175+ ],
11741176 );
1175- assert_eq!(fx.store.list_reviews(&pr.issue.id).await.unwrap().len(), 1);
1177+ let (status, location, _) = send(&fx.router, "POST", &uri, None, Some(form)).await;
1178+ assert_eq!(status, 303, "anonymous write");
1179+ assert!(location.unwrap().starts_with("/login"), "anonymous write");
1180+ let form =
1181+ format!("file_index=0&side=new&line=2&head_oid={head}&action=single&body=NoCsrf");
1182+ let (status, _, _) = send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await;
1183+ assert_eq!(status, 403, "missing csrf");
11761184 }
11771185
1186+ /// Reply, edit, resolve and delete on the threads of one pull. Each scenario acts on its
1187+ /// own thread (the first for reply, edit and resolve, the second for delete).
11781188 #[tokio::test]
1179- async fn reply_joins_the_thread_and_redirects_to_the_root() {
1189+ async fn thread_reply_edit_resolve_and_delete() {
11801190 let fx = Fixture::new(Visibility::Public).await;
11811191 owner_session(&fx).await;
11821192 sign_in(&fx, "bob", "bob").await;
11831193 let number = open_pull(&fx).await;
1184- comment_on(&fx, number, "tahinli", "2", "Root").await;
1185- let root = root_id(&fx, number).await;
1194+ comment_on(&fx, number, "tahinli", "2", "First").await;
11861195 let pr = pull(&fx, number).await;
1196+ let root = root_at(&fx, number, 0).await;
11871197
1198+ // A reader replies into the thread and is sent back to its root.
11881199 let form = csrf_form("bob", &[("body", "Reply")]);
11891200 let (status, location, _) = send(
11901201 &fx.router,
@@ -1194,96 +1205,125 @@ mod tests {
11941205 Some(form),
11951206 )
11961207 .await;
1197- assert_eq!(status, 303);
1208+ assert_eq!(status, 303, "reply");
11981209 assert_eq!(
11991210 location.as_deref(),
1200- Some(format!("/tahinli/demo/pulls/{number}#rc-{root}").as_str())
1211+ Some(format!("/tahinli/demo/pulls/{number}#rc-{root}").as_str()),
1212+ "reply"
12011213 );
12021214 let threads = fx.store.review_threads(&pr.issue.id, None).await.unwrap();
1203- assert_eq!(threads[0].replies.len(), 1);
1204- assert_eq!(threads[0].replies[0].author_name, "bob");
1205- }
1215+ assert_eq!(threads[0].replies.len(), 1, "reply");
1216+ assert_eq!(threads[0].replies[0].author_name, "bob", "reply");
12061217
1207- #[tokio::test]
1208- async fn edit_is_for_the_author_only() {
1209- let fx = Fixture::new(Visibility::Public).await;
1210- owner_session(&fx).await;
1211- sign_in(&fx, "bob", "bob").await;
1212- let number = open_pull(&fx).await;
1213- comment_on(&fx, number, "tahinli", "2", "First").await;
1214- let root = root_id(&fx, number).await;
1218+ // Only the author may open the edit form, post an edit, or resolve.
12151219 let edit = format!("/tahinli/demo/pulls/{number}/review_comments/{root}/edit");
1216-
12171220 let (status, _, body) = send(&fx.router, "GET", &edit, Some("tahinli"), None).await;
1218- assert_eq!(status, 200);
1219- assert!(body.contains("Save comment"));
1221+ assert_eq!(status, 200, "edit form");
1222+ assert!(body.contains("Save comment"), "edit form");
12201223 let (status, _, _) = send(&fx.router, "GET", &edit, Some("bob"), None).await;
1221- assert_eq!(status, 403);
1222-
1224+ assert_eq!(status, 403, "edit form for a non-author");
12231225 let form = csrf_form("bob", &[("body", "Hijacked")]);
12241226 let (status, _, _) = send(&fx.router, "POST", &edit, Some("bob"), Some(form)).await;
1225- assert_eq!(status, 403);
1226-
1227+ assert_eq!(status, 403, "edit by a non-author");
12271228 let form = csrf_form("tahinli", &[("body", "Edited")]);
12281229 let (status, location, _) =
12291230 send(&fx.router, "POST", &edit, Some("tahinli"), Some(form)).await;
1230- assert_eq!(status, 303);
1231+ assert_eq!(status, 303, "edit");
12311232 assert_eq!(
12321233 location.as_deref(),
1233- Some(format!("/tahinli/demo/pulls/{number}#rc-{root}").as_str())
1234+ Some(format!("/tahinli/demo/pulls/{number}#rc-{root}").as_str()),
1235+ "edit"
12341236 );
1235- let pr = pull(&fx, number).await;
12361237 let threads = fx.store.review_threads(&pr.issue.id, None).await.unwrap();
1237- assert_eq!(threads[0].root.body, "Edited");
1238- }
1238+ assert_eq!(threads[0].root.body, "Edited", "edit");
12391239
1240- #[tokio::test]
1241- async fn delete_removes_the_thread_for_its_author_and_refuses_others() {
1242- let fx = Fixture::new(Visibility::Public).await;
1243- owner_session(&fx).await;
1244- sign_in(&fx, "bob", "bob").await;
1245- let number = open_pull(&fx).await;
1246- comment_on(&fx, number, "tahinli", "2", "Doomed").await;
1247- let root = root_id(&fx, number).await;
1248- let pr = pull(&fx, number).await;
1249- let uri = format!("/tahinli/demo/pulls/{number}/review_comments/{root}/delete");
1240+ let resolve = format!("/tahinli/demo/pulls/{number}/review_comments/{root}/resolve");
1241+ let form = csrf_form("bob", &[("resolved", "1")]);
1242+ let (status, _, _) = send(&fx.router, "POST", &resolve, Some("bob"), Some(form)).await;
1243+ assert_eq!(status, 403, "resolve by a reader");
1244+ let form = csrf_form("tahinli", &[("resolved", "1")]);
1245+ let (status, _, _) = send(&fx.router, "POST", &resolve, Some("tahinli"), Some(form)).await;
1246+ assert_eq!(status, 303, "resolve");
1247+ let threads = fx.store.review_threads(&pr.issue.id, None).await.unwrap();
1248+ assert!(threads[0].root.resolved, "resolve");
12501249
1250+ // Delete removes only the second thread, and only for its author.
1251+ comment_on(&fx, number, "tahinli", "2", "Doomed").await;
1252+ let doomed = root_at(&fx, number, 1).await;
1253+ let uri = format!("/tahinli/demo/pulls/{number}/review_comments/{doomed}/delete");
12511254 let form = csrf_form("bob", &[]);
12521255 let (status, _, _) = send(&fx.router, "POST", &uri, Some("bob"), Some(form)).await;
1253- assert_eq!(status, 403);
1254-
1256+ assert_eq!(status, 403, "delete by a non-author");
12551257 let form = csrf_form("tahinli", &[]);
12561258 let (status, _, _) = send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await;
1257- assert_eq!(status, 303);
1258- assert!(
1259- fx.store
1260- .review_threads(&pr.issue.id, None)
1261- .await
1262- .unwrap()
1263- .is_empty()
1264- );
1259+ assert_eq!(status, 303, "delete");
1260+ let threads = fx.store.review_threads(&pr.issue.id, None).await.unwrap();
1261+ assert_eq!(threads.len(), 1, "delete");
1262+ assert_eq!(threads[0].root.id, root, "delete kept the other thread");
12651263 }
12661264
1265+ /// Reviews on one pull: a reader approves, the author cannot approve their own pull, and
1266+ /// after the pull closes it takes comments but no reviews.
12671267 #[tokio::test]
1268- async fn resolve_is_for_the_pr_author_or_write() {
1268+ async fn reviews_approve_then_close_the_pull() {
12691269 let fx = Fixture::new(Visibility::Public).await;
12701270 owner_session(&fx).await;
12711271 sign_in(&fx, "bob", "bob").await;
12721272 let number = open_pull(&fx).await;
1273- comment_on(&fx, number, "tahinli", "2", "Thread").await;
1274- let root = root_id(&fx, number).await;
12751273 let pr = pull(&fx, number).await;
1276- let uri = format!("/tahinli/demo/pulls/{number}/review_comments/{root}/resolve");
1274+ let uri = format!("/tahinli/demo/pulls/{number}/reviews");
12771275
1278- let form = csrf_form("bob", &[("resolved", "1")]);
1279- let (status, _, _) = send(&fx.router, "POST", &uri, Some("bob"), Some(form)).await;
1280- assert_eq!(status, 403);
1276+ // A reader's approval is recorded.
1277+ let form = csrf_form("bob", &[("state", "approve")]);
1278+ let (status, location, _) = send(&fx.router, "POST", &uri, Some("bob"), Some(form)).await;
1279+ assert_eq!(status, 303, "reader approval");
1280+ assert!(location.unwrap().contains("#review-"), "reader approval");
1281+ let reviews = fx.store.list_reviews(&pr.issue.id).await.unwrap();
1282+ assert_eq!(reviews.len(), 1, "reader approval");
1283+ assert_eq!(reviews[0].state, ReviewState::Approved, "reader approval");
12811284
1282- let form = csrf_form("tahinli", &[("resolved", "1")]);
1283- let (status, _, _) = send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await;
1284- assert_eq!(status, 303);
1285- let threads = fx.store.review_threads(&pr.issue.id, None).await.unwrap();
1286- assert!(threads[0].root.resolved);
1285+ // The author's approval is refused.
1286+ let form = csrf_form("tahinli", &[("state", "approve")]);
1287+ let (status, location, _) =
1288+ send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await;
1289+ assert_eq!(status, 303, "author approval");
1290+ assert_eq!(
1291+ location.as_deref(),
1292+ Some(format!("/tahinli/demo/pulls/{number}?review_error=own_pr").as_str()),
1293+ "author approval"
1294+ );
1295+ assert_eq!(
1296+ fx.store.list_reviews(&pr.issue.id).await.unwrap().len(),
1297+ 1,
1298+ "author approval"
1299+ );
1300+
1301+ // Once closed, comments still land but reviews are refused.
1302+ let tahinli = fx.store.user_by_username(OWNER).await.unwrap().unwrap();
1303+ fx.store
1304+ .set_issue_state(
1305+ &pr.issue.id,
1306+ Some(&tahinli.id),
1307+ IssueState::Closed,
1308+ None,
1309+ serde_json::Value::Null,
1310+ )
1311+ .await
1312+ .unwrap();
1313+ let (status, location) = comment_on(&fx, number, "tahinli", "2", "After close").await;
1314+ assert_eq!(status, 303, "comment on a closed pull");
1315+ assert!(
1316+ location.unwrap().contains("#L-0-new-2"),
1317+ "comment on a closed pull"
1318+ );
1319+ let form = csrf_form("tahinli", &[("state", "approve")]);
1320+ let (status, location, _) =
1321+ send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await;
1322+ assert_eq!(status, 303, "review on a closed pull");
1323+ assert!(
1324+ location.unwrap().ends_with("?review_error=closed"),
1325+ "review on a closed pull"
1326+ );
12871327 }
12881328
12891329 #[tokio::test]
@@ -1307,155 +1347,52 @@ mod tests {
13071347
13081348 let form = csrf_form("bob", &[("reason", "mine")]);
13091349 let (status, _, _) = send(&fx.router, "POST", &uri, Some("bob"), Some(form)).await;
1310- assert_eq!(status, 403);
1350+ assert_eq!(status, 403, "dismiss by a reader");
13111351
13121352 let form = csrf_form("tahinli", &[("reason", " ")]);
13131353 let (status, location, _) =
13141354 send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await;
1315- assert_eq!(status, 303);
1316- assert!(location.unwrap().ends_with("?review_error=empty"));
1355+ assert_eq!(status, 303, "blank reason");
1356+ assert!(
1357+ location.unwrap().ends_with("?review_error=empty"),
1358+ "blank reason"
1359+ );
13171360
13181361 let form = csrf_form("tahinli", &[("reason", "Addressed")]);
13191362 let (status, _, _) = send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await;
1320- assert_eq!(status, 303);
1363+ assert_eq!(status, 303, "dismiss");
13211364 let review = fx.store.list_reviews(&pr.issue.id).await.unwrap().remove(0);
1322- assert_eq!(review.state, ReviewState::Dismissed);
1323- }
1324-
1325- #[tokio::test]
1326- async fn closed_pull_takes_comments_but_not_reviews() {
1327- let fx = Fixture::new(Visibility::Public).await;
1328- owner_session(&fx).await;
1329- let number = open_pull(&fx).await;
1330- let pr = pull(&fx, number).await;
1331- let tahinli = fx.store.user_by_username(OWNER).await.unwrap().unwrap();
1332- fx.store
1333- .set_issue_state(
1334- &pr.issue.id,
1335- Some(&tahinli.id),
1336- IssueState::Closed,
1337- None,
1338- serde_json::Value::Null,
1339- )
1340- .await
1341- .unwrap();
1342-
1343- let (status, location) = comment_on(&fx, number, "tahinli", "2", "After close").await;
1344- assert_eq!(status, 303);
1345- assert!(location.unwrap().contains("#L-0-new-2"));
1346-
1347- let form = csrf_form("tahinli", &[("state", "approve")]);
1348- let (status, location, _) = send(
1349- &fx.router,
1350- "POST",
1351- &format!("/tahinli/demo/pulls/{number}/reviews"),
1352- Some("tahinli"),
1353- Some(form),
1354- )
1355- .await;
1356- assert_eq!(status, 303);
1357- assert!(location.unwrap().ends_with("?review_error=closed"));
1358- }
1359-
1360- #[tokio::test]
1361- async fn anonymous_writes_redirect_to_login_and_missing_csrf_is_forbidden() {
1362- let fx = Fixture::new(Visibility::Public).await;
1363- owner_session(&fx).await;
1364- let number = open_pull(&fx).await;
1365- let head = head_of(&fx, number).await;
1366- let uri = format!("/tahinli/demo/pulls/{number}/review_comments");
1367- let form = csrf_form(
1368- "tahinli",
1369- &[
1370- ("file_index", "0"),
1371- ("side", "new"),
1372- ("line", "2"),
1373- ("head_oid", head.as_str()),
1374- ("action", "single"),
1375- ("body", "Anon"),
1376- ],
1377- );
1378- let (status, location, _) = send(&fx.router, "POST", &uri, None, Some(form)).await;
1379- assert_eq!(status, 303);
1380- assert!(location.unwrap().starts_with("/login"));
1381-
1382- let form =
1383- format!("file_index=0&side=new&line=2&head_oid={head}&action=single&body=NoCsrf");
1384- let (status, _, _) = send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await;
1385- assert_eq!(status, 403);
1386- }
1387-
1388- /// The comment body that proposes replacing line 2 of `a.txt` with `text`.
1389- fn suggestion(text: &str) -> String {
1390- format!("Try this:\n```suggestion\n{text}\n```")
1391- }
1392-
1393- /// Posts the apply form for suggestion block 0 of comment `comment_id`.
1394- async fn apply_as(
1395- fx: &Fixture,
1396- number: i64,
1397- token: &str,
1398- comment_id: &str,
1399- ) -> (u16, Option<String>) {
1400- let form = csrf_form(token, &[("block", "0"), ("return", "files")]);
1401- let uri = format!("/tahinli/demo/pulls/{number}/review_comments/{comment_id}/apply");
1402- let (status, location, _) = send(&fx.router, "POST", &uri, Some(token), Some(form)).await;
1403- (status, location)
1365+ assert_eq!(review.state, ReviewState::Dismissed, "dismiss");
14041366 }
14051367
1368+ /// The apply-suggestion flow on one pull: a reader without push access sees no button, an
1369+ /// outdated suggestion is refused after the head moves, and a fresh suggestion applies.
14061370 #[tokio::test]
1407- async fn applying_a_suggestion_commits_it_to_the_head_and_resolves_the_thread() {
1371+ async fn suggestions_reader_outdated_and_apply() {
14081372 let fx = Fixture::new(Visibility::Public).await;
14091373 owner_session(&fx).await;
1374+ sign_in(&fx, "rita", "rita").await;
14101375 let number = open_pull(&fx).await;
1411- let before = pull(&fx, number).await.head_oid;
1412-
14131376 let (status, _) = comment_on(&fx, number, "tahinli", "2", &suggestion("two!")).await;
1414- assert_eq!(status, 303);
1415- let root = root_id(&fx, number).await;
1416- let (status, location) = apply_as(&fx, number, "tahinli", &root).await;
1417- assert_eq!(status, 303);
1418- assert!(!location.unwrap().contains("review_error"));
1377+ assert_eq!(status, 303, "first suggestion");
1378+ let first = root_at(&fx, number, 0).await;
14191379
1420- let pr = pull(&fx, number).await;
1421- assert_ne!(
1422- pr.head_oid, before,
1423- "the head moved to the suggestion commit"
1424- );
1425- assert_eq!(
1426- git(&fx.bare, &["show", &format!("{}:a.txt", pr.head_oid)]),
1427- "one\ntwo!\nthree"
1428- );
1429- assert_eq!(
1430- git(&fx.bare, &["log", "-1", "--format=%an", &pr.head_oid]),
1431- "tahinli"
1432- );
1433- let message = git(&fx.bare, &["log", "-1", "--format=%B", &pr.head_oid]);
1380+ // A reader without push access sees the diff but no apply button, and is refused.
1381+ let page = get(&fx, number, "rita").await;
1382+ assert!(page.contains("Suggested change"), "the diff still shows");
14341383 assert!(
1435- message.starts_with("Apply suggestion from review"),
1436- "{message}"
1384+ !page.contains("Apply suggestion"),
1385+ "no apply button without push access"
14371386 );
1387+ let (status, location) = apply_as(&fx, number, "rita", &first).await;
1388+ assert_eq!(status, 303, "reader apply");
14381389 assert!(
1439- message.contains("Co-authored-by: tahinli <"),
1440- "commenter trailer: {message}"
1390+ location.unwrap().contains("review_error=no_push"),
1391+ "reader apply"
14411392 );
1442- assert_eq!(git(&fx.bare, &["rev-parse", "feature"]), pr.head_oid);
1443- let threads = fx.store.review_threads(&pr.issue.id, None).await.unwrap();
1444- assert!(threads[0].root.resolved, "applying resolves the thread");
1445- }
14461393
1447- #[tokio::test]
1448- async fn outdated_suggestion_is_shown_without_apply_and_refused() {
1449- let fx = Fixture::new(Visibility::Public).await;
1450- owner_session(&fx).await;
1451- let number = open_pull(&fx).await;
1452- let (status, _) = comment_on(&fx, number, "tahinli", "2", &suggestion("two!")).await;
1453- assert_eq!(status, 303);
1454- let root = root_id(&fx, number).await;
1455-
1456- // The head moves on: its line 2 no longer reads what the suggestion was written against.
1394+ // The head moves on: the first suggestion is outdated, shown without apply, and refused.
14571395 let pr = pull(&fx, number).await;
1458- git(&fx.work, &["checkout", "-q", "feature"]);
14591396 std::fs::write(fx.work.join("a.txt"), "one\nmoved\nthree\n").unwrap();
14601397 git(&fx.work, &["commit", "-q", "-am", "Move line 2"]);
14611398 let moved = git(&fx.work, &["rev-parse", "HEAD"]);
@@ -1482,10 +1419,12 @@ mod tests {
14821419 !page.contains("Apply suggestion"),
14831420 "no apply button when outdated"
14841421 );
1485-
1486- let (status, location) = apply_as(&fx, number, "tahinli", &root).await;
1487- assert_eq!(status, 303);
1488- assert!(location.unwrap().contains("review_error=outdated"));
1422+ let (status, location) = apply_as(&fx, number, "tahinli", &first).await;
1423+ assert_eq!(status, 303, "outdated apply");
1424+ assert!(
1425+ location.unwrap().contains("review_error=outdated"),
1426+ "outdated apply"
1427+ );
14891428 assert_eq!(
14901429 git(&fx.bare, &["rev-parse", "feature"]),
14911430 moved,
@@ -1496,28 +1435,48 @@ mod tests {
14961435 !threads[0].root.resolved,
14971436 "a refused apply leaves the thread open"
14981437 );
1499- }
15001438
1501- #[tokio::test]
1502- async fn a_reader_without_push_access_cannot_apply_and_sees_no_button() {
1503- let fx = Fixture::new(Visibility::Public).await;
1504- owner_session(&fx).await;
1505- let number = open_pull(&fx).await;
1439+ // A fresh suggestion against the moved head applies as a commit by the commenter.
1440+ let before = pull(&fx, number).await.head_oid;
1441+ assert_eq!(before, moved, "the head is the moved commit");
15061442 let (status, _) = comment_on(&fx, number, "tahinli", "2", &suggestion("two!")).await;
1507- assert_eq!(status, 303);
1508- let root = root_id(&fx, number).await;
1509- sign_in(&fx, "rita", "rita").await;
1443+ assert_eq!(status, 303, "fresh suggestion");
1444+ let second = root_at(&fx, number, 1).await;
1445+ let (status, location) = apply_as(&fx, number, "tahinli", &second).await;
1446+ assert_eq!(status, 303, "apply");
1447+ assert!(!location.unwrap().contains("review_error"), "apply");
15101448
1511- let page = get(&fx, number, "rita").await;
1512- assert!(page.contains("Suggested change"), "the diff still shows");
1449+ let pr = pull(&fx, number).await;
1450+ assert_ne!(
1451+ pr.head_oid, before,
1452+ "the head moved to the suggestion commit"
1453+ );
1454+ assert_eq!(
1455+ git(&fx.bare, &["show", &format!("{}:a.txt", pr.head_oid)]),
1456+ "one\ntwo!\nthree",
1457+ "apply"
1458+ );
1459+ assert_eq!(
1460+ git(&fx.bare, &["log", "-1", "--format=%an", &pr.head_oid]),
1461+ "tahinli",
1462+ "apply"
1463+ );
1464+ let message = git(&fx.bare, &["log", "-1", "--format=%B", &pr.head_oid]);
15131465 assert!(
1514- !page.contains("Apply suggestion"),
1515- "no apply button without push access"
1466+ message.starts_with("Apply suggestion from review"),
1467+ "{message}"
15161468 );
1517-
1518- let (status, location) = apply_as(&fx, number, "rita", &root).await;
1519- assert_eq!(status, 303);
1520- assert!(location.unwrap().contains("review_error=no_push"));
1469+ assert!(
1470+ message.contains("Co-authored-by: tahinli <"),
1471+ "commenter trailer: {message}"
1472+ );
1473+ assert_eq!(
1474+ git(&fx.bare, &["rev-parse", "feature"]),
1475+ pr.head_oid,
1476+ "apply"
1477+ );
1478+ let threads = fx.store.review_threads(&pr.issue.id, None).await.unwrap();
1479+ assert!(threads[1].root.resolved, "applying resolves the thread");
15211480 }
15221481
15231482 /// The Files tab of the pull request as `token` sees it.
crates/forge-web/src/pages/pull_view.rsmodified+328 -388
@@ -1206,12 +1206,18 @@ mod tests {
12061206 pr.issue.number
12071207 }
12081208
1209+ /// Conversation tab, merge box, merge and review error codes, and the read-only tabs, on one
1210+ /// open pull. Mergeability is set by each scenario right before it is checked.
12091211 #[tokio::test]
1210- async fn turkish_viewer_gets_turkish_pull_tabs() {
1212+ async fn conversation_merge_box_and_error_codes_on_one_pull() {
12111213 let fx = Fixture::new(Visibility::Public).await;
12121214 owner_session(&fx).await;
1215+ sign_in(&fx, "bob", "bob-token").await;
12131216 let number = open_pull(&fx).await;
1217+ let pr = pull(&fx, number).await;
12141218 let uri = format!("/tahinli/demo/pulls/{number}");
1219+
1220+ // Turkish viewers get the translated tabs.
12151221 let req = Request::builder()
12161222 .method("GET")
12171223 .uri(uri.as_str())
@@ -1221,100 +1227,12 @@ mod tests {
12211227 let res = fx.router.handle(req).await;
12221228 let bytes = to_bytes(res.into_body(), usize::MAX).await.unwrap();
12231229 let body = String::from_utf8_lossy(&bytes).into_owned();
1224- assert!(body.contains("Tartışma</a>"), "Turkish conversation tab");
1225- assert!(body.contains("Commitler "), "Turkish commits tab");
1226- assert!(body.contains("Değişen dosyalar "), "Turkish files tab");
1230+ assert!(body.contains("Tartışma</a>"), "turkish conversation tab");
1231+ assert!(body.contains("Commitler "), "turkish commits tab");
1232+ assert!(body.contains("Değişen dosyalar "), "turkish files tab");
12271233 assert!(!body.contains(">Conversation<"), "no English tab label");
1228- }
1229-
1230- #[tokio::test]
1231- async fn conversation_renders_title_badge_and_merge_box() {
1232- let fx = Fixture::new(Visibility::Public).await;
1233- owner_session(&fx).await;
1234- let number = open_pull(&fx).await;
1235- let pr = pull(&fx, number).await;
1236- fx.store
1237- .set_mergeability(
1238- &pr.issue.id,
1239- &pr.base_oid,
1240- Some(&fx.root),
1241- Mergeable::Clean,
1242- &[],
1243- )
1244- .await
1245- .unwrap();
1246-
1247- let uri = format!("/tahinli/demo/pulls/{number}");
1248- let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await;
1249- assert_eq!(status, 200);
1250- assert!(body.contains("Add feature"));
1251- assert!(body.contains("issue-badge issue-badge-open"));
1252- assert!(body.contains("This branch has no conflicts with the base branch"));
1253- assert!(body.contains(&format!("action=\"{uri}/merge\"")));
1254- assert!(body.contains("name=\"style\""));
1255- assert!(body.contains(&format!("action=\"{uri}/comments\"")));
1256- assert!(body.contains("Commits 1"));
1257- assert!(body.contains("Files changed 1"));
1258- }
1259-
1260- #[tokio::test]
1261- async fn merge_error_code_shows_its_message_and_unknown_code_shows_nothing() {
1262- let fx = Fixture::new(Visibility::Public).await;
1263- owner_session(&fx).await;
1264- let number = open_pull(&fx).await;
1265- let uri = format!("/tahinli/demo/pulls/{number}");
1266-
1267- let (status, _, body) = send(
1268- &fx.router,
1269- "GET",
1270- &format!("{uri}?merge_error=conflict"),
1271- Some("tahinli"),
1272- None,
1273- )
1274- .await;
1275- assert_eq!(status, 200);
1276- assert!(body.contains("class=\"alert merge-error\""), "{body}");
1277- assert!(body.contains("This branch has conflicts that must be resolved"));
1278-
1279- let (status, _, body) = send(
1280- &fx.router,
1281- "GET",
1282- &format!("{uri}?merge_error=%3Cscript%3Ealert(1)%3C%2Fscript%3E"),
1283- Some("tahinli"),
1284- None,
1285- )
1286- .await;
1287- assert_eq!(status, 200);
1288- assert!(!body.contains("merge-error"), "{body}");
1289- assert!(!page_main(&body).contains("alert(1)"), "{body}");
1290- }
12911234
1292- #[tokio::test]
1293- async fn commits_tab_lists_the_commit() {
1294- let fx = Fixture::new(Visibility::Public).await;
1295- let number = open_pull(&fx).await;
1296- let uri = format!("/tahinli/demo/pulls/{number}/commits");
1297- let (status, _, body) = send(&fx.router, "GET", &uri, None, None).await;
1298- assert_eq!(status, 200);
1299- assert!(body.contains("Add feature file"));
1300- }
1301-
1302- #[tokio::test]
1303- async fn files_tab_shows_the_changed_file() {
1304- let fx = Fixture::new(Visibility::Public).await;
1305- let number = open_pull(&fx).await;
1306- let uri = format!("/tahinli/demo/pulls/{number}/files");
1307- let (status, _, body) = send(&fx.router, "GET", &uri, None, None).await;
1308- assert_eq!(status, 200);
1309- assert!(body.contains("feat.txt"));
1310- }
1311-
1312- #[tokio::test]
1313- async fn conflict_lists_files_and_offers_no_merge() {
1314- let fx = Fixture::new(Visibility::Public).await;
1315- owner_session(&fx).await;
1316- let number = open_pull(&fx).await;
1317- let pr = pull(&fx, number).await;
1235+ // A conflict lists the files and offers no merge.
13181236 fx.store
13191237 .set_mergeability(
13201238 &pr.issue.id,
@@ -1325,22 +1243,20 @@ mod tests {
13251243 )
13261244 .await
13271245 .unwrap();
1328-
1329- let uri = format!("/tahinli/demo/pulls/{number}");
13301246 let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await;
1331- assert_eq!(status, 200);
1332- assert!(body.contains("This branch has conflicts that must be resolved"));
1333- assert!(body.contains("<code>feat.txt</code>"));
1334- assert!(!body.contains(&format!("action=\"{uri}/merge\"")));
1335- assert!(!body.contains("name=\"style\""));
1336- }
1247+ assert_eq!(status, 200, "conflict");
1248+ assert!(
1249+ body.contains("This branch has conflicts that must be resolved"),
1250+ "conflict: {body}"
1251+ );
1252+ assert!(body.contains("<code>feat.txt</code>"), "conflict: {body}");
1253+ assert!(
1254+ !body.contains(&format!("action=\"{uri}/merge\"")),
1255+ "conflict: {body}"
1256+ );
1257+ assert!(!body.contains("name=\"style\""), "conflict: {body}");
13371258
1338- #[tokio::test]
1339- async fn read_only_viewer_sees_no_merge_form() {
1340- let fx = Fixture::new(Visibility::Public).await;
1341- sign_in(&fx, "bob", "bob").await;
1342- let number = open_pull(&fx).await;
1343- let pr = pull(&fx, number).await;
1259+ // A clean branch shows the title, badge, merge box and conversation form.
13441260 fx.store
13451261 .set_mergeability(
13461262 &pr.issue.id,
@@ -1351,167 +1267,142 @@ mod tests {
13511267 )
13521268 .await
13531269 .unwrap();
1270+ let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await;
1271+ assert_eq!(status, 200, "clean");
1272+ assert!(body.contains("Add feature"), "clean: {body}");
1273+ assert!(
1274+ body.contains("issue-badge issue-badge-open"),
1275+ "clean: {body}"
1276+ );
1277+ assert!(
1278+ body.contains("This branch has no conflicts with the base branch"),
1279+ "clean: {body}"
1280+ );
1281+ assert!(
1282+ body.contains(&format!("action=\"{uri}/merge\"")),
1283+ "clean: {body}"
1284+ );
1285+ assert!(body.contains("name=\"style\""), "clean: {body}");
1286+ assert!(
1287+ body.contains(&format!("action=\"{uri}/comments\"")),
1288+ "clean: {body}"
1289+ );
1290+ assert!(body.contains("Commits 1"), "clean: {body}");
1291+ assert!(body.contains("Files changed 1"), "clean: {body}");
13541292
1355- let uri = format!("/tahinli/demo/pulls/{number}");
1356- let (status, _, body) = send(&fx.router, "GET", &uri, Some("bob"), None).await;
1357- assert_eq!(status, 200);
1358- assert!(body.contains("This branch has no conflicts with the base branch"));
1359- assert!(!body.contains("name=\"style\""));
1360- assert!(!body.contains("Merge pull request"));
1361- }
1293+ // A reader sees the clean status but no merge form.
1294+ let (status, _, body) = send(&fx.router, "GET", &uri, Some("bob-token"), None).await;
1295+ assert_eq!(status, 200, "reader");
1296+ assert!(
1297+ body.contains("This branch has no conflicts with the base branch"),
1298+ "reader: {body}"
1299+ );
1300+ assert!(!body.contains("name=\"style\""), "reader: {body}");
1301+ assert!(!body.contains("Merge pull request"), "reader: {body}");
13621302
1363- #[tokio::test]
1364- async fn issue_url_for_a_pull_redirects_to_the_pull() {
1365- let fx = Fixture::new(Visibility::Public).await;
1366- let number = open_pull(&fx).await;
1367- let (status, location, _) = send(
1303+ // Known merge error codes show their message, unknown ones show nothing.
1304+ let (status, _, body) = send(
13681305 &fx.router,
13691306 "GET",
1370- &format!("/tahinli/demo/issues/{number}"),
1371- None,
1307+ &format!("{uri}?merge_error=conflict"),
1308+ Some("tahinli"),
13721309 None,
13731310 )
13741311 .await;
1375- assert_eq!(status, 303);
1376- assert_eq!(
1377- location.as_deref(),
1378- Some(format!("/tahinli/demo/pulls/{number}").as_str())
1312+ assert_eq!(status, 200, "merge error");
1313+ assert!(
1314+ body.contains("class=\"alert merge-error\""),
1315+ "merge error: {body}"
13791316 );
1380- }
1381-
1382- #[tokio::test]
1383- async fn comment_on_a_pull_is_saved() {
1384- let fx = Fixture::new(Visibility::Public).await;
1385- owner_session(&fx).await;
1386- let number = open_pull(&fx).await;
1387- let uri = format!("/tahinli/demo/pulls/{number}/comments");
1388- let form = csrf_form("tahinli", &[("body", "Looks good to me")]);
1389- let (status, location, _) =
1390- send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await;
1391- assert_eq!(status, 303);
13921317 assert!(
1393- location
1394- .unwrap()
1395- .starts_with(&format!("/tahinli/demo/pulls/{number}#comment-"))
1318+ body.contains("This branch has conflicts that must be resolved"),
1319+ "merge error: {body}"
13961320 );
1397-
1398- let pr = pull(&fx, number).await;
1399- let comments = fx.store.list_comments(&pr.issue.id).await.unwrap();
1400- assert_eq!(comments.len(), 1);
1401- assert_eq!(comments[0].body, "Looks good to me");
1402- }
1403-
1404- #[tokio::test]
1405- async fn state_route_closes_and_reopens_a_pull() {
1406- let fx = Fixture::new(Visibility::Public).await;
1407- owner_session(&fx).await;
1408- let number = open_pull(&fx).await;
1409- let uri = format!("/tahinli/demo/pulls/{number}/state");
1410-
1411- let form = csrf_form("tahinli", &[("action", "close_not_planned")]);
1412- let (status, _, _) = send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await;
1413- assert_eq!(status, 303);
1414- let pr = pull(&fx, number).await;
1415- assert!(matches!(pr.issue.state, IssueState::Closed));
1416-
1417- let form = csrf_form("tahinli", &[("action", "reopen")]);
1418- let (status, _, _) = send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await;
1419- assert_eq!(status, 303);
1420- let pr = pull(&fx, number).await;
1421- assert!(matches!(pr.issue.state, IssueState::Open));
1422- }
1423-
1424- #[tokio::test]
1425- async fn author_edits_pull_title_and_body() {
1426- let fx = Fixture::new(Visibility::Public).await;
1427- owner_session(&fx).await;
1428- let number = open_pull(&fx).await;
1429- let uri = format!("/tahinli/demo/pulls/{number}");
1430-
1431- let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await;
1432- assert_eq!(status, 200);
1433- assert!(body.contains(&format!("href=\"{uri}/edit\"")));
1434-
14351321 let (status, _, body) = send(
14361322 &fx.router,
14371323 "GET",
1438- &format!("{uri}/edit"),
1324+ &format!("{uri}?merge_error=%3Cscript%3Ealert(1)%3C%2Fscript%3E"),
14391325 Some("tahinli"),
14401326 None,
14411327 )
14421328 .await;
1443- assert_eq!(status, 200);
1444- assert!(body.contains("Edit pull request"));
1445-
1446- let form = csrf_form(
1447- "tahinli",
1448- &[("title", "Renamed pull"), ("body", "New text")],
1329+ assert_eq!(status, 200, "unknown merge error");
1330+ assert!(!body.contains("merge-error"), "unknown merge error: {body}");
1331+ assert!(
1332+ !page_main(&body).contains("alert(1)"),
1333+ "unknown merge error: {body}"
14491334 );
1450- let (status, location, _) = send(
1335+
1336+ // Known review error codes show their message, unknown ones show nothing.
1337+ let (status, _, body) = send(
14511338 &fx.router,
1452- "POST",
1453- &format!("{uri}/edit"),
1339+ "GET",
1340+ &format!("{uri}?review_error=own_pr"),
14541341 Some("tahinli"),
1455- Some(form),
1342+ None,
14561343 )
14571344 .await;
1458- assert_eq!(status, 303);
1459- assert_eq!(location.as_deref(), Some(uri.as_str()));
1460-
1461- let pr = pull(&fx, number).await;
1462- assert_eq!(pr.issue.title, "Renamed pull");
1463- assert_eq!(pr.issue.body, "New text");
1464- let events = fx.store.list_events(&pr.issue.id).await.unwrap();
1465- assert!(events.iter().any(|e| e.kind == "renamed"));
1466- }
1467-
1468- #[tokio::test]
1469- async fn non_author_reader_cannot_edit_a_pull() {
1470- let fx = Fixture::new(Visibility::Public).await;
1471- owner_session(&fx).await;
1472- sign_in(&fx, "bob", "bob-token").await;
1473- let number = open_pull(&fx).await;
1474- let uri = format!("/tahinli/demo/pulls/{number}");
1475-
1476- let (_, _, body) = send(&fx.router, "GET", &uri, Some("bob-token"), None).await;
1477- assert!(!body.contains(&format!("href=\"{uri}/edit\"")));
1478-
1479- let (status, _, _) = send(
1345+ assert_eq!(status, 200, "review error");
1346+ assert!(
1347+ body.contains("You cannot approve or request changes on your own pull request."),
1348+ "review error: {body}"
1349+ );
1350+ let (_, _, body) = send(
14801351 &fx.router,
14811352 "GET",
1482- &format!("{uri}/edit"),
1483- Some("bob-token"),
1353+ &format!("{uri}?review_error=%3Cscript%3E"),
1354+ Some("tahinli"),
14841355 None,
14851356 )
14861357 .await;
1487- assert_eq!(status, 403);
1488- let form = csrf_form("bob-token", &[("title", "Hijacked"), ("body", "x")]);
1489- let (status, _, _) = send(
1358+ assert!(!body.contains("<script>"), "unknown review error: {body}");
1359+ assert!(
1360+ !body.contains("You cannot approve"),
1361+ "unknown review error: {body}"
1362+ );
1363+
1364+ // The issue URL of a pull redirects to the pull.
1365+ let (status, location, _) = send(
14901366 &fx.router,
1491- "POST",
1492- &format!("{uri}/edit"),
1493- Some("bob-token"),
1494- Some(form),
1367+ "GET",
1368+ &format!("/tahinli/demo/issues/{number}"),
1369+ None,
1370+ None,
14951371 )
14961372 .await;
1497- assert_eq!(status, 403);
1498- assert_eq!(pull(&fx, number).await.issue.title, "Add feature");
1373+ assert_eq!(status, 303, "issue url");
1374+ assert_eq!(location.as_deref(), Some(uri.as_str()), "issue url");
1375+
1376+ // The commits and files tabs list the change, for anonymous viewers too.
1377+ let (status, _, body) =
1378+ send(&fx.router, "GET", &format!("{uri}/commits"), None, None).await;
1379+ assert_eq!(status, 200, "commits tab");
1380+ assert!(body.contains("Add feature file"), "commits tab: {body}");
1381+ let (status, _, body) = send(&fx.router, "GET", &format!("{uri}/files"), None, None).await;
1382+ assert_eq!(status, 200, "files tab");
1383+ assert!(body.contains("feat.txt"), "files tab: {body}");
14991384 }
15001385
1386+ /// Pull comments, state changes and title edits on one pull. The order matters: the comment
1387+ /// count is checked while the pull has no other comments, the title is checked before the
1388+ /// rename, and the state is reopened before the end.
15011389 #[tokio::test]
1502- async fn author_edits_and_deletes_a_pull_comment() {
1390+ async fn comments_state_and_title_edits_on_one_pull() {
15031391 let fx = Fixture::new(Visibility::Public).await;
15041392 owner_session(&fx).await;
1393+ sign_in(&fx, "bob", "bob-token").await;
15051394 let number = open_pull(&fx).await;
1395+ let uri = format!("/tahinli/demo/pulls/{number}");
15061396 let pr = pull(&fx, number).await;
15071397 let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap();
1398+
1399+ // The author edits and deletes their own pull comment, and the count returns to zero.
15081400 let comment = fx
15091401 .store
15101402 .add_comment(&pr.issue.id, &owner.id, "first draft")
15111403 .await
15121404 .unwrap();
1513- let base = format!("/tahinli/demo/pulls/{number}/comments/{}", comment.id);
1514-
1405+ let base = format!("{uri}/comments/{}", comment.id);
15151406 let (status, _, _) = send(
15161407 &fx.router,
15171408 "GET",
@@ -1520,7 +1411,7 @@ mod tests {
15201411 None,
15211412 )
15221413 .await;
1523- assert_eq!(status, 200);
1414+ assert_eq!(status, 200, "comment edit form");
15241415 let form = csrf_form("tahinli", &[("body", "edited text")]);
15251416 let (status, location, _) = send(
15261417 &fx.router,
@@ -1530,13 +1421,14 @@ mod tests {
15301421 Some(form),
15311422 )
15321423 .await;
1533- assert_eq!(status, 303);
1424+ assert_eq!(status, 303, "comment edit");
15341425 assert_eq!(
15351426 location,
15361427 Some(format!(
15371428 "/tahinli/demo/pulls/{number}#comment-{}",
15381429 comment.id
1539- ))
1430+ )),
1431+ "comment edit"
15401432 );
15411433 let saved = fx
15421434 .store
@@ -1544,9 +1436,8 @@ mod tests {
15441436 .await
15451437 .unwrap()
15461438 .unwrap();
1547- assert_eq!(saved.body, "edited text");
1548- assert!(saved.edited);
1549-
1439+ assert_eq!(saved.body, "edited text", "comment edit");
1440+ assert!(saved.edited, "comment edit");
15501441 let (status, location, _) = send(
15511442 &fx.router,
15521443 "POST",
@@ -1555,31 +1446,53 @@ mod tests {
15551446 Some(csrf_form("tahinli", &[])),
15561447 )
15571448 .await;
1558- assert_eq!(status, 303);
1559- assert_eq!(location, Some(format!("/tahinli/demo/pulls/{number}")));
1449+ assert_eq!(status, 303, "comment delete");
1450+ assert_eq!(
1451+ location,
1452+ Some(format!("/tahinli/demo/pulls/{number}")),
1453+ "comment delete"
1454+ );
15601455 assert!(
15611456 fx.store
15621457 .comment_by_id(&pr.issue.id, &comment.id)
15631458 .await
15641459 .unwrap()
1565- .is_none()
1460+ .is_none(),
1461+ "comment delete"
1462+ );
1463+ assert_eq!(
1464+ pull(&fx, number).await.issue.comment_count,
1465+ 0,
1466+ "comment delete"
15661467 );
1567- assert_eq!(pull(&fx, number).await.issue.comment_count, 0);
1568- }
15691468
1570- #[tokio::test]
1571- async fn issue_routes_do_not_accept_a_pull_number() {
1572- let fx = Fixture::new(Visibility::Public).await;
1573- owner_session(&fx).await;
1574- let number = open_pull(&fx).await;
1575- let pr = pull(&fx, number).await;
1576- let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap();
1577- let comment = fx
1469+ // Posting a comment saves it.
1470+ let form = csrf_form("tahinli", &[("body", "Looks good to me")]);
1471+ let (status, location, _) = send(
1472+ &fx.router,
1473+ "POST",
1474+ &format!("{uri}/comments"),
1475+ Some("tahinli"),
1476+ Some(form),
1477+ )
1478+ .await;
1479+ assert_eq!(status, 303, "post comment");
1480+ assert!(
1481+ location
1482+ .unwrap()
1483+ .starts_with(&format!("/tahinli/demo/pulls/{number}#comment-")),
1484+ "post comment"
1485+ );
1486+ let comments = fx.store.list_comments(&pr.issue.id).await.unwrap();
1487+ assert_eq!(comments.len(), 1, "post comment");
1488+ assert_eq!(comments[0].body, "Looks good to me", "post comment");
1489+
1490+ // The issue routes do not accept a pull number.
1491+ let keep = fx
15781492 .store
15791493 .add_comment(&pr.issue.id, &owner.id, "keep me")
15801494 .await
15811495 .unwrap();
1582-
15831496 let (status, _, _) = send(
15841497 &fx.router,
15851498 "GET",
@@ -1588,7 +1501,7 @@ mod tests {
15881501 None,
15891502 )
15901503 .await;
1591- assert_eq!(status, 404);
1504+ assert_eq!(status, 404, "issue edit route");
15921505 let form = csrf_form("tahinli", &[("title", "Nope"), ("body", "x")]);
15931506 let (status, _, _) = send(
15941507 &fx.router,
@@ -1598,40 +1511,130 @@ mod tests {
15981511 Some(form),
15991512 )
16001513 .await;
1601- assert_eq!(status, 404);
1602- let base = format!("/tahinli/demo/issues/{number}/comments/{}", comment.id);
1514+ assert_eq!(status, 404, "issue edit post");
1515+ let issue_base = format!("/tahinli/demo/issues/{number}/comments/{}", keep.id);
16031516 let (status, _, _) = send(
16041517 &fx.router,
16051518 "GET",
1606- &format!("{base}/edit"),
1519+ &format!("{issue_base}/edit"),
16071520 Some("tahinli"),
16081521 None,
16091522 )
16101523 .await;
1611- assert_eq!(status, 404);
1524+ assert_eq!(status, 404, "issue comment edit");
16121525 let (status, _, _) = send(
16131526 &fx.router,
16141527 "POST",
1615- &format!("{base}/delete"),
1528+ &format!("{issue_base}/delete"),
16161529 Some("tahinli"),
16171530 Some(csrf_form("tahinli", &[])),
16181531 )
16191532 .await;
1620- assert_eq!(status, 404);
1621-
1533+ assert_eq!(status, 404, "issue comment delete");
16221534 let pr = pull(&fx, number).await;
1623- assert_eq!(pr.issue.title, "Add feature");
1535+ assert_eq!(pr.issue.title, "Add feature", "issue routes");
16241536 assert!(
16251537 fx.store
1626- .comment_by_id(&pr.issue.id, &comment.id)
1538+ .comment_by_id(&pr.issue.id, &keep.id)
16271539 .await
16281540 .unwrap()
1629- .is_some()
1541+ .is_some(),
1542+ "issue routes kept the comment"
1543+ );
1544+
1545+ // Close and reopen the pull.
1546+ let uri_state = format!("{uri}/state");
1547+ let form = csrf_form("tahinli", &[("action", "close_not_planned")]);
1548+ let (status, _, _) =
1549+ send(&fx.router, "POST", &uri_state, Some("tahinli"), Some(form)).await;
1550+ assert_eq!(status, 303, "close");
1551+ assert!(
1552+ matches!(pull(&fx, number).await.issue.state, IssueState::Closed),
1553+ "close"
1554+ );
1555+ let form = csrf_form("tahinli", &[("action", "reopen")]);
1556+ let (status, _, _) =
1557+ send(&fx.router, "POST", &uri_state, Some("tahinli"), Some(form)).await;
1558+ assert_eq!(status, 303, "reopen");
1559+ assert!(
1560+ matches!(pull(&fx, number).await.issue.state, IssueState::Open),
1561+ "reopen"
1562+ );
1563+
1564+ // A reader cannot see the edit link, open the edit form, or change the title.
1565+ let (_, _, body) = send(&fx.router, "GET", &uri, Some("bob-token"), None).await;
1566+ assert!(
1567+ !body.contains(&format!("href=\"{uri}/edit\"")),
1568+ "reader edit link"
1569+ );
1570+ let (status, _, _) = send(
1571+ &fx.router,
1572+ "GET",
1573+ &format!("{uri}/edit"),
1574+ Some("bob-token"),
1575+ None,
1576+ )
1577+ .await;
1578+ assert_eq!(status, 403, "reader edit form");
1579+ let form = csrf_form("bob-token", &[("title", "Hijacked"), ("body", "x")]);
1580+ let (status, _, _) = send(
1581+ &fx.router,
1582+ "POST",
1583+ &format!("{uri}/edit"),
1584+ Some("bob-token"),
1585+ Some(form),
1586+ )
1587+ .await;
1588+ assert_eq!(status, 403, "reader edit post");
1589+ assert_eq!(
1590+ pull(&fx, number).await.issue.title,
1591+ "Add feature",
1592+ "reader edit"
1593+ );
1594+
1595+ // The author edits the title and body. This runs last because it renames the pull.
1596+ let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await;
1597+ assert_eq!(status, 200, "author edit link");
1598+ assert!(
1599+ body.contains(&format!("href=\"{uri}/edit\"")),
1600+ "author edit link"
1601+ );
1602+ let (status, _, body) = send(
1603+ &fx.router,
1604+ "GET",
1605+ &format!("{uri}/edit"),
1606+ Some("tahinli"),
1607+ None,
1608+ )
1609+ .await;
1610+ assert_eq!(status, 200, "author edit form");
1611+ assert!(body.contains("Edit pull request"), "author edit form");
1612+ let form = csrf_form(
1613+ "tahinli",
1614+ &[("title", "Renamed pull"), ("body", "New text")],
1615+ );
1616+ let (status, location, _) = send(
1617+ &fx.router,
1618+ "POST",
1619+ &format!("{uri}/edit"),
1620+ Some("tahinli"),
1621+ Some(form),
1622+ )
1623+ .await;
1624+ assert_eq!(status, 303, "author edit");
1625+ assert_eq!(location.as_deref(), Some(uri.as_str()), "author edit");
1626+ let pr = pull(&fx, number).await;
1627+ assert_eq!(pr.issue.title, "Renamed pull", "author edit");
1628+ assert_eq!(pr.issue.body, "New text", "author edit");
1629+ let events = fx.store.list_events(&pr.issue.id).await.unwrap();
1630+ assert!(
1631+ events.iter().any(|e| e.kind == "renamed"),
1632+ "author edit event"
16301633 );
16311634 }
16321635
1633- /// A pending line comment on `feat.txt` by `user`, submitted as a review with `state` and
1634- /// `body`. Returns the root comment's id.
1636+ /// A pending review comment by `user`, submitted as a review with `state` and `body`.
1637+ /// Returns the root comment's id.
16351638 async fn submit_thread(
16361639 fx: &Fixture,
16371640 number: i64,
@@ -1667,106 +1670,35 @@ mod tests {
16671670 fx.store.list_reviews(&pr.issue.id).await.unwrap()
16681671 }
16691672
1673+ /// Review summary, outdated threads and a dismissal on one pull. Bob approves with a thread
1674+ /// that goes outdated, Carol requests changes, then the approval is dismissed.
16701675 #[tokio::test]
1671- async fn approved_review_shows_its_verb_body_and_outdated_thread() {
1672- let fx = Fixture::new(Visibility::Public).await;
1673- owner_session(&fx).await;
1674- let bob = sign_in(&fx, "bob", "bob-token").await;
1675- let number = open_pull(&fx).await;
1676- let root = submit_thread(&fx, number, &bob, ReviewState::Approved, "Ship it").await;
1677- let pr = pull(&fx, number).await;
1678- fx.store
1679- .mark_outdated(&pr.issue.id, std::slice::from_ref(&root))
1680- .await
1681- .unwrap();
1682-
1683- let uri = format!("/tahinli/demo/pulls/{number}");
1684- let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await;
1685- assert_eq!(status, 200);
1686- assert!(body.contains("approved these changes"), "{body}");
1687- assert!(body.contains("Ship it"), "{body}");
1688- assert!(body.contains("review-thread"), "{body}");
1689- assert!(body.contains("Please rename this"), "{body}");
1690- assert!(body.contains("feat.txt:1"), "{body}");
1691- assert!(body.contains("@@ -0,0 +1 @@"), "{body}");
1692- assert!(body.contains("review-badge-outdated"), "{body}");
1693- assert!(body.contains("Approved by bob"), "{body}");
1694- }
1695-
1696- #[tokio::test]
1697- async fn reply_only_review_gets_no_entry_of_its_own() {
1698- let fx = Fixture::new(Visibility::Public).await;
1699- owner_session(&fx).await;
1700- let bob = sign_in(&fx, "bob", "bob-token").await;
1701- let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap();
1702- let number = open_pull(&fx).await;
1703- let root = submit_thread(&fx, number, &bob, ReviewState::Commented, "").await;
1704- let pr = pull(&fx, number).await;
1705- fx.store
1706- .reply_review_comment(&pr.issue.id, &owner.id, &root, "Done, thanks")
1707- .await
1708- .unwrap();
1709- assert_eq!(all_reviews(&fx, number).await.len(), 2);
1710-
1711- let uri = format!("/tahinli/demo/pulls/{number}");
1712- let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await;
1713- assert_eq!(status, 200);
1714- assert_eq!(body.matches("id=\"review-").count(), 1, "{body}");
1715- assert!(body.contains("Done, thanks"), "{body}");
1716- assert!(body.contains("Please rename this"), "{body}");
1717- }
1718-
1719- #[tokio::test]
1720- async fn dismissed_review_is_struck_with_its_reason() {
1676+ async fn review_summary_outdated_thread_and_dismissal() {
17211677 let fx = Fixture::new(Visibility::Public).await;
17221678 owner_session(&fx).await;
17231679 let bob = sign_in(&fx, "bob", "bob-token").await;
1680+ let carol = sign_in(&fx, "carol", "carol-token").await;
17241681 let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap();
17251682 let number = open_pull(&fx).await;
17261683 let pr = pull(&fx, number).await;
1684+ let root = submit_thread(&fx, number, &bob, ReviewState::Approved, "LGTM").await;
17271685 fx.store
1728- .submit_review(
1729- &pr.issue.id,
1730- &bob.id,
1731- ReviewState::Approved,
1732- "LGTM",
1733- &pr.head_oid,
1734- )
1735- .await
1736- .unwrap();
1737- let review = all_reviews(&fx, number).await.remove(0);
1738- fx.store
1739- .dismiss_review(&review.id, &owner.id, "superseded")
1686+ .mark_outdated(&pr.issue.id, std::slice::from_ref(&root))
17401687 .await
17411688 .unwrap();
17421689
17431690 let uri = format!("/tahinli/demo/pulls/{number}");
17441691 let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await;
1745- assert_eq!(status, 200);
1746- assert!(body.contains("review-verb review-verb-dismissed"), "{body}");
1747- assert!(body.contains("dismissed: superseded"), "{body}");
1748- assert!(body.contains("LGTM"), "{body}");
1749- assert!(!body.contains("Approved by bob"), "{body}");
1750- }
1692+ assert_eq!(status, 200, "approval");
1693+ assert!(body.contains("approved these changes"), "approval: {body}");
1694+ assert!(body.contains("LGTM"), "approval: {body}");
1695+ assert!(body.contains("review-thread"), "approval: {body}");
1696+ assert!(body.contains("Please rename this"), "approval: {body}");
1697+ assert!(body.contains("feat.txt:1"), "approval: {body}");
1698+ assert!(body.contains("@@ -0,0 +1 @@"), "approval: {body}");
1699+ assert!(body.contains("review-badge-outdated"), "approval: {body}");
1700+ assert!(body.contains("Approved by bob"), "approval: {body}");
17511701
1752- #[tokio::test]
1753- async fn merge_box_summarises_approvals_and_changes_requests() {
1754- let fx = Fixture::new(Visibility::Public).await;
1755- owner_session(&fx).await;
1756- let bob = sign_in(&fx, "bob", "bob-token").await;
1757- let carol = sign_in(&fx, "carol", "carol-token").await;
1758- let number = open_pull(&fx).await;
1759- let pr = pull(&fx, number).await;
1760- fx.store
1761- .submit_review(
1762- &pr.issue.id,
1763- &bob.id,
1764- ReviewState::Approved,
1765- "",
1766- &pr.head_oid,
1767- )
1768- .await
1769- .unwrap();
17701702 fx.store
17711703 .submit_review(
17721704 &pr.issue.id,
@@ -1778,63 +1710,74 @@ mod tests {
17781710 .await
17791711 .unwrap();
17801712 let approval = all_reviews(&fx, number).await.remove(0);
1781-
1782- let uri = format!("/tahinli/demo/pulls/{number}");
17831713 let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await;
1784- assert_eq!(status, 200);
1785- assert!(body.contains("Approved by bob"), "{body}");
1786- assert!(body.contains("Changes requested by carol"), "{body}");
1714+ assert_eq!(status, 200, "summary");
1715+ assert!(body.contains("Approved by bob"), "summary: {body}");
1716+ assert!(
1717+ body.contains("Changes requested by carol"),
1718+ "summary: {body}"
1719+ );
17871720 assert!(
17881721 body.contains(&format!("action=\"{uri}/reviews/{}/dismiss\"", approval.id)),
1789- "{body}"
1722+ "summary: {body}"
17901723 );
17911724
17921725 // A reader sees the summary but no dismiss form.
17931726 let (_, _, body) = send(&fx.router, "GET", &uri, Some("carol-token"), None).await;
1794- assert!(body.contains("Approved by bob"), "{body}");
1795- assert!(!body.contains("/dismiss\""), "{body}");
1796- }
1797-
1798- #[tokio::test]
1799- async fn review_error_code_shows_on_the_conversation() {
1800- let fx = Fixture::new(Visibility::Public).await;
1801- owner_session(&fx).await;
1802- let number = open_pull(&fx).await;
1803- let uri = format!("/tahinli/demo/pulls/{number}");
1727+ assert!(body.contains("Approved by bob"), "reader summary: {body}");
1728+ assert!(!body.contains("/dismiss\""), "reader summary: {body}");
18041729
1805- let (status, _, body) = send(
1806- &fx.router,
1807- "GET",
1808- &format!("{uri}?review_error=own_pr"),
1809- Some("tahinli"),
1810- None,
1811- )
1812- .await;
1813- assert_eq!(status, 200);
1730+ // Dismissing the approval strikes it with its reason.
1731+ fx.store
1732+ .dismiss_review(&approval.id, &owner.id, "superseded")
1733+ .await
1734+ .unwrap();
1735+ let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await;
1736+ assert_eq!(status, 200, "dismissal");
18141737 assert!(
1815- body.contains("You cannot approve or request changes on your own pull request."),
1816- "{body}"
1738+ body.contains("review-verb review-verb-dismissed"),
1739+ "dismissal: {body}"
18171740 );
1818-
1819- let (_, _, body) = send(
1820- &fx.router,
1821- "GET",
1822- &format!("{uri}?review_error=%3Cscript%3E"),
1823- Some("tahinli"),
1824- None,
1825- )
1826- .await;
1827- assert!(!body.contains("<script>"), "{body}");
1828- assert!(!body.contains("You cannot approve"), "{body}");
1741+ assert!(body.contains("dismissed: superseded"), "dismissal: {body}");
1742+ assert!(body.contains("LGTM"), "dismissal: {body}");
1743+ assert!(!body.contains("Approved by bob"), "dismissal: {body}");
18291744 }
18301745
1746+ /// A comment-only review gets no entry of its own, and another user's pending comment stays
1747+ /// hidden from the author's view.
18311748 #[tokio::test]
1832- async fn other_users_pending_review_comments_stay_hidden() {
1749+ async fn comment_only_review_and_hidden_pending_comments() {
18331750 let fx = Fixture::new(Visibility::Public).await;
18341751 owner_session(&fx).await;
18351752 let bob = sign_in(&fx, "bob", "bob-token").await;
1753+ let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap();
18361754 let number = open_pull(&fx).await;
18371755 let pr = pull(&fx, number).await;
1756+ let root = submit_thread(&fx, number, &bob, ReviewState::Commented, "").await;
1757+ fx.store
1758+ .reply_review_comment(&pr.issue.id, &owner.id, &root, "Done, thanks")
1759+ .await
1760+ .unwrap();
1761+ assert_eq!(
1762+ all_reviews(&fx, number).await.len(),
1763+ 2,
1764+ "comment-only review"
1765+ );
1766+
1767+ let uri = format!("/tahinli/demo/pulls/{number}");
1768+ let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await;
1769+ assert_eq!(status, 200, "comment-only review");
1770+ assert_eq!(
1771+ body.matches("id=\"review-").count(),
1772+ 1,
1773+ "comment-only review: {body}"
1774+ );
1775+ assert!(body.contains("Done, thanks"), "comment-only review: {body}");
1776+ assert!(
1777+ body.contains("Please rename this"),
1778+ "comment-only review: {body}"
1779+ );
1780+
18381781 fx.store
18391782 .add_review_comment(
18401783 &pr.issue.id,
@@ -1849,11 +1792,8 @@ mod tests {
18491792 )
18501793 .await
18511794 .unwrap();
1852-
1853- let uri = format!("/tahinli/demo/pulls/{number}");
18541795 let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await;
1855- assert_eq!(status, 200);
1856- assert!(!body.contains("secret draft"), "{body}");
1857- assert!(!body.contains("review-item"), "{body}");
1796+ assert_eq!(status, 200, "hidden pending");
1797+ assert!(!body.contains("secret draft"), "hidden pending: {body}");
18581798 }
18591799 }