Page tests share their fixtures and merge into tables
Browse files at this commit7cded1477fc6dbc239f4d8a92c4c5bd068f1773cParent: 6efb476- test
- page tests share their fixtures and merge into tables
5 changed files +454 -421
- crates/forge-web/src/pages/discussion_live_tests.rs +26 -27
- crates/forge-web/src/pages/edit_history.rs +145 -124
- crates/forge-web/src/pages/issue_pin_tests.rs +75 -72
- crates/forge-web/src/pages/labels_page.rs +28 -45
- crates/forge-web/src/pages/login.rs +180 -153
crates/forge-web/src/pages/discussion_live_tests.rsmodified+26 -27
| @@ -122,10 +122,14 @@ async fn seeded(visibility: Visibility) -> Seed { | ||
| 122 | 122 | } |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | +/// Writes on a public repo bump the topics they reach. Each write gets fresh subscriptions opened | |
| 126 | +/// after the earlier writes, so a receiver only sees the changes made after it was opened. | |
| 125 | 127 | #[tokio::test] |
| 126 | -async fn a_comment_bumps_the_list_and_the_discussion_only() { | |
| 128 | +async fn writes_bump_the_topics_they_reach() { | |
| 127 | 129 | let seed = seeded(Visibility::Public).await; |
| 128 | 130 | let site = &seed.site; |
| 131 | + | |
| 132 | + // A comment bumps the list and its discussion, and not an unrelated discussion. | |
| 129 | 133 | let list = site |
| 130 | 134 | .live |
| 131 | 135 | .subscribe(&Topic::Discussions(seed.repo_id.clone())); |
| @@ -133,7 +137,6 @@ async fn a_comment_bumps_the_list_and_the_discussion_only() { | ||
| 133 | 137 | .live |
| 134 | 138 | .subscribe(&Topic::Discussion(seed.discussion_id.clone())); |
| 135 | 139 | let other = site.live.subscribe(&Topic::Discussion("other".to_owned())); |
| 136 | - | |
| 137 | 140 | let status = post( |
| 138 | 141 | site, |
| 139 | 142 | "/tahinli/forge/discussions/1/comments", |
| @@ -141,31 +144,31 @@ async fn a_comment_bumps_the_list_and_the_discussion_only() { | ||
| 141 | 144 | &[("csrf", CSRF), ("body", "A reply")], |
| 142 | 145 | ) |
| 143 | 146 | .await; |
| 144 | - | |
| 145 | - assert_ne!(status, 404, "the comment was refused"); | |
| 147 | + assert_ne!(status, 404, "comment: the comment was refused"); | |
| 146 | 148 | let comments = site |
| 147 | 149 | .store |
| 148 | 150 | .list_discussion_comments(&seed.discussion_id) |
| 149 | 151 | .await |
| 150 | 152 | .expect("comments"); |
| 151 | - assert_eq!(comments.len(), 1, "the comment was stored"); | |
| 152 | - assert!(list.has_changed().expect("open"), "the list was bumped"); | |
| 153 | + assert_eq!(comments.len(), 1, "comment: the comment was stored"); | |
| 154 | + assert!( | |
| 155 | + list.has_changed().expect("open"), | |
| 156 | + "comment: the list was bumped" | |
| 157 | + ); | |
| 153 | 158 | assert!( |
| 154 | 159 | item.has_changed().expect("open"), |
| 155 | - "the discussion was bumped" | |
| 160 | + "comment: the discussion was bumped" | |
| 161 | + ); | |
| 162 | + assert!( | |
| 163 | + !other.has_changed().expect("open"), | |
| 164 | + "comment: an unrelated discussion was not bumped" | |
| 156 | 165 | ); |
| 157 | - assert!(!other.has_changed().expect("open")); | |
| 158 | -} | |
| 159 | 166 | |
| 160 | -#[tokio::test] | |
| 161 | -async fn a_new_discussion_bumps_the_list() { | |
| 162 | - let seed = seeded(Visibility::Public).await; | |
| 163 | - let site = &seed.site; | |
| 167 | + // A new discussion bumps the list. | |
| 164 | 168 | let list = site |
| 165 | 169 | .live |
| 166 | 170 | .subscribe(&Topic::Discussions(seed.repo_id.clone())); |
| 167 | 171 | let category = DiscussionCategory::General.as_str(); |
| 168 | - | |
| 169 | 172 | let status = post( |
| 170 | 173 | site, |
| 171 | 174 | "/tahinli/forge/discussions/new", |
| @@ -178,20 +181,17 @@ async fn a_new_discussion_bumps_the_list() { | ||
| 178 | 181 | ], |
| 179 | 182 | ) |
| 180 | 183 | .await; |
| 184 | + assert_ne!(status, 404, "new discussion: the discussion was refused"); | |
| 185 | + assert!( | |
| 186 | + list.has_changed().expect("open"), | |
| 187 | + "new discussion: the list was bumped" | |
| 188 | + ); | |
| 181 | 189 | |
| 182 | - assert_ne!(status, 404, "the discussion was refused"); | |
| 183 | - assert!(list.has_changed().expect("open"), "the list was bumped"); | |
| 184 | -} | |
| 185 | - | |
| 186 | -#[tokio::test] | |
| 187 | -async fn a_refused_write_still_bumps_so_the_page_reads_the_truth() { | |
| 188 | - let seed = seeded(Visibility::Public).await; | |
| 189 | - let site = &seed.site; | |
| 190 | + // A refused write still bumps, so the page reads the truth. A blank comment is refused by the | |
| 191 | + // handler; the write layer bumps after the handler either way. | |
| 190 | 192 | let item = site |
| 191 | 193 | .live |
| 192 | 194 | .subscribe(&Topic::Discussion(seed.discussion_id.clone())); |
| 193 | - | |
| 194 | - // A blank comment is refused by the handler. The write layer bumps after the handler either way. | |
| 195 | 195 | let status = post( |
| 196 | 196 | site, |
| 197 | 197 | "/tahinli/forge/discussions/1/comments", |
| @@ -199,11 +199,10 @@ async fn a_refused_write_still_bumps_so_the_page_reads_the_truth() { | ||
| 199 | 199 | &[("csrf", CSRF), ("body", "")], |
| 200 | 200 | ) |
| 201 | 201 | .await; |
| 202 | - | |
| 203 | - assert_ne!(status, 404, "the discussion page answered"); | |
| 202 | + assert_ne!(status, 404, "refused write: the discussion page answered"); | |
| 204 | 203 | assert!( |
| 205 | 204 | item.has_changed().expect("open"), |
| 206 | - "the discussion was bumped" | |
| 205 | + "refused write: the discussion was bumped" | |
| 207 | 206 | ); |
| 208 | 207 | } |
| 209 | 208 |
crates/forge-web/src/pages/edit_history.rsmodified+145 -124
| @@ -370,11 +370,15 @@ mod tests { | ||
| 370 | 370 | std::iter::repeat_n(c, 40).collect() |
| 371 | 371 | } |
| 372 | 372 | |
| 373 | + /// Every scenario on the public repo shares one fixture. Each makes its own issue or pull and | |
| 374 | + /// reads its numbers from the store, so no scenario depends on another's data. | |
| 373 | 375 | #[tokio::test] |
| 374 | - async fn comment_edited_twice_shows_marker_and_diffs() { | |
| 376 | + async fn public_histories_show_diffs_markers_and_refusals() { | |
| 375 | 377 | let fx = Fixture::new(Visibility::Public).await; |
| 376 | 378 | let owner = tahinli(&fx).await; |
| 377 | 379 | let id = repo_id(&fx).await; |
| 380 | + | |
| 381 | + // A comment edited twice: a marker on the item page and two versions with diffs. | |
| 378 | 382 | let issue = fx |
| 379 | 383 | .store |
| 380 | 384 | .create_issue(&id, &owner.id, "bug", "body") |
| @@ -393,170 +397,154 @@ mod tests { | ||
| 393 | 397 | .edit_comment(&comment.id, &owner.id, "one\nTWO\nthree\nfour") |
| 394 | 398 | .await |
| 395 | 399 | .unwrap(); |
| 396 | - | |
| 397 | - let (status, page) = send(&fx.router, "/tahinli/demo/issues/1", None).await; | |
| 398 | - assert_eq!(status, 200); | |
| 399 | - let link = format!("/tahinli/demo/issues/1/history/comment/{}", comment.id); | |
| 400 | - assert!(page.contains("issue-edited"), "{page}"); | |
| 401 | - assert!(page.contains(&format!("href=\"{link}\"")), "{page}"); | |
| 400 | + let item = format!("/tahinli/demo/issues/{}", issue.number); | |
| 401 | + let link = format!("{item}/history/comment/{}", comment.id); | |
| 402 | + | |
| 403 | + let (status, page) = send(&fx.router, &item, None).await; | |
| 404 | + assert_eq!(status, 200, "edited comment: item page"); | |
| 405 | + assert!( | |
| 406 | + page.contains("issue-edited"), | |
| 407 | + "edited comment: marker {page}" | |
| 408 | + ); | |
| 409 | + assert!( | |
| 410 | + page.contains(&format!("href=\"{link}\"")), | |
| 411 | + "edited comment: link {page}" | |
| 412 | + ); | |
| 402 | 413 | |
| 403 | 414 | let (status, history) = send(&fx.router, &link, None).await; |
| 404 | - assert_eq!(status, 200); | |
| 405 | - assert_eq!(history.matches("<article").count(), 2, "{history}"); | |
| 406 | - assert!(history.contains("diff-line del"), "{history}"); | |
| 407 | - assert!(history.contains("-two"), "{history}"); | |
| 408 | - assert!(history.contains("+TWO"), "{history}"); | |
| 409 | - assert!(history.contains("+four"), "{history}"); | |
| 410 | - assert!(history.contains("tahinli"), "{history}"); | |
| 411 | - } | |
| 415 | + assert_eq!(status, 200, "edited comment: history"); | |
| 416 | + assert_eq!( | |
| 417 | + history.matches("<article").count(), | |
| 418 | + 2, | |
| 419 | + "edited comment: versions {history}" | |
| 420 | + ); | |
| 421 | + assert!( | |
| 422 | + history.contains("diff-line del"), | |
| 423 | + "edited comment: del {history}" | |
| 424 | + ); | |
| 425 | + assert!(history.contains("-two"), "edited comment: -two {history}"); | |
| 426 | + assert!(history.contains("+TWO"), "edited comment: +TWO {history}"); | |
| 427 | + assert!(history.contains("+four"), "edited comment: +four {history}"); | |
| 428 | + assert!( | |
| 429 | + history.contains("tahinli"), | |
| 430 | + "edited comment: editor {history}" | |
| 431 | + ); | |
| 412 | 432 | |
| 413 | - #[tokio::test] | |
| 414 | - async fn unedited_comment_has_no_marker() { | |
| 415 | - let fx = Fixture::new(Visibility::Public).await; | |
| 416 | - let owner = tahinli(&fx).await; | |
| 417 | - let id = repo_id(&fx).await; | |
| 418 | - let issue = fx | |
| 433 | + // A comment that was never edited has no marker. | |
| 434 | + let plain = fx | |
| 419 | 435 | .store |
| 420 | - .create_issue(&id, &owner.id, "bug", "body") | |
| 436 | + .create_issue(&id, &owner.id, "plain", "body") | |
| 421 | 437 | .await |
| 422 | 438 | .unwrap(); |
| 423 | 439 | fx.store |
| 424 | - .add_comment(&issue.id, &owner.id, "plain") | |
| 440 | + .add_comment(&plain.id, &owner.id, "plain") | |
| 425 | 441 | .await |
| 426 | 442 | .unwrap(); |
| 427 | - let (status, page) = send(&fx.router, "/tahinli/demo/issues/1", None).await; | |
| 428 | - assert_eq!(status, 200); | |
| 429 | - assert!(!page.contains("issue-edited"), "{page}"); | |
| 430 | - } | |
| 443 | + let (status, page) = send( | |
| 444 | + &fx.router, | |
| 445 | + &format!("/tahinli/demo/issues/{}", plain.number), | |
| 446 | + None, | |
| 447 | + ) | |
| 448 | + .await; | |
| 449 | + assert_eq!(status, 200, "unedited comment: item page"); | |
| 450 | + assert!( | |
| 451 | + !page.contains("issue-edited"), | |
| 452 | + "unedited comment: no marker {page}" | |
| 453 | + ); | |
| 431 | 454 | |
| 432 | - #[tokio::test] | |
| 433 | - async fn body_edit_history_is_listed_and_linked() { | |
| 434 | - let fx = Fixture::new(Visibility::Public).await; | |
| 435 | - let owner = tahinli(&fx).await; | |
| 436 | - let id = repo_id(&fx).await; | |
| 437 | - let issue = fx | |
| 455 | + // An issue body and title edit: the old title and the changed lines show. | |
| 456 | + let titled = fx | |
| 438 | 457 | .store |
| 439 | 458 | .create_issue(&id, &owner.id, "old title", "a\nb\nc") |
| 440 | 459 | .await |
| 441 | 460 | .unwrap(); |
| 442 | 461 | fx.store |
| 443 | - .edit_issue(&issue.id, &owner.id, "new title", "a\nB\nc") | |
| 462 | + .edit_issue(&titled.id, &owner.id, "new title", "a\nB\nc") | |
| 444 | 463 | .await |
| 445 | 464 | .unwrap(); |
| 446 | - | |
| 447 | - let link = format!("/tahinli/demo/issues/1/history/issue/{}", issue.id); | |
| 448 | - let (status, page) = send(&fx.router, "/tahinli/demo/issues/1", None).await; | |
| 449 | - assert_eq!(status, 200); | |
| 450 | - assert!(page.contains(&format!("href=\"{link}\"")), "{page}"); | |
| 451 | - | |
| 465 | + let link = format!( | |
| 466 | + "/tahinli/demo/issues/{}/history/issue/{}", | |
| 467 | + titled.number, titled.id | |
| 468 | + ); | |
| 469 | + let (status, page) = send( | |
| 470 | + &fx.router, | |
| 471 | + &format!("/tahinli/demo/issues/{}", titled.number), | |
| 472 | + None, | |
| 473 | + ) | |
| 474 | + .await; | |
| 475 | + assert_eq!(status, 200, "body edit: item page"); | |
| 476 | + assert!( | |
| 477 | + page.contains(&format!("href=\"{link}\"")), | |
| 478 | + "body edit: link {page}" | |
| 479 | + ); | |
| 452 | 480 | let (status, history) = send(&fx.router, &link, None).await; |
| 453 | - assert_eq!(status, 200); | |
| 454 | - assert!(history.contains("Title: old title"), "{history}"); | |
| 455 | - assert!(history.contains("-b"), "{history}"); | |
| 456 | - assert!(history.contains("+B"), "{history}"); | |
| 457 | - } | |
| 481 | + assert_eq!(status, 200, "body edit: history"); | |
| 482 | + assert!( | |
| 483 | + history.contains("Title: old title"), | |
| 484 | + "body edit: title {history}" | |
| 485 | + ); | |
| 486 | + assert!(history.contains("-b"), "body edit: -b {history}"); | |
| 487 | + assert!(history.contains("+B"), "body edit: +B {history}"); | |
| 458 | 488 | |
| 459 | - #[tokio::test] | |
| 460 | - async fn target_from_another_issue_is_not_found() { | |
| 461 | - let fx = Fixture::new(Visibility::Public).await; | |
| 462 | - let owner = tahinli(&fx).await; | |
| 463 | - let id = repo_id(&fx).await; | |
| 464 | - fx.store | |
| 465 | - .create_issue(&id, &owner.id, "first", "body") | |
| 466 | - .await | |
| 467 | - .unwrap(); | |
| 489 | + // A comment on one issue is not found under another issue's URL. | |
| 468 | 490 | let other = fx |
| 469 | 491 | .store |
| 470 | 492 | .create_issue(&id, &owner.id, "second", "body") |
| 471 | 493 | .await |
| 472 | 494 | .unwrap(); |
| 473 | - let comment = fx | |
| 495 | + let other_comment = fx | |
| 474 | 496 | .store |
| 475 | 497 | .add_comment(&other.id, &owner.id, "x") |
| 476 | 498 | .await |
| 477 | 499 | .unwrap(); |
| 478 | 500 | fx.store |
| 479 | - .edit_comment(&comment.id, &owner.id, "y") | |
| 501 | + .edit_comment(&other_comment.id, &owner.id, "y") | |
| 480 | 502 | .await |
| 481 | 503 | .unwrap(); |
| 482 | - | |
| 483 | 504 | let (status, _) = send( |
| 484 | 505 | &fx.router, |
| 485 | - &format!("/tahinli/demo/issues/1/history/comment/{}", comment.id), | |
| 506 | + &format!( | |
| 507 | + "/tahinli/demo/issues/{}/history/comment/{}", | |
| 508 | + issue.number, other_comment.id | |
| 509 | + ), | |
| 486 | 510 | None, |
| 487 | 511 | ) |
| 488 | 512 | .await; |
| 489 | - assert_eq!(status, 404); | |
| 513 | + assert_eq!(status, 404, "wrong item: not found"); | |
| 490 | 514 | let (status, _) = send( |
| 491 | 515 | &fx.router, |
| 492 | - &format!("/tahinli/demo/issues/2/history/comment/{}", comment.id), | |
| 516 | + &format!( | |
| 517 | + "/tahinli/demo/issues/{}/history/comment/{}", | |
| 518 | + other.number, other_comment.id | |
| 519 | + ), | |
| 493 | 520 | None, |
| 494 | 521 | ) |
| 495 | 522 | .await; |
| 496 | - assert_eq!(status, 200); | |
| 497 | - } | |
| 523 | + assert_eq!(status, 200, "right item: found"); | |
| 498 | 524 | |
| 499 | - #[tokio::test] | |
| 500 | - async fn unknown_kind_and_missing_target_are_not_found() { | |
| 501 | - let fx = Fixture::new(Visibility::Public).await; | |
| 502 | - let owner = tahinli(&fx).await; | |
| 503 | - let id = repo_id(&fx).await; | |
| 504 | - let issue = fx | |
| 505 | - .store | |
| 506 | - .create_issue(&id, &owner.id, "bug", "body") | |
| 507 | - .await | |
| 508 | - .unwrap(); | |
| 509 | - let (status, _) = send( | |
| 510 | - &fx.router, | |
| 511 | - &format!("/tahinli/demo/issues/1/history/label/{}", issue.id), | |
| 512 | - None, | |
| 513 | - ) | |
| 514 | - .await; | |
| 515 | - assert_eq!(status, 404); | |
| 525 | + // An unknown kind and a missing target are both not found. | |
| 516 | 526 | let (status, _) = send( |
| 517 | 527 | &fx.router, |
| 518 | - "/tahinli/demo/issues/1/history/comment/missing", | |
| 528 | + &format!( | |
| 529 | + "/tahinli/demo/issues/{}/history/label/{}", | |
| 530 | + issue.number, issue.id | |
| 531 | + ), | |
| 519 | 532 | None, |
| 520 | 533 | ) |
| 521 | 534 | .await; |
| 522 | - assert_eq!(status, 404); | |
| 523 | - } | |
| 524 | - | |
| 525 | - #[tokio::test] | |
| 526 | - async fn private_repo_is_not_found_for_anonymous() { | |
| 527 | - let fx = Fixture::new(Visibility::Private).await; | |
| 528 | - let owner = tahinli(&fx).await; | |
| 529 | - let id = repo_id(&fx).await; | |
| 530 | - let issue = fx | |
| 531 | - .store | |
| 532 | - .create_issue(&id, &owner.id, "secret", "body") | |
| 533 | - .await | |
| 534 | - .unwrap(); | |
| 535 | - fx.store | |
| 536 | - .edit_issue(&issue.id, &owner.id, "secret", "body 2") | |
| 537 | - .await | |
| 538 | - .unwrap(); | |
| 535 | + assert_eq!(status, 404, "unknown kind"); | |
| 539 | 536 | let (status, _) = send( |
| 540 | 537 | &fx.router, |
| 541 | - &format!("/tahinli/demo/issues/1/history/issue/{}", issue.id), | |
| 538 | + &format!( | |
| 539 | + "/tahinli/demo/issues/{}/history/comment/missing", | |
| 540 | + issue.number | |
| 541 | + ), | |
| 542 | 542 | None, |
| 543 | 543 | ) |
| 544 | 544 | .await; |
| 545 | - assert_eq!(status, 404); | |
| 546 | - let (status, _) = send( | |
| 547 | - &fx.router, | |
| 548 | - &format!("/tahinli/demo/issues/1/history/issue/{}", issue.id), | |
| 549 | - Some("tahinli"), | |
| 550 | - ) | |
| 551 | - .await; | |
| 552 | - assert_eq!(status, 200); | |
| 553 | - } | |
| 545 | + assert_eq!(status, 404, "missing target"); | |
| 554 | 546 | |
| 555 | - #[tokio::test] | |
| 556 | - async fn review_comment_history_on_a_pull_request() { | |
| 557 | - let fx = Fixture::new(Visibility::Public).await; | |
| 558 | - let owner = tahinli(&fx).await; | |
| 559 | - let id = repo_id(&fx).await; | |
| 547 | + // Review comment history on a pull request, and not under the issue URL of the same number. | |
| 560 | 548 | let pr = fx |
| 561 | 549 | .store |
| 562 | 550 | .create_pull( |
| @@ -572,7 +560,7 @@ mod tests { | ||
| 572 | 560 | ) |
| 573 | 561 | .await |
| 574 | 562 | .unwrap(); |
| 575 | - let comment = fx | |
| 563 | + let review = fx | |
| 576 | 564 | .store |
| 577 | 565 | .add_review_comment( |
| 578 | 566 | &pr.issue.id, |
| @@ -588,29 +576,62 @@ mod tests { | ||
| 588 | 576 | .await |
| 589 | 577 | .unwrap(); |
| 590 | 578 | fx.store |
| 591 | - .edit_review_comment(&comment.id, "second") | |
| 579 | + .edit_review_comment(&review.id, "second") | |
| 592 | 580 | .await |
| 593 | 581 | .unwrap(); |
| 594 | - | |
| 595 | 582 | let link = format!( |
| 596 | - "/tahinli/demo/pulls/1/history/review_comment/{}", | |
| 597 | - comment.id | |
| 583 | + "/tahinli/demo/pulls/{}/history/review_comment/{}", | |
| 584 | + pr.issue.number, review.id | |
| 598 | 585 | ); |
| 599 | 586 | let (status, history) = send(&fx.router, &link, None).await; |
| 600 | - assert_eq!(status, 200); | |
| 601 | - assert!(history.contains("-first"), "{history}"); | |
| 602 | - assert!(history.contains("+second"), "{history}"); | |
| 603 | - | |
| 604 | - // The same comment under the issue URL is not on that item. | |
| 587 | + assert_eq!(status, 200, "review comment: history"); | |
| 588 | + assert!( | |
| 589 | + history.contains("-first"), | |
| 590 | + "review comment: -first {history}" | |
| 591 | + ); | |
| 592 | + assert!( | |
| 593 | + history.contains("+second"), | |
| 594 | + "review comment: +second {history}" | |
| 595 | + ); | |
| 605 | 596 | let (status, _) = send( |
| 606 | 597 | &fx.router, |
| 607 | 598 | &format!( |
| 608 | - "/tahinli/demo/issues/1/history/review_comment/{}", | |
| 609 | - comment.id | |
| 599 | + "/tahinli/demo/issues/{}/history/review_comment/{}", | |
| 600 | + pr.issue.number, review.id | |
| 610 | 601 | ), |
| 611 | 602 | None, |
| 612 | 603 | ) |
| 613 | 604 | .await; |
| 605 | + assert_eq!(status, 404, "review comment under the issue URL"); | |
| 606 | + } | |
| 607 | + | |
| 608 | + #[tokio::test] | |
| 609 | + async fn private_repo_is_not_found_for_anonymous() { | |
| 610 | + let fx = Fixture::new(Visibility::Private).await; | |
| 611 | + let owner = tahinli(&fx).await; | |
| 612 | + let id = repo_id(&fx).await; | |
| 613 | + let issue = fx | |
| 614 | + .store | |
| 615 | + .create_issue(&id, &owner.id, "secret", "body") | |
| 616 | + .await | |
| 617 | + .unwrap(); | |
| 618 | + fx.store | |
| 619 | + .edit_issue(&issue.id, &owner.id, "secret", "body 2") | |
| 620 | + .await | |
| 621 | + .unwrap(); | |
| 622 | + let (status, _) = send( | |
| 623 | + &fx.router, | |
| 624 | + &format!("/tahinli/demo/issues/1/history/issue/{}", issue.id), | |
| 625 | + None, | |
| 626 | + ) | |
| 627 | + .await; | |
| 614 | 628 | assert_eq!(status, 404); |
| 629 | + let (status, _) = send( | |
| 630 | + &fx.router, | |
| 631 | + &format!("/tahinli/demo/issues/1/history/issue/{}", issue.id), | |
| 632 | + Some("tahinli"), | |
| 633 | + ) | |
| 634 | + .await; | |
| 635 | + assert_eq!(status, 200); | |
| 615 | 636 | } |
| 616 | 637 | } |
crates/forge-web/src/pages/issue_pin_tests.rsmodified+75 -72
| @@ -149,12 +149,18 @@ fn pin_uri(number: i64) -> String { | ||
| 149 | 149 | format!("{PIN_PATH}{number}/pin") |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | +/// Write rules on one repo, run in order because the pin count only grows: triage pins and unpins | |
| 153 | +/// (the timeline records both); a read-only collaborator and an anonymous viewer are refused; a | |
| 154 | +/// missing CSRF token changes nothing; a bad value is 400 and an unknown issue is 404; and the | |
| 155 | +/// fourth pin is refused with the limit shown. | |
| 152 | 156 | #[tokio::test] |
| 153 | -async fn triage_pins_and_unpins_and_records_the_timeline() { | |
| 157 | +async fn pin_writes_follow_the_rules_in_order() { | |
| 154 | 158 | let s = seeded().await; |
| 159 | + let store = &s.site.store; | |
| 155 | 160 | let (issue_id, number) = s.issues[0].clone(); |
| 156 | 161 | let uri = pin_uri(number); |
| 157 | 162 | |
| 163 | + // Triage pins, then unpins. | |
| 158 | 164 | let reply = call( |
| 159 | 165 | &s.site, |
| 160 | 166 | "POST", |
| @@ -163,20 +169,21 @@ async fn triage_pins_and_unpins_and_records_the_timeline() { | ||
| 163 | 169 | Some(pin_fields("1").as_slice()), |
| 164 | 170 | ) |
| 165 | 171 | .await; |
| 166 | - assert_eq!(reply.status, 303); | |
| 172 | + assert_eq!(reply.status, 303, "triage pin"); | |
| 167 | 173 | assert_eq!( |
| 168 | 174 | reply.location.as_deref(), |
| 169 | - Some(format!("{PIN_PATH}{number}").as_str()) | |
| 175 | + Some(format!("{PIN_PATH}{number}").as_str()), | |
| 176 | + "triage pin redirect" | |
| 170 | 177 | ); |
| 171 | 178 | assert!( |
| 172 | - s.site | |
| 173 | - .store | |
| 179 | + store | |
| 174 | 180 | .is_pinned(&s.repo_id, &issue_id) |
| 175 | 181 | .await |
| 176 | - .expect("pinned") | |
| 182 | + .expect("pinned"), | |
| 183 | + "triage pin stored" | |
| 177 | 184 | ); |
| 178 | - let events = s.site.store.list_events(&issue_id).await.expect("events"); | |
| 179 | - assert!(events.iter().any(|e| e.kind == "pinned")); | |
| 185 | + let events = store.list_events(&issue_id).await.expect("events"); | |
| 186 | + assert!(events.iter().any(|e| e.kind == "pinned"), "pinned event"); | |
| 180 | 187 | |
| 181 | 188 | let reply = call( |
| 182 | 189 | &s.site, |
| @@ -186,24 +193,21 @@ async fn triage_pins_and_unpins_and_records_the_timeline() { | ||
| 186 | 193 | Some(pin_fields("0").as_slice()), |
| 187 | 194 | ) |
| 188 | 195 | .await; |
| 189 | - assert_eq!(reply.status, 303); | |
| 196 | + assert_eq!(reply.status, 303, "triage unpin"); | |
| 190 | 197 | assert!( |
| 191 | - !s.site | |
| 192 | - .store | |
| 198 | + !store | |
| 193 | 199 | .is_pinned(&s.repo_id, &issue_id) |
| 194 | 200 | .await |
| 195 | - .expect("pinned") | |
| 201 | + .expect("pinned"), | |
| 202 | + "triage unpin stored" | |
| 203 | + ); | |
| 204 | + let events = store.list_events(&issue_id).await.expect("events"); | |
| 205 | + assert!( | |
| 206 | + events.iter().any(|e| e.kind == "unpinned"), | |
| 207 | + "unpinned event" | |
| 196 | 208 | ); |
| 197 | - let events = s.site.store.list_events(&issue_id).await.expect("events"); | |
| 198 | - assert!(events.iter().any(|e| e.kind == "unpinned")); | |
| 199 | -} | |
| 200 | - | |
| 201 | -#[tokio::test] | |
| 202 | -async fn read_only_collaborator_and_anonymous_are_forbidden() { | |
| 203 | - let s = seeded().await; | |
| 204 | - let (issue_id, number) = s.issues[0].clone(); | |
| 205 | - let uri = pin_uri(number); | |
| 206 | 209 | |
| 210 | + // A read-only collaborator and an anonymous viewer are refused. | |
| 207 | 211 | let reply = call( |
| 208 | 212 | &s.site, |
| 209 | 213 | "POST", |
| @@ -212,7 +216,7 @@ async fn read_only_collaborator_and_anonymous_are_forbidden() { | ||
| 212 | 216 | Some(pin_fields("1").as_slice()), |
| 213 | 217 | ) |
| 214 | 218 | .await; |
| 215 | - assert_eq!(reply.status, 403); | |
| 219 | + assert_eq!(reply.status, 403, "read-only pin"); | |
| 216 | 220 | let reply = call( |
| 217 | 221 | &s.site, |
| 218 | 222 | "POST", |
| @@ -221,52 +225,48 @@ async fn read_only_collaborator_and_anonymous_are_forbidden() { | ||
| 221 | 225 | Some(pin_fields("1").as_slice()), |
| 222 | 226 | ) |
| 223 | 227 | .await; |
| 224 | - assert!(reply.status == 403 || reply.status == 303); | |
| 225 | 228 | assert!( |
| 226 | - !s.site | |
| 227 | - .store | |
| 229 | + reply.status == 403 || reply.status == 303, | |
| 230 | + "anonymous pin: {}", | |
| 231 | + reply.status | |
| 232 | + ); | |
| 233 | + assert!( | |
| 234 | + !store | |
| 228 | 235 | .is_pinned(&s.repo_id, &issue_id) |
| 229 | 236 | .await |
| 230 | - .expect("pinned") | |
| 237 | + .expect("pinned"), | |
| 238 | + "refused pins stored nothing" | |
| 231 | 239 | ); |
| 232 | -} | |
| 233 | 240 | |
| 234 | -#[tokio::test] | |
| 235 | -async fn missing_csrf_is_forbidden_and_changes_nothing() { | |
| 236 | - let s = seeded().await; | |
| 237 | - let (issue_id, number) = s.issues[0].clone(); | |
| 241 | + // A missing CSRF token is forbidden and changes nothing. | |
| 238 | 242 | let no_token: &[(&str, &str)] = &[("pinned", "1")]; |
| 239 | 243 | let reply = call( |
| 240 | 244 | &s.site, |
| 241 | 245 | "POST", |
| 242 | - &pin_uri(number), | |
| 246 | + &uri, | |
| 243 | 247 | Some(s.tahinli_cookie.as_str()), |
| 244 | 248 | Some(no_token), |
| 245 | 249 | ) |
| 246 | 250 | .await; |
| 247 | - assert_eq!(reply.status, 403); | |
| 251 | + assert_eq!(reply.status, 403, "missing csrf"); | |
| 248 | 252 | assert!( |
| 249 | - !s.site | |
| 250 | - .store | |
| 253 | + !store | |
| 251 | 254 | .is_pinned(&s.repo_id, &issue_id) |
| 252 | 255 | .await |
| 253 | - .expect("pinned") | |
| 256 | + .expect("pinned"), | |
| 257 | + "missing csrf stored nothing" | |
| 254 | 258 | ); |
| 255 | -} | |
| 256 | 259 | |
| 257 | -#[tokio::test] | |
| 258 | -async fn bad_pinned_value_is_rejected_and_unknown_issue_is_404() { | |
| 259 | - let s = seeded().await; | |
| 260 | - let (_, number) = s.issues[0].clone(); | |
| 260 | + // A bad pinned value is 400; an unknown issue is 404. | |
| 261 | 261 | let reply = call( |
| 262 | 262 | &s.site, |
| 263 | 263 | "POST", |
| 264 | - &pin_uri(number), | |
| 264 | + &uri, | |
| 265 | 265 | Some(s.carol_cookie.as_str()), |
| 266 | 266 | Some(&[("csrf", CSRF), ("pinned", "yes")][..]), |
| 267 | 267 | ) |
| 268 | 268 | .await; |
| 269 | - assert_eq!(reply.status, 400); | |
| 269 | + assert_eq!(reply.status, 400, "bad pinned value"); | |
| 270 | 270 | let reply = call( |
| 271 | 271 | &s.site, |
| 272 | 272 | "POST", |
| @@ -275,14 +275,11 @@ async fn bad_pinned_value_is_rejected_and_unknown_issue_is_404() { | ||
| 275 | 275 | Some(pin_fields("1").as_slice()), |
| 276 | 276 | ) |
| 277 | 277 | .await; |
| 278 | - assert_eq!(reply.status, 404); | |
| 279 | -} | |
| 278 | + assert_eq!(reply.status, 404, "unknown issue"); | |
| 280 | 279 | |
| 281 | -#[tokio::test] | |
| 282 | -async fn fourth_pin_shows_the_limit_and_pins_nothing() { | |
| 283 | - let s = seeded().await; | |
| 280 | + // Three pins fill the limit; the fourth is shown the limit and pins nothing. | |
| 284 | 281 | for (id, _) in &s.issues[..3] { |
| 285 | - s.site.store.pin_issue(&s.repo_id, id).await.expect("pin"); | |
| 282 | + store.pin_issue(&s.repo_id, id).await.expect("pin"); | |
| 286 | 283 | } |
| 287 | 284 | let (fourth_id, fourth_number) = s.issues[3].clone(); |
| 288 | 285 | let reply = call( |
| @@ -293,50 +290,56 @@ async fn fourth_pin_shows_the_limit_and_pins_nothing() { | ||
| 293 | 290 | Some(pin_fields("1").as_slice()), |
| 294 | 291 | ) |
| 295 | 292 | .await; |
| 296 | - assert_eq!(reply.status, 200); | |
| 297 | - assert!(reply.body.contains("already has 3 pinned issues")); | |
| 293 | + assert_eq!(reply.status, 200, "fourth pin"); | |
| 298 | 294 | assert!( |
| 299 | - !s.site | |
| 300 | - .store | |
| 295 | + reply.body.contains("already has 3 pinned issues"), | |
| 296 | + "fourth pin: limit shown" | |
| 297 | + ); | |
| 298 | + assert!( | |
| 299 | + !store | |
| 301 | 300 | .is_pinned(&s.repo_id, &fourth_id) |
| 302 | 301 | .await |
| 303 | - .expect("pinned") | |
| 302 | + .expect("pinned"), | |
| 303 | + "fourth pin stored nothing" | |
| 304 | 304 | ); |
| 305 | 305 | } |
| 306 | 306 | |
| 307 | +/// The read side on its own repo: the issue page offers pin to triage only, and the issue list | |
| 308 | +/// shows pinned cards above the list. | |
| 307 | 309 | #[tokio::test] |
| 308 | -async fn issue_list_shows_pinned_cards_above_the_list() { | |
| 310 | +async fn pin_controls_and_cards_show_for_the_right_viewers() { | |
| 309 | 311 | let s = seeded().await; |
| 310 | - let (_, number) = s.issues[1].clone(); | |
| 312 | + let (issue_id, number) = s.issues[1].clone(); | |
| 311 | 313 | s.site |
| 312 | 314 | .store |
| 313 | - .pin_issue(&s.repo_id, &s.issues[1].0) | |
| 315 | + .pin_issue(&s.repo_id, &issue_id) | |
| 314 | 316 | .await |
| 315 | 317 | .expect("pin"); |
| 318 | + | |
| 316 | 319 | let reply = call(&s.site, "GET", "/tahinli/forge/issues", None, None).await; |
| 317 | - assert_eq!(reply.status, 200); | |
| 318 | - assert!(reply.body.contains("pinned-card")); | |
| 320 | + assert_eq!(reply.status, 200, "issue list"); | |
| 321 | + assert!(reply.body.contains("pinned-card"), "pinned card shown"); | |
| 319 | 322 | assert!( |
| 320 | 323 | reply |
| 321 | 324 | .body |
| 322 | - .contains(&format!("/tahinli/forge/issues/{number}\"")) | |
| 325 | + .contains(&format!("/tahinli/forge/issues/{number}\"")), | |
| 326 | + "pinned card links the pinned issue" | |
| 323 | 327 | ); |
| 324 | 328 | let card_at = reply.body.find("pinned-card").expect("card"); |
| 325 | 329 | let list_at = reply.body.find("issue-list").expect("list"); |
| 326 | - assert!(card_at < list_at); | |
| 327 | -} | |
| 330 | + assert!(card_at < list_at, "pinned cards sit above the list"); | |
| 328 | 331 | |
| 329 | -#[tokio::test] | |
| 330 | -async fn issue_page_offers_pin_to_triage_only() { | |
| 331 | - let s = seeded().await; | |
| 332 | - let (_, number) = s.issues[0].clone(); | |
| 333 | - let page = format!("{PIN_PATH}{number}"); | |
| 332 | + let (_, first_number) = s.issues[0].clone(); | |
| 333 | + let page = format!("{PIN_PATH}{first_number}"); | |
| 334 | 334 | let reply = call(&s.site, "GET", &page, Some(s.carol_cookie.as_str()), None).await; |
| 335 | - assert_eq!(reply.status, 200); | |
| 336 | - assert!(reply.body.contains("Pin issue")); | |
| 337 | - assert!(reply.body.contains(&format!("{page}/pin"))); | |
| 335 | + assert_eq!(reply.status, 200, "triage issue page"); | |
| 336 | + assert!(reply.body.contains("Pin issue"), "triage sees pin"); | |
| 337 | + assert!( | |
| 338 | + reply.body.contains(&format!("{page}/pin")), | |
| 339 | + "triage pin form" | |
| 340 | + ); | |
| 338 | 341 | |
| 339 | 342 | let reply = call(&s.site, "GET", &page, Some(s.bob_cookie.as_str()), None).await; |
| 340 | - assert_eq!(reply.status, 200); | |
| 341 | - assert!(!reply.body.contains("Pin issue")); | |
| 343 | + assert_eq!(reply.status, 200, "read issue page"); | |
| 344 | + assert!(!reply.body.contains("Pin issue"), "read-only has no pin"); | |
| 342 | 345 | } |
crates/forge-web/src/pages/labels_page.rsmodified+28 -45
| @@ -423,51 +423,15 @@ mod tests { | ||
| 423 | 423 | .len() |
| 424 | 424 | } |
| 425 | 425 | |
| 426 | + /// One repo, run in order because the label count only grows. The button shows to triage | |
| 427 | + /// holders while there are no labels. A read-only viewer and a bad CSRF token add nothing. A | |
| 428 | + /// triage holder adds the defaults once, and a repeated submit adds nothing. | |
| 426 | 429 | #[tokio::test] |
| 427 | - async fn triage_adds_defaults_once_and_repeat_adds_nothing() { | |
| 430 | + async fn defaults_button_and_adding_follow_the_rules() { | |
| 428 | 431 | let site = site().await; |
| 429 | 432 | let url = "/tahinli/forge/labels/defaults"; |
| 430 | - let (status, _) = send(&site, "POST", url, Some(&site.carol), CSRF).await; | |
| 431 | - assert_eq!(status, 303); | |
| 432 | - assert_eq!(label_count(&site).await, 9); | |
| 433 | - let (status, _) = send(&site, "POST", url, Some(&site.carol), CSRF).await; | |
| 434 | - assert_eq!(status, 303); | |
| 435 | - assert_eq!(label_count(&site).await, 9); | |
| 436 | - } | |
| 437 | 433 | |
| 438 | - #[tokio::test] | |
| 439 | - async fn read_only_viewer_cannot_add_defaults() { | |
| 440 | - let site = site().await; | |
| 441 | - let (status, _) = send( | |
| 442 | - &site, | |
| 443 | - "POST", | |
| 444 | - "/tahinli/forge/labels/defaults", | |
| 445 | - Some(&site.bob), | |
| 446 | - CSRF, | |
| 447 | - ) | |
| 448 | - .await; | |
| 449 | - assert_eq!(status, 403); | |
| 450 | - assert_eq!(label_count(&site).await, 0); | |
| 451 | - } | |
| 452 | - | |
| 453 | - #[tokio::test] | |
| 454 | - async fn defaults_need_a_valid_csrf_token() { | |
| 455 | - let site = site().await; | |
| 456 | - let (status, _) = send( | |
| 457 | - &site, | |
| 458 | - "POST", | |
| 459 | - "/tahinli/forge/labels/defaults", | |
| 460 | - Some(&site.carol), | |
| 461 | - "wrong", | |
| 462 | - ) | |
| 463 | - .await; | |
| 464 | - assert_ne!(status, 303); | |
| 465 | - assert_eq!(label_count(&site).await, 0); | |
| 466 | - } | |
| 467 | - | |
| 468 | - #[tokio::test] | |
| 469 | - async fn button_shows_only_for_triage_when_no_labels() { | |
| 470 | - let site = site().await; | |
| 434 | + // No labels yet: triage sees the button, read-only does not. | |
| 471 | 435 | let (status, body) = send( |
| 472 | 436 | &site, |
| 473 | 437 | "GET", |
| @@ -476,11 +440,30 @@ mod tests { | ||
| 476 | 440 | CSRF, |
| 477 | 441 | ) |
| 478 | 442 | .await; |
| 479 | - assert_eq!(status, 200); | |
| 480 | - assert!(body.contains("/tahinli/forge/labels/defaults")); | |
| 443 | + assert_eq!(status, 200, "triage labels page"); | |
| 444 | + assert!(body.contains(url), "triage sees the defaults button"); | |
| 481 | 445 | let (status, body) = |
| 482 | 446 | send(&site, "GET", "/tahinli/forge/labels", Some(&site.bob), CSRF).await; |
| 483 | - assert_eq!(status, 200); | |
| 484 | - assert!(!body.contains("/labels/defaults")); | |
| 447 | + assert_eq!(status, 200, "read-only labels page"); | |
| 448 | + assert!( | |
| 449 | + !body.contains("/labels/defaults"), | |
| 450 | + "read-only has no button" | |
| 451 | + ); | |
| 452 | + | |
| 453 | + // A read-only viewer is refused; a bad CSRF token is refused. Neither adds labels. | |
| 454 | + let (status, _) = send(&site, "POST", url, Some(&site.bob), CSRF).await; | |
| 455 | + assert_eq!(status, 403, "read-only add"); | |
| 456 | + assert_eq!(label_count(&site).await, 0, "read-only add stored nothing"); | |
| 457 | + let (status, _) = send(&site, "POST", url, Some(&site.carol), "wrong").await; | |
| 458 | + assert_ne!(status, 303, "bad csrf"); | |
| 459 | + assert_eq!(label_count(&site).await, 0, "bad csrf stored nothing"); | |
| 460 | + | |
| 461 | + // Triage adds the defaults once; a repeat adds nothing. | |
| 462 | + let (status, _) = send(&site, "POST", url, Some(&site.carol), CSRF).await; | |
| 463 | + assert_eq!(status, 303, "first add"); | |
| 464 | + assert_eq!(label_count(&site).await, 9, "defaults added"); | |
| 465 | + let (status, _) = send(&site, "POST", url, Some(&site.carol), CSRF).await; | |
| 466 | + assert_eq!(status, 303, "repeat add"); | |
| 467 | + assert_eq!(label_count(&site).await, 9, "repeat added nothing"); | |
| 485 | 468 | } |
| 486 | 469 | } |
crates/forge-web/src/pages/login.rsmodified+180 -153
| @@ -493,27 +493,96 @@ mod tests { | ||
| 493 | 493 | tempfile::tempdir().expect("tempdir") |
| 494 | 494 | } |
| 495 | 495 | |
| 496 | + /// Password-step throttles on one app. The throttle counts per user and per address, so each | |
| 497 | + /// scenario uses its own address and username and cannot reach another scenario's limit. | |
| 496 | 498 | #[tokio::test] |
| 497 | - async fn eleventh_attempt_for_a_user_is_throttled_even_with_the_right_password() { | |
| 498 | - let tmp = scratch("user-limit"); | |
| 499 | + async fn password_throttles_are_per_user_and_per_address() { | |
| 500 | + let tmp = scratch("password-throttles"); | |
| 499 | 501 | let (router, state) = app(tmp.path()).await; |
| 500 | - with_user(&state, "tahinli").await; | |
| 502 | + for name in ["tahinli", "bob", "frank", "erin", "gus"] { | |
| 503 | + with_user(&state, name).await; | |
| 504 | + } | |
| 505 | + | |
| 506 | + // Ten wrong passwords lock the user, and the right password is then refused too. | |
| 501 | 507 | let from = peer(10); |
| 502 | 508 | let token = login_page(&router, from).await; |
| 503 | - | |
| 504 | 509 | for _ in 0..10 { |
| 505 | 510 | let (status, body) = |
| 506 | 511 | post_login(&router, from, (&token.0, &token.1), "tahinli", "wrong").await; |
| 507 | - assert_eq!(status, 200); | |
| 508 | - assert!(body.contains("invalid username or password")); | |
| 512 | + assert_eq!(status, 200, "user limit: wrong password"); | |
| 513 | + assert!( | |
| 514 | + body.contains("invalid username or password"), | |
| 515 | + "user limit: generic message" | |
| 516 | + ); | |
| 509 | 517 | } |
| 510 | 518 | let (status, body) = |
| 511 | 519 | post_login(&router, from, (&token.0, &token.1), "tahinli", PASSWORD).await; |
| 512 | - assert_eq!(status, 429); | |
| 513 | - assert!(body.contains("Too many attempts. Try again later.")); | |
| 514 | - assert!(!body.contains("invalid username or password")); | |
| 520 | + assert_eq!(status, 429, "user limit: right password while locked"); | |
| 521 | + assert!( | |
| 522 | + body.contains("Too many attempts. Try again later."), | |
| 523 | + "user limit: message" | |
| 524 | + ); | |
| 525 | + assert!( | |
| 526 | + !body.contains("invalid username or password"), | |
| 527 | + "user limit: not the generic message" | |
| 528 | + ); | |
| 529 | + | |
| 530 | + // Another user from an address that is already failing still logs in. | |
| 531 | + let from = peer(11); | |
| 532 | + let token = login_page(&router, from).await; | |
| 533 | + for _ in 0..10 { | |
| 534 | + post_login(&router, from, (&token.0, &token.1), "frank", "wrong").await; | |
| 535 | + } | |
| 536 | + let (status, _) = post_login(&router, from, (&token.0, &token.1), "bob", PASSWORD).await; | |
| 537 | + assert_eq!(status, 303, "other user from the same address logs in"); | |
| 538 | + | |
| 539 | + // A successful login resets the user's counter: 9 failures, a success, 9 more, a success. | |
| 540 | + let from = peer(12); | |
| 541 | + let token = login_page(&router, from).await; | |
| 542 | + for _ in 0..9 { | |
| 543 | + post_login(&router, from, (&token.0, &token.1), "erin", "wrong").await; | |
| 544 | + } | |
| 545 | + let (status, _) = post_login(&router, from, (&token.0, &token.1), "erin", PASSWORD).await; | |
| 546 | + assert_eq!(status, 303, "reset: first success"); | |
| 547 | + for _ in 0..9 { | |
| 548 | + post_login(&router, from, (&token.0, &token.1), "erin", "wrong").await; | |
| 549 | + } | |
| 550 | + let (status, _) = post_login(&router, from, (&token.0, &token.1), "erin", PASSWORD).await; | |
| 551 | + assert_eq!(status, 303, "reset: second success after the counter reset"); | |
| 552 | + | |
| 553 | + // The address limit applies across usernames: 30 failures for ghosts, then a 31st is refused. | |
| 554 | + let from = peer(13); | |
| 555 | + let token = login_page(&router, from).await; | |
| 556 | + for i in 0..30 { | |
| 557 | + let name = format!("ghost{i}"); | |
| 558 | + let (status, _) = post_login(&router, from, (&token.0, &token.1), &name, "x").await; | |
| 559 | + assert_eq!(status, 200, "address limit: attempt {i}"); | |
| 560 | + } | |
| 561 | + let (status, body) = | |
| 562 | + post_login(&router, from, (&token.0, &token.1), "ghost-new", "x").await; | |
| 563 | + assert_eq!(status, 429, "address limit: 31st attempt"); | |
| 564 | + assert!( | |
| 565 | + body.contains("Too many attempts. Try again later."), | |
| 566 | + "address limit: message" | |
| 567 | + ); | |
| 568 | + | |
| 569 | + // Known and unknown users get the same generic answer. The body echoes the typed username, | |
| 570 | + // so only the status and the generic message are compared. | |
| 571 | + let from = peer(14); | |
| 572 | + let token = login_page(&router, from).await; | |
| 573 | + for name in ["gus", "nobody"] { | |
| 574 | + let (status, body) = | |
| 575 | + post_login(&router, from, (&token.0, &token.1), name, "wrong").await; | |
| 576 | + assert_eq!(status, 200, "uniform: {name}"); | |
| 577 | + assert!( | |
| 578 | + body.contains("invalid username or password"), | |
| 579 | + "uniform: generic message for {name}" | |
| 580 | + ); | |
| 581 | + } | |
| 515 | 582 | } |
| 516 | 583 | |
| 584 | + /// Audit rows for failed and successful logins, without the password. This needs an empty | |
| 585 | + /// audit log, so it runs on its own app. | |
| 517 | 586 | #[tokio::test] |
| 518 | 587 | async fn failed_and_successful_logins_are_audited_without_the_password() { |
| 519 | 588 | use forge_core::{AuditAction, AuditFilter}; |
| @@ -572,79 +641,6 @@ mod tests { | ||
| 572 | 641 | } |
| 573 | 642 | } |
| 574 | 643 | |
| 575 | - #[tokio::test] | |
| 576 | - async fn other_users_from_the_same_ip_still_log_in() { | |
| 577 | - let tmp = scratch("other-user"); | |
| 578 | - let (router, state) = app(tmp.path()).await; | |
| 579 | - with_user(&state, "tahinli").await; | |
| 580 | - with_user(&state, "bob").await; | |
| 581 | - let from = peer(11); | |
| 582 | - let token = login_page(&router, from).await; | |
| 583 | - | |
| 584 | - for _ in 0..10 { | |
| 585 | - post_login(&router, from, (&token.0, &token.1), "tahinli", "wrong").await; | |
| 586 | - } | |
| 587 | - let (status, _) = post_login(&router, from, (&token.0, &token.1), "bob", PASSWORD).await; | |
| 588 | - assert_eq!(status, 303); | |
| 589 | - } | |
| 590 | - | |
| 591 | - #[tokio::test] | |
| 592 | - async fn successful_login_resets_the_user_counter() { | |
| 593 | - let tmp = scratch("reset"); | |
| 594 | - let (router, state) = app(tmp.path()).await; | |
| 595 | - with_user(&state, "tahinli").await; | |
| 596 | - let from = peer(12); | |
| 597 | - let token = login_page(&router, from).await; | |
| 598 | - | |
| 599 | - for _ in 0..9 { | |
| 600 | - post_login(&router, from, (&token.0, &token.1), "tahinli", "wrong").await; | |
| 601 | - } | |
| 602 | - let (status, _) = | |
| 603 | - post_login(&router, from, (&token.0, &token.1), "tahinli", PASSWORD).await; | |
| 604 | - assert_eq!(status, 303); | |
| 605 | - for _ in 0..9 { | |
| 606 | - post_login(&router, from, (&token.0, &token.1), "tahinli", "wrong").await; | |
| 607 | - } | |
| 608 | - let (status, _) = | |
| 609 | - post_login(&router, from, (&token.0, &token.1), "tahinli", PASSWORD).await; | |
| 610 | - assert_eq!(status, 303); | |
| 611 | - } | |
| 612 | - | |
| 613 | - #[tokio::test] | |
| 614 | - async fn ip_limit_applies_across_usernames() { | |
| 615 | - let tmp = scratch("ip-limit"); | |
| 616 | - let (router, _state) = app(tmp.path()).await; | |
| 617 | - let from = peer(13); | |
| 618 | - let token = login_page(&router, from).await; | |
| 619 | - | |
| 620 | - for i in 0..30 { | |
| 621 | - let name = format!("ghost{i}"); | |
| 622 | - let (status, _) = post_login(&router, from, (&token.0, &token.1), &name, "x").await; | |
| 623 | - assert_eq!(status, 200); | |
| 624 | - } | |
| 625 | - let (status, body) = | |
| 626 | - post_login(&router, from, (&token.0, &token.1), "ghost-new", "x").await; | |
| 627 | - assert_eq!(status, 429); | |
| 628 | - assert!(body.contains("Too many attempts. Try again later.")); | |
| 629 | - } | |
| 630 | - | |
| 631 | - #[tokio::test] | |
| 632 | - async fn unknown_and_known_users_get_the_same_response() { | |
| 633 | - let tmp = scratch("uniform"); | |
| 634 | - let (router, state) = app(tmp.path()).await; | |
| 635 | - with_user(&state, "tahinli").await; | |
| 636 | - let from = peer(14); | |
| 637 | - let token = login_page(&router, from).await; | |
| 638 | - | |
| 639 | - // The body echoes the typed username, so compare status and the generic message only. | |
| 640 | - for name in ["tahinli", "nobody"] { | |
| 641 | - let (status, body) = | |
| 642 | - post_login(&router, from, (&token.0, &token.1), name, "wrong").await; | |
| 643 | - assert_eq!(status, 200); | |
| 644 | - assert!(body.contains("invalid username or password")); | |
| 645 | - } | |
| 646 | - } | |
| 647 | - | |
| 648 | 644 | /// Reply parts the tests look at. |
| 649 | 645 | struct Reply { |
| 650 | 646 | status: u16, |
| @@ -751,20 +747,33 @@ mod tests { | ||
| 751 | 747 | format!("{pending}; {precsrf_cookie}") |
| 752 | 748 | } |
| 753 | 749 | |
| 750 | + /// The two-factor login flow on one app, with one user per scenario: the pending cookie and no | |
| 751 | + /// session after the password step; a page with no pending cookie goes to login; a wrong code | |
| 752 | + /// fails, the right code logs in, and a replay of that code fails; a recovery code works once. | |
| 754 | 753 | #[tokio::test] |
| 755 | - async fn totp_user_gets_a_pending_cookie_and_no_session() { | |
| 756 | - let tmp = scratch("totp-pending"); | |
| 754 | + async fn two_factor_login_steps_and_recovery_codes() { | |
| 755 | + let tmp = scratch("totp-flows"); | |
| 757 | 756 | let (router, state) = app(tmp.path()).await; |
| 758 | - totp_user(&state, "tahinli").await; | |
| 757 | + | |
| 758 | + // Pending cookie, no session, and the second-factor page with the pending cookie. | |
| 759 | + totp_user(&state, "ada").await; | |
| 759 | 760 | let from = peer(20); |
| 760 | 761 | let (cookie, precsrf) = login_page(&router, from).await; |
| 761 | - | |
| 762 | - let reply = password_step(&router, from, (&cookie, &precsrf), "tahinli", PASSWORD).await; | |
| 763 | - assert_eq!(reply.status, 303); | |
| 764 | - assert_eq!(reply.location.as_deref(), Some("/login/2fa")); | |
| 765 | - assert!(pair(&reply, "__Host-forge_2fa").is_some()); | |
| 766 | - assert!(pair(&reply, "__Host-forge_session").is_none()); | |
| 767 | - | |
| 762 | + let reply = password_step(&router, from, (&cookie, &precsrf), "ada", PASSWORD).await; | |
| 763 | + assert_eq!(reply.status, 303, "pending: password step"); | |
| 764 | + assert_eq!( | |
| 765 | + reply.location.as_deref(), | |
| 766 | + Some("/login/2fa"), | |
| 767 | + "pending: redirect" | |
| 768 | + ); | |
| 769 | + assert!( | |
| 770 | + pair(&reply, "__Host-forge_2fa").is_some(), | |
| 771 | + "pending: cookie set" | |
| 772 | + ); | |
| 773 | + assert!( | |
| 774 | + pair(&reply, "__Host-forge_session").is_none(), | |
| 775 | + "pending: no session yet" | |
| 776 | + ); | |
| 768 | 777 | let page = send( |
| 769 | 778 | &router, |
| 770 | 779 | Request::builder() |
| @@ -779,14 +788,10 @@ mod tests { | ||
| 779 | 788 | .unwrap(), |
| 780 | 789 | ) |
| 781 | 790 | .await; |
| 782 | - assert_eq!(page.status, 200); | |
| 783 | - assert!(page.body.contains("Two-factor check")); | |
| 784 | - } | |
| 791 | + assert_eq!(page.status, 200, "pending: second-factor page"); | |
| 792 | + assert!(page.body.contains("Two-factor check"), "pending: page text"); | |
| 785 | 793 | |
| 786 | - #[tokio::test] | |
| 787 | - async fn two_factor_page_without_pending_cookie_goes_to_login() { | |
| 788 | - let tmp = scratch("totp-no-pending"); | |
| 789 | - let (router, _state) = app(tmp.path()).await; | |
| 794 | + // The second-factor page without a pending cookie goes back to login. | |
| 790 | 795 | let reply = send( |
| 791 | 796 | &router, |
| 792 | 797 | Request::builder() |
| @@ -797,24 +802,26 @@ mod tests { | ||
| 797 | 802 | .unwrap(), |
| 798 | 803 | ) |
| 799 | 804 | .await; |
| 800 | - assert_eq!(reply.status, 303); | |
| 801 | - assert_eq!(reply.location.as_deref(), Some("/login")); | |
| 802 | - } | |
| 805 | + assert_eq!(reply.status, 303, "no pending: status"); | |
| 806 | + assert_eq!( | |
| 807 | + reply.location.as_deref(), | |
| 808 | + Some("/login"), | |
| 809 | + "no pending: redirect" | |
| 810 | + ); | |
| 803 | 811 | |
| 804 | - #[tokio::test] | |
| 805 | - async fn wrong_code_fails_right_code_logs_in_and_replay_fails() { | |
| 806 | - let tmp = scratch("totp-flow"); | |
| 807 | - let (router, state) = app(tmp.path()).await; | |
| 808 | - let shared = totp_user(&state, "tahinli").await; | |
| 812 | + // A wrong code fails, the right code logs in, and the same code again is a replay. | |
| 813 | + let shared = totp_user(&state, "grace").await; | |
| 809 | 814 | let from = peer(22); |
| 810 | 815 | let (cookie, precsrf) = login_page(&router, from).await; |
| 811 | - | |
| 812 | - let step = password_step(&router, from, (&cookie, &precsrf), "tahinli", PASSWORD).await; | |
| 816 | + let step = password_step(&router, from, (&cookie, &precsrf), "grace", PASSWORD).await; | |
| 813 | 817 | let pending = both(&pair(&step, "__Host-forge_2fa").unwrap(), &cookie); |
| 814 | 818 | let wrong = second_factor_step(&router, from, &pending, &precsrf, "000000").await; |
| 815 | 819 | // The chance that 000000 is the live code is about 1 in a million per window. |
| 816 | 820 | if wrong.status == 200 { |
| 817 | - assert!(wrong.body.contains("That code is not right")); | |
| 821 | + assert!( | |
| 822 | + wrong.body.contains("That code is not right"), | |
| 823 | + "wrong code: message" | |
| 824 | + ); | |
| 818 | 825 | } |
| 819 | 826 | |
| 820 | 827 | let now_step = totp::step_for( |
| @@ -825,59 +832,77 @@ mod tests { | ||
| 825 | 832 | ); |
| 826 | 833 | let code = totp::code_at(&shared, now_step); |
| 827 | 834 | let ok = second_factor_step(&router, from, &pending, &precsrf, &code).await; |
| 828 | - assert_eq!(ok.status, 303, "body {}", ok.body); | |
| 829 | - assert_eq!(ok.location.as_deref(), Some("/dash")); | |
| 830 | - assert!(pair(&ok, "__Host-forge_session").is_some()); | |
| 831 | - assert!(ok.cookies.iter().any(|c| c.starts_with("__Host-forge_2fa=") | |
| 832 | - && c.len() == "__Host-forge_2fa=".len())); | |
| 835 | + assert_eq!(ok.status, 303, "right code: body {}", ok.body); | |
| 836 | + assert_eq!( | |
| 837 | + ok.location.as_deref(), | |
| 838 | + Some("/dash"), | |
| 839 | + "right code: redirect" | |
| 840 | + ); | |
| 841 | + assert!( | |
| 842 | + pair(&ok, "__Host-forge_session").is_some(), | |
| 843 | + "right code: session" | |
| 844 | + ); | |
| 845 | + assert!( | |
| 846 | + ok.cookies.iter().any(|c| c.starts_with("__Host-forge_2fa=") | |
| 847 | + && c.len() == "__Host-forge_2fa=".len()), | |
| 848 | + "right code: pending cookie cleared" | |
| 849 | + ); | |
| 833 | 850 | |
| 834 | - // The same code again, from a fresh password step, is a replay and must fail. | |
| 835 | 851 | let (cookie2, precsrf2) = login_page(&router, from).await; |
| 836 | - let step2 = password_step(&router, from, (&cookie2, &precsrf2), "tahinli", PASSWORD).await; | |
| 852 | + let step2 = password_step(&router, from, (&cookie2, &precsrf2), "grace", PASSWORD).await; | |
| 837 | 853 | let pending2 = both(&pair(&step2, "__Host-forge_2fa").unwrap(), &cookie2); |
| 838 | 854 | let replay = second_factor_step(&router, from, &pending2, &precsrf2, &code).await; |
| 839 | - assert_eq!(replay.status, 200); | |
| 840 | - assert!(replay.body.contains("That code is not right")); | |
| 841 | - assert!(pair(&replay, "__Host-forge_session").is_none()); | |
| 842 | - } | |
| 855 | + assert_eq!(replay.status, 200, "replay: status"); | |
| 856 | + assert!( | |
| 857 | + replay.body.contains("That code is not right"), | |
| 858 | + "replay: message" | |
| 859 | + ); | |
| 860 | + assert!( | |
| 861 | + pair(&replay, "__Host-forge_session").is_none(), | |
| 862 | + "replay: no session" | |
| 863 | + ); | |
| 843 | 864 | |
| 844 | - #[tokio::test] | |
| 845 | - async fn recovery_code_works_once() { | |
| 846 | - let tmp = scratch("totp-recovery"); | |
| 847 | - let (router, state) = app(tmp.path()).await; | |
| 848 | - totp_user(&state, "tahinli").await; | |
| 865 | + // A recovery code works once. | |
| 866 | + totp_user(&state, "linus").await; | |
| 849 | 867 | let from = peer(23); |
| 850 | 868 | let (cookie, precsrf) = login_page(&router, from).await; |
| 851 | - | |
| 852 | - let step = password_step(&router, from, (&cookie, &precsrf), "tahinli", PASSWORD).await; | |
| 869 | + let step = password_step(&router, from, (&cookie, &precsrf), "linus", PASSWORD).await; | |
| 853 | 870 | let pending = both(&pair(&step, "__Host-forge_2fa").unwrap(), &cookie); |
| 854 | 871 | let first = second_factor_step(&router, from, &pending, &precsrf, "AAAA-bbbb").await; |
| 855 | - assert_eq!(first.status, 303); | |
| 856 | - assert!(pair(&first, "__Host-forge_session").is_some()); | |
| 872 | + assert_eq!(first.status, 303, "recovery: first use"); | |
| 873 | + assert!( | |
| 874 | + pair(&first, "__Host-forge_session").is_some(), | |
| 875 | + "recovery: session" | |
| 876 | + ); | |
| 857 | 877 | |
| 858 | 878 | let (cookie2, precsrf2) = login_page(&router, from).await; |
| 859 | - let step2 = password_step(&router, from, (&cookie2, &precsrf2), "tahinli", PASSWORD).await; | |
| 879 | + let step2 = password_step(&router, from, (&cookie2, &precsrf2), "linus", PASSWORD).await; | |
| 860 | 880 | let pending2 = both(&pair(&step2, "__Host-forge_2fa").unwrap(), &cookie2); |
| 861 | 881 | let second = second_factor_step(&router, from, &pending2, &precsrf2, "aaaabbbb").await; |
| 862 | - assert_eq!(second.status, 200); | |
| 863 | - assert!(pair(&second, "__Host-forge_session").is_none()); | |
| 882 | + assert_eq!(second.status, 200, "recovery: second use"); | |
| 883 | + assert!( | |
| 884 | + pair(&second, "__Host-forge_session").is_none(), | |
| 885 | + "recovery: no session" | |
| 886 | + ); | |
| 864 | 887 | } |
| 865 | 888 | |
| 889 | + /// Two-factor expiry and the per-user limit on one app, one user per scenario. | |
| 866 | 890 | #[tokio::test] |
| 867 | - async fn expired_pending_cookie_is_rejected() { | |
| 868 | - let tmp = scratch("totp-expired"); | |
| 891 | + async fn two_factor_expiry_and_per_user_limit() { | |
| 892 | + let tmp = scratch("totp-expiry-limit"); | |
| 869 | 893 | let (router, state) = app(tmp.path()).await; |
| 870 | - let shared = totp_user(&state, "tahinli").await; | |
| 894 | + | |
| 895 | + // A correctly sealed pending cookie whose expiry is in the past is rejected, even with a | |
| 896 | + // valid code. | |
| 897 | + let shared = totp_user(&state, "margaret").await; | |
| 871 | 898 | let user = state |
| 872 | 899 | .store |
| 873 | - .user_by_username("tahinli") | |
| 900 | + .user_by_username("margaret") | |
| 874 | 901 | .await |
| 875 | 902 | .unwrap() |
| 876 | 903 | .unwrap(); |
| 877 | 904 | let from = peer(24); |
| 878 | 905 | let (cookie, precsrf) = login_page(&router, from).await; |
| 879 | - | |
| 880 | - // A correctly sealed pending cookie whose expiry is in the past. | |
| 881 | 906 | let stale = secret::seal(&state.key, format!("1|{}|/", user.id).as_bytes()).unwrap(); |
| 882 | 907 | let pending = format!("__Host-forge_2fa={stale}"); |
| 883 | 908 | let code = totp::code_at( |
| @@ -891,24 +916,26 @@ mod tests { | ||
| 891 | 916 | ); |
| 892 | 917 | let reply = |
| 893 | 918 | second_factor_step(&router, from, &both(&pending, &cookie), &precsrf, &code).await; |
| 894 | - assert_eq!(reply.status, 303); | |
| 895 | - assert_eq!(reply.location.as_deref(), Some("/login")); | |
| 896 | - assert!(pair(&reply, "__Host-forge_session").is_none()); | |
| 897 | - } | |
| 919 | + assert_eq!(reply.status, 303, "expired: status"); | |
| 920 | + assert_eq!( | |
| 921 | + reply.location.as_deref(), | |
| 922 | + Some("/login"), | |
| 923 | + "expired: redirect" | |
| 924 | + ); | |
| 925 | + assert!( | |
| 926 | + pair(&reply, "__Host-forge_session").is_none(), | |
| 927 | + "expired: no session" | |
| 928 | + ); | |
| 898 | 929 | |
| 899 | - #[tokio::test] | |
| 900 | - async fn second_factor_attempts_are_throttled_per_user() { | |
| 901 | - let tmp = scratch("totp-throttle"); | |
| 902 | - let (router, state) = app(tmp.path()).await; | |
| 903 | - let shared = totp_user(&state, "tahinli").await; | |
| 930 | + // Wrong codes until the user limit; then even the right code is refused. | |
| 931 | + let shared = totp_user(&state, "ken").await; | |
| 904 | 932 | let from = peer(25); |
| 905 | 933 | let (cookie, precsrf) = login_page(&router, from).await; |
| 906 | - let step = password_step(&router, from, (&cookie, &precsrf), "tahinli", PASSWORD).await; | |
| 934 | + let step = password_step(&router, from, (&cookie, &precsrf), "ken", PASSWORD).await; | |
| 907 | 935 | let pending = both(&pair(&step, "__Host-forge_2fa").unwrap(), &cookie); |
| 908 | - | |
| 909 | 936 | for _ in 0..TWO_FACTOR_USER_LIMIT { |
| 910 | 937 | let reply = second_factor_step(&router, from, &pending, &precsrf, "111111").await; |
| 911 | - assert_eq!(reply.status, 200); | |
| 938 | + assert_eq!(reply.status, 200, "limit: wrong code"); | |
| 912 | 939 | } |
| 913 | 940 | let code = totp::code_at( |
| 914 | 941 | &shared, |
| @@ -920,7 +947,7 @@ mod tests { | ||
| 920 | 947 | ), |
| 921 | 948 | ); |
| 922 | 949 | let blocked = second_factor_step(&router, from, &pending, &precsrf, &code).await; |
| 923 | - assert_eq!(blocked.status, 429); | |
| 924 | - assert!(blocked.body.contains("Too many attempts")); | |
| 950 | + assert_eq!(blocked.status, 429, "limit: right code while locked"); | |
| 951 | + assert!(blocked.body.contains("Too many attempts"), "limit: message"); | |
| 925 | 952 | } |
| 926 | 953 | } |