Pull page tests share their fixtures and merge into tables
Browse files at this commitf23b28cdf6c1034b5debcbfd748fee004f9a2f12Parent: eb7e617- test
- pull page tests share their fixtures and merge into tables
3 changed files +699 -829
- crates/forge-web/src/pages/pull_files.rs +125 -154
- crates/forge-web/src/pages/pull_reviews.rs +246 -287
- crates/forge-web/src/pages/pull_view.rs +328 -388
crates/forge-web/src/pages/pull_files.rsmodified+125 -154
| @@ -364,15 +364,23 @@ mod tests { | ||
| 364 | 364 | format!("/{OWNER}/{REPO}/pulls/{number}/files{query}") |
| 365 | 365 | } |
| 366 | 366 | |
| 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. | |
| 367 | 370 | #[tokio::test] |
| 368 | - async fn turkish_viewer_gets_turkish_review_bar() { | |
| 371 | + async fn files_tab_scenarios_on_one_open_pull() { | |
| 369 | 372 | let fx = Fixture::new(Visibility::Public).await; |
| 370 | 373 | owner_session(&fx).await; |
| 374 | + sign_in(&fx, "bob", "bob").await; | |
| 371 | 375 | 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, ""); | |
| 372 | 379 | |
| 380 | + // Turkish viewers get the translated review bar. | |
| 373 | 381 | let req = Request::builder() |
| 374 | 382 | .method("GET") |
| 375 | - .uri(files_uri(number, "")) | |
| 383 | + .uri(uri.as_str()) | |
| 376 | 384 | .header("cookie", cookie("tahinli")) |
| 377 | 385 | .header("accept-language", "tr") |
| 378 | 386 | .body(Body::empty()) |
| @@ -380,57 +388,47 @@ mod tests { | ||
| 380 | 388 | let res = fx.router.handle(req).await; |
| 381 | 389 | let bytes = to_bytes(res.into_body(), usize::MAX).await.unwrap(); |
| 382 | 390 | 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 | + ); | |
| 399 | 396 | |
| 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}"); | |
| 406 | 406 | |
| 407 | + // A well-formed `comment` param opens the form under its line. | |
| 407 | 408 | let (status, body) = get( |
| 408 | 409 | &fx.router, |
| 409 | 410 | &files_uri(number, "?comment=0:new:1"), |
| 410 | 411 | Some("tahinli"), |
| 411 | 412 | ) |
| 412 | 413 | .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}"); | |
| 417 | 421 | assert!( |
| 418 | 422 | body.contains(&format!("name=\"head_oid\" value=\"{head}\"")), |
| 419 | - "{body}" | |
| 423 | + "form head: {body}" | |
| 420 | 424 | ); |
| 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}"); | |
| 423 | 427 | let line = body.find("id=\"L-0-new-1\"").unwrap(); |
| 424 | 428 | let form = body.find("name=\"file_index\"").unwrap(); |
| 425 | 429 | 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; | |
| 433 | 430 | |
| 431 | + // Malformed `comment` params are ignored. | |
| 434 | 432 | for query in [ |
| 435 | 433 | "?comment=abc", |
| 436 | 434 | "?comment=0:bogus:1", |
| @@ -441,48 +439,59 @@ mod tests { | ||
| 441 | 439 | "?comment=0:new:-1", |
| 442 | 440 | ] { |
| 443 | 441 | 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}"); | |
| 446 | 444 | } |
| 447 | - } | |
| 448 | 445 | |
| 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 | + ); | |
| 470 | 463 | |
| 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}"); | |
| 478 | 493 | |
| 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. | |
| 486 | 495 | let comment = fx |
| 487 | 496 | .store |
| 488 | 497 | .add_review_comment( |
| @@ -502,21 +511,15 @@ mod tests { | ||
| 502 | 511 | .mark_outdated(&issue_id, std::slice::from_ref(&comment.id)) |
| 503 | 512 | .await |
| 504 | 513 | .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 | + ); | |
| 505 | 521 | |
| 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. | |
| 520 | 523 | fx.store |
| 521 | 524 | .add_review_comment( |
| 522 | 525 | &issue_id, |
| @@ -531,71 +534,39 @@ mod tests { | ||
| 531 | 534 | ) |
| 532 | 535 | .await |
| 533 | 536 | .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 | + ); | |
| 534 | 548 | |
| 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"); | |
| 599 | 570 | } |
| 600 | 571 | |
| 601 | 572 | #[tokio::test] |
crates/forge-web/src/pages/pull_reviews.rsmodified+246 -287
| @@ -997,64 +997,59 @@ mod tests { | ||
| 997 | 997 | (status, location) |
| 998 | 998 | } |
| 999 | 999 | |
| 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 { | |
| 1002 | 1002 | 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] | |
| 1004 | 1004 | .root |
| 1005 | 1005 | .id |
| 1006 | 1006 | .clone() |
| 1007 | 1007 | } |
| 1008 | 1008 | |
| 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 | + } | |
| 1015 | 1013 | |
| 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) | |
| 1031 | 1025 | } |
| 1032 | 1026 | |
| 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. | |
| 1033 | 1029 | #[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() { | |
| 1035 | 1031 | let fx = Fixture::new(Visibility::Public).await; |
| 1036 | 1032 | owner_session(&fx).await; |
| 1037 | 1033 | let number = open_pull(&fx).await; |
| 1038 | 1034 | let pr = pull(&fx, number).await; |
| 1039 | 1035 | |
| 1036 | + // A line outside the diff is rejected and leaves no thread. | |
| 1040 | 1037 | 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 | + ); | |
| 1043 | 1043 | assert!( |
| 1044 | 1044 | fx.store |
| 1045 | 1045 | .review_threads(&pr.issue.id, None) |
| 1046 | 1046 | .await |
| 1047 | 1047 | .unwrap() |
| 1048 | - .is_empty() | |
| 1048 | + .is_empty(), | |
| 1049 | + "outside diff left a thread" | |
| 1049 | 1050 | ); |
| 1050 | - } | |
| 1051 | 1051 | |
| 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. | |
| 1058 | 1053 | let zero = "0".repeat(40); |
| 1059 | 1054 | let form = csrf_form( |
| 1060 | 1055 | "tahinli", |
| @@ -1075,26 +1070,41 @@ mod tests { | ||
| 1075 | 1070 | Some(form), |
| 1076 | 1071 | ) |
| 1077 | 1072 | .await; |
| 1078 | - assert_eq!(status, 303); | |
| 1073 | + assert_eq!(status, 303, "stale head"); | |
| 1079 | 1074 | assert_eq!( |
| 1080 | 1075 | 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" | |
| 1082 | 1078 | ); |
| 1083 | 1079 | assert!( |
| 1084 | 1080 | fx.store |
| 1085 | 1081 | .review_threads(&pr.issue.id, None) |
| 1086 | 1082 | .await |
| 1087 | 1083 | .unwrap() |
| 1088 | - .is_empty() | |
| 1084 | + .is_empty(), | |
| 1085 | + "stale head left a thread" | |
| 1089 | 1086 | ); |
| 1090 | - } | |
| 1091 | 1087 | |
| 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. | |
| 1098 | 1108 | let tahinli = fx.store.user_by_username(OWNER).await.unwrap().unwrap(); |
| 1099 | 1109 | let head = pr.head_oid.clone(); |
| 1100 | 1110 | let form = csrf_form( |
| @@ -1116,21 +1126,26 @@ mod tests { | ||
| 1116 | 1126 | Some(form), |
| 1117 | 1127 | ) |
| 1118 | 1128 | .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!( | |
| 1122 | 1131 | fx.store |
| 1123 | 1132 | .review_threads(&pr.issue.id, None) |
| 1124 | 1133 | .await |
| 1125 | 1134 | .unwrap() |
| 1126 | - .is_empty() | |
| 1135 | + .len(), | |
| 1136 | + 1, | |
| 1137 | + "pending thread hidden from everyone" | |
| 1127 | 1138 | ); |
| 1128 | 1139 | let mine = fx |
| 1129 | 1140 | .store |
| 1130 | 1141 | .review_threads(&pr.issue.id, Some(&tahinli.id)) |
| 1131 | 1142 | .await |
| 1132 | 1143 | .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 | + ); | |
| 1134 | 1149 | |
| 1135 | 1150 | let form = csrf_form("tahinli", &[("state", "comment"), ("body", "Done")]); |
| 1136 | 1151 | let (status, _, _) = send( |
| @@ -1141,50 +1156,46 @@ mod tests { | ||
| 1141 | 1156 | Some(form), |
| 1142 | 1157 | ) |
| 1143 | 1158 | .await; |
| 1144 | - assert_eq!(status, 303); | |
| 1159 | + assert_eq!(status, 303, "submit review"); | |
| 1145 | 1160 | 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"); | |
| 1158 | 1163 | |
| 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 | + ], | |
| 1174 | 1176 | ); |
| 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"); | |
| 1176 | 1184 | } |
| 1177 | 1185 | |
| 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). | |
| 1178 | 1188 | #[tokio::test] |
| 1179 | - async fn reply_joins_the_thread_and_redirects_to_the_root() { | |
| 1189 | + async fn thread_reply_edit_resolve_and_delete() { | |
| 1180 | 1190 | let fx = Fixture::new(Visibility::Public).await; |
| 1181 | 1191 | owner_session(&fx).await; |
| 1182 | 1192 | sign_in(&fx, "bob", "bob").await; |
| 1183 | 1193 | 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; | |
| 1186 | 1195 | let pr = pull(&fx, number).await; |
| 1196 | + let root = root_at(&fx, number, 0).await; | |
| 1187 | 1197 | |
| 1198 | + // A reader replies into the thread and is sent back to its root. | |
| 1188 | 1199 | let form = csrf_form("bob", &[("body", "Reply")]); |
| 1189 | 1200 | let (status, location, _) = send( |
| 1190 | 1201 | &fx.router, |
| @@ -1194,96 +1205,125 @@ mod tests { | ||
| 1194 | 1205 | Some(form), |
| 1195 | 1206 | ) |
| 1196 | 1207 | .await; |
| 1197 | - assert_eq!(status, 303); | |
| 1208 | + assert_eq!(status, 303, "reply"); | |
| 1198 | 1209 | assert_eq!( |
| 1199 | 1210 | 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" | |
| 1201 | 1213 | ); |
| 1202 | 1214 | 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"); | |
| 1206 | 1217 | |
| 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. | |
| 1215 | 1219 | let edit = format!("/tahinli/demo/pulls/{number}/review_comments/{root}/edit"); |
| 1216 | - | |
| 1217 | 1220 | 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"); | |
| 1220 | 1223 | 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"); | |
| 1223 | 1225 | let form = csrf_form("bob", &[("body", "Hijacked")]); |
| 1224 | 1226 | 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"); | |
| 1227 | 1228 | let form = csrf_form("tahinli", &[("body", "Edited")]); |
| 1228 | 1229 | let (status, location, _) = |
| 1229 | 1230 | send(&fx.router, "POST", &edit, Some("tahinli"), Some(form)).await; |
| 1230 | - assert_eq!(status, 303); | |
| 1231 | + assert_eq!(status, 303, "edit"); | |
| 1231 | 1232 | assert_eq!( |
| 1232 | 1233 | 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" | |
| 1234 | 1236 | ); |
| 1235 | - let pr = pull(&fx, number).await; | |
| 1236 | 1237 | 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"); | |
| 1239 | 1239 | |
| 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"); | |
| 1250 | 1249 | |
| 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"); | |
| 1251 | 1254 | let form = csrf_form("bob", &[]); |
| 1252 | 1255 | 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"); | |
| 1255 | 1257 | let form = csrf_form("tahinli", &[]); |
| 1256 | 1258 | 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"); | |
| 1265 | 1263 | } |
| 1266 | 1264 | |
| 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. | |
| 1267 | 1267 | #[tokio::test] |
| 1268 | - async fn resolve_is_for_the_pr_author_or_write() { | |
| 1268 | + async fn reviews_approve_then_close_the_pull() { | |
| 1269 | 1269 | let fx = Fixture::new(Visibility::Public).await; |
| 1270 | 1270 | owner_session(&fx).await; |
| 1271 | 1271 | sign_in(&fx, "bob", "bob").await; |
| 1272 | 1272 | let number = open_pull(&fx).await; |
| 1273 | - comment_on(&fx, number, "tahinli", "2", "Thread").await; | |
| 1274 | - let root = root_id(&fx, number).await; | |
| 1275 | 1273 | 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"); | |
| 1277 | 1275 | |
| 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"); | |
| 1281 | 1284 | |
| 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 | + ); | |
| 1287 | 1327 | } |
| 1288 | 1328 | |
| 1289 | 1329 | #[tokio::test] |
| @@ -1307,155 +1347,52 @@ mod tests { | ||
| 1307 | 1347 | |
| 1308 | 1348 | let form = csrf_form("bob", &[("reason", "mine")]); |
| 1309 | 1349 | 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"); | |
| 1311 | 1351 | |
| 1312 | 1352 | let form = csrf_form("tahinli", &[("reason", " ")]); |
| 1313 | 1353 | let (status, location, _) = |
| 1314 | 1354 | 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 | + ); | |
| 1317 | 1360 | |
| 1318 | 1361 | let form = csrf_form("tahinli", &[("reason", "Addressed")]); |
| 1319 | 1362 | let (status, _, _) = send(&fx.router, "POST", &uri, Some("tahinli"), Some(form)).await; |
| 1320 | - assert_eq!(status, 303); | |
| 1363 | + assert_eq!(status, 303, "dismiss"); | |
| 1321 | 1364 | 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"); | |
| 1404 | 1366 | } |
| 1405 | 1367 | |
| 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. | |
| 1406 | 1370 | #[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() { | |
| 1408 | 1372 | let fx = Fixture::new(Visibility::Public).await; |
| 1409 | 1373 | owner_session(&fx).await; |
| 1374 | + sign_in(&fx, "rita", "rita").await; | |
| 1410 | 1375 | let number = open_pull(&fx).await; |
| 1411 | - let before = pull(&fx, number).await.head_oid; | |
| 1412 | - | |
| 1413 | 1376 | 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; | |
| 1419 | 1379 | |
| 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"); | |
| 1434 | 1383 | assert!( |
| 1435 | - message.starts_with("Apply suggestion from review"), | |
| 1436 | - "{message}" | |
| 1384 | + !page.contains("Apply suggestion"), | |
| 1385 | + "no apply button without push access" | |
| 1437 | 1386 | ); |
| 1387 | + let (status, location) = apply_as(&fx, number, "rita", &first).await; | |
| 1388 | + assert_eq!(status, 303, "reader apply"); | |
| 1438 | 1389 | assert!( |
| 1439 | - message.contains("Co-authored-by: tahinli <"), | |
| 1440 | - "commenter trailer: {message}" | |
| 1390 | + location.unwrap().contains("review_error=no_push"), | |
| 1391 | + "reader apply" | |
| 1441 | 1392 | ); |
| 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 | - } | |
| 1446 | 1393 | |
| 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. | |
| 1457 | 1395 | let pr = pull(&fx, number).await; |
| 1458 | - git(&fx.work, &["checkout", "-q", "feature"]); | |
| 1459 | 1396 | std::fs::write(fx.work.join("a.txt"), "one\nmoved\nthree\n").unwrap(); |
| 1460 | 1397 | git(&fx.work, &["commit", "-q", "-am", "Move line 2"]); |
| 1461 | 1398 | let moved = git(&fx.work, &["rev-parse", "HEAD"]); |
| @@ -1482,10 +1419,12 @@ mod tests { | ||
| 1482 | 1419 | !page.contains("Apply suggestion"), |
| 1483 | 1420 | "no apply button when outdated" |
| 1484 | 1421 | ); |
| 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 | + ); | |
| 1489 | 1428 | assert_eq!( |
| 1490 | 1429 | git(&fx.bare, &["rev-parse", "feature"]), |
| 1491 | 1430 | moved, |
| @@ -1496,28 +1435,48 @@ mod tests { | ||
| 1496 | 1435 | !threads[0].root.resolved, |
| 1497 | 1436 | "a refused apply leaves the thread open" |
| 1498 | 1437 | ); |
| 1499 | - } | |
| 1500 | 1438 | |
| 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"); | |
| 1506 | 1442 | 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"); | |
| 1510 | 1448 | |
| 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]); | |
| 1513 | 1465 | assert!( |
| 1514 | - !page.contains("Apply suggestion"), | |
| 1515 | - "no apply button without push access" | |
| 1466 | + message.starts_with("Apply suggestion from review"), | |
| 1467 | + "{message}" | |
| 1516 | 1468 | ); |
| 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"); | |
| 1521 | 1480 | } |
| 1522 | 1481 | |
| 1523 | 1482 | /// 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 { | ||
| 1206 | 1206 | pr.issue.number |
| 1207 | 1207 | } |
| 1208 | 1208 | |
| 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. | |
| 1209 | 1211 | #[tokio::test] |
| 1210 | - async fn turkish_viewer_gets_turkish_pull_tabs() { | |
| 1212 | + async fn conversation_merge_box_and_error_codes_on_one_pull() { | |
| 1211 | 1213 | let fx = Fixture::new(Visibility::Public).await; |
| 1212 | 1214 | owner_session(&fx).await; |
| 1215 | + sign_in(&fx, "bob", "bob-token").await; | |
| 1213 | 1216 | let number = open_pull(&fx).await; |
| 1217 | + let pr = pull(&fx, number).await; | |
| 1214 | 1218 | let uri = format!("/tahinli/demo/pulls/{number}"); |
| 1219 | + | |
| 1220 | + // Turkish viewers get the translated tabs. | |
| 1215 | 1221 | let req = Request::builder() |
| 1216 | 1222 | .method("GET") |
| 1217 | 1223 | .uri(uri.as_str()) |
| @@ -1221,100 +1227,12 @@ mod tests { | ||
| 1221 | 1227 | let res = fx.router.handle(req).await; |
| 1222 | 1228 | let bytes = to_bytes(res.into_body(), usize::MAX).await.unwrap(); |
| 1223 | 1229 | 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"); | |
| 1227 | 1233 | 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 | - } | |
| 1291 | 1234 | |
| 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. | |
| 1318 | 1236 | fx.store |
| 1319 | 1237 | .set_mergeability( |
| 1320 | 1238 | &pr.issue.id, |
| @@ -1325,22 +1243,20 @@ mod tests { | ||
| 1325 | 1243 | ) |
| 1326 | 1244 | .await |
| 1327 | 1245 | .unwrap(); |
| 1328 | - | |
| 1329 | - let uri = format!("/tahinli/demo/pulls/{number}"); | |
| 1330 | 1246 | 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}"); | |
| 1337 | 1258 | |
| 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. | |
| 1344 | 1260 | fx.store |
| 1345 | 1261 | .set_mergeability( |
| 1346 | 1262 | &pr.issue.id, |
| @@ -1351,167 +1267,142 @@ mod tests { | ||
| 1351 | 1267 | ) |
| 1352 | 1268 | .await |
| 1353 | 1269 | .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}"); | |
| 1354 | 1292 | |
| 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}"); | |
| 1362 | 1302 | |
| 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( | |
| 1368 | 1305 | &fx.router, |
| 1369 | 1306 | "GET", |
| 1370 | - &format!("/tahinli/demo/issues/{number}"), | |
| 1371 | - None, | |
| 1307 | + &format!("{uri}?merge_error=conflict"), | |
| 1308 | + Some("tahinli"), | |
| 1372 | 1309 | None, |
| 1373 | 1310 | ) |
| 1374 | 1311 | .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}" | |
| 1379 | 1316 | ); |
| 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); | |
| 1392 | 1317 | 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}" | |
| 1396 | 1320 | ); |
| 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 | - | |
| 1435 | 1321 | let (status, _, body) = send( |
| 1436 | 1322 | &fx.router, |
| 1437 | 1323 | "GET", |
| 1438 | - &format!("{uri}/edit"), | |
| 1324 | + &format!("{uri}?merge_error=%3Cscript%3Ealert(1)%3C%2Fscript%3E"), | |
| 1439 | 1325 | Some("tahinli"), |
| 1440 | 1326 | None, |
| 1441 | 1327 | ) |
| 1442 | 1328 | .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}" | |
| 1449 | 1334 | ); |
| 1450 | - let (status, location, _) = send( | |
| 1335 | + | |
| 1336 | + // Known review error codes show their message, unknown ones show nothing. | |
| 1337 | + let (status, _, body) = send( | |
| 1451 | 1338 | &fx.router, |
| 1452 | - "POST", | |
| 1453 | - &format!("{uri}/edit"), | |
| 1339 | + "GET", | |
| 1340 | + &format!("{uri}?review_error=own_pr"), | |
| 1454 | 1341 | Some("tahinli"), |
| 1455 | - Some(form), | |
| 1342 | + None, | |
| 1456 | 1343 | ) |
| 1457 | 1344 | .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( | |
| 1480 | 1351 | &fx.router, |
| 1481 | 1352 | "GET", |
| 1482 | - &format!("{uri}/edit"), | |
| 1483 | - Some("bob-token"), | |
| 1353 | + &format!("{uri}?review_error=%3Cscript%3E"), | |
| 1354 | + Some("tahinli"), | |
| 1484 | 1355 | None, |
| 1485 | 1356 | ) |
| 1486 | 1357 | .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( | |
| 1490 | 1366 | &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, | |
| 1495 | 1371 | ) |
| 1496 | 1372 | .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}"); | |
| 1499 | 1384 | } |
| 1500 | 1385 | |
| 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. | |
| 1501 | 1389 | #[tokio::test] |
| 1502 | - async fn author_edits_and_deletes_a_pull_comment() { | |
| 1390 | + async fn comments_state_and_title_edits_on_one_pull() { | |
| 1503 | 1391 | let fx = Fixture::new(Visibility::Public).await; |
| 1504 | 1392 | owner_session(&fx).await; |
| 1393 | + sign_in(&fx, "bob", "bob-token").await; | |
| 1505 | 1394 | let number = open_pull(&fx).await; |
| 1395 | + let uri = format!("/tahinli/demo/pulls/{number}"); | |
| 1506 | 1396 | let pr = pull(&fx, number).await; |
| 1507 | 1397 | 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. | |
| 1508 | 1400 | let comment = fx |
| 1509 | 1401 | .store |
| 1510 | 1402 | .add_comment(&pr.issue.id, &owner.id, "first draft") |
| 1511 | 1403 | .await |
| 1512 | 1404 | .unwrap(); |
| 1513 | - let base = format!("/tahinli/demo/pulls/{number}/comments/{}", comment.id); | |
| 1514 | - | |
| 1405 | + let base = format!("{uri}/comments/{}", comment.id); | |
| 1515 | 1406 | let (status, _, _) = send( |
| 1516 | 1407 | &fx.router, |
| 1517 | 1408 | "GET", |
| @@ -1520,7 +1411,7 @@ mod tests { | ||
| 1520 | 1411 | None, |
| 1521 | 1412 | ) |
| 1522 | 1413 | .await; |
| 1523 | - assert_eq!(status, 200); | |
| 1414 | + assert_eq!(status, 200, "comment edit form"); | |
| 1524 | 1415 | let form = csrf_form("tahinli", &[("body", "edited text")]); |
| 1525 | 1416 | let (status, location, _) = send( |
| 1526 | 1417 | &fx.router, |
| @@ -1530,13 +1421,14 @@ mod tests { | ||
| 1530 | 1421 | Some(form), |
| 1531 | 1422 | ) |
| 1532 | 1423 | .await; |
| 1533 | - assert_eq!(status, 303); | |
| 1424 | + assert_eq!(status, 303, "comment edit"); | |
| 1534 | 1425 | assert_eq!( |
| 1535 | 1426 | location, |
| 1536 | 1427 | Some(format!( |
| 1537 | 1428 | "/tahinli/demo/pulls/{number}#comment-{}", |
| 1538 | 1429 | comment.id |
| 1539 | - )) | |
| 1430 | + )), | |
| 1431 | + "comment edit" | |
| 1540 | 1432 | ); |
| 1541 | 1433 | let saved = fx |
| 1542 | 1434 | .store |
| @@ -1544,9 +1436,8 @@ mod tests { | ||
| 1544 | 1436 | .await |
| 1545 | 1437 | .unwrap() |
| 1546 | 1438 | .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"); | |
| 1550 | 1441 | let (status, location, _) = send( |
| 1551 | 1442 | &fx.router, |
| 1552 | 1443 | "POST", |
| @@ -1555,31 +1446,53 @@ mod tests { | ||
| 1555 | 1446 | Some(csrf_form("tahinli", &[])), |
| 1556 | 1447 | ) |
| 1557 | 1448 | .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 | + ); | |
| 1560 | 1455 | assert!( |
| 1561 | 1456 | fx.store |
| 1562 | 1457 | .comment_by_id(&pr.issue.id, &comment.id) |
| 1563 | 1458 | .await |
| 1564 | 1459 | .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" | |
| 1566 | 1467 | ); |
| 1567 | - assert_eq!(pull(&fx, number).await.issue.comment_count, 0); | |
| 1568 | - } | |
| 1569 | 1468 | |
| 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 | |
| 1578 | 1492 | .store |
| 1579 | 1493 | .add_comment(&pr.issue.id, &owner.id, "keep me") |
| 1580 | 1494 | .await |
| 1581 | 1495 | .unwrap(); |
| 1582 | - | |
| 1583 | 1496 | let (status, _, _) = send( |
| 1584 | 1497 | &fx.router, |
| 1585 | 1498 | "GET", |
| @@ -1588,7 +1501,7 @@ mod tests { | ||
| 1588 | 1501 | None, |
| 1589 | 1502 | ) |
| 1590 | 1503 | .await; |
| 1591 | - assert_eq!(status, 404); | |
| 1504 | + assert_eq!(status, 404, "issue edit route"); | |
| 1592 | 1505 | let form = csrf_form("tahinli", &[("title", "Nope"), ("body", "x")]); |
| 1593 | 1506 | let (status, _, _) = send( |
| 1594 | 1507 | &fx.router, |
| @@ -1598,40 +1511,130 @@ mod tests { | ||
| 1598 | 1511 | Some(form), |
| 1599 | 1512 | ) |
| 1600 | 1513 | .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); | |
| 1603 | 1516 | let (status, _, _) = send( |
| 1604 | 1517 | &fx.router, |
| 1605 | 1518 | "GET", |
| 1606 | - &format!("{base}/edit"), | |
| 1519 | + &format!("{issue_base}/edit"), | |
| 1607 | 1520 | Some("tahinli"), |
| 1608 | 1521 | None, |
| 1609 | 1522 | ) |
| 1610 | 1523 | .await; |
| 1611 | - assert_eq!(status, 404); | |
| 1524 | + assert_eq!(status, 404, "issue comment edit"); | |
| 1612 | 1525 | let (status, _, _) = send( |
| 1613 | 1526 | &fx.router, |
| 1614 | 1527 | "POST", |
| 1615 | - &format!("{base}/delete"), | |
| 1528 | + &format!("{issue_base}/delete"), | |
| 1616 | 1529 | Some("tahinli"), |
| 1617 | 1530 | Some(csrf_form("tahinli", &[])), |
| 1618 | 1531 | ) |
| 1619 | 1532 | .await; |
| 1620 | - assert_eq!(status, 404); | |
| 1621 | - | |
| 1533 | + assert_eq!(status, 404, "issue comment delete"); | |
| 1622 | 1534 | let pr = pull(&fx, number).await; |
| 1623 | - assert_eq!(pr.issue.title, "Add feature"); | |
| 1535 | + assert_eq!(pr.issue.title, "Add feature", "issue routes"); | |
| 1624 | 1536 | assert!( |
| 1625 | 1537 | fx.store |
| 1626 | - .comment_by_id(&pr.issue.id, &comment.id) | |
| 1538 | + .comment_by_id(&pr.issue.id, &keep.id) | |
| 1627 | 1539 | .await |
| 1628 | 1540 | .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" | |
| 1630 | 1633 | ); |
| 1631 | 1634 | } |
| 1632 | 1635 | |
| 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. | |
| 1635 | 1638 | async fn submit_thread( |
| 1636 | 1639 | fx: &Fixture, |
| 1637 | 1640 | number: i64, |
| @@ -1667,106 +1670,35 @@ mod tests { | ||
| 1667 | 1670 | fx.store.list_reviews(&pr.issue.id).await.unwrap() |
| 1668 | 1671 | } |
| 1669 | 1672 | |
| 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. | |
| 1670 | 1675 | #[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() { | |
| 1721 | 1677 | let fx = Fixture::new(Visibility::Public).await; |
| 1722 | 1678 | owner_session(&fx).await; |
| 1723 | 1679 | let bob = sign_in(&fx, "bob", "bob-token").await; |
| 1680 | + let carol = sign_in(&fx, "carol", "carol-token").await; | |
| 1724 | 1681 | let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap(); |
| 1725 | 1682 | let number = open_pull(&fx).await; |
| 1726 | 1683 | let pr = pull(&fx, number).await; |
| 1684 | + let root = submit_thread(&fx, number, &bob, ReviewState::Approved, "LGTM").await; | |
| 1727 | 1685 | 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)) | |
| 1740 | 1687 | .await |
| 1741 | 1688 | .unwrap(); |
| 1742 | 1689 | |
| 1743 | 1690 | let uri = format!("/tahinli/demo/pulls/{number}"); |
| 1744 | 1691 | 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}"); | |
| 1751 | 1701 | |
| 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(); | |
| 1770 | 1702 | fx.store |
| 1771 | 1703 | .submit_review( |
| 1772 | 1704 | &pr.issue.id, |
| @@ -1778,63 +1710,74 @@ mod tests { | ||
| 1778 | 1710 | .await |
| 1779 | 1711 | .unwrap(); |
| 1780 | 1712 | let approval = all_reviews(&fx, number).await.remove(0); |
| 1781 | - | |
| 1782 | - let uri = format!("/tahinli/demo/pulls/{number}"); | |
| 1783 | 1713 | 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 | + ); | |
| 1787 | 1720 | assert!( |
| 1788 | 1721 | body.contains(&format!("action=\"{uri}/reviews/{}/dismiss\"", approval.id)), |
| 1789 | - "{body}" | |
| 1722 | + "summary: {body}" | |
| 1790 | 1723 | ); |
| 1791 | 1724 | |
| 1792 | 1725 | // A reader sees the summary but no dismiss form. |
| 1793 | 1726 | 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}"); | |
| 1804 | 1729 | |
| 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"); | |
| 1814 | 1737 | 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}" | |
| 1817 | 1740 | ); |
| 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}"); | |
| 1829 | 1744 | } |
| 1830 | 1745 | |
| 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. | |
| 1831 | 1748 | #[tokio::test] |
| 1832 | - async fn other_users_pending_review_comments_stay_hidden() { | |
| 1749 | + async fn comment_only_review_and_hidden_pending_comments() { | |
| 1833 | 1750 | let fx = Fixture::new(Visibility::Public).await; |
| 1834 | 1751 | owner_session(&fx).await; |
| 1835 | 1752 | let bob = sign_in(&fx, "bob", "bob-token").await; |
| 1753 | + let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap(); | |
| 1836 | 1754 | let number = open_pull(&fx).await; |
| 1837 | 1755 | 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 | + | |
| 1838 | 1781 | fx.store |
| 1839 | 1782 | .add_review_comment( |
| 1840 | 1783 | &pr.issue.id, |
| @@ -1849,11 +1792,8 @@ mod tests { | ||
| 1849 | 1792 | ) |
| 1850 | 1793 | .await |
| 1851 | 1794 | .unwrap(); |
| 1852 | - | |
| 1853 | - let uri = format!("/tahinli/demo/pulls/{number}"); | |
| 1854 | 1795 | 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}"); | |
| 1858 | 1798 | } |
| 1859 | 1799 | } |