Forge-web page tests share their fixtures and merge into tables
Browse files at this commitb757c23591d6b46f070c35bc03c8b2d97937e89bParent: 40e7f7d- test
- forge-web page tests share their fixtures and merge into tables
11 changed files +254 -396
- crates/forge-web/src/api/gitea_tests.rs +14 -23
- crates/forge-web/src/api/releases.rs +21 -14
- crates/forge-web/src/api/statuses.rs +35 -149
- crates/forge-web/src/backup/tests.rs +38 -38
- crates/forge-web/src/i18n/mod.rs +27 -31
- crates/forge-web/src/importer/tests.rs +29 -45
- crates/forge-web/src/mail/templates.rs +10 -9
- crates/forge-web/src/pulls_sync.rs +2 -5
- crates/forge-web/src/replication/mod.rs +1 -9
- crates/forge-web/src/views/heatmap.rs +29 -22
- crates/forge-web/src/views/mod.rs +48 -51
crates/forge-web/src/api/gitea_tests.rsmodified+14 -23
| @@ -273,8 +273,10 @@ fn repo_path_of(rest: &str) -> String { | |||
| 273 | format!("/api/v1/repos/{OWNER}/{REPO}{rest}") | 273 | format!("/api/v1/repos/{OWNER}/{REPO}{rest}") |
| 274 | } | 274 | } |
| 275 | 275 | ||
| 276 | +/// Read routes that share one fixture: version and settings, the repo, its issues, and | ||
| 277 | +/// pagination. None of them changes state, so the scenarios cannot interfere. | ||
| 276 | #[tokio::test] | 278 | #[tokio::test] |
| 277 | -async fn version_and_settings_unlock_every_feature() { | 279 | +async fn version_repo_issues_and_pagination() { |
| 278 | let f = fixture().await; | 280 | let f = fixture().await; |
| 279 | let (status, _, body) = f.get_as("/api/v1/version", None).await; | 281 | let (status, _, body) = f.get_as("/api/v1/version", None).await; |
| 280 | assert_eq!(status, 200); | 282 | assert_eq!(status, 200); |
| @@ -282,11 +284,8 @@ async fn version_and_settings_unlock_every_feature() { | |||
| 282 | let (status, _, body) = f.get_as("/api/v1/settings/api", None).await; | 284 | let (status, _, body) = f.get_as("/api/v1/settings/api", None).await; |
| 283 | assert_eq!(status, 200); | 285 | assert_eq!(status, 200); |
| 284 | assert_eq!(body["max_response_items"], 50); | 286 | assert_eq!(body["max_response_items"], 50); |
| 285 | -} | ||
| 286 | 287 | ||
| 287 | -#[tokio::test] | 288 | + // The repo, its topics, labels and milestones. |
| 288 | -async fn repo_info_topics_labels_and_milestones() { | ||
| 289 | - let f = fixture().await; | ||
| 290 | let (status, _, repo) = f.get(&repo_path_of("")).await; | 289 | let (status, _, repo) = f.get(&repo_path_of("")).await; |
| 291 | assert_eq!(status, 200); | 290 | assert_eq!(status, 200); |
| 292 | assert_eq!(repo["name"], REPO); | 291 | assert_eq!(repo["name"], REPO); |
| @@ -326,11 +325,8 @@ async fn repo_info_topics_labels_and_milestones() { | |||
| 326 | assert_eq!(m["state"], "open"); | 325 | assert_eq!(m["state"], "open"); |
| 327 | assert_eq!(m["due_on"], "2026-12-01T00:00:00Z"); | 326 | assert_eq!(m["due_on"], "2026-12-01T00:00:00Z"); |
| 328 | assert!(m["created_at"].is_string()); | 327 | assert!(m["created_at"].is_string()); |
| 329 | -} | ||
| 330 | 328 | ||
| 331 | -#[tokio::test] | 329 | + // Issues with their labels, milestone, assignees, comments and reactions. |
| 332 | -async fn issues_carry_labels_milestone_assignees_and_comments() { | ||
| 333 | - let f = fixture().await; | ||
| 334 | let (status, headers, issues) = f | 330 | let (status, headers, issues) = f |
| 335 | .get(&repo_path_of( | 331 | .get(&repo_path_of( |
| 336 | "/issues?type=issues&state=all&page=1&limit=10", | 332 | "/issues?type=issues&state=all&page=1&limit=10", |
| @@ -376,11 +372,8 @@ async fn issues_carry_labels_milestone_assignees_and_comments() { | |||
| 376 | assert_eq!(status, 200); | 372 | assert_eq!(status, 200); |
| 377 | assert_eq!(reactions[0]["reaction"], "+1"); | 373 | assert_eq!(reactions[0]["reaction"], "+1"); |
| 378 | assert_eq!(reactions[0]["user"]["login"], OWNER); | 374 | assert_eq!(reactions[0]["user"]["login"], OWNER); |
| 379 | -} | ||
| 380 | 375 | ||
| 381 | -#[tokio::test] | 376 | + // Pagination headers, the Link header, and the empty last page. |
| 382 | -async fn pagination_headers_and_link() { | ||
| 383 | - let f = fixture().await; | ||
| 384 | let (status, headers, items) = f | 377 | let (status, headers, items) = f |
| 385 | .get(&repo_path_of("/issues/1/comments?page=2&limit=1")) | 378 | .get(&repo_path_of("/issues/1/comments?page=2&limit=1")) |
| 386 | .await; | 379 | .await; |
| @@ -413,9 +406,11 @@ async fn pagination_headers_and_link() { | |||
| 413 | assert_eq!(items.as_array().unwrap().len(), 0); | 406 | assert_eq!(items.as_array().unwrap().len(), 0); |
| 414 | } | 407 | } |
| 415 | 408 | ||
| 409 | +/// Pull requests, reviews and releases, which share one fixture as read-only scenarios. | ||
| 416 | #[tokio::test] | 410 | #[tokio::test] |
| 417 | -async fn pulls_reviews_and_review_comments() { | 411 | +async fn pulls_reviews_and_releases_in_gitea_shape() { |
| 418 | let f = fixture().await; | 412 | let f = fixture().await; |
| 413 | + // Pull requests and their reviews and review comments. | ||
| 419 | let (status, headers, pulls) = f | 414 | let (status, headers, pulls) = f |
| 420 | .get(&repo_path_of("/pulls?state=all&page=1&limit=10")) | 415 | .get(&repo_path_of("/pulls?state=all&page=1&limit=10")) |
| 421 | .await; | 416 | .await; |
| @@ -477,11 +472,8 @@ async fn pulls_reviews_and_review_comments() { | |||
| 477 | // An issue number is not a pull request. | 472 | // An issue number is not a pull request. |
| 478 | let (status, _, _) = f.get(&repo_path_of("/pulls/1/reviews")).await; | 473 | let (status, _, _) = f.get(&repo_path_of("/pulls/1/reviews")).await; |
| 479 | assert_eq!(status, 404); | 474 | assert_eq!(status, 404); |
| 480 | -} | ||
| 481 | 475 | ||
| 482 | -#[tokio::test] | 476 | + // Releases and their assets in the Gitea shape. |
| 483 | -async fn releases_and_assets_in_gitea_shape() { | ||
| 484 | - let f = fixture().await; | ||
| 485 | let (status, headers, releases) = f.get(&repo_path_of("/releases?page=1&limit=10")).await; | 477 | let (status, headers, releases) = f.get(&repo_path_of("/releases?page=1&limit=10")).await; |
| 486 | assert_eq!(status, 200); | 478 | assert_eq!(status, 200); |
| 487 | assert_eq!(headers["x-total-count"], "1"); | 479 | assert_eq!(headers["x-total-count"], "1"); |
| @@ -515,9 +507,11 @@ async fn releases_and_assets_in_gitea_shape() { | |||
| 515 | assert_eq!(one["browser_download_url"], url); | 507 | assert_eq!(one["browser_download_url"], url); |
| 516 | } | 508 | } |
| 517 | 509 | ||
| 510 | +/// Private repos and release asset downloads, which both follow the token rules. | ||
| 518 | #[tokio::test] | 511 | #[tokio::test] |
| 519 | -async fn private_repos_follow_the_token() { | 512 | +async fn private_repos_and_asset_downloads_follow_the_token() { |
| 520 | let f = fixture().await; | 513 | let f = fixture().await; |
| 514 | + // Private repos follow the token. | ||
| 521 | let secret = "/api/v1/repos/tahinli/secret"; | 515 | let secret = "/api/v1/repos/tahinli/secret"; |
| 522 | let (status, _, _) = f.get_as(secret, None).await; | 516 | let (status, _, _) = f.get_as(secret, None).await; |
| 523 | assert_eq!(status, 401, "anonymous"); | 517 | assert_eq!(status, 401, "anonymous"); |
| @@ -558,11 +552,8 @@ async fn private_repos_follow_the_token() { | |||
| 558 | .get_as(&repo_path_of(""), Some("token forge_nope".into())) | 552 | .get_as(&repo_path_of(""), Some("token forge_nope".into())) |
| 559 | .await; | 553 | .await; |
| 560 | assert_eq!(status, 401); | 554 | assert_eq!(status, 401); |
| 561 | -} | ||
| 562 | 555 | ||
| 563 | -#[tokio::test] | 556 | + // Release asset downloads take the token too. |
| 564 | -async fn release_asset_downloads_with_a_token() { | ||
| 565 | - let f = fixture().await; | ||
| 566 | let path = "/tahinli/proj/releases/download/v1.0/app.bin"; | 557 | let path = "/tahinli/proj/releases/download/v1.0/app.bin"; |
| 567 | let owner = f.owner_token.clone(); | 558 | let owner = f.owner_token.clone(); |
| 568 | let basic = format!("Basic {}", BASE64_STANDARD.encode(format!("x:{owner}"))); | 559 | let basic = format!("Basic {}", BASE64_STANDARD.encode(format!("x:{owner}"))); |
crates/forge-web/src/api/releases.rsmodified+21 -14
| @@ -642,22 +642,29 @@ mod tests { | |||
| 642 | .await | 642 | .await |
| 643 | } | 643 | } |
| 644 | 644 | ||
| 645 | + /// Refused creates share one fixture and store nothing. Each case is labelled in its message. | ||
| 645 | #[tokio::test] | 646 | #[tokio::test] |
| 646 | - async fn anonymous_and_bad_tokens_are_unauthorized() { | 647 | + async fn refused_creates_return_their_status() { |
| 647 | let f = fixture(None).await; | 648 | let f = fixture(None).await; |
| 648 | - let (status, body) = post_json(&f, None, r#"{"tag":"v1"}"#).await; | 649 | + let scopeless = bearer(&f.scopeless_token); |
| 649 | - assert_eq!(status, 401); | 650 | + // (case, auth, expected status, expects a JSON error body) |
| 650 | - assert!(body["error"].is_string()); | 651 | + let cases: [(&str, Option<&str>, u16, bool); 3] = [ |
| 651 | - let (status, _) = | 652 | + ("anonymous", None, 401, true), |
| 652 | - post_json(&f, Some("Bearer forge_not_a_real_token"), r#"{"tag":"v1"}"#).await; | 653 | + ( |
| 653 | - assert_eq!(status, 401); | 654 | + "bad token", |
| 654 | - } | 655 | + Some("Bearer forge_not_a_real_token"), |
| 655 | - | 656 | + 401, |
| 656 | - #[tokio::test] | 657 | + false, |
| 657 | - async fn token_without_repo_scope_is_forbidden() { | 658 | + ), |
| 658 | - let f = fixture(None).await; | 659 | + ("token without repo scope", Some(scopeless.as_str()), 403, false), |
| 659 | - let (status, _) = post_json(&f, Some(&bearer(&f.scopeless_token)), r#"{"tag":"v1"}"#).await; | 660 | + ]; |
| 660 | - assert_eq!(status, 403); | 661 | + for (name, auth, want, error_body) in cases { |
| 662 | + let (status, body) = post_json(&f, auth, r#"{"tag":"v1"}"#).await; | ||
| 663 | + assert_eq!(status, want, "case {name}: {body}"); | ||
| 664 | + if error_body { | ||
| 665 | + assert!(body["error"].is_string(), "case {name}"); | ||
| 666 | + } | ||
| 667 | + } | ||
| 661 | } | 668 | } |
| 662 | 669 | ||
| 663 | #[tokio::test] | 670 | #[tokio::test] |
crates/forge-web/src/api/statuses.rsmodified+35 -149
| @@ -340,160 +340,46 @@ mod tests { | |||
| 340 | assert_eq!(body["statuses"].as_array().map(Vec::len), Some(1)); | 340 | assert_eq!(body["statuses"].as_array().map(Vec::len), Some(1)); |
| 341 | } | 341 | } |
| 342 | 342 | ||
| 343 | + /// Every refused write shares one repo fixture, and each case is labelled in its message. | ||
| 343 | #[tokio::test] | 344 | #[tokio::test] |
| 344 | - async fn read_only_token_is_forbidden_to_post() { | 345 | + async fn refused_writes_return_their_status() { |
| 345 | let s = setup(Visibility::Public).await; | 346 | let s = setup(Visibility::Public).await; |
| 346 | - let (status, body) = send( | 347 | + let owner = s |
| 347 | - &s.fx.router, | 348 | + .fx |
| 348 | - "POST", | 349 | + .store |
| 349 | - &report_uri(&s.sha), | 350 | + .user_by_username(OWNER) |
| 350 | - Some(&bearer(&s.reader_token)), | 351 | + .await |
| 351 | - None, | 352 | + .unwrap() |
| 352 | - Some(SUCCESS), | 353 | + .expect("owner"); |
| 353 | - ) | ||
| 354 | - .await; | ||
| 355 | - assert_eq!(status, 403, "{body}"); | ||
| 356 | - assert!(body["error"].is_string()); | ||
| 357 | - } | ||
| 358 | - | ||
| 359 | - #[tokio::test] | ||
| 360 | - async fn anonymous_and_bad_tokens_are_unauthorized() { | ||
| 361 | - let s = setup(Visibility::Public).await; | ||
| 362 | - let (status, body) = send( | ||
| 363 | - &s.fx.router, | ||
| 364 | - "POST", | ||
| 365 | - &report_uri(&s.sha), | ||
| 366 | - None, | ||
| 367 | - None, | ||
| 368 | - Some(SUCCESS), | ||
| 369 | - ) | ||
| 370 | - .await; | ||
| 371 | - assert_eq!(status, 401); | ||
| 372 | - assert!(body["error"].is_string()); | ||
| 373 | - | ||
| 374 | - let (status, _) = send( | ||
| 375 | - &s.fx.router, | ||
| 376 | - "POST", | ||
| 377 | - &report_uri(&s.sha), | ||
| 378 | - Some("Bearer forge_not_a_real_token"), | ||
| 379 | - None, | ||
| 380 | - Some(SUCCESS), | ||
| 381 | - ) | ||
| 382 | - .await; | ||
| 383 | - assert_eq!(status, 401); | ||
| 384 | - } | ||
| 385 | - | ||
| 386 | - #[tokio::test] | ||
| 387 | - async fn session_cookie_alone_is_rejected() { | ||
| 388 | - let s = setup(Visibility::Public).await; | ||
| 389 | - let owner = | ||
| 390 | - s.fx.store | ||
| 391 | - .user_by_username(OWNER) | ||
| 392 | - .await | ||
| 393 | - .unwrap() | ||
| 394 | - .expect("owner"); | ||
| 395 | let session = generate_token(); | 354 | let session = generate_token(); |
| 396 | crate::test_support::create_session(&s.fx.store, &owner.id, &session, "csrf-value").await; | 355 | crate::test_support::create_session(&s.fx.store, &owner.id, &session, "csrf-value").await; |
| 397 | let cookie = format!("__Host-{SESSION_COOKIE}={session}"); | 356 | let cookie = format!("__Host-{SESSION_COOKIE}={session}"); |
| 398 | - let (status, _) = send( | 357 | + let unscoped = bearer(&token_for(&s.fx.store, &owner.id, &[]).await); |
| 399 | - &s.fx.router, | 358 | + let writer = bearer(&s.writer_token); |
| 400 | - "POST", | 359 | + let reader = bearer(&s.reader_token); |
| 401 | - &report_uri(&s.sha), | 360 | + let report = report_uri(&s.sha); |
| 402 | - None, | 361 | + let bad_sha = report_uri("abc123"); |
| 403 | - Some(&cookie), | 362 | + let missing = report_uri(&"0".repeat(40)); |
| 404 | - Some(SUCCESS), | 363 | + // (case, uri, auth, cookie, body, expected status, expects a JSON error body) |
| 405 | - ) | 364 | + let cases: [(&str, &str, Option<&str>, Option<&str>, Option<&str>, u16, bool); 10] = [ |
| 406 | - .await; | 365 | + ("read-only token", report.as_str(), Some(reader.as_str()), None, Some(SUCCESS), 403, true), |
| 407 | - assert_eq!(status, 401); | 366 | + ("anonymous", report.as_str(), None, None, Some(SUCCESS), 401, true), |
| 408 | - } | 367 | + ("bad token", report.as_str(), Some("Bearer forge_not_a_real_token"), None, Some(SUCCESS), 401, false), |
| 409 | - | 368 | + ("session cookie alone", report.as_str(), None, Some(cookie.as_str()), Some(SUCCESS), 401, false), |
| 410 | - #[tokio::test] | 369 | + ("token without repo scope", report.as_str(), Some(unscoped.as_str()), None, Some(SUCCESS), 403, false), |
| 411 | - async fn token_without_repo_scope_is_forbidden() { | 370 | + ("sha too short", bad_sha.as_str(), Some(writer.as_str()), None, Some(SUCCESS), 422, false), |
| 412 | - let s = setup(Visibility::Public).await; | 371 | + ("unknown state", report.as_str(), Some(writer.as_str()), None, Some(r#"{"state":"maybe"}"#), 422, false), |
| 413 | - let owner = | 372 | + ("malformed json", report.as_str(), Some(writer.as_str()), None, Some("{not json"), 400, false), |
| 414 | - s.fx.store | 373 | + ("javascript target url", report.as_str(), Some(writer.as_str()), None, Some(r#"{"state":"success","target_url":"javascript:alert(1)"}"#), 422, false), |
| 415 | - .user_by_username(OWNER) | 374 | + ("unknown commit", missing.as_str(), Some(writer.as_str()), None, Some(SUCCESS), 404, false), |
| 416 | - .await | 375 | + ]; |
| 417 | - .unwrap() | 376 | + for (name, uri, auth, cookie, body, want, error_body) in cases { |
| 418 | - .expect("owner"); | 377 | + let (status, json) = send(&s.fx.router, "POST", uri, auth, cookie, body).await; |
| 419 | - let raw = token_for(&s.fx.store, &owner.id, &[]).await; | 378 | + assert_eq!(status, want, "case {name}: {json}"); |
| 420 | - let (status, _) = send( | 379 | + if error_body { |
| 421 | - &s.fx.router, | 380 | + assert!(json["error"].is_string(), "case {name}"); |
| 422 | - "POST", | 381 | + } |
| 423 | - &report_uri(&s.sha), | 382 | + } |
| 424 | - Some(&bearer(&raw)), | ||
| 425 | - None, | ||
| 426 | - Some(SUCCESS), | ||
| 427 | - ) | ||
| 428 | - .await; | ||
| 429 | - assert_eq!(status, 403); | ||
| 430 | - } | ||
| 431 | - | ||
| 432 | - #[tokio::test] | ||
| 433 | - async fn bad_sha_and_bad_body_are_rejected() { | ||
| 434 | - let s = setup(Visibility::Public).await; | ||
| 435 | - let auth = bearer(&s.writer_token); | ||
| 436 | - let (status, _) = send( | ||
| 437 | - &s.fx.router, | ||
| 438 | - "POST", | ||
| 439 | - &report_uri("abc123"), | ||
| 440 | - Some(&auth), | ||
| 441 | - None, | ||
| 442 | - Some(SUCCESS), | ||
| 443 | - ) | ||
| 444 | - .await; | ||
| 445 | - assert_eq!(status, 422); | ||
| 446 | - | ||
| 447 | - let (status, _) = send( | ||
| 448 | - &s.fx.router, | ||
| 449 | - "POST", | ||
| 450 | - &report_uri(&s.sha), | ||
| 451 | - Some(&auth), | ||
| 452 | - None, | ||
| 453 | - Some(r#"{"state":"maybe"}"#), | ||
| 454 | - ) | ||
| 455 | - .await; | ||
| 456 | - assert_eq!(status, 422); | ||
| 457 | - | ||
| 458 | - let (status, _) = send( | ||
| 459 | - &s.fx.router, | ||
| 460 | - "POST", | ||
| 461 | - &report_uri(&s.sha), | ||
| 462 | - Some(&auth), | ||
| 463 | - None, | ||
| 464 | - Some("{not json"), | ||
| 465 | - ) | ||
| 466 | - .await; | ||
| 467 | - assert_eq!(status, 400); | ||
| 468 | - | ||
| 469 | - let (status, _) = send( | ||
| 470 | - &s.fx.router, | ||
| 471 | - "POST", | ||
| 472 | - &report_uri(&s.sha), | ||
| 473 | - Some(&auth), | ||
| 474 | - None, | ||
| 475 | - Some(r#"{"state":"success","target_url":"javascript:alert(1)"}"#), | ||
| 476 | - ) | ||
| 477 | - .await; | ||
| 478 | - assert_eq!(status, 422); | ||
| 479 | - } | ||
| 480 | - | ||
| 481 | - #[tokio::test] | ||
| 482 | - async fn unknown_commit_is_not_found() { | ||
| 483 | - let s = setup(Visibility::Public).await; | ||
| 484 | - let missing = "0".repeat(40); | ||
| 485 | - let (status, _) = send( | ||
| 486 | - &s.fx.router, | ||
| 487 | - "POST", | ||
| 488 | - &report_uri(&missing), | ||
| 489 | - Some(&bearer(&s.writer_token)), | ||
| 490 | - None, | ||
| 491 | - Some(SUCCESS), | ||
| 492 | - ) | ||
| 493 | - .await; | ||
| 494 | - assert_eq!(status, 404); | ||
| 495 | - let (status, _) = send(&s.fx.router, "GET", &list_uri(&missing), None, None, None).await; | ||
| 496 | - assert_eq!(status, 404); | ||
| 497 | } | 383 | } |
| 498 | 384 | ||
| 499 | #[tokio::test] | 385 | #[tokio::test] |
crates/forge-web/src/backup/tests.rsmodified+38 -38
| @@ -222,10 +222,14 @@ async fn end_to_end_set_verifies_with_correct_contents_and_modes() { | |||
| 222 | assert_eq!(only_entry(&out), summary.path); | 222 | assert_eq!(only_entry(&out), summary.path); |
| 223 | } | 223 | } |
| 224 | 224 | ||
| 225 | +/// Each tampering gets its own backup set, so one check cannot hide another. Every case is | ||
| 226 | +/// labelled in its message. | ||
| 225 | #[tokio::test] | 227 | #[tokio::test] |
| 226 | -async fn one_flipped_bundle_byte_fails_the_sha_check() { | 228 | +async fn tampered_sets_fail_the_check_that_catches_them() { |
| 227 | let fx = fixture().await; | 229 | let fx = fixture().await; |
| 228 | - let out = fx.dir.path().join("out"); | 230 | + |
| 231 | + // A flipped bundle byte fails the bundle's sha256. | ||
| 232 | + let out = fx.dir.path().join("out-bundle"); | ||
| 229 | let summary = create_backup(&fx.source, &opts(&out, false, Scope::All)) | 233 | let summary = create_backup(&fx.source, &opts(&out, false, Scope::All)) |
| 230 | .await | 234 | .await |
| 231 | .expect("backup"); | 235 | .expect("backup"); |
| @@ -236,19 +240,16 @@ async fn one_flipped_bundle_byte_fails_the_sha_check() { | |||
| 236 | std::fs::write(&bundle, &bytes).expect("tamper"); | 240 | std::fs::write(&bundle, &bytes).expect("tamper"); |
| 237 | 241 | ||
| 238 | let report = verify_backup(&summary.path, &fx.git).await.expect("verify"); | 242 | let report = verify_backup(&summary.path, &fx.git).await.expect("verify"); |
| 239 | - assert!(!report.is_ok()); | 243 | + assert!(!report.is_ok(), "case bundle byte"); |
| 240 | let needle = format!("repos/{}.bundle: sha256 mismatch", fx.demo_id); | 244 | let needle = format!("repos/{}.bundle: sha256 mismatch", fx.demo_id); |
| 241 | assert!( | 245 | assert!( |
| 242 | report.problems.iter().any(|p| p.contains(&needle)), | 246 | report.problems.iter().any(|p| p.contains(&needle)), |
| 243 | - "{:?}", | 247 | + "case bundle byte: {:?}", |
| 244 | report.problems | 248 | report.problems |
| 245 | ); | 249 | ); |
| 246 | -} | ||
| 247 | 250 | ||
| 248 | -#[tokio::test] | 251 | + // An appended byte fails both the size and the sha256 of the database. |
| 249 | -async fn tampered_db_fails_the_sha_check() { | 252 | + let out = fx.dir.path().join("out-db"); |
| 250 | - let fx = fixture().await; | ||
| 251 | - let out = fx.dir.path().join("out"); | ||
| 252 | let summary = create_backup(&fx.source, &opts(&out, false, Scope::All)) | 253 | let summary = create_backup(&fx.source, &opts(&out, false, Scope::All)) |
| 253 | .await | 254 | .await |
| 254 | .expect("backup"); | 255 | .expect("backup"); |
| @@ -263,7 +264,7 @@ async fn tampered_db_fails_the_sha_check() { | |||
| 263 | .problems | 264 | .problems |
| 264 | .iter() | 265 | .iter() |
| 265 | .any(|p| p.starts_with("db/forge.db: size mismatch")), | 266 | .any(|p| p.starts_with("db/forge.db: size mismatch")), |
| 266 | - "{:?}", | 267 | + "case appended db byte: {:?}", |
| 267 | report.problems | 268 | report.problems |
| 268 | ); | 269 | ); |
| 269 | assert!( | 270 | assert!( |
| @@ -271,15 +272,13 @@ async fn tampered_db_fails_the_sha_check() { | |||
| 271 | .problems | 272 | .problems |
| 272 | .iter() | 273 | .iter() |
| 273 | .any(|p| p.starts_with("db/forge.db: sha256 mismatch")), | 274 | .any(|p| p.starts_with("db/forge.db: sha256 mismatch")), |
| 274 | - "{:?}", | 275 | + "case appended db byte: {:?}", |
| 275 | report.problems | 276 | report.problems |
| 276 | ); | 277 | ); |
| 277 | -} | ||
| 278 | 278 | ||
| 279 | -#[tokio::test] | 279 | + // A truncated database fails the snapshot check even when the manifest is rewritten to |
| 280 | -async fn a_damaged_db_fails_even_when_the_manifest_is_rewritten_to_match() { | 280 | + // match, so only the DB checks can catch it. |
| 281 | - let fx = fixture().await; | 281 | + let out = fx.dir.path().join("out-truncated"); |
| 282 | - let out = fx.dir.path().join("out"); | ||
| 283 | let summary = create_backup(&fx.source, &opts(&out, false, Scope::All)) | 282 | let summary = create_backup(&fx.source, &opts(&out, false, Scope::All)) |
| 284 | .await | 283 | .await |
| 285 | .expect("backup"); | 284 | .expect("backup"); |
| @@ -287,7 +286,6 @@ async fn a_damaged_db_fails_even_when_the_manifest_is_rewritten_to_match() { | |||
| 287 | let bytes = std::fs::read(&db).expect("read db"); | 286 | let bytes = std::fs::read(&db).expect("read db"); |
| 288 | std::fs::write(&db, &bytes[..bytes.len() / 2]).expect("truncate"); | 287 | std::fs::write(&db, &bytes[..bytes.len() / 2]).expect("truncate"); |
| 289 | 288 | ||
| 290 | - // Make the manifest agree with the damaged file, so only the DB checks can catch it. | ||
| 291 | let mut m = read_manifest(&summary.path); | 289 | let mut m = read_manifest(&summary.path); |
| 292 | let (new_bytes, new_sha) = { | 290 | let (new_bytes, new_sha) = { |
| 293 | let data = std::fs::read(&db).expect("read"); | 291 | let data = std::fs::read(&db).expect("read"); |
| @@ -307,15 +305,18 @@ async fn a_damaged_db_fails_even_when_the_manifest_is_rewritten_to_match() { | |||
| 307 | .problems | 305 | .problems |
| 308 | .iter() | 306 | .iter() |
| 309 | .any(|p| p.starts_with("database snapshot failed verification")), | 307 | .any(|p| p.starts_with("database snapshot failed verification")), |
| 310 | - "{:?}", | 308 | + "case truncated db: {:?}", |
| 311 | report.problems | 309 | report.problems |
| 312 | ); | 310 | ); |
| 313 | } | 311 | } |
| 314 | 312 | ||
| 313 | +/// A manifest repo list that disagrees with the DB snapshot, and a stray file in the set, are | ||
| 314 | +/// each reported. Each case has its own backup set. | ||
| 315 | #[tokio::test] | 315 | #[tokio::test] |
| 316 | -async fn manifest_repo_list_must_match_the_db_snapshot() { | 316 | +async fn manifest_and_stray_file_problems_are_reported() { |
| 317 | let fx = fixture().await; | 317 | let fx = fixture().await; |
| 318 | - let out = fx.dir.path().join("out"); | 318 | + |
| 319 | + let out = fx.dir.path().join("out-manifest"); | ||
| 319 | let summary = create_backup(&fx.source, &opts(&out, false, Scope::All)) | 320 | let summary = create_backup(&fx.source, &opts(&out, false, Scope::All)) |
| 320 | .await | 321 | .await |
| 321 | .expect("backup"); | 322 | .expect("backup"); |
| @@ -329,15 +330,11 @@ async fn manifest_repo_list_must_match_the_db_snapshot() { | |||
| 329 | .problems | 330 | .problems |
| 330 | .iter() | 331 | .iter() |
| 331 | .any(|p| p.contains("does not match the database snapshot")), | 332 | .any(|p| p.contains("does not match the database snapshot")), |
| 332 | - "{:?}", | 333 | + "case manifest repo list: {:?}", |
| 333 | report.problems | 334 | report.problems |
| 334 | ); | 335 | ); |
| 335 | -} | ||
| 336 | 336 | ||
| 337 | -#[tokio::test] | 337 | + let out = fx.dir.path().join("out-stray"); |
| 338 | -async fn unlisted_file_in_the_set_is_reported() { | ||
| 339 | - let fx = fixture().await; | ||
| 340 | - let out = fx.dir.path().join("out"); | ||
| 341 | let summary = create_backup(&fx.source, &opts(&out, false, Scope::All)) | 338 | let summary = create_backup(&fx.source, &opts(&out, false, Scope::All)) |
| 342 | .await | 339 | .await |
| 343 | .expect("backup"); | 340 | .expect("backup"); |
| @@ -348,7 +345,7 @@ async fn unlisted_file_in_the_set_is_reported() { | |||
| 348 | .problems | 345 | .problems |
| 349 | .iter() | 346 | .iter() |
| 350 | .any(|p| p.contains("unlisted file in the set: repos/stray.txt")), | 347 | .any(|p| p.contains("unlisted file in the set: repos/stray.txt")), |
| 351 | - "{:?}", | 348 | + "case stray file: {:?}", |
| 352 | report.problems | 349 | report.problems |
| 353 | ); | 350 | ); |
| 354 | } | 351 | } |
| @@ -394,30 +391,33 @@ async fn key_is_excluded_by_default_and_included_on_request() { | |||
| 394 | ); | 391 | ); |
| 395 | } | 392 | } |
| 396 | 393 | ||
| 394 | +/// A failed backup leaves nothing behind. Each failure has its own output directory, and the | ||
| 395 | +/// repo breaking case runs last because it damages the shared fixture. | ||
| 397 | #[tokio::test] | 396 | #[tokio::test] |
| 398 | -async fn include_key_without_a_key_fails_and_leaves_nothing() { | 397 | +async fn failed_backups_leave_no_partial_directory() { |
| 399 | let fx = fixture().await; | 398 | let fx = fixture().await; |
| 400 | - let out = fx.dir.path().join("out"); | 399 | + |
| 400 | + let out = fx.dir.path().join("out-no-key"); | ||
| 401 | let err = create_backup(&fx.source, &opts(&out, true, Scope::All)).await; | 401 | let err = create_backup(&fx.source, &opts(&out, true, Scope::All)).await; |
| 402 | - assert!(err.is_err()); | 402 | + assert!(err.is_err(), "case include key without a key"); |
| 403 | - assert_eq!(std::fs::read_dir(&out).expect("out").count(), 0); | 403 | + assert_eq!( |
| 404 | -} | 404 | + std::fs::read_dir(&out).expect("out").count(), |
| 405 | + 0, | ||
| 406 | + "case include key without a key" | ||
| 407 | + ); | ||
| 405 | 408 | ||
| 406 | -#[tokio::test] | ||
| 407 | -async fn failure_in_a_repo_leaves_no_partial_directory() { | ||
| 408 | - let fx = fixture().await; | ||
| 409 | - let out = fx.dir.path().join("out"); | ||
| 410 | // A repo with no HEAD is not a repository, so reading its refs fails mid-backup. | 409 | // A repo with no HEAD is not a repository, so reading its refs fails mid-backup. |
| 410 | + let out = fx.dir.path().join("out-broken-repo"); | ||
| 411 | let broken = repo_path(&fx.data_dir, &fx.demo_id).expect("path"); | 411 | let broken = repo_path(&fx.data_dir, &fx.demo_id).expect("path"); |
| 412 | std::fs::remove_file(broken.join("HEAD")).expect("break repo"); | 412 | std::fs::remove_file(broken.join("HEAD")).expect("break repo"); |
| 413 | 413 | ||
| 414 | let result = create_backup(&fx.source, &opts(&out, false, Scope::All)).await; | 414 | let result = create_backup(&fx.source, &opts(&out, false, Scope::All)).await; |
| 415 | - assert!(result.is_err(), "backup must fail"); | 415 | + assert!(result.is_err(), "case broken repo: backup must fail"); |
| 416 | let left: Vec<String> = std::fs::read_dir(&out) | 416 | let left: Vec<String> = std::fs::read_dir(&out) |
| 417 | .expect("out") | 417 | .expect("out") |
| 418 | .map(|e| e.expect("entry").file_name().to_string_lossy().into_owned()) | 418 | .map(|e| e.expect("entry").file_name().to_string_lossy().into_owned()) |
| 419 | .collect(); | 419 | .collect(); |
| 420 | - assert!(left.is_empty(), "nothing may be left behind: {left:?}"); | 420 | + assert!(left.is_empty(), "case broken repo, nothing may be left behind: {left:?}"); |
| 421 | } | 421 | } |
| 422 | 422 | ||
| 423 | #[tokio::test] | 423 | #[tokio::test] |
crates/forge-web/src/i18n/mod.rsmodified+27 -31
| @@ -1360,12 +1360,6 @@ mod tests { | |||
| 1360 | use super::*; | 1360 | use super::*; |
| 1361 | use http::HeaderValue; | 1361 | use http::HeaderValue; |
| 1362 | 1362 | ||
| 1363 | - fn headers(value: &'static str) -> HeaderMap { | ||
| 1364 | - let mut map = HeaderMap::new(); | ||
| 1365 | - map.insert(ACCEPT_LANGUAGE, HeaderValue::from_static(value)); | ||
| 1366 | - map | ||
| 1367 | - } | ||
| 1368 | - | ||
| 1369 | #[test] | 1363 | #[test] |
| 1370 | fn every_key_has_text_in_both_languages() { | 1364 | fn every_key_has_text_in_both_languages() { |
| 1371 | for key in ALL { | 1365 | for key in ALL { |
| @@ -1382,36 +1376,38 @@ mod tests { | |||
| 1382 | } | 1376 | } |
| 1383 | 1377 | ||
| 1384 | #[test] | 1378 | #[test] |
| 1385 | - fn stored_language_wins_over_the_browser() { | 1379 | + fn stored_preference_then_browser_picks_the_language() { |
| 1386 | - let turkish = headers("tr"); | 1380 | + // (case, stored preference, Accept-Language, expected) |
| 1387 | - assert_eq!(resolve(Some("en"), &turkish), Lang::En); | 1381 | + // An empty accept string means the header is absent. |
| 1388 | - assert_eq!(resolve(Some("tr"), &HeaderMap::new()), Lang::Tr); | 1382 | + let cases: [(&str, Option<&str>, &str, Lang); 10] = [ |
| 1389 | - } | 1383 | + ("stored en beats browser tr", Some("en"), "tr", Lang::En), |
| 1390 | - | 1384 | + ("stored tr with no header", Some("tr"), "", Lang::Tr), |
| 1391 | - #[test] | 1385 | + ("auto follows browser", Some("auto"), "tr-TR,en;q=0.5", Lang::Tr), |
| 1392 | - fn auto_and_missing_preferences_follow_the_browser() { | 1386 | + ("missing preference follows browser", None, "tr", Lang::Tr), |
| 1393 | - assert_eq!(resolve(Some("auto"), &headers("tr-TR,en;q=0.5")), Lang::Tr); | 1387 | + ("no preference, no header", None, "", Lang::En), |
| 1394 | - assert_eq!(resolve(None, &headers("tr")), Lang::Tr); | 1388 | + ("highest q supported wins", None, "de,en;q=0.8,tr;q=0.9", Lang::Tr), |
| 1395 | - assert_eq!(resolve(None, &HeaderMap::new()), Lang::En); | 1389 | + ("higher q for en", None, "en;q=0.9,tr;q=0.5", Lang::En), |
| 1396 | - } | 1390 | + ("q=0 rejects tr", None, "tr;q=0,en", Lang::En), |
| 1397 | - | 1391 | + ("unsupported tags fall back to en", None, "de,fr;q=0.9", Lang::En), |
| 1398 | - #[test] | 1392 | + ("malformed q is ignored", None, "tr;q=oops,en;q=0.1", Lang::En), |
| 1399 | - fn accept_language_picks_the_highest_quality_supported_tag() { | 1393 | + ]; |
| 1400 | - assert_eq!(resolve(None, &headers("de,en;q=0.8,tr;q=0.9")), Lang::Tr); | 1394 | + for (name, stored, accept, want) in cases { |
| 1401 | - assert_eq!(resolve(None, &headers("en;q=0.9,tr;q=0.5")), Lang::En); | 1395 | + let map = if accept.is_empty() { |
| 1402 | - assert_eq!(resolve(None, &headers("tr;q=0,en")), Lang::En); | 1396 | + HeaderMap::new() |
| 1403 | - assert_eq!(resolve(None, &headers("de,fr;q=0.9")), Lang::En); | 1397 | + } else { |
| 1404 | - assert_eq!(resolve(None, &headers("tr;q=oops,en;q=0.1")), Lang::En); | 1398 | + let mut map = HeaderMap::new(); |
| 1399 | + map.insert(ACCEPT_LANGUAGE, HeaderValue::from_str(accept).expect("ascii")); | ||
| 1400 | + map | ||
| 1401 | + }; | ||
| 1402 | + assert_eq!(resolve(stored, &map), want, "case {name}"); | ||
| 1403 | + } | ||
| 1405 | } | 1404 | } |
| 1406 | 1405 | ||
| 1407 | #[test] | 1406 | #[test] |
| 1408 | - fn unknown_stored_code_is_english() { | 1407 | + fn language_codes_round_trip_and_unknown_is_english() { |
| 1408 | + // The codes are stored in the database, so the literals are checked explicitly. | ||
| 1409 | assert_eq!(Lang::from_code("fr"), Lang::En); | 1409 | assert_eq!(Lang::from_code("fr"), Lang::En); |
| 1410 | assert_eq!(Lang::from_code("tr"), Lang::Tr); | 1410 | assert_eq!(Lang::from_code("tr"), Lang::Tr); |
| 1411 | - } | ||
| 1412 | - | ||
| 1413 | - #[test] | ||
| 1414 | - fn html_codes_match_the_languages() { | ||
| 1415 | assert_eq!(Lang::En.code(), "en"); | 1411 | assert_eq!(Lang::En.code(), "en"); |
| 1416 | assert_eq!(Lang::Tr.code(), "tr"); | 1412 | assert_eq!(Lang::Tr.code(), "tr"); |
| 1417 | } | 1413 | } |
crates/forge-web/src/importer/tests.rsmodified+29 -45
| @@ -260,11 +260,11 @@ fn fake() -> Fake { | |||
| 260 | async fn git_url_import_copies_all_branches_and_tags_into_a_normal_repo() { | 260 | async fn git_url_import_copies_all_branches_and_tags_into_a_normal_repo() { |
| 261 | let fx = fixture().await; | 261 | let fx = fixture().await; |
| 262 | let job = new_job(&fx, Source::Git).await; | 262 | let job = new_job(&fx, Source::Git).await; |
| 263 | - let spec = spec(Source::Git, ImportOptions::default()); | 263 | + let git_spec = spec(Source::Git, ImportOptions::default()); |
| 264 | run_job::<Fake>( | 264 | run_job::<Fake>( |
| 265 | &fx.state, | 265 | &fx.state, |
| 266 | &job, | 266 | &job, |
| 267 | - &spec, | 267 | + &git_spec, |
| 268 | None, | 268 | None, |
| 269 | &Transport::Local(fx.upstream.clone()), | 269 | &Transport::Local(fx.upstream.clone()), |
| 270 | ) | 270 | ) |
| @@ -294,6 +294,21 @@ async fn git_url_import_copies_all_branches_and_tags_into_a_normal_repo() { | |||
| 294 | .unwrap() | 294 | .unwrap() |
| 295 | .is_none() | 295 | .is_none() |
| 296 | ); | 296 | ); |
| 297 | + | ||
| 298 | + // A fetch from a missing remote fails its job, and the failure is recorded. | ||
| 299 | + let bad_job = new_job(&fx, Source::Git).await; | ||
| 300 | + let bad_spec = spec(Source::Git, ImportOptions::default()); | ||
| 301 | + run_job::<Fake>( | ||
| 302 | + &fx.state, | ||
| 303 | + &bad_job, | ||
| 304 | + &bad_spec, | ||
| 305 | + None, | ||
| 306 | + &Transport::Local("/nonexistent/path.git".into()), | ||
| 307 | + ) | ||
| 308 | + .await; | ||
| 309 | + let bad_job = fx.state.store.import_job(&bad_job.id).await.unwrap().unwrap(); | ||
| 310 | + assert_eq!(bad_job.state, ImportState::Failed); | ||
| 311 | + assert!(bad_job.error.is_some()); | ||
| 297 | } | 312 | } |
| 298 | 313 | ||
| 299 | #[tokio::test] | 314 | #[tokio::test] |
| @@ -366,32 +381,19 @@ async fn full_api_import_brings_issues_pulls_comments_labels_and_releases() { | |||
| 366 | assert_eq!(assets.len(), 1); | 381 | assert_eq!(assets.len(), 1); |
| 367 | assert_eq!(assets[0].name, "app_1.tar.gz"); | 382 | assert_eq!(assets[0].name, "app_1.tar.gz"); |
| 368 | assert_eq!(assets[0].size, 5); | 383 | assert_eq!(assets[0].size, 5); |
| 369 | -} | ||
| 370 | 384 | ||
| 371 | -#[tokio::test] | 385 | + // A second run of the same import creates nothing twice. |
| 372 | -async fn running_an_import_twice_creates_nothing_twice() { | 386 | + let again = new_job(&fx, Source::GitHub).await; |
| 373 | - let fx = fixture().await; | 387 | + run_job( |
| 374 | - let api = fake(); | 388 | + &fx.state, |
| 375 | - let spec = spec(Source::GitHub, everything()); | 389 | + &again, |
| 376 | - for _ in 0..2 { | 390 | + &spec, |
| 377 | - let job = new_job(&fx, Source::GitHub).await; | 391 | + Some(&api), |
| 378 | - run_job( | 392 | + &Transport::Local(fx.upstream.clone()), |
| 379 | - &fx.state, | 393 | + ) |
| 380 | - &job, | 394 | + .await; |
| 381 | - &spec, | 395 | + let again = store.import_job(&again.id).await.unwrap().unwrap(); |
| 382 | - Some(&api), | 396 | + assert_eq!(again.state, ImportState::Done, "{:?}", again.error); |
| 383 | - &Transport::Local(fx.upstream.clone()), | ||
| 384 | - ) | ||
| 385 | - .await; | ||
| 386 | - let job = fx.state.store.import_job(&job.id).await.unwrap().unwrap(); | ||
| 387 | - assert_eq!(job.state, ImportState::Done, "{:?}", job.error); | ||
| 388 | - } | ||
| 389 | - let store = &fx.state.store; | ||
| 390 | - let all = IssueFilter { | ||
| 391 | - state: None, | ||
| 392 | - per_page: 100, | ||
| 393 | - ..IssueFilter::default() | ||
| 394 | - }; | ||
| 395 | let (items, total) = store.list_issues(&fx.repo.id, &all).await.unwrap(); | 397 | let (items, total) = store.list_issues(&fx.repo.id, &all).await.unwrap(); |
| 396 | assert_eq!(total, 2); | 398 | assert_eq!(total, 2); |
| 397 | let first = items.iter().find(|i| i.issue.title == "It breaks").unwrap(); | 399 | let first = items.iter().find(|i| i.issue.title == "It breaks").unwrap(); |
| @@ -489,24 +491,6 @@ async fn the_token_is_never_written_to_the_database() { | |||
| 489 | ); | 491 | ); |
| 490 | } | 492 | } |
| 491 | 493 | ||
| 492 | -#[tokio::test] | ||
| 493 | -async fn a_failed_git_fetch_marks_the_job_failed() { | ||
| 494 | - let fx = fixture().await; | ||
| 495 | - let job = new_job(&fx, Source::Git).await; | ||
| 496 | - let spec = spec(Source::Git, ImportOptions::default()); | ||
| 497 | - run_job::<Fake>( | ||
| 498 | - &fx.state, | ||
| 499 | - &job, | ||
| 500 | - &spec, | ||
| 501 | - None, | ||
| 502 | - &Transport::Local("/nonexistent/path.git".into()), | ||
| 503 | - ) | ||
| 504 | - .await; | ||
| 505 | - let job = fx.state.store.import_job(&job.id).await.unwrap().unwrap(); | ||
| 506 | - assert_eq!(job.state, ImportState::Failed); | ||
| 507 | - assert!(job.error.is_some()); | ||
| 508 | -} | ||
| 509 | - | ||
| 510 | #[test] | 494 | #[test] |
| 511 | fn the_origin_note_escapes_the_mention_and_cleans_the_name() { | 495 | fn the_origin_note_escapes_the_mention_and_cleans_the_name() { |
| 512 | let note = origin_note( | 496 | let note = origin_note( |
crates/forge-web/src/mail/templates.rsmodified+10 -9
| @@ -214,15 +214,16 @@ mod tests { | |||
| 214 | } | 214 | } |
| 215 | 215 | ||
| 216 | #[test] | 216 | #[test] |
| 217 | - fn subject_names_the_repo_title_and_number() { | 217 | + fn subject_names_the_repo_title_and_number_and_stays_on_one_line() { |
| 218 | - let (subject, _) = notification(Lang::En, &sample(None, "Crash on start")); | 218 | + // A plain title is named as-is; a newline in the title cannot split the subject. |
| 219 | - assert_eq!(subject, "[bob/widgets] Crash on start (#7)"); | 219 | + let cases = [ |
| 220 | - } | 220 | + ("plain", "Crash on start", "[bob/widgets] Crash on start (#7)"), |
| 221 | - | 221 | + ("newline", "two\r\nlines", "[bob/widgets] two lines (#7)"), |
| 222 | - #[test] | 222 | + ]; |
| 223 | - fn a_newline_in_the_title_cannot_split_the_subject() { | 223 | + for (name, title, want) in cases { |
| 224 | - let (subject, _) = notification(Lang::En, &sample(None, "two\r\nlines")); | 224 | + let (subject, _) = notification(Lang::En, &sample(None, title)); |
| 225 | - assert_eq!(subject, "[bob/widgets] two lines (#7)"); | 225 | + assert_eq!(subject, want, "case {name}"); |
| 226 | + } | ||
| 226 | } | 227 | } |
| 227 | 228 | ||
| 228 | #[test] | 229 | #[test] |
crates/forge-web/src/pulls_sync.rsmodified+2 -5
| @@ -738,7 +738,7 @@ mod tests { | |||
| 738 | } | 738 | } |
| 739 | 739 | ||
| 740 | #[test] | 740 | #[test] |
| 741 | - fn unchanged_line_follows_insertions_above() { | 741 | + fn unchanged_line_follows_insertions_and_skips_changed_lines() { |
| 742 | let old = "a\nb\nc\n"; | 742 | let old = "a\nb\nc\n"; |
| 743 | let new = "x\na\nb\nc\n"; | 743 | let new = "x\na\nb\nc\n"; |
| 744 | assert_eq!(unchanged_line(old, new, 1), Some(2)); | 744 | assert_eq!(unchanged_line(old, new, 1), Some(2)); |
| @@ -746,10 +746,7 @@ mod tests { | |||
| 746 | assert_eq!(unchanged_line(old, new, 3), Some(4)); | 746 | assert_eq!(unchanged_line(old, new, 3), Some(4)); |
| 747 | assert_eq!(unchanged_line(old, new, 0), None); | 747 | assert_eq!(unchanged_line(old, new, 0), None); |
| 748 | assert_eq!(unchanged_line(old, new, 9), None); | 748 | assert_eq!(unchanged_line(old, new, 9), None); |
| 749 | - } | 749 | + // A modified or deleted line has no unchanged counterpart. |
| 750 | - | ||
| 751 | - #[test] | ||
| 752 | - fn unchanged_line_is_none_for_modified_or_deleted_lines() { | ||
| 753 | assert_eq!(unchanged_line("a\nb\nc\n", "a\nB\nc\n", 2), None); | 750 | assert_eq!(unchanged_line("a\nb\nc\n", "a\nB\nc\n", 2), None); |
| 754 | assert_eq!(unchanged_line("a\nb\nc\n", "a\nB\nc\n", 3), Some(3)); | 751 | assert_eq!(unchanged_line("a\nb\nc\n", "a\nB\nc\n", 3), Some(3)); |
| 755 | assert_eq!(unchanged_line("a\nb\n", "a\n", 2), None); | 752 | assert_eq!(unchanged_line("a\nb\n", "a\n", 2), None); |
crates/forge-web/src/replication/mod.rsmodified+1 -9
| @@ -651,7 +651,7 @@ mod tests { | |||
| 651 | } | 651 | } |
| 652 | 652 | ||
| 653 | #[test] | 653 | #[test] |
| 654 | - fn target_parses_user_host_and_base() { | 654 | + fn target_parses_user_host_and_base_and_refuses_bad_specs() { |
| 655 | let t = Target::parse("sb@standby.example:/srv/forge-standby", 2222).expect("parse"); | 655 | let t = Target::parse("sb@standby.example:/srv/forge-standby", 2222).expect("parse"); |
| 656 | assert_eq!(t.user.as_deref(), Some("sb")); | 656 | assert_eq!(t.user.as_deref(), Some("sb")); |
| 657 | assert_eq!(t.host, "standby.example"); | 657 | assert_eq!(t.host, "standby.example"); |
| @@ -663,10 +663,6 @@ mod tests { | |||
| 663 | ); | 663 | ); |
| 664 | let bare = Target::parse("standby:/srv/x", 22).expect("parse"); | 664 | let bare = Target::parse("standby:/srv/x", 22).expect("parse"); |
| 665 | assert_eq!(bare.destination(), "standby"); | 665 | assert_eq!(bare.destination(), "standby"); |
| 666 | - } | ||
| 667 | - | ||
| 668 | - #[test] | ||
| 669 | - fn target_rejects_bad_specs() { | ||
| 670 | for bad in [ | 666 | for bad in [ |
| 671 | "no-colon", | 667 | "no-colon", |
| 672 | "sb@host", | 668 | "sb@host", |
| @@ -708,10 +704,6 @@ mod tests { | |||
| 708 | assert!(!is_snapshot_name(".tmp-forge-20260101T120000Z.db")); | 704 | assert!(!is_snapshot_name(".tmp-forge-20260101T120000Z.db")); |
| 709 | assert!(!is_snapshot_name("forge-2026-01-01T12:00:00Z.db")); | 705 | assert!(!is_snapshot_name("forge-2026-01-01T12:00:00Z.db")); |
| 710 | assert!(!is_snapshot_name("forge-20260101X120000Z.db")); | 706 | assert!(!is_snapshot_name("forge-20260101X120000Z.db")); |
| 711 | - } | ||
| 712 | - | ||
| 713 | - #[test] | ||
| 714 | - fn snapshot_stamp_has_the_expected_shape() { | ||
| 715 | let stamp = snapshot_stamp(); | 707 | let stamp = snapshot_stamp(); |
| 716 | assert!(is_snapshot_name(&format!("forge-{stamp}.db")), "{stamp}"); | 708 | assert!(is_snapshot_name(&format!("forge-{stamp}.db")), "{stamp}"); |
| 717 | } | 709 | } |
crates/forge-web/src/views/heatmap.rsmodified+29 -22
| @@ -352,28 +352,35 @@ mod tests { | |||
| 352 | } | 352 | } |
| 353 | 353 | ||
| 354 | #[test] | 354 | #[test] |
| 355 | - fn a_gap_ends_the_current_streak_but_not_the_longest() { | 355 | + fn a_gap_or_an_old_run_ends_the_current_streak() { |
| 356 | - let cal = build( | 356 | + // (case, counts, current streak, longest streak) |
| 357 | - &counts(&[ | 357 | + let cases: [(&str, &[(&str, u32)], u64, u64); 2] = [ |
| 358 | - ("2026-09-20", 1), | 358 | + ( |
| 359 | - ("2026-09-21", 1), | 359 | + "old run", |
| 360 | - ("2026-09-22", 1), | 360 | + &[("2026-10-01", 3), ("2026-10-02", 3)], |
| 361 | - ("2026-09-23", 1), | 361 | + 0, |
| 362 | - ("2026-09-24", 1), | 362 | + 2, |
| 363 | - ("2026-10-15", 2), | 363 | + ), |
| 364 | - ("2026-10-16", 2), | 364 | + ( |
| 365 | - ]), | 365 | + "gap then two days", |
| 366 | - TODAY, | 366 | + &[ |
| 367 | - ); | 367 | + ("2026-09-20", 1), |
| 368 | - assert_eq!(cal.longest_streak, 5); | 368 | + ("2026-09-21", 1), |
| 369 | - assert_eq!(cal.current_streak, 2); | 369 | + ("2026-09-22", 1), |
| 370 | - } | 370 | + ("2026-09-23", 1), |
| 371 | - | 371 | + ("2026-09-24", 1), |
| 372 | - #[test] | 372 | + ("2026-10-15", 2), |
| 373 | - fn an_old_run_is_not_the_current_streak() { | 373 | + ("2026-10-16", 2), |
| 374 | - let cal = build(&counts(&[("2026-10-01", 3), ("2026-10-02", 3)]), TODAY); | 374 | + ], |
| 375 | - assert_eq!(cal.current_streak, 0); | 375 | + 2, |
| 376 | - assert_eq!(cal.longest_streak, 2); | 376 | + 5, |
| 377 | + ), | ||
| 378 | + ]; | ||
| 379 | + for (name, input, current, longest) in cases { | ||
| 380 | + let cal = build(&counts(input), TODAY); | ||
| 381 | + assert_eq!(cal.current_streak, current, "case {name}"); | ||
| 382 | + assert_eq!(cal.longest_streak, longest, "case {name}"); | ||
| 383 | + } | ||
| 377 | } | 384 | } |
| 378 | 385 | ||
| 379 | #[test] | 386 | #[test] |
crates/forge-web/src/views/mod.rsmodified+48 -51
| @@ -1171,16 +1171,13 @@ mod tests { | |||
| 1171 | } | 1171 | } |
| 1172 | 1172 | ||
| 1173 | #[test] | 1173 | #[test] |
| 1174 | - fn csp_allows_the_raw_origin_for_images() { | 1174 | + fn csp_allows_the_raw_origin_for_images_and_falls_back_otherwise() { |
| 1175 | let origin = origin_of("https://raw.forge.test:8443/some/path/"); | 1175 | let origin = origin_of("https://raw.forge.test:8443/some/path/"); |
| 1176 | assert_eq!(origin.as_deref(), Some("https://raw.forge.test:8443")); | 1176 | assert_eq!(origin.as_deref(), Some("https://raw.forge.test:8443")); |
| 1177 | let csp = csp_for(origin.as_deref()); | 1177 | let csp = csp_for(origin.as_deref()); |
| 1178 | assert!(csp.contains("img-src 'self' data: https://raw.forge.test:8443;")); | 1178 | assert!(csp.contains("img-src 'self' data: https://raw.forge.test:8443;")); |
| 1179 | assert!(csp.contains("form-action 'self'; frame-ancestors 'none'")); | 1179 | assert!(csp.contains("form-action 'self'; frame-ancestors 'none'")); |
| 1180 | - } | 1180 | + // A base URL that is not an origin falls back to the plain policy. |
| 1181 | - | ||
| 1182 | - #[test] | ||
| 1183 | - fn csp_falls_back_when_the_base_url_is_not_an_origin() { | ||
| 1184 | assert_eq!(origin_of("not a url"), None); | 1181 | assert_eq!(origin_of("not a url"), None); |
| 1185 | assert_eq!(origin_of("ftp://files.test"), None); | 1182 | assert_eq!(origin_of("ftp://files.test"), None); |
| 1186 | assert_eq!(origin_of("https://user:pw@files.test"), None); | 1183 | assert_eq!(origin_of("https://user:pw@files.test"), None); |
| @@ -1239,25 +1236,26 @@ mod tests { | |||
| 1239 | } | 1236 | } |
| 1240 | 1237 | ||
| 1241 | /// Renders `/settings/sessions` (a page that uses the shell) for a freshly signed-in user. | 1238 | /// Renders `/settings/sessions` (a page that uses the shell) for a freshly signed-in user. |
| 1242 | - async fn menu_html(is_admin: bool) -> String { | 1239 | + async fn menu_html( |
| 1240 | + router: &topcoat::router::Router, | ||
| 1241 | + store: &forge_core::Store, | ||
| 1242 | + name: &str, | ||
| 1243 | + is_admin: bool, | ||
| 1244 | + token: &str, | ||
| 1245 | + ) -> String { | ||
| 1243 | use crate::auth::SESSION_COOKIE; | 1246 | use crate::auth::SESSION_COOKIE; |
| 1244 | use topcoat::router::request::Request; | 1247 | use topcoat::router::request::Request; |
| 1245 | use topcoat::router::to_bytes; | 1248 | use topcoat::router::to_bytes; |
| 1246 | 1249 | ||
| 1247 | - let dir = tempfile::tempdir().expect("tempdir"); | ||
| 1248 | - let state = crate::test_support::test_state(dir.path()).await; | ||
| 1249 | - let store = state.store.clone(); | ||
| 1250 | - let router = crate::test_support::app_router(state.clone()); | ||
| 1251 | - let name = if is_admin { "root" } else { "tahinli" }; | ||
| 1252 | let user = store | 1250 | let user = store |
| 1253 | .create_user(name, None, "x", is_admin) | 1251 | .create_user(name, None, "x", is_admin) |
| 1254 | .await | 1252 | .await |
| 1255 | .expect("user"); | 1253 | .expect("user"); |
| 1256 | - crate::test_support::create_session(&store, &user.id, "menu-token", "menu-csrf").await; | 1254 | + crate::test_support::create_session(store, &user.id, token, "menu-csrf").await; |
| 1257 | let req = Request::builder() | 1255 | let req = Request::builder() |
| 1258 | .method("GET") | 1256 | .method("GET") |
| 1259 | .uri("/settings/sessions") | 1257 | .uri("/settings/sessions") |
| 1260 | - .header("cookie", format!("__Host-{SESSION_COOKIE}=menu-token")) | 1258 | + .header("cookie", format!("__Host-{SESSION_COOKIE}={token}")) |
| 1261 | .body(Body::empty()) | 1259 | .body(Body::empty()) |
| 1262 | .expect("request"); | 1260 | .expect("request"); |
| 1263 | let res = router.handle(req).await; | 1261 | let res = router.handle(req).await; |
| @@ -1266,17 +1264,21 @@ mod tests { | |||
| 1266 | String::from_utf8_lossy(&bytes).into_owned() | 1264 | String::from_utf8_lossy(&bytes).into_owned() |
| 1267 | } | 1265 | } |
| 1268 | 1266 | ||
| 1267 | + /// A user sees no admin links and an admin sees the one Admin entry. Both render from one | ||
| 1268 | + /// fixture. | ||
| 1269 | #[tokio::test] | 1269 | #[tokio::test] |
| 1270 | - async fn admin_links_are_hidden_from_non_admins() { | 1270 | + async fn admin_links_follow_the_role() { |
| 1271 | - let html = menu_html(false).await; | 1271 | + let dir = tempfile::tempdir().expect("tempdir"); |
| 1272 | + let state = crate::test_support::test_state(dir.path()).await; | ||
| 1273 | + let store = state.store.clone(); | ||
| 1274 | + let router = crate::test_support::app_router(state); | ||
| 1275 | + | ||
| 1276 | + let html = menu_html(&router, &store, "tahinli", false, "menu-token-user").await; | ||
| 1272 | assert!(html.contains("Log out"), "user menu is rendered"); | 1277 | assert!(html.contains("Log out"), "user menu is rendered"); |
| 1273 | assert!(!html.contains("/admin/users"), "no admin link for a user"); | 1278 | assert!(!html.contains("/admin/users"), "no admin link for a user"); |
| 1274 | assert!(!html.contains("/admin/audit"), "no audit link for a user"); | 1279 | assert!(!html.contains("/admin/audit"), "no audit link for a user"); |
| 1275 | - } | ||
| 1276 | 1280 | ||
| 1277 | - #[tokio::test] | 1281 | + let html = menu_html(&router, &store, "root", true, "menu-token-admin").await; |
| 1278 | - async fn admin_links_are_shown_to_admins() { | ||
| 1279 | - let html = menu_html(true).await; | ||
| 1280 | assert!(html.contains("href=\"/admin/users\"")); | 1282 | assert!(html.contains("href=\"/admin/users\"")); |
| 1281 | assert!( | 1283 | assert!( |
| 1282 | !html.contains("Admin:"), | 1284 | !html.contains("Admin:"), |
| @@ -1340,6 +1342,13 @@ mod shell_tests { | |||
| 1340 | 1342 | ||
| 1341 | assert!(html.contains("<main tabindex=\"-1\">")); | 1343 | assert!(html.contains("<main tabindex=\"-1\">")); |
| 1342 | assert!(html.contains("data-hard=\"true\" method=\"post\" action=\"/login\"")); | 1344 | assert!(html.contains("data-hard=\"true\" method=\"post\" action=\"/login\"")); |
| 1345 | + | ||
| 1346 | + // An anonymous page renders the default colours. | ||
| 1347 | + let tag = html_tag(&html); | ||
| 1348 | + assert!(tag.contains("data-accent-light=\"sage\""), "{tag}"); | ||
| 1349 | + assert!(tag.contains("data-accent-dark=\"sage\""), "{tag}"); | ||
| 1350 | + assert!(tag.contains("data-base-light=\"fern\""), "{tag}"); | ||
| 1351 | + assert!(tag.contains("data-base-dark=\"fern\""), "{tag}"); | ||
| 1343 | } | 1352 | } |
| 1344 | 1353 | ||
| 1345 | /// The `<html …>` start tag of a page. | 1354 | /// The `<html …>` start tag of a page. |
| @@ -1351,14 +1360,16 @@ mod shell_tests { | |||
| 1351 | 1360 | ||
| 1352 | /// The `<html>` tag of `/settings/sessions` for a signed-in user whose stored colours are the | 1361 | /// The `<html>` tag of `/settings/sessions` for a signed-in user whose stored colours are the |
| 1353 | /// four ids given, in the order light accent, dark accent, light base, dark base. | 1362 | /// four ids given, in the order light accent, dark accent, light base, dark base. |
| 1354 | - async fn html_tag_for_colours(colours: [&str; 4]) -> String { | 1363 | + async fn html_tag_for_colours( |
| 1364 | + router: &topcoat::router::Router, | ||
| 1365 | + store: &forge_core::Store, | ||
| 1366 | + name: &str, | ||
| 1367 | + token: &str, | ||
| 1368 | + colours: [&str; 4], | ||
| 1369 | + ) -> String { | ||
| 1355 | use crate::auth::SESSION_COOKIE; | 1370 | use crate::auth::SESSION_COOKIE; |
| 1356 | - let dir = tempfile::tempdir().expect("tempdir"); | ||
| 1357 | - let state = crate::test_support::test_state(dir.path()).await; | ||
| 1358 | - let store = state.store.clone(); | ||
| 1359 | - let router = crate::test_support::app_router(state); | ||
| 1360 | let user = store | 1371 | let user = store |
| 1361 | - .create_user("tahinli", None, "x", false) | 1372 | + .create_user(name, None, "x", false) |
| 1362 | .await | 1373 | .await |
| 1363 | .expect("user"); | 1374 | .expect("user"); |
| 1364 | let [accent_light, accent_dark, base_light, base_dark] = colours; | 1375 | let [accent_light, accent_dark, base_light, base_dark] = colours; |
| @@ -1366,11 +1377,11 @@ mod shell_tests { | |||
| 1366 | .set_colours(&user.id, accent_light, accent_dark, base_light, base_dark) | 1377 | .set_colours(&user.id, accent_light, accent_dark, base_light, base_dark) |
| 1367 | .await | 1378 | .await |
| 1368 | .expect("colours"); | 1379 | .expect("colours"); |
| 1369 | - crate::test_support::create_session(&store, &user.id, "colour-token", "colour-csrf").await; | 1380 | + crate::test_support::create_session(store, &user.id, token, "colour-csrf").await; |
| 1370 | let req = Request::builder() | 1381 | let req = Request::builder() |
| 1371 | .method("GET") | 1382 | .method("GET") |
| 1372 | .uri("/settings/sessions") | 1383 | .uri("/settings/sessions") |
| 1373 | - .header("cookie", format!("__Host-{SESSION_COOKIE}=colour-token")) | 1384 | + .header("cookie", format!("__Host-{SESSION_COOKIE}={token}")) |
| 1374 | .body(Body::empty()) | 1385 | .body(Body::empty()) |
| 1375 | .unwrap(); | 1386 | .unwrap(); |
| 1376 | let res = router.handle(req).await; | 1387 | let res = router.handle(req).await; |
| @@ -1379,50 +1390,36 @@ mod shell_tests { | |||
| 1379 | html_tag(&String::from_utf8_lossy(&bytes)).to_owned() | 1390 | html_tag(&String::from_utf8_lossy(&bytes)).to_owned() |
| 1380 | } | 1391 | } |
| 1381 | 1392 | ||
| 1393 | + /// Every colour case shares one fixture; each has its own user and session. | ||
| 1382 | #[tokio::test] | 1394 | #[tokio::test] |
| 1383 | async fn the_html_tag_carries_the_viewers_colours() { | 1395 | async fn the_html_tag_carries_the_viewers_colours() { |
| 1384 | - let tag = html_tag_for_colours(["ocean", "plum", "ink", "dusk"]).await; | 1396 | + let dir = tempfile::tempdir().expect("tempdir"); |
| 1397 | + let state = crate::test_support::test_state(dir.path()).await; | ||
| 1398 | + let store = state.store.clone(); | ||
| 1399 | + let router = crate::test_support::app_router(state); | ||
| 1400 | + | ||
| 1401 | + let tag = html_tag_for_colours(&router, &store, "tahinli", "colour-token", ["ocean", "plum", "ink", "dusk"]).await; | ||
| 1385 | assert!(tag.contains("data-accent-light=\"ocean\""), "{tag}"); | 1402 | assert!(tag.contains("data-accent-light=\"ocean\""), "{tag}"); |
| 1386 | assert!(tag.contains("data-accent-dark=\"plum\""), "{tag}"); | 1403 | assert!(tag.contains("data-accent-dark=\"plum\""), "{tag}"); |
| 1387 | assert!(tag.contains("data-base-light=\"ink\""), "{tag}"); | 1404 | assert!(tag.contains("data-base-light=\"ink\""), "{tag}"); |
| 1388 | assert!(tag.contains("data-base-dark=\"dusk\""), "{tag}"); | 1405 | assert!(tag.contains("data-base-dark=\"dusk\""), "{tag}"); |
| 1389 | assert!(!tag.contains("data-accent="), "{tag}"); | 1406 | assert!(!tag.contains("data-accent="), "{tag}"); |
| 1390 | // Stored ids that are no longer offered fall back to the defaults, never to nothing. | 1407 | // Stored ids that are no longer offered fall back to the defaults, never to nothing. |
| 1391 | - let removed = html_tag_for_colours(["removed-palette", "removed-palette", "x", "x"]).await; | 1408 | + let removed = html_tag_for_colours(&router, &store, "removed", "removed-token", ["removed-palette", "removed-palette", "x", "x"]).await; |
| 1392 | assert!(removed.contains("data-accent-light=\"sage\""), "{removed}"); | 1409 | assert!(removed.contains("data-accent-light=\"sage\""), "{removed}"); |
| 1393 | assert!(removed.contains("data-accent-dark=\"sage\""), "{removed}"); | 1410 | assert!(removed.contains("data-accent-dark=\"sage\""), "{removed}"); |
| 1394 | assert!(removed.contains("data-base-light=\"fern\""), "{removed}"); | 1411 | assert!(removed.contains("data-base-light=\"fern\""), "{removed}"); |
| 1395 | assert!(removed.contains("data-base-dark=\"fern\""), "{removed}"); | 1412 | assert!(removed.contains("data-base-dark=\"fern\""), "{removed}"); |
| 1396 | // A removed preset (`ochre`, `mist`) falls back too, and a hue passes through as itself. | 1413 | // A removed preset (`ochre`, `mist`) falls back too, and a hue passes through as itself. |
| 1397 | - let retired = html_tag_for_colours(["ochre", "ember", "mist", "stone"]).await; | 1414 | + let retired = html_tag_for_colours(&router, &store, "retired", "retired-token", ["ochre", "ember", "mist", "stone"]).await; |
| 1398 | assert!(retired.contains("data-accent-light=\"sage\""), "{retired}"); | 1415 | assert!(retired.contains("data-accent-light=\"sage\""), "{retired}"); |
| 1399 | assert!(retired.contains("data-accent-dark=\"sage\""), "{retired}"); | 1416 | assert!(retired.contains("data-accent-dark=\"sage\""), "{retired}"); |
| 1400 | assert!(retired.contains("data-base-light=\"fern\""), "{retired}"); | 1417 | assert!(retired.contains("data-base-light=\"fern\""), "{retired}"); |
| 1401 | assert!(retired.contains("data-base-dark=\"fern\""), "{retired}"); | 1418 | assert!(retired.contains("data-base-dark=\"fern\""), "{retired}"); |
| 1402 | - let hued = html_tag_for_colours(["hue-215", "hue-7", "hue-300", "spark"]).await; | 1419 | + let hued = html_tag_for_colours(&router, &store, "hued", "hued-token", ["hue-215", "hue-7", "hue-300", "spark"]).await; |
| 1403 | assert!(hued.contains("data-accent-light=\"hue-215\""), "{hued}"); | 1420 | assert!(hued.contains("data-accent-light=\"hue-215\""), "{hued}"); |
| 1404 | assert!(hued.contains("data-accent-dark=\"hue-7\""), "{hued}"); | 1421 | assert!(hued.contains("data-accent-dark=\"hue-7\""), "{hued}"); |
| 1405 | assert!(hued.contains("data-base-light=\"hue-300\""), "{hued}"); | 1422 | assert!(hued.contains("data-base-light=\"hue-300\""), "{hued}"); |
| 1406 | assert!(hued.contains("data-base-dark=\"spark\""), "{hued}"); | 1423 | assert!(hued.contains("data-base-dark=\"spark\""), "{hued}"); |
| 1407 | } | 1424 | } |
| 1408 | - | ||
| 1409 | - #[tokio::test] | ||
| 1410 | - async fn anonymous_pages_render_the_default_colours() { | ||
| 1411 | - let dir = tempfile::tempdir().expect("tempdir"); | ||
| 1412 | - let state = crate::test_support::test_state(dir.path()).await; | ||
| 1413 | - let router = crate::test_support::app_router(state); | ||
| 1414 | - let req = Request::builder() | ||
| 1415 | - .method("GET") | ||
| 1416 | - .uri("/login") | ||
| 1417 | - .body(Body::empty()) | ||
| 1418 | - .unwrap(); | ||
| 1419 | - let res = router.handle(req).await; | ||
| 1420 | - let bytes = to_bytes(res.into_body(), usize::MAX).await.unwrap(); | ||
| 1421 | - let html = String::from_utf8_lossy(&bytes); | ||
| 1422 | - let tag = html_tag(&html); | ||
| 1423 | - assert!(tag.contains("data-accent-light=\"sage\""), "{tag}"); | ||
| 1424 | - assert!(tag.contains("data-accent-dark=\"sage\""), "{tag}"); | ||
| 1425 | - assert!(tag.contains("data-base-light=\"fern\""), "{tag}"); | ||
| 1426 | - assert!(tag.contains("data-base-dark=\"fern\""), "{tag}"); | ||
| 1427 | - } | ||
| 1428 | } | 1425 | } |