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