forge
Explore
Log in

Don't you dare go Hollow

Page tests share their fixtures and merge into tables

Browse files at this commit

7cded1477fc6dbc239f4d8a92c4c5bd068f1773cAhmet Kaan GümüşParent: 6efb476
  • test
    • page tests share their fixtures and merge into tables
Show full message
✅ test: page tests share their fixtures and merge into tables

Per file, test count before -> after:
- login.rs: 12 -> 4 (password throttles on one app, audit kept pristine, two-factor flows on one app, two-factor expiry and limit on one app)
- issue_pin_tests.rs: 7 -> 2 (write rules in order on one repo, read views on one repo)
- edit_history.rs: 7 -> 2 (public scenarios share one fixture, private repo kept separate)
- discussion_live_tests.rs: 4 -> 2 (public writes share one seed, streams test kept)
- labels_page.rs: 4 -> 1 (defaults button and adding in order on one site)
- labels_css.rs: 4 -> 4 (unchanged, no shared fixture to merge)

Total: 38 -> 15. Every assertion is kept, with the scenario name in its message.
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 #[tokio::test]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 let seed = seeded(Visibility::Public).await;129 let seed = seeded(Visibility::Public).await;
128 let site = &seed.site;130 let site = &seed.site;
131+
132+ // A comment bumps the list and its discussion, and not an unrelated discussion.
129 let list = site133 let list = site
130 .live134 .live
131 .subscribe(&Topic::Discussions(seed.repo_id.clone()));135 .subscribe(&Topic::Discussions(seed.repo_id.clone()));
@@ -133,7 +137,6 @@ async fn a_comment_bumps_the_list_and_the_discussion_only() {
133 .live137 .live
134 .subscribe(&Topic::Discussion(seed.discussion_id.clone()));138 .subscribe(&Topic::Discussion(seed.discussion_id.clone()));
135 let other = site.live.subscribe(&Topic::Discussion("other".to_owned()));139 let other = site.live.subscribe(&Topic::Discussion("other".to_owned()));
136-
137 let status = post(140 let status = post(
138 site,141 site,
139 "/tahinli/forge/discussions/1/comments",142 "/tahinli/forge/discussions/1/comments",
@@ -141,31 +144,31 @@ async fn a_comment_bumps_the_list_and_the_discussion_only() {
141 &[("csrf", CSRF), ("body", "A reply")],144 &[("csrf", CSRF), ("body", "A reply")],
142 )145 )
143 .await;146 .await;
144-147+ assert_ne!(status, 404, "comment: the comment was refused");
145- assert_ne!(status, 404, "the comment was refused");
146 let comments = site148 let comments = site
147 .store149 .store
148 .list_discussion_comments(&seed.discussion_id)150 .list_discussion_comments(&seed.discussion_id)
149 .await151 .await
150 .expect("comments");152 .expect("comments");
151- assert_eq!(comments.len(), 1, "the comment was stored");153+ assert_eq!(comments.len(), 1, "comment: the comment was stored");
152- assert!(list.has_changed().expect("open"), "the list was bumped");154+ assert!(
155+ list.has_changed().expect("open"),
156+ "comment: the list was bumped"
157+ );
153 assert!(158 assert!(
154 item.has_changed().expect("open"),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]167+ // A new discussion bumps the list.
161-async fn a_new_discussion_bumps_the_list() {
162- let seed = seeded(Visibility::Public).await;
163- let site = &seed.site;
164 let list = site168 let list = site
165 .live169 .live
166 .subscribe(&Topic::Discussions(seed.repo_id.clone()));170 .subscribe(&Topic::Discussions(seed.repo_id.clone()));
167 let category = DiscussionCategory::General.as_str();171 let category = DiscussionCategory::General.as_str();
168-
169 let status = post(172 let status = post(
170 site,173 site,
171 "/tahinli/forge/discussions/new",174 "/tahinli/forge/discussions/new",
@@ -178,20 +181,17 @@ async fn a_new_discussion_bumps_the_list() {
178 ],181 ],
179 )182 )
180 .await;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");190+ // A refused write still bumps, so the page reads the truth. A blank comment is refused by the
183- assert!(list.has_changed().expect("open"), "the list was bumped");191+ // handler; the write layer bumps after the handler either way.
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 let item = site192 let item = site
191 .live193 .live
192 .subscribe(&Topic::Discussion(seed.discussion_id.clone()));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 let status = post(195 let status = post(
196 site,196 site,
197 "/tahinli/forge/discussions/1/comments",197 "/tahinli/forge/discussions/1/comments",
@@ -199,11 +199,10 @@ async fn a_refused_write_still_bumps_so_the_page_reads_the_truth() {
199 &[("csrf", CSRF), ("body", "")],199 &[("csrf", CSRF), ("body", "")],
200 )200 )
201 .await;201 .await;
202-202+ assert_ne!(status, 404, "refused write: the discussion page answered");
203- assert_ne!(status, 404, "the discussion page answered");
204 assert!(203 assert!(
205 item.has_changed().expect("open"),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 std::iter::repeat_n(c, 40).collect()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 #[tokio::test]375 #[tokio::test]
374- async fn comment_edited_twice_shows_marker_and_diffs() {376+ async fn public_histories_show_diffs_markers_and_refusals() {
375 let fx = Fixture::new(Visibility::Public).await;377 let fx = Fixture::new(Visibility::Public).await;
376 let owner = tahinli(&fx).await;378 let owner = tahinli(&fx).await;
377 let id = repo_id(&fx).await;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 let issue = fx382 let issue = fx
379 .store383 .store
380 .create_issue(&id, &owner.id, "bug", "body")384 .create_issue(&id, &owner.id, "bug", "body")
@@ -393,170 +397,154 @@ mod tests {
393 .edit_comment(&comment.id, &owner.id, "one\nTWO\nthree\nfour")397 .edit_comment(&comment.id, &owner.id, "one\nTWO\nthree\nfour")
394 .await398 .await
395 .unwrap();399 .unwrap();
396-400+ let item = format!("/tahinli/demo/issues/{}", issue.number);
397- let (status, page) = send(&fx.router, "/tahinli/demo/issues/1", None).await;401+ let link = format!("{item}/history/comment/{}", comment.id);
398- assert_eq!(status, 200);402+
399- let link = format!("/tahinli/demo/issues/1/history/comment/{}", comment.id);403+ let (status, page) = send(&fx.router, &item, None).await;
400- assert!(page.contains("issue-edited"), "{page}");404+ assert_eq!(status, 200, "edited comment: item page");
401- assert!(page.contains(&format!("href=\"{link}\"")), "{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 let (status, history) = send(&fx.router, &link, None).await;414 let (status, history) = send(&fx.router, &link, None).await;
404- assert_eq!(status, 200);415+ assert_eq!(status, 200, "edited comment: history");
405- assert_eq!(history.matches("<article").count(), 2, "{history}");416+ assert_eq!(
406- assert!(history.contains("diff-line del"), "{history}");417+ history.matches("<article").count(),
407- assert!(history.contains("-two"), "{history}");418+ 2,
408- assert!(history.contains("+TWO"), "{history}");419+ "edited comment: versions {history}"
409- assert!(history.contains("+four"), "{history}");420+ );
410- assert!(history.contains("tahinli"), "{history}");421+ assert!(
411- }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]433+ // A comment that was never edited has no marker.
414- async fn unedited_comment_has_no_marker() {434+ let plain = fx
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
419 .store435 .store
420- .create_issue(&id, &owner.id, "bug", "body")436+ .create_issue(&id, &owner.id, "plain", "body")
421 .await437 .await
422 .unwrap();438 .unwrap();
423 fx.store439 fx.store
424- .add_comment(&issue.id, &owner.id, "plain")440+ .add_comment(&plain.id, &owner.id, "plain")
425 .await441 .await
426 .unwrap();442 .unwrap();
427- let (status, page) = send(&fx.router, "/tahinli/demo/issues/1", None).await;443+ let (status, page) = send(
428- assert_eq!(status, 200);444+ &fx.router,
429- assert!(!page.contains("issue-edited"), "{page}");445+ &format!("/tahinli/demo/issues/{}", plain.number),
430- }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]455+ // An issue body and title edit: the old title and the changed lines show.
433- async fn body_edit_history_is_listed_and_linked() {456+ let titled = fx
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
438 .store457 .store
439 .create_issue(&id, &owner.id, "old title", "a\nb\nc")458 .create_issue(&id, &owner.id, "old title", "a\nb\nc")
440 .await459 .await
441 .unwrap();460 .unwrap();
442 fx.store461 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 .await463 .await
445 .unwrap();464 .unwrap();
446-465+ let link = format!(
447- let link = format!("/tahinli/demo/issues/1/history/issue/{}", issue.id);466+ "/tahinli/demo/issues/{}/history/issue/{}",
448- let (status, page) = send(&fx.router, "/tahinli/demo/issues/1", None).await;467+ titled.number, titled.id
449- assert_eq!(status, 200);468+ );
450- assert!(page.contains(&format!("href=\"{link}\"")), "{page}");469+ let (status, page) = send(
451-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 let (status, history) = send(&fx.router, &link, None).await;480 let (status, history) = send(&fx.router, &link, None).await;
453- assert_eq!(status, 200);481+ assert_eq!(status, 200, "body edit: history");
454- assert!(history.contains("Title: old title"), "{history}");482+ assert!(
455- assert!(history.contains("-b"), "{history}");483+ history.contains("Title: old title"),
456- assert!(history.contains("+B"), "{history}");484+ "body edit: title {history}"
457- }485+ );
486+ assert!(history.contains("-b"), "body edit: -b {history}");
487+ assert!(history.contains("+B"), "body edit: +B {history}");
458 488
459- #[tokio::test]489+ // A comment on one issue is not found under another issue's URL.
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();
468 let other = fx490 let other = fx
469 .store491 .store
470 .create_issue(&id, &owner.id, "second", "body")492 .create_issue(&id, &owner.id, "second", "body")
471 .await493 .await
472 .unwrap();494 .unwrap();
473- let comment = fx495+ let other_comment = fx
474 .store496 .store
475 .add_comment(&other.id, &owner.id, "x")497 .add_comment(&other.id, &owner.id, "x")
476 .await498 .await
477 .unwrap();499 .unwrap();
478 fx.store500 fx.store
479- .edit_comment(&comment.id, &owner.id, "y")501+ .edit_comment(&other_comment.id, &owner.id, "y")
480 .await502 .await
481 .unwrap();503 .unwrap();
482-
483 let (status, _) = send(504 let (status, _) = send(
484 &fx.router,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 None,510 None,
487 )511 )
488 .await;512 .await;
489- assert_eq!(status, 404);513+ assert_eq!(status, 404, "wrong item: not found");
490 let (status, _) = send(514 let (status, _) = send(
491 &fx.router,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 None,520 None,
494 )521 )
495 .await;522 .await;
496- assert_eq!(status, 200);523+ assert_eq!(status, 200, "right item: found");
497- }
498 524
499- #[tokio::test]525+ // An unknown kind and a missing target are both not found.
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);
516 let (status, _) = send(526 let (status, _) = send(
517 &fx.router,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 None,532 None,
520 )533 )
521 .await;534 .await;
522- assert_eq!(status, 404);535+ assert_eq!(status, 404, "unknown kind");
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();
539 let (status, _) = send(536 let (status, _) = send(
540 &fx.router,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 None,542 None,
543 )543 )
544 .await;544 .await;
545- assert_eq!(status, 404);545+ assert_eq!(status, 404, "missing target");
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- }
554 546
555- #[tokio::test]547+ // Review comment history on a pull request, and not under the issue URL of the same number.
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;
560 let pr = fx548 let pr = fx
561 .store549 .store
562 .create_pull(550 .create_pull(
@@ -572,7 +560,7 @@ mod tests {
572 )560 )
573 .await561 .await
574 .unwrap();562 .unwrap();
575- let comment = fx563+ let review = fx
576 .store564 .store
577 .add_review_comment(565 .add_review_comment(
578 &pr.issue.id,566 &pr.issue.id,
@@ -588,29 +576,62 @@ mod tests {
588 .await576 .await
589 .unwrap();577 .unwrap();
590 fx.store578 fx.store
591- .edit_review_comment(&comment.id, "second")579+ .edit_review_comment(&review.id, "second")
592 .await580 .await
593 .unwrap();581 .unwrap();
594-
595 let link = format!(582 let link = format!(
596- "/tahinli/demo/pulls/1/history/review_comment/{}",583+ "/tahinli/demo/pulls/{}/history/review_comment/{}",
597- comment.id584+ pr.issue.number, review.id
598 );585 );
599 let (status, history) = send(&fx.router, &link, None).await;586 let (status, history) = send(&fx.router, &link, None).await;
600- assert_eq!(status, 200);587+ assert_eq!(status, 200, "review comment: history");
601- assert!(history.contains("-first"), "{history}");588+ assert!(
602- assert!(history.contains("+second"), "{history}");589+ history.contains("-first"),
603-590+ "review comment: -first {history}"
604- // The same comment under the issue URL is not on that item.591+ );
592+ assert!(
593+ history.contains("+second"),
594+ "review comment: +second {history}"
595+ );
605 let (status, _) = send(596 let (status, _) = send(
606 &fx.router,597 &fx.router,
607 &format!(598 &format!(
608- "/tahinli/demo/issues/1/history/review_comment/{}",599+ "/tahinli/demo/issues/{}/history/review_comment/{}",
609- comment.id600+ pr.issue.number, review.id
610 ),601 ),
611 None,602 None,
612 )603 )
613 .await;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 assert_eq!(status, 404);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 format!("{PIN_PATH}{number}/pin")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 #[tokio::test]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 let s = seeded().await;158 let s = seeded().await;
159+ let store = &s.site.store;
155 let (issue_id, number) = s.issues[0].clone();160 let (issue_id, number) = s.issues[0].clone();
156 let uri = pin_uri(number);161 let uri = pin_uri(number);
157 162
163+ // Triage pins, then unpins.
158 let reply = call(164 let reply = call(
159 &s.site,165 &s.site,
160 "POST",166 "POST",
@@ -163,20 +169,21 @@ async fn triage_pins_and_unpins_and_records_the_timeline() {
163 Some(pin_fields("1").as_slice()),169 Some(pin_fields("1").as_slice()),
164 )170 )
165 .await;171 .await;
166- assert_eq!(reply.status, 303);172+ assert_eq!(reply.status, 303, "triage pin");
167 assert_eq!(173 assert_eq!(
168 reply.location.as_deref(),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 assert!(178 assert!(
172- s.site179+ store
173- .store
174 .is_pinned(&s.repo_id, &issue_id)180 .is_pinned(&s.repo_id, &issue_id)
175 .await181 .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");185+ let events = store.list_events(&issue_id).await.expect("events");
179- assert!(events.iter().any(|e| e.kind == "pinned"));186+ assert!(events.iter().any(|e| e.kind == "pinned"), "pinned event");
180 187
181 let reply = call(188 let reply = call(
182 &s.site,189 &s.site,
@@ -186,24 +193,21 @@ async fn triage_pins_and_unpins_and_records_the_timeline() {
186 Some(pin_fields("0").as_slice()),193 Some(pin_fields("0").as_slice()),
187 )194 )
188 .await;195 .await;
189- assert_eq!(reply.status, 303);196+ assert_eq!(reply.status, 303, "triage unpin");
190 assert!(197 assert!(
191- !s.site198+ !store
192- .store
193 .is_pinned(&s.repo_id, &issue_id)199 .is_pinned(&s.repo_id, &issue_id)
194 .await200 .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 let reply = call(211 let reply = call(
208 &s.site,212 &s.site,
209 "POST",213 "POST",
@@ -212,7 +216,7 @@ async fn read_only_collaborator_and_anonymous_are_forbidden() {
212 Some(pin_fields("1").as_slice()),216 Some(pin_fields("1").as_slice()),
213 )217 )
214 .await;218 .await;
215- assert_eq!(reply.status, 403);219+ assert_eq!(reply.status, 403, "read-only pin");
216 let reply = call(220 let reply = call(
217 &s.site,221 &s.site,
218 "POST",222 "POST",
@@ -221,52 +225,48 @@ async fn read_only_collaborator_and_anonymous_are_forbidden() {
221 Some(pin_fields("1").as_slice()),225 Some(pin_fields("1").as_slice()),
222 )226 )
223 .await;227 .await;
224- assert!(reply.status == 403 || reply.status == 303);
225 assert!(228 assert!(
226- !s.site229+ reply.status == 403 || reply.status == 303,
227- .store230+ "anonymous pin: {}",
231+ reply.status
232+ );
233+ assert!(
234+ !store
228 .is_pinned(&s.repo_id, &issue_id)235 .is_pinned(&s.repo_id, &issue_id)
229 .await236 .await
230- .expect("pinned")237+ .expect("pinned"),
238+ "refused pins stored nothing"
231 );239 );
232-}
233 240
234-#[tokio::test]241+ // A missing CSRF token is forbidden and changes nothing.
235-async fn missing_csrf_is_forbidden_and_changes_nothing() {
236- let s = seeded().await;
237- let (issue_id, number) = s.issues[0].clone();
238 let no_token: &[(&str, &str)] = &[("pinned", "1")];242 let no_token: &[(&str, &str)] = &[("pinned", "1")];
239 let reply = call(243 let reply = call(
240 &s.site,244 &s.site,
241 "POST",245 "POST",
242- &pin_uri(number),246+ &uri,
243 Some(s.tahinli_cookie.as_str()),247 Some(s.tahinli_cookie.as_str()),
244 Some(no_token),248 Some(no_token),
245 )249 )
246 .await;250 .await;
247- assert_eq!(reply.status, 403);251+ assert_eq!(reply.status, 403, "missing csrf");
248 assert!(252 assert!(
249- !s.site253+ !store
250- .store
251 .is_pinned(&s.repo_id, &issue_id)254 .is_pinned(&s.repo_id, &issue_id)
252 .await255 .await
253- .expect("pinned")256+ .expect("pinned"),
257+ "missing csrf stored nothing"
254 );258 );
255-}
256 259
257-#[tokio::test]260+ // A bad pinned value is 400; an unknown issue is 404.
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();
261 let reply = call(261 let reply = call(
262 &s.site,262 &s.site,
263 "POST",263 "POST",
264- &pin_uri(number),264+ &uri,
265 Some(s.carol_cookie.as_str()),265 Some(s.carol_cookie.as_str()),
266 Some(&[("csrf", CSRF), ("pinned", "yes")][..]),266 Some(&[("csrf", CSRF), ("pinned", "yes")][..]),
267 )267 )
268 .await;268 .await;
269- assert_eq!(reply.status, 400);269+ assert_eq!(reply.status, 400, "bad pinned value");
270 let reply = call(270 let reply = call(
271 &s.site,271 &s.site,
272 "POST",272 "POST",
@@ -275,14 +275,11 @@ async fn bad_pinned_value_is_rejected_and_unknown_issue_is_404() {
275 Some(pin_fields("1").as_slice()),275 Some(pin_fields("1").as_slice()),
276 )276 )
277 .await;277 .await;
278- assert_eq!(reply.status, 404);278+ assert_eq!(reply.status, 404, "unknown issue");
279-}
280 279
281-#[tokio::test]280+ // Three pins fill the limit; the fourth is shown the limit and pins nothing.
282-async fn fourth_pin_shows_the_limit_and_pins_nothing() {
283- let s = seeded().await;
284 for (id, _) in &s.issues[..3] {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 let (fourth_id, fourth_number) = s.issues[3].clone();284 let (fourth_id, fourth_number) = s.issues[3].clone();
288 let reply = call(285 let reply = call(
@@ -293,50 +290,56 @@ async fn fourth_pin_shows_the_limit_and_pins_nothing() {
293 Some(pin_fields("1").as_slice()),290 Some(pin_fields("1").as_slice()),
294 )291 )
295 .await;292 .await;
296- assert_eq!(reply.status, 200);293+ assert_eq!(reply.status, 200, "fourth pin");
297- assert!(reply.body.contains("already has 3 pinned issues"));
298 assert!(294 assert!(
299- !s.site295+ reply.body.contains("already has 3 pinned issues"),
300- .store296+ "fourth pin: limit shown"
297+ );
298+ assert!(
299+ !store
301 .is_pinned(&s.repo_id, &fourth_id)300 .is_pinned(&s.repo_id, &fourth_id)
302 .await301 .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 #[tokio::test]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 let s = seeded().await;311 let s = seeded().await;
310- let (_, number) = s.issues[1].clone();312+ let (issue_id, number) = s.issues[1].clone();
311 s.site313 s.site
312 .store314 .store
313- .pin_issue(&s.repo_id, &s.issues[1].0)315+ .pin_issue(&s.repo_id, &issue_id)
314 .await316 .await
315 .expect("pin");317 .expect("pin");
318+
316 let reply = call(&s.site, "GET", "/tahinli/forge/issues", None, None).await;319 let reply = call(&s.site, "GET", "/tahinli/forge/issues", None, None).await;
317- assert_eq!(reply.status, 200);320+ assert_eq!(reply.status, 200, "issue list");
318- assert!(reply.body.contains("pinned-card"));321+ assert!(reply.body.contains("pinned-card"), "pinned card shown");
319 assert!(322 assert!(
320 reply323 reply
321 .body324 .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 let card_at = reply.body.find("pinned-card").expect("card");328 let card_at = reply.body.find("pinned-card").expect("card");
325 let list_at = reply.body.find("issue-list").expect("list");329 let list_at = reply.body.find("issue-list").expect("list");
326- assert!(card_at < list_at);330+ assert!(card_at < list_at, "pinned cards sit above the list");
327-}
328 331
329-#[tokio::test]332+ let (_, first_number) = s.issues[0].clone();
330-async fn issue_page_offers_pin_to_triage_only() {333+ let page = format!("{PIN_PATH}{first_number}");
331- let s = seeded().await;
332- let (_, number) = s.issues[0].clone();
333- let page = format!("{PIN_PATH}{number}");
334 let reply = call(&s.site, "GET", &page, Some(s.carol_cookie.as_str()), None).await;334 let reply = call(&s.site, "GET", &page, Some(s.carol_cookie.as_str()), None).await;
335- assert_eq!(reply.status, 200);335+ assert_eq!(reply.status, 200, "triage issue page");
336- assert!(reply.body.contains("Pin issue"));336+ assert!(reply.body.contains("Pin issue"), "triage sees pin");
337- assert!(reply.body.contains(&format!("{page}/pin")));337+ assert!(
338+ reply.body.contains(&format!("{page}/pin")),
339+ "triage pin form"
340+ );
338 341
339 let reply = call(&s.site, "GET", &page, Some(s.bob_cookie.as_str()), None).await;342 let reply = call(&s.site, "GET", &page, Some(s.bob_cookie.as_str()), None).await;
340- assert_eq!(reply.status, 200);343+ assert_eq!(reply.status, 200, "read issue page");
341- assert!(!reply.body.contains("Pin issue"));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 .len()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 #[tokio::test]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 let site = site().await;431 let site = site().await;
429 let url = "/tahinli/forge/labels/defaults";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]434+ // No labels yet: triage sees the button, read-only does not.
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;
471 let (status, body) = send(435 let (status, body) = send(
472 &site,436 &site,
473 "GET",437 "GET",
@@ -476,11 +440,30 @@ mod tests {
476 CSRF,440 CSRF,
477 )441 )
478 .await;442 .await;
479- assert_eq!(status, 200);443+ assert_eq!(status, 200, "triage labels page");
480- assert!(body.contains("/tahinli/forge/labels/defaults"));444+ assert!(body.contains(url), "triage sees the defaults button");
481 let (status, body) =445 let (status, body) =
482 send(&site, "GET", "/tahinli/forge/labels", Some(&site.bob), CSRF).await;446 send(&site, "GET", "/tahinli/forge/labels", Some(&site.bob), CSRF).await;
483- assert_eq!(status, 200);447+ assert_eq!(status, 200, "read-only labels page");
484- assert!(!body.contains("/labels/defaults"));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 tempfile::tempdir().expect("tempdir")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 #[tokio::test]498 #[tokio::test]
497- async fn eleventh_attempt_for_a_user_is_throttled_even_with_the_right_password() {499+ async fn password_throttles_are_per_user_and_per_address() {
498- let tmp = scratch("user-limit");500+ let tmp = scratch("password-throttles");
499 let (router, state) = app(tmp.path()).await;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 let from = peer(10);507 let from = peer(10);
502 let token = login_page(&router, from).await;508 let token = login_page(&router, from).await;
503-
504 for _ in 0..10 {509 for _ in 0..10 {
505 let (status, body) =510 let (status, body) =
506 post_login(&router, from, (&token.0, &token.1), "tahinli", "wrong").await;511 post_login(&router, from, (&token.0, &token.1), "tahinli", "wrong").await;
507- assert_eq!(status, 200);512+ assert_eq!(status, 200, "user limit: wrong password");
508- assert!(body.contains("invalid username or password"));513+ assert!(
514+ body.contains("invalid username or password"),
515+ "user limit: generic message"
516+ );
509 }517 }
510 let (status, body) =518 let (status, body) =
511 post_login(&router, from, (&token.0, &token.1), "tahinli", PASSWORD).await;519 post_login(&router, from, (&token.0, &token.1), "tahinli", PASSWORD).await;
512- assert_eq!(status, 429);520+ assert_eq!(status, 429, "user limit: right password while locked");
513- assert!(body.contains("Too many attempts. Try again later."));521+ assert!(
514- assert!(!body.contains("invalid username or password"));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 #[tokio::test]586 #[tokio::test]
518 async fn failed_and_successful_logins_are_audited_without_the_password() {587 async fn failed_and_successful_logins_are_audited_without_the_password() {
519 use forge_core::{AuditAction, AuditFilter};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 /// Reply parts the tests look at.644 /// Reply parts the tests look at.
649 struct Reply {645 struct Reply {
650 status: u16,646 status: u16,
@@ -751,20 +747,33 @@ mod tests {
751 format!("{pending}; {precsrf_cookie}")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 #[tokio::test]753 #[tokio::test]
755- async fn totp_user_gets_a_pending_cookie_and_no_session() {754+ async fn two_factor_login_steps_and_recovery_codes() {
756- let tmp = scratch("totp-pending");755+ let tmp = scratch("totp-flows");
757 let (router, state) = app(tmp.path()).await;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 let from = peer(20);760 let from = peer(20);
760 let (cookie, precsrf) = login_page(&router, from).await;761 let (cookie, precsrf) = login_page(&router, from).await;
761-762+ let reply = password_step(&router, from, (&cookie, &precsrf), "ada", PASSWORD).await;
762- let reply = password_step(&router, from, (&cookie, &precsrf), "tahinli", PASSWORD).await;763+ assert_eq!(reply.status, 303, "pending: password step");
763- assert_eq!(reply.status, 303);764+ assert_eq!(
764- assert_eq!(reply.location.as_deref(), Some("/login/2fa"));765+ reply.location.as_deref(),
765- assert!(pair(&reply, "__Host-forge_2fa").is_some());766+ Some("/login/2fa"),
766- assert!(pair(&reply, "__Host-forge_session").is_none());767+ "pending: redirect"
767-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 let page = send(777 let page = send(
769 &router,778 &router,
770 Request::builder()779 Request::builder()
@@ -779,14 +788,10 @@ mod tests {
779 .unwrap(),788 .unwrap(),
780 )789 )
781 .await;790 .await;
782- assert_eq!(page.status, 200);791+ assert_eq!(page.status, 200, "pending: second-factor page");
783- assert!(page.body.contains("Two-factor check"));792+ assert!(page.body.contains("Two-factor check"), "pending: page text");
784- }
785 793
786- #[tokio::test]794+ // The second-factor page without a pending cookie goes back to login.
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;
790 let reply = send(795 let reply = send(
791 &router,796 &router,
792 Request::builder()797 Request::builder()
@@ -797,24 +802,26 @@ mod tests {
797 .unwrap(),802 .unwrap(),
798 )803 )
799 .await;804 .await;
800- assert_eq!(reply.status, 303);805+ assert_eq!(reply.status, 303, "no pending: status");
801- assert_eq!(reply.location.as_deref(), Some("/login"));806+ assert_eq!(
802- }807+ reply.location.as_deref(),
808+ Some("/login"),
809+ "no pending: redirect"
810+ );
803 811
804- #[tokio::test]812+ // A wrong code fails, the right code logs in, and the same code again is a replay.
805- async fn wrong_code_fails_right_code_logs_in_and_replay_fails() {813+ let shared = totp_user(&state, "grace").await;
806- let tmp = scratch("totp-flow");
807- let (router, state) = app(tmp.path()).await;
808- let shared = totp_user(&state, "tahinli").await;
809 let from = peer(22);814 let from = peer(22);
810 let (cookie, precsrf) = login_page(&router, from).await;815 let (cookie, precsrf) = login_page(&router, from).await;
811-816+ let step = password_step(&router, from, (&cookie, &precsrf), "grace", PASSWORD).await;
812- let step = password_step(&router, from, (&cookie, &precsrf), "tahinli", PASSWORD).await;
813 let pending = both(&pair(&step, "__Host-forge_2fa").unwrap(), &cookie);817 let pending = both(&pair(&step, "__Host-forge_2fa").unwrap(), &cookie);
814 let wrong = second_factor_step(&router, from, &pending, &precsrf, "000000").await;818 let wrong = second_factor_step(&router, from, &pending, &precsrf, "000000").await;
815 // The chance that 000000 is the live code is about 1 in a million per window.819 // The chance that 000000 is the live code is about 1 in a million per window.
816 if wrong.status == 200 {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 let now_step = totp::step_for(827 let now_step = totp::step_for(
@@ -825,59 +832,77 @@ mod tests {
825 );832 );
826 let code = totp::code_at(&shared, now_step);833 let code = totp::code_at(&shared, now_step);
827 let ok = second_factor_step(&router, from, &pending, &precsrf, &code).await;834 let ok = second_factor_step(&router, from, &pending, &precsrf, &code).await;
828- assert_eq!(ok.status, 303, "body {}", ok.body);835+ assert_eq!(ok.status, 303, "right code: body {}", ok.body);
829- assert_eq!(ok.location.as_deref(), Some("/dash"));836+ assert_eq!(
830- assert!(pair(&ok, "__Host-forge_session").is_some());837+ ok.location.as_deref(),
831- assert!(ok.cookies.iter().any(|c| c.starts_with("__Host-forge_2fa=")838+ Some("/dash"),
832- && c.len() == "__Host-forge_2fa=".len()));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 let (cookie2, precsrf2) = login_page(&router, from).await;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 let pending2 = both(&pair(&step2, "__Host-forge_2fa").unwrap(), &cookie2);853 let pending2 = both(&pair(&step2, "__Host-forge_2fa").unwrap(), &cookie2);
838 let replay = second_factor_step(&router, from, &pending2, &precsrf2, &code).await;854 let replay = second_factor_step(&router, from, &pending2, &precsrf2, &code).await;
839- assert_eq!(replay.status, 200);855+ assert_eq!(replay.status, 200, "replay: status");
840- assert!(replay.body.contains("That code is not right"));856+ assert!(
841- assert!(pair(&replay, "__Host-forge_session").is_none());857+ replay.body.contains("That code is not right"),
842- }858+ "replay: message"
859+ );
860+ assert!(
861+ pair(&replay, "__Host-forge_session").is_none(),
862+ "replay: no session"
863+ );
843 864
844- #[tokio::test]865+ // A recovery code works once.
845- async fn recovery_code_works_once() {866+ totp_user(&state, "linus").await;
846- let tmp = scratch("totp-recovery");
847- let (router, state) = app(tmp.path()).await;
848- totp_user(&state, "tahinli").await;
849 let from = peer(23);867 let from = peer(23);
850 let (cookie, precsrf) = login_page(&router, from).await;868 let (cookie, precsrf) = login_page(&router, from).await;
851-869+ let step = password_step(&router, from, (&cookie, &precsrf), "linus", PASSWORD).await;
852- let step = password_step(&router, from, (&cookie, &precsrf), "tahinli", PASSWORD).await;
853 let pending = both(&pair(&step, "__Host-forge_2fa").unwrap(), &cookie);870 let pending = both(&pair(&step, "__Host-forge_2fa").unwrap(), &cookie);
854 let first = second_factor_step(&router, from, &pending, &precsrf, "AAAA-bbbb").await;871 let first = second_factor_step(&router, from, &pending, &precsrf, "AAAA-bbbb").await;
855- assert_eq!(first.status, 303);872+ assert_eq!(first.status, 303, "recovery: first use");
856- assert!(pair(&first, "__Host-forge_session").is_some());873+ assert!(
874+ pair(&first, "__Host-forge_session").is_some(),
875+ "recovery: session"
876+ );
857 877
858 let (cookie2, precsrf2) = login_page(&router, from).await;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 let pending2 = both(&pair(&step2, "__Host-forge_2fa").unwrap(), &cookie2);880 let pending2 = both(&pair(&step2, "__Host-forge_2fa").unwrap(), &cookie2);
861 let second = second_factor_step(&router, from, &pending2, &precsrf2, "aaaabbbb").await;881 let second = second_factor_step(&router, from, &pending2, &precsrf2, "aaaabbbb").await;
862- assert_eq!(second.status, 200);882+ assert_eq!(second.status, 200, "recovery: second use");
863- assert!(pair(&second, "__Host-forge_session").is_none());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 #[tokio::test]890 #[tokio::test]
867- async fn expired_pending_cookie_is_rejected() {891+ async fn two_factor_expiry_and_per_user_limit() {
868- let tmp = scratch("totp-expired");892+ let tmp = scratch("totp-expiry-limit");
869 let (router, state) = app(tmp.path()).await;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 let user = state898 let user = state
872 .store899 .store
873- .user_by_username("tahinli")900+ .user_by_username("margaret")
874 .await901 .await
875 .unwrap()902 .unwrap()
876 .unwrap();903 .unwrap();
877 let from = peer(24);904 let from = peer(24);
878 let (cookie, precsrf) = login_page(&router, from).await;905 let (cookie, precsrf) = login_page(&router, from).await;
879-
880- // A correctly sealed pending cookie whose expiry is in the past.
881 let stale = secret::seal(&state.key, format!("1|{}|/", user.id).as_bytes()).unwrap();906 let stale = secret::seal(&state.key, format!("1|{}|/", user.id).as_bytes()).unwrap();
882 let pending = format!("__Host-forge_2fa={stale}");907 let pending = format!("__Host-forge_2fa={stale}");
883 let code = totp::code_at(908 let code = totp::code_at(
@@ -891,24 +916,26 @@ mod tests {
891 );916 );
892 let reply =917 let reply =
893 second_factor_step(&router, from, &both(&pending, &cookie), &precsrf, &code).await;918 second_factor_step(&router, from, &both(&pending, &cookie), &precsrf, &code).await;
894- assert_eq!(reply.status, 303);919+ assert_eq!(reply.status, 303, "expired: status");
895- assert_eq!(reply.location.as_deref(), Some("/login"));920+ assert_eq!(
896- assert!(pair(&reply, "__Host-forge_session").is_none());921+ reply.location.as_deref(),
897- }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]930+ // Wrong codes until the user limit; then even the right code is refused.
900- async fn second_factor_attempts_are_throttled_per_user() {931+ let shared = totp_user(&state, "ken").await;
901- let tmp = scratch("totp-throttle");
902- let (router, state) = app(tmp.path()).await;
903- let shared = totp_user(&state, "tahinli").await;
904 let from = peer(25);932 let from = peer(25);
905 let (cookie, precsrf) = login_page(&router, from).await;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 let pending = both(&pair(&step, "__Host-forge_2fa").unwrap(), &cookie);935 let pending = both(&pair(&step, "__Host-forge_2fa").unwrap(), &cookie);
908-
909 for _ in 0..TWO_FACTOR_USER_LIMIT {936 for _ in 0..TWO_FACTOR_USER_LIMIT {
910 let reply = second_factor_step(&router, from, &pending, &precsrf, "111111").await;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 let code = totp::code_at(940 let code = totp::code_at(
914 &shared,941 &shared,
@@ -920,7 +947,7 @@ mod tests {
920 ),947 ),
921 );948 );
922 let blocked = second_factor_step(&router, from, &pending, &precsrf, &code).await;949 let blocked = second_factor_step(&router, from, &pending, &precsrf, &code).await;
923- assert_eq!(blocked.status, 429);950+ assert_eq!(blocked.status, 429, "limit: right code while locked");
924- assert!(blocked.body.contains("Too many attempts"));951+ assert!(blocked.body.contains("Too many attempts"), "limit: message");
925 }952 }
926 }953 }