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 {
122122 }
123123 }
124124
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.
125127 #[tokio::test]
126-async fn a_comment_bumps_the_list_and_the_discussion_only() {
128+async fn writes_bump_the_topics_they_reach() {
127129 let seed = seeded(Visibility::Public).await;
128130 let site = &seed.site;
131+
132+ // A comment bumps the list and its discussion, and not an unrelated discussion.
129133 let list = site
130134 .live
131135 .subscribe(&Topic::Discussions(seed.repo_id.clone()));
@@ -133,7 +137,6 @@ async fn a_comment_bumps_the_list_and_the_discussion_only() {
133137 .live
134138 .subscribe(&Topic::Discussion(seed.discussion_id.clone()));
135139 let other = site.live.subscribe(&Topic::Discussion("other".to_owned()));
136-
137140 let status = post(
138141 site,
139142 "/tahinli/forge/discussions/1/comments",
@@ -141,31 +144,31 @@ async fn a_comment_bumps_the_list_and_the_discussion_only() {
141144 &[("csrf", CSRF), ("body", "A reply")],
142145 )
143146 .await;
144-
145- assert_ne!(status, 404, "the comment was refused");
147+ assert_ne!(status, 404, "comment: the comment was refused");
146148 let comments = site
147149 .store
148150 .list_discussion_comments(&seed.discussion_id)
149151 .await
150152 .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+ );
153158 assert!(
154159 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"
156165 );
157- assert!(!other.has_changed().expect("open"));
158-}
159166
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.
164168 let list = site
165169 .live
166170 .subscribe(&Topic::Discussions(seed.repo_id.clone()));
167171 let category = DiscussionCategory::General.as_str();
168-
169172 let status = post(
170173 site,
171174 "/tahinli/forge/discussions/new",
@@ -178,20 +181,17 @@ async fn a_new_discussion_bumps_the_list() {
178181 ],
179182 )
180183 .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+ );
181189
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.
190192 let item = site
191193 .live
192194 .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.
195195 let status = post(
196196 site,
197197 "/tahinli/forge/discussions/1/comments",
@@ -199,11 +199,10 @@ async fn a_refused_write_still_bumps_so_the_page_reads_the_truth() {
199199 &[("csrf", CSRF), ("body", "")],
200200 )
201201 .await;
202-
203- assert_ne!(status, 404, "the discussion page answered");
202+ assert_ne!(status, 404, "refused write: the discussion page answered");
204203 assert!(
205204 item.has_changed().expect("open"),
206- "the discussion was bumped"
205+ "refused write: the discussion was bumped"
207206 );
208207 }
209208
crates/forge-web/src/pages/edit_history.rsmodified+145 -124
@@ -370,11 +370,15 @@ mod tests {
370370 std::iter::repeat_n(c, 40).collect()
371371 }
372372
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.
373375 #[tokio::test]
374- async fn comment_edited_twice_shows_marker_and_diffs() {
376+ async fn public_histories_show_diffs_markers_and_refusals() {
375377 let fx = Fixture::new(Visibility::Public).await;
376378 let owner = tahinli(&fx).await;
377379 let id = repo_id(&fx).await;
380+
381+ // A comment edited twice: a marker on the item page and two versions with diffs.
378382 let issue = fx
379383 .store
380384 .create_issue(&id, &owner.id, "bug", "body")
@@ -393,170 +397,154 @@ mod tests {
393397 .edit_comment(&comment.id, &owner.id, "one\nTWO\nthree\nfour")
394398 .await
395399 .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+ );
402413
403414 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+ );
412432
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
419435 .store
420- .create_issue(&id, &owner.id, "bug", "body")
436+ .create_issue(&id, &owner.id, "plain", "body")
421437 .await
422438 .unwrap();
423439 fx.store
424- .add_comment(&issue.id, &owner.id, "plain")
440+ .add_comment(&plain.id, &owner.id, "plain")
425441 .await
426442 .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+ );
431454
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
438457 .store
439458 .create_issue(&id, &owner.id, "old title", "a\nb\nc")
440459 .await
441460 .unwrap();
442461 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")
444463 .await
445464 .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+ );
452480 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}");
458488
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.
468490 let other = fx
469491 .store
470492 .create_issue(&id, &owner.id, "second", "body")
471493 .await
472494 .unwrap();
473- let comment = fx
495+ let other_comment = fx
474496 .store
475497 .add_comment(&other.id, &owner.id, "x")
476498 .await
477499 .unwrap();
478500 fx.store
479- .edit_comment(&comment.id, &owner.id, "y")
501+ .edit_comment(&other_comment.id, &owner.id, "y")
480502 .await
481503 .unwrap();
482-
483504 let (status, _) = send(
484505 &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+ ),
486510 None,
487511 )
488512 .await;
489- assert_eq!(status, 404);
513+ assert_eq!(status, 404, "wrong item: not found");
490514 let (status, _) = send(
491515 &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+ ),
493520 None,
494521 )
495522 .await;
496- assert_eq!(status, 200);
497- }
523+ assert_eq!(status, 200, "right item: found");
498524
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.
516526 let (status, _) = send(
517527 &fx.router,
518- "/tahinli/demo/issues/1/history/comment/missing",
528+ &format!(
529+ "/tahinli/demo/issues/{}/history/label/{}",
530+ issue.number, issue.id
531+ ),
519532 None,
520533 )
521534 .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");
539536 let (status, _) = send(
540537 &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+ ),
542542 None,
543543 )
544544 .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");
554546
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.
560548 let pr = fx
561549 .store
562550 .create_pull(
@@ -572,7 +560,7 @@ mod tests {
572560 )
573561 .await
574562 .unwrap();
575- let comment = fx
563+ let review = fx
576564 .store
577565 .add_review_comment(
578566 &pr.issue.id,
@@ -588,29 +576,62 @@ mod tests {
588576 .await
589577 .unwrap();
590578 fx.store
591- .edit_review_comment(&comment.id, "second")
579+ .edit_review_comment(&review.id, "second")
592580 .await
593581 .unwrap();
594-
595582 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
598585 );
599586 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+ );
605596 let (status, _) = send(
606597 &fx.router,
607598 &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
610601 ),
611602 None,
612603 )
613604 .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;
614628 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);
615636 }
616637 }
crates/forge-web/src/pages/issue_pin_tests.rsmodified+75 -72
@@ -149,12 +149,18 @@ fn pin_uri(number: i64) -> String {
149149 format!("{PIN_PATH}{number}/pin")
150150 }
151151
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.
152156 #[tokio::test]
153-async fn triage_pins_and_unpins_and_records_the_timeline() {
157+async fn pin_writes_follow_the_rules_in_order() {
154158 let s = seeded().await;
159+ let store = &s.site.store;
155160 let (issue_id, number) = s.issues[0].clone();
156161 let uri = pin_uri(number);
157162
163+ // Triage pins, then unpins.
158164 let reply = call(
159165 &s.site,
160166 "POST",
@@ -163,20 +169,21 @@ async fn triage_pins_and_unpins_and_records_the_timeline() {
163169 Some(pin_fields("1").as_slice()),
164170 )
165171 .await;
166- assert_eq!(reply.status, 303);
172+ assert_eq!(reply.status, 303, "triage pin");
167173 assert_eq!(
168174 reply.location.as_deref(),
169- Some(format!("{PIN_PATH}{number}").as_str())
175+ Some(format!("{PIN_PATH}{number}").as_str()),
176+ "triage pin redirect"
170177 );
171178 assert!(
172- s.site
173- .store
179+ store
174180 .is_pinned(&s.repo_id, &issue_id)
175181 .await
176- .expect("pinned")
182+ .expect("pinned"),
183+ "triage pin stored"
177184 );
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");
180187
181188 let reply = call(
182189 &s.site,
@@ -186,24 +193,21 @@ async fn triage_pins_and_unpins_and_records_the_timeline() {
186193 Some(pin_fields("0").as_slice()),
187194 )
188195 .await;
189- assert_eq!(reply.status, 303);
196+ assert_eq!(reply.status, 303, "triage unpin");
190197 assert!(
191- !s.site
192- .store
198+ !store
193199 .is_pinned(&s.repo_id, &issue_id)
194200 .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"
196208 );
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);
206209
210+ // A read-only collaborator and an anonymous viewer are refused.
207211 let reply = call(
208212 &s.site,
209213 "POST",
@@ -212,7 +216,7 @@ async fn read_only_collaborator_and_anonymous_are_forbidden() {
212216 Some(pin_fields("1").as_slice()),
213217 )
214218 .await;
215- assert_eq!(reply.status, 403);
219+ assert_eq!(reply.status, 403, "read-only pin");
216220 let reply = call(
217221 &s.site,
218222 "POST",
@@ -221,52 +225,48 @@ async fn read_only_collaborator_and_anonymous_are_forbidden() {
221225 Some(pin_fields("1").as_slice()),
222226 )
223227 .await;
224- assert!(reply.status == 403 || reply.status == 303);
225228 assert!(
226- !s.site
227- .store
229+ reply.status == 403 || reply.status == 303,
230+ "anonymous pin: {}",
231+ reply.status
232+ );
233+ assert!(
234+ !store
228235 .is_pinned(&s.repo_id, &issue_id)
229236 .await
230- .expect("pinned")
237+ .expect("pinned"),
238+ "refused pins stored nothing"
231239 );
232-}
233240
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.
238242 let no_token: &[(&str, &str)] = &[("pinned", "1")];
239243 let reply = call(
240244 &s.site,
241245 "POST",
242- &pin_uri(number),
246+ &uri,
243247 Some(s.tahinli_cookie.as_str()),
244248 Some(no_token),
245249 )
246250 .await;
247- assert_eq!(reply.status, 403);
251+ assert_eq!(reply.status, 403, "missing csrf");
248252 assert!(
249- !s.site
250- .store
253+ !store
251254 .is_pinned(&s.repo_id, &issue_id)
252255 .await
253- .expect("pinned")
256+ .expect("pinned"),
257+ "missing csrf stored nothing"
254258 );
255-}
256259
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.
261261 let reply = call(
262262 &s.site,
263263 "POST",
264- &pin_uri(number),
264+ &uri,
265265 Some(s.carol_cookie.as_str()),
266266 Some(&[("csrf", CSRF), ("pinned", "yes")][..]),
267267 )
268268 .await;
269- assert_eq!(reply.status, 400);
269+ assert_eq!(reply.status, 400, "bad pinned value");
270270 let reply = call(
271271 &s.site,
272272 "POST",
@@ -275,14 +275,11 @@ async fn bad_pinned_value_is_rejected_and_unknown_issue_is_404() {
275275 Some(pin_fields("1").as_slice()),
276276 )
277277 .await;
278- assert_eq!(reply.status, 404);
279-}
278+ assert_eq!(reply.status, 404, "unknown issue");
280279
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.
284281 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");
286283 }
287284 let (fourth_id, fourth_number) = s.issues[3].clone();
288285 let reply = call(
@@ -293,50 +290,56 @@ async fn fourth_pin_shows_the_limit_and_pins_nothing() {
293290 Some(pin_fields("1").as_slice()),
294291 )
295292 .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");
298294 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
301300 .is_pinned(&s.repo_id, &fourth_id)
302301 .await
303- .expect("pinned")
302+ .expect("pinned"),
303+ "fourth pin stored nothing"
304304 );
305305 }
306306
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.
307309 #[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() {
309311 let s = seeded().await;
310- let (_, number) = s.issues[1].clone();
312+ let (issue_id, number) = s.issues[1].clone();
311313 s.site
312314 .store
313- .pin_issue(&s.repo_id, &s.issues[1].0)
315+ .pin_issue(&s.repo_id, &issue_id)
314316 .await
315317 .expect("pin");
318+
316319 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");
319322 assert!(
320323 reply
321324 .body
322- .contains(&format!("/tahinli/forge/issues/{number}\""))
325+ .contains(&format!("/tahinli/forge/issues/{number}\"")),
326+ "pinned card links the pinned issue"
323327 );
324328 let card_at = reply.body.find("pinned-card").expect("card");
325329 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");
328331
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}");
334334 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+ );
338341
339342 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");
342345 }
crates/forge-web/src/pages/labels_page.rsmodified+28 -45
@@ -423,51 +423,15 @@ mod tests {
423423 .len()
424424 }
425425
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.
426429 #[tokio::test]
427- async fn triage_adds_defaults_once_and_repeat_adds_nothing() {
430+ async fn defaults_button_and_adding_follow_the_rules() {
428431 let site = site().await;
429432 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- }
437433
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.
471435 let (status, body) = send(
472436 &site,
473437 "GET",
@@ -476,11 +440,30 @@ mod tests {
476440 CSRF,
477441 )
478442 .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");
481445 let (status, body) =
482446 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");
485468 }
486469 }
crates/forge-web/src/pages/login.rsmodified+180 -153
@@ -493,27 +493,96 @@ mod tests {
493493 tempfile::tempdir().expect("tempdir")
494494 }
495495
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.
496498 #[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");
499501 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.
501507 let from = peer(10);
502508 let token = login_page(&router, from).await;
503-
504509 for _ in 0..10 {
505510 let (status, body) =
506511 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+ );
509517 }
510518 let (status, body) =
511519 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+ }
515582 }
516583
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.
517586 #[tokio::test]
518587 async fn failed_and_successful_logins_are_audited_without_the_password() {
519588 use forge_core::{AuditAction, AuditFilter};
@@ -572,79 +641,6 @@ mod tests {
572641 }
573642 }
574643
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-
648644 /// Reply parts the tests look at.
649645 struct Reply {
650646 status: u16,
@@ -751,20 +747,33 @@ mod tests {
751747 format!("{pending}; {precsrf_cookie}")
752748 }
753749
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.
754753 #[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");
757756 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;
759760 let from = peer(20);
760761 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+ );
768777 let page = send(
769778 &router,
770779 Request::builder()
@@ -779,14 +788,10 @@ mod tests {
779788 .unwrap(),
780789 )
781790 .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");
785793
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.
790795 let reply = send(
791796 &router,
792797 Request::builder()
@@ -797,24 +802,26 @@ mod tests {
797802 .unwrap(),
798803 )
799804 .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+ );
803811
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;
809814 let from = peer(22);
810815 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;
813817 let pending = both(&pair(&step, "__Host-forge_2fa").unwrap(), &cookie);
814818 let wrong = second_factor_step(&router, from, &pending, &precsrf, "000000").await;
815819 // The chance that 000000 is the live code is about 1 in a million per window.
816820 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+ );
818825 }
819826
820827 let now_step = totp::step_for(
@@ -825,59 +832,77 @@ mod tests {
825832 );
826833 let code = totp::code_at(&shared, now_step);
827834 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+ );
833850
834- // The same code again, from a fresh password step, is a replay and must fail.
835851 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;
837853 let pending2 = both(&pair(&step2, "__Host-forge_2fa").unwrap(), &cookie2);
838854 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+ );
843864
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;
849867 let from = peer(23);
850868 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;
853870 let pending = both(&pair(&step, "__Host-forge_2fa").unwrap(), &cookie);
854871 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+ );
857877
858878 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;
860880 let pending2 = both(&pair(&step2, "__Host-forge_2fa").unwrap(), &cookie2);
861881 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+ );
864887 }
865888
889+ /// Two-factor expiry and the per-user limit on one app, one user per scenario.
866890 #[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");
869893 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;
871898 let user = state
872899 .store
873- .user_by_username("tahinli")
900+ .user_by_username("margaret")
874901 .await
875902 .unwrap()
876903 .unwrap();
877904 let from = peer(24);
878905 let (cookie, precsrf) = login_page(&router, from).await;
879-
880- // A correctly sealed pending cookie whose expiry is in the past.
881906 let stale = secret::seal(&state.key, format!("1|{}|/", user.id).as_bytes()).unwrap();
882907 let pending = format!("__Host-forge_2fa={stale}");
883908 let code = totp::code_at(
@@ -891,24 +916,26 @@ mod tests {
891916 );
892917 let reply =
893918 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+ );
898929
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;
904932 let from = peer(25);
905933 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;
907935 let pending = both(&pair(&step, "__Host-forge_2fa").unwrap(), &cookie);
908-
909936 for _ in 0..TWO_FACTOR_USER_LIMIT {
910937 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");
912939 }
913940 let code = totp::code_at(
914941 &shared,
@@ -920,7 +947,7 @@ mod tests {
920947 ),
921948 );
922949 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");
925952 }
926953 }