Page tests share their fixtures and merge into tables
Browse files at this commitb00f3f4cccccd1fa6327b8a807dc66feb5d43fb2Parent: d052c07- test
- page tests share their fixtures and merge into tables
6 changed files +398 -361
- crates/forge-web/src/pages/media.rs +75 -48
- crates/forge-web/src/pages/new_repo.rs +103 -113
- crates/forge-web/src/pages/reactions_tests.rs +69 -80
- crates/forge-web/src/pages/register.rs +85 -44
- crates/forge-web/src/pages/security_tab_tests.rs +8 -15
- crates/forge-web/src/pages/snippets.rs +58 -61
crates/forge-web/src/pages/media.rsmodified+75 -48
| @@ -339,97 +339,120 @@ mod tests { | ||
| 339 | 339 | .to_vec() |
| 340 | 340 | } |
| 341 | 341 | |
| 342 | + /// The public route, read-only, on one fixture: the linked object as a download, HEAD, and | |
| 343 | + /// the 404 cases (unlinked object, non-pointer, absent path, unknown repo). | |
| 342 | 344 | #[tokio::test] |
| 343 | - async fn media_serves_the_linked_object_as_a_download() { | |
| 345 | + async fn public_media_serves_downloads_heads_and_404s() { | |
| 344 | 346 | let fx = fixture().await; |
| 347 | + | |
| 348 | + // Scenario: a linked object is served as a download with its headers. | |
| 345 | 349 | let res = get(&fx.router, "GET", "/tahinli/pub/-/media/main/big.bin").await; |
| 346 | - assert_eq!(res.status(), StatusCode::OK); | |
| 347 | - assert_eq!(header_str(&res, "content-disposition"), "attachment"); | |
| 348 | - assert_eq!(header_str(&res, "x-content-type-options"), "nosniff"); | |
| 349 | - assert_eq!(header_str(&res, "content-type"), OCTET); | |
| 350 | + assert_eq!(res.status(), StatusCode::OK, "linked object"); | |
| 351 | + assert_eq!( | |
| 352 | + header_str(&res, "content-disposition"), | |
| 353 | + "attachment", | |
| 354 | + "linked object disposition" | |
| 355 | + ); | |
| 356 | + assert_eq!( | |
| 357 | + header_str(&res, "x-content-type-options"), | |
| 358 | + "nosniff", | |
| 359 | + "linked object nosniff" | |
| 360 | + ); | |
| 361 | + assert_eq!( | |
| 362 | + header_str(&res, "content-type"), | |
| 363 | + OCTET, | |
| 364 | + "linked object content type" | |
| 365 | + ); | |
| 350 | 366 | assert_eq!( |
| 351 | 367 | header_str(&res, "content-length"), |
| 352 | - fx.object.len().to_string() | |
| 368 | + fx.object.len().to_string(), | |
| 369 | + "linked object length" | |
| 353 | 370 | ); |
| 354 | - assert_eq!(header_str(&res, "etag"), format!("\"{}\"", fx.oid)); | |
| 355 | - assert_eq!(body_bytes(res).await, fx.object); | |
| 356 | - } | |
| 371 | + assert_eq!( | |
| 372 | + header_str(&res, "etag"), | |
| 373 | + format!("\"{}\"", fx.oid), | |
| 374 | + "linked object etag" | |
| 375 | + ); | |
| 376 | + assert_eq!(body_bytes(res).await, fx.object, "linked object body"); | |
| 357 | 377 | |
| 358 | - #[tokio::test] | |
| 359 | - async fn head_has_headers_and_no_body() { | |
| 360 | - let fx = fixture().await; | |
| 378 | + // Scenario: HEAD has the headers and no body. | |
| 361 | 379 | let res = get(&fx.router, "HEAD", "/tahinli/pub/-/media/main/big.bin").await; |
| 362 | - assert_eq!(res.status(), StatusCode::OK); | |
| 380 | + assert_eq!(res.status(), StatusCode::OK, "head"); | |
| 363 | 381 | assert_eq!( |
| 364 | 382 | header_str(&res, "content-length"), |
| 365 | - fx.object.len().to_string() | |
| 383 | + fx.object.len().to_string(), | |
| 384 | + "head length" | |
| 366 | 385 | ); |
| 367 | - assert!(body_bytes(res).await.is_empty()); | |
| 368 | - } | |
| 386 | + assert!(body_bytes(res).await.is_empty(), "head body"); | |
| 369 | 387 | |
| 370 | - #[tokio::test] | |
| 371 | - async fn missing_object_is_404() { | |
| 372 | - let fx = fixture().await; | |
| 373 | - // The pointer exists, but the repo does not link its object. | |
| 388 | + // Scenario: the pointer exists, but the repo does not link its object. | |
| 374 | 389 | let res = get(&fx.router, "GET", "/tahinli/pub/-/media/main/missing.bin").await; |
| 375 | - assert_eq!(res.status(), StatusCode::NOT_FOUND); | |
| 376 | - } | |
| 390 | + assert_eq!(res.status(), StatusCode::NOT_FOUND, "unlinked object"); | |
| 377 | 391 | |
| 378 | - #[tokio::test] | |
| 379 | - async fn non_pointer_and_missing_path_are_404() { | |
| 380 | - let fx = fixture().await; | |
| 392 | + // Scenario: a non-pointer file, an absent path and an unknown repo all 404. | |
| 381 | 393 | let text = get(&fx.router, "GET", "/tahinli/pub/-/media/main/plain.txt").await; |
| 382 | - assert_eq!(text.status(), StatusCode::NOT_FOUND); | |
| 394 | + assert_eq!(text.status(), StatusCode::NOT_FOUND, "non-pointer"); | |
| 383 | 395 | let absent = get(&fx.router, "GET", "/tahinli/pub/-/media/main/nope.bin").await; |
| 384 | - assert_eq!(absent.status(), StatusCode::NOT_FOUND); | |
| 396 | + assert_eq!(absent.status(), StatusCode::NOT_FOUND, "absent path"); | |
| 385 | 397 | let unknown_repo = get(&fx.router, "GET", "/tahinli/nope/-/media/main/big.bin").await; |
| 386 | - assert_eq!(unknown_repo.status(), StatusCode::NOT_FOUND); | |
| 398 | + assert_eq!(unknown_repo.status(), StatusCode::NOT_FOUND, "unknown repo"); | |
| 387 | 399 | } |
| 388 | 400 | |
| 401 | + /// Private media and signed URLs, read-only, on one fixture: a private repo needs a valid | |
| 402 | + /// token, and `media_url` encodes paths and signs only private repos. | |
| 389 | 403 | #[tokio::test] |
| 390 | - async fn private_repo_needs_a_valid_token() { | |
| 404 | + async fn private_media_needs_a_valid_token_and_urls_sign_private_repos_only() { | |
| 391 | 405 | let fx = fixture().await; |
| 392 | 406 | let path = "/tahinli/priv/-/media/main/big.bin"; |
| 407 | + | |
| 408 | + // Scenario: no token is a 404. | |
| 393 | 409 | let none = get(&fx.router, "GET", path).await; |
| 394 | - assert_eq!(none.status(), StatusCode::NOT_FOUND); | |
| 410 | + assert_eq!(none.status(), StatusCode::NOT_FOUND, "no token"); | |
| 395 | 411 | |
| 412 | + // Scenario: a valid token serves the object. | |
| 396 | 413 | let now = now_unix(); |
| 397 | 414 | let valid = sign_raw(&fx.state.key, &fx.private.id, now + 60); |
| 398 | 415 | let ok = get(&fx.router, "GET", &format!("{path}?token={valid}")).await; |
| 399 | - assert_eq!(ok.status(), StatusCode::OK); | |
| 400 | - assert_eq!(body_bytes(ok).await, fx.object); | |
| 416 | + assert_eq!(ok.status(), StatusCode::OK, "valid token"); | |
| 417 | + assert_eq!(body_bytes(ok).await, fx.object, "valid token body"); | |
| 401 | 418 | |
| 419 | + // Scenario: an expired token is a 404. | |
| 402 | 420 | let expired = sign_raw(&fx.state.key, &fx.private.id, now - 1); |
| 403 | 421 | let res = get(&fx.router, "GET", &format!("{path}?token={expired}")).await; |
| 404 | - assert_eq!(res.status(), StatusCode::NOT_FOUND); | |
| 422 | + assert_eq!(res.status(), StatusCode::NOT_FOUND, "expired token"); | |
| 405 | 423 | |
| 406 | - // A token signed for the public repo does not open the private one. | |
| 424 | + // Scenario: a token signed for the public repo does not open the private one. | |
| 407 | 425 | let other = sign_raw(&fx.state.key, &fx.public.id, now + 60); |
| 408 | 426 | let res = get(&fx.router, "GET", &format!("{path}?token={other}")).await; |
| 409 | - assert_eq!(res.status(), StatusCode::NOT_FOUND); | |
| 410 | - } | |
| 427 | + assert_eq!(res.status(), StatusCode::NOT_FOUND, "token for another repo"); | |
| 411 | 428 | |
| 412 | - #[tokio::test] | |
| 413 | - async fn media_url_encodes_and_signs_private_repos_only() { | |
| 414 | - let fx = fixture().await; | |
| 429 | + // Scenario: a public URL is encoded and unsigned. | |
| 415 | 430 | let public = media_url(&fx.state, &fx.public, "main", "a b/big.bin"); |
| 416 | 431 | assert_eq!( |
| 417 | 432 | public, |
| 418 | - "http://127.0.0.1:7661/tahinli/pub/-/media/main/a%20b/big.bin" | |
| 433 | + "http://127.0.0.1:7661/tahinli/pub/-/media/main/a%20b/big.bin", | |
| 434 | + "public url" | |
| 419 | 435 | ); |
| 436 | + | |
| 437 | + // Scenario: a private URL carries a token that verifies for the private repo. | |
| 420 | 438 | let private = media_url(&fx.state, &fx.private, "main", "big.bin"); |
| 421 | 439 | assert!( |
| 422 | - private.starts_with("http://127.0.0.1:7661/tahinli/priv/-/media/main/big.bin?token=") | |
| 440 | + private.starts_with("http://127.0.0.1:7661/tahinli/priv/-/media/main/big.bin?token="), | |
| 441 | + "private url prefix" | |
| 423 | 442 | ); |
| 424 | 443 | let token = private |
| 425 | 444 | .split_once("?token=") |
| 426 | 445 | .map(|(_, t)| t) |
| 427 | 446 | .unwrap_or_default(); |
| 428 | - assert!(verify_raw(&fx.state.key, token, &fx.private.id, now_unix())); | |
| 447 | + assert!( | |
| 448 | + verify_raw(&fx.state.key, token, &fx.private.id, now_unix()), | |
| 449 | + "private url token" | |
| 450 | + ); | |
| 429 | 451 | } |
| 430 | 452 | |
| 431 | 453 | /// A branch named `media` must not shadow the media route. The raw route serves its files, |
| 432 | - /// and the URL from `media_url` still serves the linked LFS object. | |
| 454 | + /// and the URL from `media_url` still serves the linked LFS object. This test pushes a | |
| 455 | + /// branch, so it keeps its own fixture. | |
| 433 | 456 | #[tokio::test] |
| 434 | 457 | async fn branch_named_media_does_not_shadow_the_media_route() { |
| 435 | 458 | let fx = fixture().await; |
| @@ -446,19 +469,23 @@ mod tests { | ||
| 446 | 469 | ); |
| 447 | 470 | |
| 448 | 471 | let readme = get(&fx.router, "GET", "/tahinli/pub/media/README.md").await; |
| 449 | - assert_eq!(readme.status(), StatusCode::OK); | |
| 450 | - assert_eq!(body_bytes(readme).await, b"# On a media branch\n".to_vec()); | |
| 472 | + assert_eq!(readme.status(), StatusCode::OK, "branch raw file"); | |
| 473 | + assert_eq!( | |
| 474 | + body_bytes(readme).await, | |
| 475 | + b"# On a media branch\n".to_vec(), | |
| 476 | + "branch raw body" | |
| 477 | + ); | |
| 451 | 478 | |
| 452 | 479 | // `-` is never a ref, so a raw path under it is not resolved. |
| 453 | 480 | let dash = get(&fx.router, "GET", "/tahinli/pub/-/README.md").await; |
| 454 | - assert_eq!(dash.status(), StatusCode::NOT_FOUND); | |
| 481 | + assert_eq!(dash.status(), StatusCode::NOT_FOUND, "dash is not a ref"); | |
| 455 | 482 | |
| 456 | 483 | let url = media_url(&fx.state, &fx.public, "main", "big.bin"); |
| 457 | 484 | let path = url |
| 458 | 485 | .strip_prefix("http://127.0.0.1:7661") |
| 459 | 486 | .expect("configured base"); |
| 460 | 487 | let media = get(&fx.router, "GET", path).await; |
| 461 | - assert_eq!(media.status(), StatusCode::OK); | |
| 462 | - assert_eq!(body_bytes(media).await, fx.object); | |
| 488 | + assert_eq!(media.status(), StatusCode::OK, "media route still wins"); | |
| 489 | + assert_eq!(body_bytes(media).await, fx.object, "media body"); | |
| 463 | 490 | } |
| 464 | 491 | } |
crates/forge-web/src/pages/new_repo.rsmodified+103 -113
| @@ -436,64 +436,16 @@ mod tests { | ||
| 436 | 436 | |
| 437 | 437 | const CSRF: &str = "new-repo-test-csrf"; |
| 438 | 438 | |
| 439 | - /// POSTs `/new` as tahinli with the given extra fields; returns the status and the label count | |
| 440 | - /// of the repo it created, or `None` when no repo was created. | |
| 441 | - async fn create_with(fields: &[(&str, &str)]) -> (u16, Option<usize>) { | |
| 442 | - let dir = tempfile::tempdir().expect("tempdir"); | |
| 443 | - let state = crate::test_support::test_state(dir.path()).await; | |
| 444 | - let store: Store = state.store.clone(); | |
| 445 | - let router: Router = crate::test_support::app_router(state); | |
| 446 | - let tahinli = store | |
| 447 | - .create_user("tahinli", None, "x", false) | |
| 448 | - .await | |
| 449 | - .expect("tahinli"); | |
| 450 | - crate::test_support::create_session(&store, &tahinli.id, "tahinli-token", CSRF).await; | |
| 451 | - let cookie = format!("__Host-{SESSION_COOKIE}=tahinli-token"); | |
| 452 | - | |
| 453 | - let mut pairs = vec![("csrf", CSRF), ("name", "fresh")]; | |
| 454 | - pairs.extend_from_slice(fields); | |
| 455 | - let body = form_urlencoded::Serializer::new(String::new()) | |
| 456 | - .extend_pairs(pairs) | |
| 457 | - .finish(); | |
| 458 | - let req = Request::builder() | |
| 459 | - .method("POST") | |
| 460 | - .uri("/new") | |
| 461 | - .header("cookie", cookie) | |
| 462 | - .header("content-type", "application/x-www-form-urlencoded") | |
| 463 | - .body(Body::from(body)) | |
| 464 | - .expect("request"); | |
| 465 | - let res = router.handle(req).await; | |
| 466 | - let status = res.status().as_u16(); | |
| 467 | - let _ = to_bytes(res.into_body(), usize::MAX).await.expect("body"); | |
| 468 | - | |
| 469 | - let repo = store | |
| 470 | - .repo_by_owner_and_name("tahinli", "fresh") | |
| 471 | - .await | |
| 472 | - .expect("query"); | |
| 473 | - let count = match repo { | |
| 474 | - Some(repo) => Some(store.list_labels(&repo.id).await.expect("labels").len()), | |
| 475 | - None => None, | |
| 476 | - }; | |
| 477 | - (status, count) | |
| 478 | - } | |
| 479 | - | |
| 480 | - #[tokio::test] | |
| 481 | - async fn new_repo_with_default_labels_checked_gets_nine() { | |
| 482 | - let (status, count) = create_with(&[("default_labels", "on")]).await; | |
| 483 | - assert_eq!(status, 303); | |
| 484 | - assert_eq!(count, Some(9)); | |
| 485 | - } | |
| 486 | - | |
| 487 | - #[tokio::test] | |
| 488 | - async fn new_repo_with_default_labels_unchecked_gets_none() { | |
| 489 | - let (status, count) = create_with(&[]).await; | |
| 490 | - assert_eq!(status, 303); | |
| 491 | - assert_eq!(count, Some(0)); | |
| 439 | + /// One store and router with `tahinli` signed in. Every scenario below uses its own repo name, | |
| 440 | + /// so they share this site. | |
| 441 | + struct Site { | |
| 442 | + store: Store, | |
| 443 | + router: Router, | |
| 444 | + tahinli_id: String, | |
| 445 | + _dir: tempfile::TempDir, | |
| 492 | 446 | } |
| 493 | 447 | |
| 494 | - /// POSTs `/new` as tahinli with the mirror fields and returns the status, the store and the repo | |
| 495 | - /// created (if any). The form's token is the shared CSRF value. | |
| 496 | - async fn submit_with_mirror(fields: &[(&str, &str)]) -> (u16, Store, Option<forge_core::Repo>) { | |
| 448 | + async fn site() -> Site { | |
| 497 | 449 | let dir = tempfile::tempdir().expect("tempdir"); |
| 498 | 450 | let state = crate::test_support::test_state(dir.path()).await; |
| 499 | 451 | let store: Store = state.store.clone(); |
| @@ -503,9 +455,18 @@ mod tests { | ||
| 503 | 455 | .await |
| 504 | 456 | .expect("tahinli"); |
| 505 | 457 | crate::test_support::create_session(&store, &tahinli.id, "tahinli-token", CSRF).await; |
| 506 | - let cookie = format!("__Host-{SESSION_COOKIE}=tahinli-token"); | |
| 458 | + Site { | |
| 459 | + store, | |
| 460 | + router, | |
| 461 | + tahinli_id: tahinli.id, | |
| 462 | + _dir: dir, | |
| 463 | + } | |
| 464 | + } | |
| 507 | 465 | |
| 508 | - let mut pairs = vec![("csrf", CSRF), ("name", "mirrored")]; | |
| 466 | + /// POSTs `/new` as tahinli for a repo called `name` with the extra fields. Returns the status | |
| 467 | + /// and the repo created, if any. The form's token is the shared CSRF value. | |
| 468 | + async fn post_new(site: &Site, name: &str, fields: &[(&str, &str)]) -> (u16, Option<forge_core::Repo>) { | |
| 469 | + let mut pairs = vec![("csrf", CSRF), ("name", name)]; | |
| 509 | 470 | pairs.extend_from_slice(fields); |
| 510 | 471 | let body = form_urlencoded::Serializer::new(String::new()) |
| 511 | 472 | .extend_pairs(pairs) |
| @@ -513,97 +474,126 @@ mod tests { | ||
| 513 | 474 | let req = Request::builder() |
| 514 | 475 | .method("POST") |
| 515 | 476 | .uri("/new") |
| 516 | - .header("cookie", cookie) | |
| 477 | + .header("cookie", format!("__Host-{SESSION_COOKIE}=tahinli-token")) | |
| 517 | 478 | .header("content-type", "application/x-www-form-urlencoded") |
| 518 | 479 | .body(Body::from(body)) |
| 519 | 480 | .expect("request"); |
| 520 | - let res = router.handle(req).await; | |
| 481 | + let res = site.router.handle(req).await; | |
| 521 | 482 | let status = res.status().as_u16(); |
| 522 | - let body = to_bytes(res.into_body(), usize::MAX).await.expect("body"); | |
| 523 | - let text = String::from_utf8_lossy(&body).into_owned(); | |
| 483 | + let bytes = to_bytes(res.into_body(), usize::MAX).await.expect("body"); | |
| 484 | + let text = String::from_utf8_lossy(&bytes).into_owned(); | |
| 524 | 485 | assert!( |
| 525 | 486 | !text.contains("hunter2-secret"), |
| 526 | - "the mirror password is rendered" | |
| 487 | + "case {name}: the mirror password is rendered" | |
| 527 | 488 | ); |
| 528 | - let repo = store | |
| 529 | - .repo_by_owner_and_name("tahinli", "mirrored") | |
| 489 | + let repo = site | |
| 490 | + .store | |
| 491 | + .repo_by_owner_and_name("tahinli", name) | |
| 530 | 492 | .await |
| 531 | 493 | .expect("query"); |
| 532 | - (status, store, repo) | |
| 494 | + (status, repo) | |
| 533 | 495 | } |
| 534 | 496 | |
| 497 | + /// The default labels are added only when the box is checked. | |
| 535 | 498 | #[tokio::test] |
| 536 | - async fn new_repo_with_mirror_url_creates_a_pull_mirror() { | |
| 537 | - let (status, store, repo) = submit_with_mirror(&[ | |
| 538 | - ("mirror_url", "https://example.com/upstream/proj.git"), | |
| 539 | - ("mirror_username", "bot"), | |
| 540 | - ("mirror_secret", "hunter2-secret"), | |
| 541 | - ("mirror_interval", "60"), | |
| 542 | - ]) | |
| 499 | + async fn default_labels_are_added_only_when_checked() { | |
| 500 | + let site = site().await; | |
| 501 | + let cases: [(&str, &[(&str, &str)], usize); 2] = [ | |
| 502 | + ("checked", &[("default_labels", "on")], 9), | |
| 503 | + ("unchecked", &[], 0), | |
| 504 | + ]; | |
| 505 | + for (name, fields, want) in cases { | |
| 506 | + let (status, repo) = post_new(&site, name, fields).await; | |
| 507 | + assert_eq!(status, 303, "case {name}: status"); | |
| 508 | + let repo = repo.unwrap_or_else(|| panic!("case {name}: the repo is created")); | |
| 509 | + let labels = site | |
| 510 | + .store | |
| 511 | + .list_labels(&repo.id) | |
| 512 | + .await | |
| 513 | + .expect("labels") | |
| 514 | + .len(); | |
| 515 | + assert_eq!(labels, want, "case {name}: label count"); | |
| 516 | + } | |
| 517 | + } | |
| 518 | + | |
| 519 | + /// Mirror fields: a valid URL makes a pull mirror, a loopback URL is refused and creates | |
| 520 | + /// nothing, and a blank URL makes the repo without a mirror. | |
| 521 | + #[tokio::test] | |
| 522 | + async fn mirror_url_decides_whether_a_pull_mirror_is_made() { | |
| 523 | + let site = site().await; | |
| 524 | + | |
| 525 | + // Case: a valid URL creates a pull mirror with the given settings. | |
| 526 | + let (status, repo) = post_new( | |
| 527 | + &site, | |
| 528 | + "mirror-ok", | |
| 529 | + &[ | |
| 530 | + ("mirror_url", "https://example.com/upstream/proj.git"), | |
| 531 | + ("mirror_username", "bot"), | |
| 532 | + ("mirror_secret", "hunter2-secret"), | |
| 533 | + ("mirror_interval", "60"), | |
| 534 | + ], | |
| 535 | + ) | |
| 543 | 536 | .await; |
| 544 | - assert_eq!(status, 303); | |
| 545 | - let repo = repo.expect("the repo is created"); | |
| 546 | - let mirror = store | |
| 537 | + assert_eq!(status, 303, "valid mirror: status"); | |
| 538 | + let repo = repo.expect("valid mirror: the repo is created"); | |
| 539 | + let mirror = site | |
| 540 | + .store | |
| 547 | 541 | .pull_mirror(&repo.id) |
| 548 | 542 | .await |
| 549 | 543 | .expect("query") |
| 550 | - .expect("a pull mirror"); | |
| 551 | - assert_eq!(mirror.direction, MirrorDirection::Pull); | |
| 552 | - assert_eq!(mirror.url, "https://example.com/upstream/proj.git"); | |
| 553 | - assert_eq!(mirror.username, "bot"); | |
| 554 | - assert!(mirror.has_secret); | |
| 555 | - assert_eq!(mirror.interval_minutes, 60); | |
| 544 | + .expect("valid mirror: a pull mirror"); | |
| 545 | + assert_eq!(mirror.direction, MirrorDirection::Pull, "valid mirror: direction"); | |
| 546 | + assert_eq!( | |
| 547 | + mirror.url, "https://example.com/upstream/proj.git", | |
| 548 | + "valid mirror: url" | |
| 549 | + ); | |
| 550 | + assert_eq!(mirror.username, "bot", "valid mirror: username"); | |
| 551 | + assert!(mirror.has_secret, "valid mirror: secret kept"); | |
| 552 | + assert_eq!(mirror.interval_minutes, 60, "valid mirror: interval"); | |
| 556 | 553 | assert!( |
| 557 | 554 | mirror.next_sync_at <= now_stamp(), |
| 558 | - "the first sync is due now" | |
| 555 | + "valid mirror: the first sync is due now" | |
| 559 | 556 | ); |
| 560 | - } | |
| 561 | 557 | |
| 562 | - #[tokio::test] | |
| 563 | - async fn new_repo_with_loopback_mirror_url_is_refused_and_creates_nothing() { | |
| 564 | - let (status, store, repo) = | |
| 565 | - submit_with_mirror(&[("mirror_url", "https://127.0.0.1/upstream/proj.git")]).await; | |
| 566 | - assert_eq!(status, 200, "the form is shown again with the error"); | |
| 567 | - assert!(repo.is_none(), "no repo is created for a refused mirror"); | |
| 558 | + // Case: a loopback URL shows the form again and creates nothing. | |
| 559 | + let (status, repo) = post_new( | |
| 560 | + &site, | |
| 561 | + "mirror-loopback", | |
| 562 | + &[("mirror_url", "https://127.0.0.1/upstream/proj.git")], | |
| 563 | + ) | |
| 564 | + .await; | |
| 565 | + assert_eq!(status, 200, "loopback: the form is shown again with the error"); | |
| 566 | + assert!(repo.is_none(), "loopback: no repo is created for a refused mirror"); | |
| 567 | + | |
| 568 | + // Case: a blank URL creates the repo without a mirror. | |
| 569 | + let (status, repo) = post_new(&site, "mirror-blank", &[("mirror_url", " ")]).await; | |
| 570 | + assert_eq!(status, 303, "blank: status"); | |
| 571 | + let repo = repo.expect("blank: the repo is created"); | |
| 568 | 572 | assert!( |
| 569 | - store | |
| 570 | - .repo_by_owner_and_name("tahinli", "mirrored") | |
| 573 | + site.store | |
| 574 | + .pull_mirror(&repo.id) | |
| 571 | 575 | .await |
| 572 | 576 | .expect("query") |
| 573 | - .is_none() | |
| 577 | + .is_none(), | |
| 578 | + "blank: no mirror" | |
| 574 | 579 | ); |
| 575 | 580 | } |
| 576 | 581 | |
| 577 | - #[tokio::test] | |
| 578 | - async fn new_repo_with_blank_mirror_url_creates_no_mirror() { | |
| 579 | - let (status, store, repo) = submit_with_mirror(&[("mirror_url", " ")]).await; | |
| 580 | - assert_eq!(status, 303); | |
| 581 | - let repo = repo.expect("the repo is created"); | |
| 582 | - assert!(store.pull_mirror(&repo.id).await.expect("query").is_none()); | |
| 583 | - } | |
| 584 | - | |
| 582 | + /// A viewer who chose Turkish gets the Turkish form. | |
| 585 | 583 | #[tokio::test] |
| 586 | 584 | async fn the_form_is_in_turkish_for_a_turkish_viewer() { |
| 587 | - let dir = tempfile::tempdir().expect("tempdir"); | |
| 588 | - let state = crate::test_support::test_state(dir.path()).await; | |
| 589 | - let store: Store = state.store.clone(); | |
| 590 | - let router: Router = crate::test_support::app_router(state); | |
| 591 | - let tahinli = store | |
| 592 | - .create_user("tahinli", None, "x", false) | |
| 593 | - .await | |
| 594 | - .expect("tahinli"); | |
| 595 | - store | |
| 596 | - .set_ui_prefs(&tahinli.id, "auto", "tr") | |
| 585 | + let site = site().await; | |
| 586 | + site.store | |
| 587 | + .set_ui_prefs(&site.tahinli_id, "auto", "tr") | |
| 597 | 588 | .await |
| 598 | 589 | .expect("prefs"); |
| 599 | - crate::test_support::create_session(&store, &tahinli.id, "tahinli-token", CSRF).await; | |
| 600 | 590 | let req = Request::builder() |
| 601 | 591 | .method("GET") |
| 602 | 592 | .uri("/new") |
| 603 | 593 | .header("cookie", format!("__Host-{SESSION_COOKIE}=tahinli-token")) |
| 604 | 594 | .body(Body::empty()) |
| 605 | 595 | .expect("request"); |
| 606 | - let res = router.handle(req).await; | |
| 596 | + let res = site.router.handle(req).await; | |
| 607 | 597 | let status = res.status().as_u16(); |
| 608 | 598 | let body = to_bytes(res.into_body(), usize::MAX).await.expect("body"); |
| 609 | 599 | let text = String::from_utf8_lossy(&body).into_owned(); |
crates/forge-web/src/pages/reactions_tests.rsmodified+69 -80
| @@ -152,14 +152,17 @@ async fn on_issue(s: &Seed) -> Vec<forge_core::ReactionSummary> { | ||
| 152 | 152 | .unwrap_or_default() |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | +/// Reactions that succeed: a signed-in reaction toggles on and off and redirects to the item's | |
| 156 | +/// anchor, and a reaction on a comment anchors on that comment. One seeded site serves both. | |
| 155 | 157 | #[tokio::test] |
| 156 | -async fn signed_in_reaction_toggles_on_and_off_and_redirects_to_anchor() { | |
| 158 | +async fn reactions_toggle_and_comment_reactions_anchor_on_their_target() { | |
| 157 | 159 | let s = seeded().await; |
| 158 | 160 | let uri = issue_uri(s.issue_number); |
| 159 | - let body = fields("issue", &s.issue_id, "heart"); | |
| 160 | 161 | |
| 162 | + // Case: a signed-in reaction turns on and redirects to the issue anchor. | |
| 163 | + let body = fields("issue", &s.issue_id, "heart"); | |
| 161 | 164 | let on = post(&s.site, &uri, Some(&s.bob_cookie), &body).await; |
| 162 | - assert_eq!(on.status, 303); | |
| 165 | + assert_eq!(on.status, 303, "toggle on: status"); | |
| 163 | 166 | assert_eq!( |
| 164 | 167 | on.location.as_deref(), |
| 165 | 168 | Some( |
| @@ -168,23 +171,21 @@ async fn signed_in_reaction_toggles_on_and_off_and_redirects_to_anchor() { | ||
| 168 | 171 | s.issue_number, s.issue_number |
| 169 | 172 | ) |
| 170 | 173 | .as_str() |
| 171 | - ) | |
| 174 | + ), | |
| 175 | + "toggle on: anchor" | |
| 172 | 176 | ); |
| 173 | 177 | let summaries = on_issue(&s).await; |
| 174 | - assert_eq!(summaries.len(), 1); | |
| 175 | - assert_eq!(summaries[0].reaction, Reaction::Heart); | |
| 176 | - assert_eq!(summaries[0].count, 1); | |
| 177 | - assert_eq!(summaries[0].users, vec!["bob"]); | |
| 178 | + assert_eq!(summaries.len(), 1, "toggle on: one reaction"); | |
| 179 | + assert_eq!(summaries[0].reaction, Reaction::Heart, "toggle on: kind"); | |
| 180 | + assert_eq!(summaries[0].count, 1, "toggle on: count"); | |
| 181 | + assert_eq!(summaries[0].users, vec!["bob"], "toggle on: who"); | |
| 178 | 182 | |
| 183 | + // Case: the same reaction again turns it off. | |
| 179 | 184 | let off = post(&s.site, &uri, Some(&s.bob_cookie), &body).await; |
| 180 | - assert_eq!(off.status, 303); | |
| 181 | - assert!(on_issue(&s).await.is_empty()); | |
| 182 | -} | |
| 185 | + assert_eq!(off.status, 303, "toggle off: status"); | |
| 186 | + assert!(on_issue(&s).await.is_empty(), "toggle off: nothing left"); | |
| 183 | 187 | |
| 184 | -#[tokio::test] | |
| 185 | -async fn comment_reaction_anchors_on_the_comment() { | |
| 186 | - let s = seeded().await; | |
| 187 | - let uri = issue_uri(s.issue_number); | |
| 188 | + // Case: a reaction on a comment anchors on the comment. | |
| 188 | 189 | let reply = post( |
| 189 | 190 | &s.site, |
| 190 | 191 | &uri, |
| @@ -192,7 +193,7 @@ async fn comment_reaction_anchors_on_the_comment() { | ||
| 192 | 193 | &fields("comment", &s.comment_id, "eyes"), |
| 193 | 194 | ) |
| 194 | 195 | .await; |
| 195 | - assert_eq!(reply.status, 303); | |
| 196 | + assert_eq!(reply.status, 303, "comment: status"); | |
| 196 | 197 | assert_eq!( |
| 197 | 198 | reply.location.as_deref(), |
| 198 | 199 | Some( |
| @@ -201,7 +202,8 @@ async fn comment_reaction_anchors_on_the_comment() { | ||
| 201 | 202 | s.issue_number, s.comment_id |
| 202 | 203 | ) |
| 203 | 204 | .as_str() |
| 204 | - ) | |
| 205 | + ), | |
| 206 | + "comment: anchor" | |
| 205 | 207 | ); |
| 206 | 208 | let map = s |
| 207 | 209 | .site |
| @@ -213,13 +215,20 @@ async fn comment_reaction_anchors_on_the_comment() { | ||
| 213 | 215 | ) |
| 214 | 216 | .await |
| 215 | 217 | .expect("reactions"); |
| 216 | - assert!(map[&s.comment_id][0].reacted_by_viewer); | |
| 218 | + assert!( | |
| 219 | + map[&s.comment_id][0].reacted_by_viewer, | |
| 220 | + "comment: reacted by the viewer" | |
| 221 | + ); | |
| 217 | 222 | } |
| 218 | 223 | |
| 224 | +/// Reactions that must be refused, in one seeded site. Every refusal leaves the issue without | |
| 225 | +/// reactions, and the archived-repo case runs last because it changes the repo. | |
| 219 | 226 | #[tokio::test] |
| 220 | -async fn missing_or_wrong_csrf_is_forbidden_and_changes_nothing() { | |
| 227 | +async fn refused_reactions_are_rejected_and_change_nothing() { | |
| 221 | 228 | let s = seeded().await; |
| 222 | 229 | let uri = issue_uri(s.issue_number); |
| 230 | + | |
| 231 | + // Case: a missing or wrong CSRF token is 403. | |
| 223 | 232 | let no_token = vec![ |
| 224 | 233 | ("target_kind", "issue"), |
| 225 | 234 | ("target_id", s.issue_id.as_str()), |
| @@ -229,7 +238,8 @@ async fn missing_or_wrong_csrf_is_forbidden_and_changes_nothing() { | ||
| 229 | 238 | post(&s.site, &uri, Some(&s.bob_cookie), &no_token) |
| 230 | 239 | .await |
| 231 | 240 | .status, |
| 232 | - 403 | |
| 241 | + 403, | |
| 242 | + "missing csrf" | |
| 233 | 243 | ); |
| 234 | 244 | let wrong = vec![ |
| 235 | 245 | ("csrf", "not-the-token"), |
| @@ -241,24 +251,21 @@ async fn missing_or_wrong_csrf_is_forbidden_and_changes_nothing() { | ||
| 241 | 251 | post(&s.site, &uri, Some(&s.bob_cookie), &wrong) |
| 242 | 252 | .await |
| 243 | 253 | .status, |
| 244 | - 403 | |
| 254 | + 403, | |
| 255 | + "wrong csrf" | |
| 245 | 256 | ); |
| 246 | - assert!(on_issue(&s).await.is_empty()); | |
| 247 | -} | |
| 257 | + assert!(on_issue(&s).await.is_empty(), "csrf: nothing changes"); | |
| 248 | 258 | |
| 249 | -#[tokio::test] | |
| 250 | -async fn signed_out_viewer_is_sent_to_login() { | |
| 251 | - let s = seeded().await; | |
| 252 | - let uri = issue_uri(s.issue_number); | |
| 259 | + // Case: a signed-out viewer is sent to login. | |
| 253 | 260 | let reply = post(&s.site, &uri, None, &fields("issue", &s.issue_id, "+1")).await; |
| 254 | - assert_eq!(reply.status, 303); | |
| 255 | - assert!(reply.location.unwrap_or_default().starts_with("/login")); | |
| 256 | - assert!(on_issue(&s).await.is_empty()); | |
| 257 | -} | |
| 261 | + assert_eq!(reply.status, 303, "signed out: status"); | |
| 262 | + assert!( | |
| 263 | + reply.location.unwrap_or_default().starts_with("/login"), | |
| 264 | + "signed out: redirect to login" | |
| 265 | + ); | |
| 266 | + assert!(on_issue(&s).await.is_empty(), "signed out: nothing changes"); | |
| 258 | 267 | |
| 259 | -#[tokio::test] | |
| 260 | -async fn hidden_repo_answers_not_found_for_a_viewer_without_access() { | |
| 261 | - let s = seeded().await; | |
| 268 | + // Case: a hidden repo answers 404 for a viewer without access. | |
| 262 | 269 | let reply = post( |
| 263 | 270 | &s.site, |
| 264 | 271 | "/tahinli/secret/issues/1/reactions", |
| @@ -266,14 +273,10 @@ async fn hidden_repo_answers_not_found_for_a_viewer_without_access() { | ||
| 266 | 273 | &fields("issue", "anything", "+1"), |
| 267 | 274 | ) |
| 268 | 275 | .await; |
| 269 | - assert_eq!(reply.status, 404); | |
| 270 | -} | |
| 276 | + assert_eq!(reply.status, 404, "hidden repo"); | |
| 271 | 277 | |
| 272 | -#[tokio::test] | |
| 273 | -async fn target_must_belong_to_this_issue() { | |
| 274 | - let s = seeded().await; | |
| 275 | - let uri = issue_uri(s.issue_number); | |
| 276 | - // An issue id from another item in the same repo is not this item's body. | |
| 278 | + // Case: a target must belong to this issue. An issue id from another item in the same repo | |
| 279 | + // is not this item's body. | |
| 277 | 280 | let wrong_issue = post( |
| 278 | 281 | &s.site, |
| 279 | 282 | &uri, |
| @@ -281,9 +284,8 @@ async fn target_must_belong_to_this_issue() { | ||
| 281 | 284 | &fields("issue", &s.other_issue_id, "+1"), |
| 282 | 285 | ) |
| 283 | 286 | .await; |
| 284 | - assert_eq!(wrong_issue.status, 404); | |
| 285 | - // A comment of the other issue (the comment belongs to the first issue) is rejected on the | |
| 286 | - // second issue's page. | |
| 287 | + assert_eq!(wrong_issue.status, 404, "issue of another item"); | |
| 288 | + // A comment of the first issue is rejected on the second issue's page. | |
| 287 | 289 | let other_uri = issue_uri(s.issue_number + 1); |
| 288 | 290 | let wrong_comment = post( |
| 289 | 291 | &s.site, |
| @@ -292,41 +294,28 @@ async fn target_must_belong_to_this_issue() { | ||
| 292 | 294 | &fields("comment", &s.comment_id, "+1"), |
| 293 | 295 | ) |
| 294 | 296 | .await; |
| 295 | - assert_eq!(wrong_comment.status, 404); | |
| 296 | - assert!(on_issue(&s).await.is_empty()); | |
| 297 | -} | |
| 297 | + assert_eq!(wrong_comment.status, 404, "comment of another issue"); | |
| 298 | + assert!(on_issue(&s).await.is_empty(), "wrong target: nothing changes"); | |
| 298 | 299 | |
| 299 | -#[tokio::test] | |
| 300 | -async fn unknown_reaction_and_target_kind_are_bad_requests() { | |
| 301 | - let s = seeded().await; | |
| 302 | - let uri = issue_uri(s.issue_number); | |
| 303 | - assert_eq!( | |
| 304 | - post( | |
| 305 | - &s.site, | |
| 306 | - &uri, | |
| 307 | - Some(&s.bob_cookie), | |
| 308 | - &fields("issue", &s.issue_id, "party") | |
| 309 | - ) | |
| 310 | - .await | |
| 311 | - .status, | |
| 312 | - 400 | |
| 313 | - ); | |
| 314 | - assert_eq!( | |
| 315 | - post( | |
| 316 | - &s.site, | |
| 317 | - &uri, | |
| 318 | - Some(&s.bob_cookie), | |
| 319 | - &fields("pull", &s.issue_id, "+1") | |
| 320 | - ) | |
| 321 | - .await | |
| 322 | - .status, | |
| 323 | - 400 | |
| 324 | - ); | |
| 325 | -} | |
| 300 | + // Case: an unknown reaction and an unknown target kind are 400. | |
| 301 | + let bad_reaction = post( | |
| 302 | + &s.site, | |
| 303 | + &uri, | |
| 304 | + Some(&s.bob_cookie), | |
| 305 | + &fields("issue", &s.issue_id, "party"), | |
| 306 | + ) | |
| 307 | + .await; | |
| 308 | + assert_eq!(bad_reaction.status, 400, "unknown reaction"); | |
| 309 | + let bad_kind = post( | |
| 310 | + &s.site, | |
| 311 | + &uri, | |
| 312 | + Some(&s.bob_cookie), | |
| 313 | + &fields("pull", &s.issue_id, "+1"), | |
| 314 | + ) | |
| 315 | + .await; | |
| 316 | + assert_eq!(bad_kind.status, 400, "unknown target kind"); | |
| 326 | 317 | |
| 327 | -#[tokio::test] | |
| 328 | -async fn archived_repo_refuses_reactions() { | |
| 329 | - let s = seeded().await; | |
| 318 | + // Case: an archived repo refuses reactions. This runs last because it archives the repo. | |
| 330 | 319 | s.site |
| 331 | 320 | .store |
| 332 | 321 | .set_repo_archived(&s.repo_id, true) |
| @@ -334,11 +323,11 @@ async fn archived_repo_refuses_reactions() { | ||
| 334 | 323 | .expect("archive"); |
| 335 | 324 | let reply = post( |
| 336 | 325 | &s.site, |
| 337 | - &issue_uri(s.issue_number), | |
| 326 | + &uri, | |
| 338 | 327 | Some(&s.bob_cookie), |
| 339 | 328 | &fields("issue", &s.issue_id, "+1"), |
| 340 | 329 | ) |
| 341 | 330 | .await; |
| 342 | - assert_eq!(reply.status, 403); | |
| 343 | - assert!(on_issue(&s).await.is_empty()); | |
| 331 | + assert_eq!(reply.status, 403, "archived repo"); | |
| 332 | + assert!(on_issue(&s).await.is_empty(), "archived: nothing changes"); | |
| 344 | 333 | } |
crates/forge-web/src/pages/register.rsmodified+85 -44
| @@ -527,7 +527,7 @@ mod tests { | ||
| 527 | 527 | let tmp = tempfile::tempdir().expect("tempdir"); |
| 528 | 528 | let (router, _state) = app(tmp.path(), Registration::Closed).await; |
| 529 | 529 | let (status, _, _) = send(&router, "GET", "/register", None, None).await; |
| 530 | - assert_eq!(status, 404); | |
| 530 | + assert_eq!(status, 404, "closed: form"); | |
| 531 | 531 | let (status, _, _) = send( |
| 532 | 532 | &router, |
| 533 | 533 | "POST", |
| @@ -536,30 +536,35 @@ mod tests { | ||
| 536 | 536 | Some("username=bob&password=x&confirm=x"), |
| 537 | 537 | ) |
| 538 | 538 | .await; |
| 539 | - assert_eq!(status, 404); | |
| 539 | + assert_eq!(status, 404, "closed: post"); | |
| 540 | 540 | } |
| 541 | 541 | |
| 542 | + /// Invite mode on one app: no invite shows no form, a bad invite is refused, a valid invite | |
| 543 | + /// makes one verified account, and the same link is spent afterwards. | |
| 542 | 544 | #[tokio::test] |
| 543 | - async fn invite_mode_needs_an_invite() { | |
| 545 | + async fn invite_mode_needs_a_valid_single_use_invite() { | |
| 544 | 546 | let tmp = tempfile::tempdir().expect("tempdir"); |
| 545 | - let (router, _state) = app(tmp.path(), Registration::Invite).await; | |
| 547 | + let (router, state) = app(tmp.path(), Registration::Invite).await; | |
| 548 | + | |
| 549 | + // Case: without an invite the form is not shown. | |
| 546 | 550 | let (status, _, html) = send(&router, "GET", "/register", None, None).await; |
| 547 | - assert_eq!(status, 200); | |
| 548 | - assert!(html.contains("Registration is by invitation only")); | |
| 551 | + assert_eq!(status, 200, "no invite: status"); | |
| 552 | + assert!( | |
| 553 | + html.contains("Registration is by invitation only"), | |
| 554 | + "no invite: notice" | |
| 555 | + ); | |
| 549 | 556 | assert!( |
| 550 | 557 | !html.contains("name=\"username\""), |
| 551 | - "no form without an invite" | |
| 558 | + "no invite: no form without an invite" | |
| 552 | 559 | ); |
| 560 | + | |
| 561 | + // Case: an invalid invite is refused. | |
| 553 | 562 | let (status, _, html) = |
| 554 | 563 | send(&router, "GET", "/register?invite=forge_nope", None, None).await; |
| 555 | - assert_eq!(status, 200); | |
| 556 | - assert!(html.contains("This invite is invalid")); | |
| 557 | - } | |
| 564 | + assert_eq!(status, 200, "bad invite: status"); | |
| 565 | + assert!(html.contains("This invite is invalid"), "bad invite: notice"); | |
| 558 | 566 | |
| 559 | - #[tokio::test] | |
| 560 | - async fn invite_creates_one_verified_account_and_the_link_is_single_use() { | |
| 561 | - let tmp = tempfile::tempdir().expect("tempdir"); | |
| 562 | - let (router, state) = app(tmp.path(), Registration::Invite).await; | |
| 567 | + // Case: a valid invite creates one verified account with the invited address. | |
| 563 | 568 | let admin = state |
| 564 | 569 | .store |
| 565 | 570 | .create_user("admin", None, "hash", true) |
| @@ -573,9 +578,12 @@ mod tests { | ||
| 573 | 578 | let uri = format!("/register?invite={token}"); |
| 574 | 579 | let (cookie, precsrf) = precsrf_for(&router, &uri).await; |
| 575 | 580 | let (status, _, html) = send(&router, "GET", &uri, Some(&cookie), None).await; |
| 576 | - assert_eq!(status, 200); | |
| 577 | - assert!(html.contains("bob@example.com"), "invited address is shown"); | |
| 578 | - assert_eq!(field_from(&html, "invite"), token); | |
| 581 | + assert_eq!(status, 200, "valid invite: form status"); | |
| 582 | + assert!( | |
| 583 | + html.contains("bob@example.com"), | |
| 584 | + "valid invite: invited address is shown" | |
| 585 | + ); | |
| 586 | + assert_eq!(field_from(&html, "invite"), token, "valid invite: token"); | |
| 579 | 587 | |
| 580 | 588 | let form = format!( |
| 581 | 589 | "precsrf={precsrf}&invite={token}&username=bob&email=mallory%40example.com\ |
| @@ -583,8 +591,12 @@ mod tests { | ||
| 583 | 591 | ); |
| 584 | 592 | let (status, headers, _) = |
| 585 | 593 | send(&router, "POST", "/register", Some(&cookie), Some(&form)).await; |
| 586 | - assert_eq!(status, 303); | |
| 587 | - assert_eq!(headers["location"].to_str().unwrap(), "/"); | |
| 594 | + assert_eq!(status, 303, "valid invite: status"); | |
| 595 | + assert_eq!( | |
| 596 | + headers["location"].to_str().unwrap(), | |
| 597 | + "/", | |
| 598 | + "valid invite: redirect" | |
| 599 | + ); | |
| 588 | 600 | |
| 589 | 601 | let bob = state |
| 590 | 602 | .store |
| @@ -595,11 +607,14 @@ mod tests { | ||
| 595 | 607 | assert_eq!( |
| 596 | 608 | bob.email.as_deref(), |
| 597 | 609 | Some("bob@example.com"), |
| 598 | - "the invite's address wins" | |
| 610 | + "valid invite: the invite's address wins" | |
| 611 | + ); | |
| 612 | + assert!( | |
| 613 | + state.store.email_is_verified(&bob.id).await.unwrap(), | |
| 614 | + "valid invite: verified" | |
| 599 | 615 | ); |
| 600 | - assert!(state.store.email_is_verified(&bob.id).await.unwrap()); | |
| 601 | 616 | |
| 602 | - // The same link now answers as spent, and creates nothing. | |
| 617 | + // Case: the same link now answers as spent, and creates nothing. | |
| 603 | 618 | let (cookie, precsrf) = precsrf_for(&router, &uri).await; |
| 604 | 619 | let form = format!( |
| 605 | 620 | "precsrf={precsrf}&invite={token}&username=carol&email=\ |
| @@ -607,48 +622,53 @@ mod tests { | ||
| 607 | 622 | ); |
| 608 | 623 | let (status, _, html) = |
| 609 | 624 | send(&router, "POST", "/register", Some(&cookie), Some(&form)).await; |
| 610 | - assert_eq!(status, 200); | |
| 611 | - assert!(html.contains("This invite is invalid")); | |
| 625 | + assert_eq!(status, 200, "spent: status"); | |
| 626 | + assert!(html.contains("This invite is invalid"), "spent: notice"); | |
| 612 | 627 | assert!( |
| 613 | 628 | state |
| 614 | 629 | .store |
| 615 | 630 | .user_by_username("carol") |
| 616 | 631 | .await |
| 617 | 632 | .unwrap() |
| 618 | - .is_none() | |
| 633 | + .is_none(), | |
| 634 | + "spent: no account" | |
| 619 | 635 | ); |
| 620 | 636 | } |
| 621 | 637 | |
| 638 | + /// Open registration on one app: a new account is unverified and gets a verification mail, | |
| 639 | + /// and a verify link marks its address verified once. | |
| 622 | 640 | #[tokio::test] |
| 623 | - async fn open_registration_queues_a_verification_mail_and_no_verified_mark() { | |
| 641 | + async fn open_registration_mails_a_verify_link_and_verifies_once() { | |
| 624 | 642 | let tmp = tempfile::tempdir().expect("tempdir"); |
| 625 | 643 | let (router, state) = app(tmp.path(), Registration::Open).await; |
| 644 | + | |
| 645 | + // Case: a new account is unverified and a verification link is queued for it. | |
| 626 | 646 | let (cookie, precsrf) = precsrf_for(&router, "/register").await; |
| 627 | 647 | let form = format!( |
| 628 | 648 | "precsrf={precsrf}&invite=&username=dave&email=dave%40example.com\ |
| 629 | 649 | &password=correct-horse-battery&confirm=correct-horse-battery" |
| 630 | 650 | ); |
| 631 | 651 | let (status, _, _) = send(&router, "POST", "/register", Some(&cookie), Some(&form)).await; |
| 632 | - assert_eq!(status, 303); | |
| 652 | + assert_eq!(status, 303, "open: status"); | |
| 633 | 653 | let dave = state |
| 634 | 654 | .store |
| 635 | 655 | .user_by_username("dave") |
| 636 | 656 | .await |
| 637 | 657 | .unwrap() |
| 638 | 658 | .expect("account"); |
| 639 | - assert!(!state.store.email_is_verified(&dave.id).await.unwrap()); | |
| 659 | + assert!( | |
| 660 | + !state.store.email_is_verified(&dave.id).await.unwrap(), | |
| 661 | + "open: not verified yet" | |
| 662 | + ); | |
| 640 | 663 | let mail = state.store.recent_mail(10).await.unwrap(); |
| 641 | 664 | assert!( |
| 642 | 665 | mail.iter() |
| 643 | 666 | .any(|m| m.to_addr == "dave@example.com" && m.body.contains("/verify/")), |
| 644 | - "a verification link is queued" | |
| 667 | + "open: a verification link is queued" | |
| 645 | 668 | ); |
| 646 | - } | |
| 647 | 669 | |
| 648 | - #[tokio::test] | |
| 649 | - async fn verify_link_marks_the_address_verified_once() { | |
| 650 | - let tmp = tempfile::tempdir().expect("tempdir"); | |
| 651 | - let (router, state) = app(tmp.path(), Registration::Open).await; | |
| 670 | + // Case: the verify link marks the address verified once. The mail is picked by its | |
| 671 | + // recipient, since the dave mail is queued too. | |
| 652 | 672 | let user = state |
| 653 | 673 | .store |
| 654 | 674 | .create_user("erin", Some("erin@example.com"), "hash", false) |
| @@ -657,26 +677,47 @@ mod tests { | ||
| 657 | 677 | crate::mail::account::send_verify(&state, &user, "erin@example.com") |
| 658 | 678 | .await |
| 659 | 679 | .unwrap(); |
| 660 | - let body = state.store.recent_mail(10).await.unwrap()[0].body.clone(); | |
| 680 | + let mail = state.store.recent_mail(10).await.unwrap(); | |
| 681 | + let body = mail | |
| 682 | + .iter() | |
| 683 | + .find(|m| m.to_addr == "erin@example.com") | |
| 684 | + .expect("erin mail") | |
| 685 | + .body | |
| 686 | + .clone(); | |
| 661 | 687 | let link = link_in(&body, "/verify/"); |
| 662 | 688 | let path = link.trim_start_matches("https://forge.test"); |
| 663 | - assert!(!state.store.email_is_verified(&user.id).await.unwrap()); | |
| 689 | + assert!( | |
| 690 | + !state.store.email_is_verified(&user.id).await.unwrap(), | |
| 691 | + "verify: starts unverified" | |
| 692 | + ); | |
| 664 | 693 | |
| 665 | 694 | // A GET only shows the confirm button; it spends nothing. |
| 666 | 695 | let (status, _, html) = send(&router, "GET", path, None, None).await; |
| 667 | - assert_eq!(status, 200); | |
| 668 | - assert!(html.contains("Confirm address")); | |
| 669 | - assert!(!state.store.email_is_verified(&user.id).await.unwrap()); | |
| 696 | + assert_eq!(status, 200, "verify GET: status"); | |
| 697 | + assert!(html.contains("Confirm address"), "verify GET: button"); | |
| 698 | + assert!( | |
| 699 | + !state.store.email_is_verified(&user.id).await.unwrap(), | |
| 700 | + "verify GET: still unverified" | |
| 701 | + ); | |
| 670 | 702 | |
| 671 | 703 | let (cookie, precsrf) = precsrf_for(&router, path).await; |
| 672 | 704 | let form = format!("precsrf={precsrf}"); |
| 673 | 705 | let (status, _, html) = send(&router, "POST", path, Some(&cookie), Some(&form)).await; |
| 674 | - assert_eq!(status, 200); | |
| 675 | - assert!(html.contains("Your email address is verified")); | |
| 676 | - assert!(state.store.email_is_verified(&user.id).await.unwrap()); | |
| 706 | + assert_eq!(status, 200, "verify POST: status"); | |
| 707 | + assert!( | |
| 708 | + html.contains("Your email address is verified"), | |
| 709 | + "verify POST: notice" | |
| 710 | + ); | |
| 711 | + assert!( | |
| 712 | + state.store.email_is_verified(&user.id).await.unwrap(), | |
| 713 | + "verify POST: verified" | |
| 714 | + ); | |
| 677 | 715 | |
| 678 | 716 | let (status, _, html) = send(&router, "POST", path, Some(&cookie), Some(&form)).await; |
| 679 | - assert_eq!(status, 200); | |
| 680 | - assert!(html.contains("This verification link is invalid")); | |
| 717 | + assert_eq!(status, 200, "verify again: status"); | |
| 718 | + assert!( | |
| 719 | + html.contains("This verification link is invalid"), | |
| 720 | + "verify again: spent" | |
| 721 | + ); | |
| 681 | 722 | } |
| 682 | 723 | } |
crates/forge-web/src/pages/security_tab_tests.rsmodified+8 -15
| @@ -76,23 +76,20 @@ async fn fixture(site: &Site) -> (String, String) { | ||
| 76 | 76 | (owner_cookie, reader_cookie) |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | +/// One advisories-on site serves the owner, a Write collaborator and a reader in turn. | |
| 79 | 80 | #[tokio::test] |
| 80 | -async fn security_tab_shows_to_the_owner_when_advisories_are_on() { | |
| 81 | +async fn security_tab_follows_the_role_while_advisories_are_on() { | |
| 81 | 82 | let site = site(true).await; |
| 82 | - let (owner_cookie, _) = fixture(&site).await; | |
| 83 | + let (owner_cookie, reader_cookie) = fixture(&site).await; | |
| 84 | + | |
| 83 | 85 | let (status, body) = get(&site, "/owner/proj/issues", Some(&owner_cookie)).await; |
| 84 | - assert_eq!(status, 200); | |
| 86 | + assert_eq!(status, 200, "owner page"); | |
| 85 | 87 | assert!( |
| 86 | 88 | body.contains(SECURITY_TAB), |
| 87 | 89 | "owner should see the Security tab" |
| 88 | 90 | ); |
| 89 | 91 | assert!(body.contains("/owner/proj/security")); |
| 90 | -} | |
| 91 | 92 | |
| 92 | -#[tokio::test] | |
| 93 | -async fn security_tab_shows_to_write_collaborators() { | |
| 94 | - let site = site(true).await; | |
| 95 | - fixture(&site).await; | |
| 96 | 93 | let writer = site |
| 97 | 94 | .store |
| 98 | 95 | .create_user("writer", None, "unused", false) |
| @@ -110,25 +107,21 @@ async fn security_tab_shows_to_write_collaborators() { | ||
| 110 | 107 | .unwrap(); |
| 111 | 108 | let writer_cookie = sign_in(&site.store, &writer.id, "writer-token").await; |
| 112 | 109 | let (status, body) = get(&site, "/owner/proj/issues", Some(&writer_cookie)).await; |
| 113 | - assert_eq!(status, 200); | |
| 110 | + assert_eq!(status, 200, "writer page"); | |
| 114 | 111 | assert!( |
| 115 | 112 | body.contains(SECURITY_TAB), |
| 116 | 113 | "a Write collaborator should see the tab" |
| 117 | 114 | ); |
| 118 | -} | |
| 119 | 115 | |
| 120 | -#[tokio::test] | |
| 121 | -async fn security_tab_is_hidden_from_readers() { | |
| 122 | - let site = site(true).await; | |
| 123 | - let (_, reader_cookie) = fixture(&site).await; | |
| 124 | 116 | let (status, body) = get(&site, "/owner/proj/issues", Some(&reader_cookie)).await; |
| 125 | - assert_eq!(status, 200); | |
| 117 | + assert_eq!(status, 200, "reader page"); | |
| 126 | 118 | assert!( |
| 127 | 119 | !body.contains(SECURITY_TAB), |
| 128 | 120 | "a reader must not see the tab" |
| 129 | 121 | ); |
| 130 | 122 | } |
| 131 | 123 | |
| 124 | +/// With advisories off, even the owner gets no tab. | |
| 132 | 125 | #[tokio::test] |
| 133 | 126 | async fn security_tab_is_hidden_when_advisories_are_off() { |
| 134 | 127 | let site = site(false).await; |
crates/forge-web/src/pages/snippets.rsmodified+58 -61
| @@ -833,8 +833,11 @@ mod tests { | ||
| 833 | 833 | .collect() |
| 834 | 834 | } |
| 835 | 835 | |
| 836 | + /// Saving drops blank cards but editing keeps them, and a leading newline survives the | |
| 837 | + /// textarea. | |
| 836 | 838 | #[test] |
| 837 | - fn blank_cards_are_dropped_only_on_save() { | |
| 839 | + fn draft_saves_drop_blank_cards_and_keep_leading_newlines() { | |
| 840 | + // Case: every rendered card is kept while editing; a save drops the blank ones. | |
| 838 | 841 | let draft = Draft::from_form(&form(&[ |
| 839 | 842 | ("title", "t"), |
| 840 | 843 | ("visibility", "public"), |
| @@ -845,38 +848,52 @@ mod tests { | ||
| 845 | 848 | ("name_2", "b.rs"), |
| 846 | 849 | ("content_2", ""), |
| 847 | 850 | ])); |
| 848 | - assert_eq!( | |
| 849 | - draft.files.len(), | |
| 850 | - 3, | |
| 851 | - "every rendered card is kept while editing" | |
| 852 | - ); | |
| 851 | + assert_eq!(draft.files.len(), 3, "blank cards: every card is kept while editing"); | |
| 853 | 852 | let saved = draft.saved_files(); |
| 854 | - assert_eq!(saved.len(), 2); | |
| 855 | - assert_eq!(saved[0].content, "one\ntwo"); | |
| 856 | - assert_eq!(saved[1].name, "b.rs"); | |
| 853 | + assert_eq!(saved.len(), 2, "blank cards: a save drops the blank ones"); | |
| 854 | + assert_eq!(saved[0].content, "one\ntwo", "blank cards: CRLF is normalised"); | |
| 855 | + assert_eq!(saved[1].name, "b.rs", "blank cards: the kept file"); | |
| 856 | + | |
| 857 | + // Case: a leading newline in a file survives the textarea. | |
| 858 | + let mut draft = Draft::blank(); | |
| 859 | + draft.files = vec![( | |
| 860 | + 0, | |
| 861 | + SnippetFile { | |
| 862 | + name: "a".into(), | |
| 863 | + content: "\nx".into(), | |
| 864 | + }, | |
| 865 | + )]; | |
| 866 | + assert_eq!(draft.slots()[0].content, "\n\nx", "leading newline"); | |
| 857 | 867 | } |
| 858 | 868 | |
| 869 | + /// The card steps: a new form shows one card, add and remove move the cards, the shown tab | |
| 870 | + /// follows the step, and a save or a failed save keeps the posted tab. | |
| 859 | 871 | #[test] |
| 860 | - fn new_form_shows_one_empty_card() { | |
| 872 | + fn card_steps_add_remove_and_show_the_right_tab() { | |
| 873 | + // Case: a new form shows one empty card. | |
| 861 | 874 | let draft = Draft::blank(); |
| 862 | 875 | let slots = draft.slots(); |
| 863 | - assert_eq!(slots.len(), 1); | |
| 864 | - assert_eq!((slots[0].number, slots[0].name.as_str()), (0, "")); | |
| 865 | - } | |
| 876 | + assert_eq!(slots.len(), 1, "new form: one card"); | |
| 877 | + assert_eq!( | |
| 878 | + (slots[0].number, slots[0].name.as_str()), | |
| 879 | + (0, ""), | |
| 880 | + "new form: the card is empty" | |
| 881 | + ); | |
| 866 | 882 | |
| 867 | - #[test] | |
| 868 | - fn add_appends_a_card_until_the_snippet_is_full() { | |
| 883 | + // Case: add appends a card until the snippet is full. | |
| 869 | 884 | let mut draft = Draft::blank(); |
| 870 | 885 | for _ in 0..MAX_FILES + 3 { |
| 871 | 886 | draft.add_blank(); |
| 872 | 887 | } |
| 873 | - assert_eq!(draft.slots().len(), MAX_FILES); | |
| 888 | + assert_eq!(draft.slots().len(), MAX_FILES, "add: stops at the limit"); | |
| 874 | 889 | let numbers: Vec<usize> = draft.slots().iter().map(|s| s.number).collect(); |
| 875 | - assert_eq!(numbers, (0..MAX_FILES).collect::<Vec<_>>()); | |
| 876 | - } | |
| 890 | + assert_eq!( | |
| 891 | + numbers, | |
| 892 | + (0..MAX_FILES).collect::<Vec<_>>(), | |
| 893 | + "add: numbers run from zero" | |
| 894 | + ); | |
| 877 | 895 | |
| 878 | - #[test] | |
| 879 | - fn remove_keeps_the_other_field_numbers() { | |
| 896 | + // Case: removing a card keeps the other field numbers. | |
| 880 | 897 | let mut draft = Draft::blank(); |
| 881 | 898 | draft.add_blank(); |
| 882 | 899 | draft.add_blank(); |
| @@ -884,72 +901,52 @@ mod tests { | ||
| 884 | 901 | draft.files[1].1.name = "b.txt".into(); |
| 885 | 902 | draft.remove(0); |
| 886 | 903 | let numbers: Vec<usize> = draft.slots().iter().map(|s| s.number).collect(); |
| 887 | - assert_eq!(numbers, vec![1, 2]); | |
| 888 | - assert_eq!(draft.slots()[0].name, "b.txt"); | |
| 889 | - } | |
| 904 | + assert_eq!(numbers, vec![1, 2], "remove: numbers are kept"); | |
| 905 | + assert_eq!(draft.slots()[0].name, "b.txt", "remove: the other card's name"); | |
| 890 | 906 | |
| 891 | - #[test] | |
| 892 | - fn add_and_remove_clicks_are_told_apart_from_a_save() { | |
| 907 | + // Case: add and remove clicks are told apart from a save. | |
| 893 | 908 | let add = form(&[("op", "add"), ("name_0", "a")]); |
| 894 | 909 | let mut draft = Draft::from_form(&add); |
| 895 | - assert_eq!(apply_slot_step(&mut draft, &add), Some(1)); | |
| 896 | - assert_eq!(draft.slots().len(), 2); | |
| 910 | + assert_eq!(apply_slot_step(&mut draft, &add), Some(1), "add click: tab"); | |
| 911 | + assert_eq!(draft.slots().len(), 2, "add click: two cards"); | |
| 897 | 912 | let remove = form(&[("remove", "1"), ("name_0", "a"), ("name_1", "")]); |
| 898 | 913 | let mut draft = Draft::from_form(&remove); |
| 899 | - assert_eq!(apply_slot_step(&mut draft, &remove), Some(0)); | |
| 900 | - assert_eq!(draft.slots().len(), 1); | |
| 914 | + assert_eq!(apply_slot_step(&mut draft, &remove), Some(0), "remove click: tab"); | |
| 915 | + assert_eq!(draft.slots().len(), 1, "remove click: one card"); | |
| 901 | 916 | assert_eq!( |
| 902 | 917 | apply_slot_step(&mut Draft::blank(), &form(&[("title", "t")])), |
| 903 | - None | |
| 918 | + None, | |
| 919 | + "save: not a card step" | |
| 904 | 920 | ); |
| 905 | - } | |
| 906 | 921 | |
| 907 | - #[test] | |
| 908 | - fn add_shows_the_new_last_file() { | |
| 922 | + // Case: add shows the new last file. | |
| 909 | 923 | let mut draft = Draft::blank(); |
| 910 | 924 | let add = form(&[("op", "add"), ("name_0", "a")]); |
| 911 | - assert_eq!(apply_slot_step(&mut draft, &add), Some(0)); | |
| 912 | - assert_eq!(apply_slot_step(&mut draft, &add), Some(1)); | |
| 913 | - assert_eq!(draft.slots().len(), 2); | |
| 914 | - } | |
| 925 | + assert_eq!(apply_slot_step(&mut draft, &add), Some(0), "add shows: first add"); | |
| 926 | + assert_eq!(apply_slot_step(&mut draft, &add), Some(1), "add shows: new last"); | |
| 927 | + assert_eq!(draft.slots().len(), 2, "add shows: two cards"); | |
| 915 | 928 | |
| 916 | - #[test] | |
| 917 | - fn remove_shows_the_file_before_the_removed_one() { | |
| 929 | + // Case: removing a card shows the file before it, and the first one when it is first. | |
| 918 | 930 | let mut draft = Draft::blank(); |
| 919 | 931 | for _ in 0..3 { |
| 920 | 932 | draft.add_blank(); |
| 921 | 933 | } |
| 922 | 934 | // Files 0..=2 at positions 0..=2; removing the file at position 2 shows position 1. |
| 923 | 935 | let remove = form(&[("remove", "2")]); |
| 924 | - assert_eq!(apply_slot_step(&mut draft, &remove), Some(1)); | |
| 936 | + assert_eq!(apply_slot_step(&mut draft, &remove), Some(1), "remove shows: before"); | |
| 925 | 937 | // Removing the first file shows the new first one. |
| 926 | 938 | let remove = form(&[("remove", "0")]); |
| 927 | - assert_eq!(apply_slot_step(&mut draft, &remove), Some(0)); | |
| 928 | - } | |
| 939 | + assert_eq!(apply_slot_step(&mut draft, &remove), Some(0), "remove shows: first"); | |
| 929 | 940 | |
| 930 | - #[test] | |
| 931 | - fn a_failed_save_keeps_the_posted_tab() { | |
| 941 | + // Case: a failed save keeps the posted tab, and falls back to the first. | |
| 932 | 942 | let mut draft = Draft::blank(); |
| 933 | 943 | for _ in 0..3 { |
| 934 | 944 | draft.add_blank(); |
| 935 | 945 | } |
| 936 | 946 | let numbers: Vec<usize> = draft.slots().iter().map(|s| s.number).collect(); |
| 937 | - assert_eq!(numbers, vec![0, 1, 2]); | |
| 938 | - assert_eq!(posted_tab(&draft, &form(&[("tab", "2")])), 2); | |
| 939 | - assert_eq!(posted_tab(&draft, &form(&[("tab", "9")])), 0); | |
| 940 | - assert_eq!(posted_tab(&draft, &form(&[])), 0); | |
| 941 | - } | |
| 942 | - | |
| 943 | - #[test] | |
| 944 | - fn leading_newline_survives_the_textarea() { | |
| 945 | - let mut draft = Draft::blank(); | |
| 946 | - draft.files = vec![( | |
| 947 | - 0, | |
| 948 | - SnippetFile { | |
| 949 | - name: "a".into(), | |
| 950 | - content: "\nx".into(), | |
| 951 | - }, | |
| 952 | - )]; | |
| 953 | - assert_eq!(draft.slots()[0].content, "\n\nx"); | |
| 947 | + assert_eq!(numbers, vec![0, 1, 2], "failed save: three cards"); | |
| 948 | + assert_eq!(posted_tab(&draft, &form(&[("tab", "2")])), 2, "failed save: posted tab"); | |
| 949 | + assert_eq!(posted_tab(&draft, &form(&[("tab", "9")])), 0, "failed save: unknown tab"); | |
| 950 | + assert_eq!(posted_tab(&draft, &form(&[])), 0, "failed save: no tab"); | |
| 954 | 951 | } |
| 955 | 952 | } |