Page tests share their fixtures and merge into tables
Browse files at this commiteb7e617ee40f6d51f5eab3d48edbbd151cc76b53Parent: a83ea02- test
- page tests share their fixtures and merge into tables
3 changed files +849 -845
- crates/forge-web/src/pages/releases_tests.rs +789 -766
- crates/forge-web/src/pages/repo.rs +25 -46
- crates/forge-web/src/pages/repo_secrets.rs +35 -33
crates/forge-web/src/pages/releases_tests.rsmodified+789 -766
| @@ -214,843 +214,866 @@ impl Fixture { | |||
| 214 | } | 214 | } |
| 215 | } | 215 | } |
| 216 | 216 | ||
| 217 | -#[tokio::test] | 217 | +/// The release's own fields, unchanged, with no tag change. |
| 218 | -async fn drafts_are_hidden_from_readers_and_visible_to_writers() { | 218 | +fn keep(rel: &Release) -> EditRelease { |
| 219 | - let fx = fixture(None).await; | 219 | + EditRelease { |
| 220 | - let published = fx.release("v1.0", false).await; | 220 | + title: rel.title.clone(), |
| 221 | - let draft = fx.release("v2.0", true).await; | 221 | + body: rel.body.clone(), |
| 222 | - let draft_asset = fx | 222 | + draft: rel.draft, |
| 223 | - .upload(&draft.id, "secret.bin", b"x", &fx.tahinli_cookie) | 223 | + prerelease: rel.prerelease, |
| 224 | - .await; | 224 | + tag: None, |
| 225 | - assert_eq!(draft_asset.status, 303, "writer uploads to a draft"); | 225 | + target: None, |
| 226 | - | 226 | + } |
| 227 | - let list = send( | ||
| 228 | - &fx.app, | ||
| 229 | - "GET", | ||
| 230 | - "/tahinli/proj/releases", | ||
| 231 | - None, | ||
| 232 | - None, | ||
| 233 | - Vec::new(), | ||
| 234 | - ) | ||
| 235 | - .await; | ||
| 236 | - assert_eq!(list.status, 200); | ||
| 237 | - assert!(list.text().contains("Release v1.0")); | ||
| 238 | - assert!( | ||
| 239 | - !list.text().contains("Release v2.0"), | ||
| 240 | - "anonymous list hides drafts" | ||
| 241 | - ); | ||
| 242 | - | ||
| 243 | - let anon_draft = send( | ||
| 244 | - &fx.app, | ||
| 245 | - "GET", | ||
| 246 | - "/tahinli/proj/releases/tag/v2.0", | ||
| 247 | - None, | ||
| 248 | - None, | ||
| 249 | - Vec::new(), | ||
| 250 | - ) | ||
| 251 | - .await; | ||
| 252 | - assert_eq!(anon_draft.status, 404); | ||
| 253 | - let anon_download = send( | ||
| 254 | - &fx.app, | ||
| 255 | - "GET", | ||
| 256 | - "/tahinli/proj/releases/download/v2.0/secret.bin", | ||
| 257 | - None, | ||
| 258 | - None, | ||
| 259 | - Vec::new(), | ||
| 260 | - ) | ||
| 261 | - .await; | ||
| 262 | - assert_eq!( | ||
| 263 | - anon_download.status, 404, | ||
| 264 | - "a draft asset is not downloadable by readers" | ||
| 265 | - ); | ||
| 266 | - | ||
| 267 | - let writer_draft = send( | ||
| 268 | - &fx.app, | ||
| 269 | - "GET", | ||
| 270 | - "/tahinli/proj/releases/tag/v2.0", | ||
| 271 | - Some(&fx.tahinli_cookie), | ||
| 272 | - None, | ||
| 273 | - Vec::new(), | ||
| 274 | - ) | ||
| 275 | - .await; | ||
| 276 | - assert_eq!(writer_draft.status, 200, "the owner sees the draft"); | ||
| 277 | - let writer_list = send( | ||
| 278 | - &fx.app, | ||
| 279 | - "GET", | ||
| 280 | - "/tahinli/proj/releases", | ||
| 281 | - Some(&fx.tahinli_cookie), | ||
| 282 | - None, | ||
| 283 | - Vec::new(), | ||
| 284 | - ) | ||
| 285 | - .await; | ||
| 286 | - assert!(writer_list.text().contains("Release v2.0")); | ||
| 287 | - assert_eq!(published.tag, "v1.0"); | ||
| 288 | } | 227 | } |
| 289 | 228 | ||
| 290 | -#[tokio::test] | 229 | +/// Adds a commit on top of `main` and points branch `next` at it. Returns the new commit. |
| 291 | -async fn readers_cannot_create_edit_upload_or_delete() { | 230 | +fn second_commit(fx: &Fixture) -> String { |
| 292 | - let fx = fixture(None).await; | 231 | + let tree = git(&fx.bare, &["rev-parse", "refs/heads/main^{tree}"]); |
| 293 | - let rel = fx.release("v1.0", false).await; | 232 | + let next = git( |
| 294 | - | 233 | + &fx.bare, |
| 295 | - let create = send( | 234 | + &["commit-tree", &tree, "-p", &fx.main_tip(), "-m", "two"], |
| 296 | - &fx.app, | ||
| 297 | - "POST", | ||
| 298 | - "/tahinli/proj/releases/new", | ||
| 299 | - Some(&fx.bob_cookie), | ||
| 300 | - Some(FORM_TYPE), | ||
| 301 | - form(&[ | ||
| 302 | - ("csrf", CSRF), | ||
| 303 | - ("tag", "v9"), | ||
| 304 | - ("target", "main"), | ||
| 305 | - ("title", "x"), | ||
| 306 | - ]), | ||
| 307 | - ) | ||
| 308 | - .await; | ||
| 309 | - assert_ne!(create.status, 303, "a reader cannot create a release"); | ||
| 310 | - assert!( | ||
| 311 | - fx.tag_target("v9").is_none(), | ||
| 312 | - "no tag is created for a refused release" | ||
| 313 | - ); | ||
| 314 | - | ||
| 315 | - let edit = send( | ||
| 316 | - &fx.app, | ||
| 317 | - "POST", | ||
| 318 | - &format!("/tahinli/proj/releases/{}/edit", rel.id), | ||
| 319 | - Some(&fx.bob_cookie), | ||
| 320 | - Some(FORM_TYPE), | ||
| 321 | - form(&[("csrf", CSRF), ("title", "hacked"), ("body", "")]), | ||
| 322 | - ) | ||
| 323 | - .await; | ||
| 324 | - assert_ne!(edit.status, 303); | ||
| 325 | - assert_eq!( | ||
| 326 | - fx.store | ||
| 327 | - .release(&rel.id) | ||
| 328 | - .await | ||
| 329 | - .expect("read") | ||
| 330 | - .expect("row") | ||
| 331 | - .title, | ||
| 332 | - "Release v1.0" | ||
| 333 | - ); | ||
| 334 | - | ||
| 335 | - let upload = fx.upload(&rel.id, "a.bin", b"data", &fx.bob_cookie).await; | ||
| 336 | - assert_ne!(upload.status, 303); | ||
| 337 | - assert!( | ||
| 338 | - fx.store | ||
| 339 | - .list_release_assets(&rel.id) | ||
| 340 | - .await | ||
| 341 | - .expect("assets") | ||
| 342 | - .is_empty() | ||
| 343 | ); | 235 | ); |
| 344 | - | 236 | + git(&fx.bare, &["update-ref", "refs/heads/next", &next]); |
| 345 | - let delete = send( | 237 | + next |
| 346 | - &fx.app, | ||
| 347 | - "POST", | ||
| 348 | - &format!("/tahinli/proj/releases/{}/delete", rel.id), | ||
| 349 | - Some(&fx.bob_cookie), | ||
| 350 | - Some(FORM_TYPE), | ||
| 351 | - form(&[("csrf", CSRF), ("confirm", "on")]), | ||
| 352 | - ) | ||
| 353 | - .await; | ||
| 354 | - assert_ne!(delete.status, 303); | ||
| 355 | - assert!(fx.store.release(&rel.id).await.expect("read").is_some()); | ||
| 356 | } | 238 | } |
| 357 | 239 | ||
| 358 | -#[tokio::test] | 240 | +// Each test below builds one fixture and runs its scenarios in order. Scenarios use their own |
| 359 | -async fn a_missing_tag_is_created_as_a_lightweight_tag_on_the_branch_head() { | 241 | +// tags and releases so they do not interfere, and the blob-count check in the size-cap test runs |
| 360 | - let fx = fixture(None).await; | 242 | +// first, before any blob exists in its fixture. |
| 361 | - let reply = send( | ||
| 362 | - &fx.app, | ||
| 363 | - "POST", | ||
| 364 | - "/tahinli/proj/releases/new", | ||
| 365 | - Some(&fx.tahinli_cookie), | ||
| 366 | - Some(FORM_TYPE), | ||
| 367 | - form(&[ | ||
| 368 | - ("csrf", CSRF), | ||
| 369 | - ("tag", "v1.0"), | ||
| 370 | - ("target", "main"), | ||
| 371 | - ("title", "First"), | ||
| 372 | - ("body", "hello"), | ||
| 373 | - ]), | ||
| 374 | - ) | ||
| 375 | - .await; | ||
| 376 | - assert_eq!(reply.status, 303, "body: {}", reply.text()); | ||
| 377 | - assert_eq!( | ||
| 378 | - reply.location.as_deref(), | ||
| 379 | - Some("/tahinli/proj/releases/tag/v1.0") | ||
| 380 | - ); | ||
| 381 | - let tip = fx.main_tip(); | ||
| 382 | - assert_eq!(fx.tag_target("v1.0").as_deref(), Some(tip.as_str())); | ||
| 383 | - let release = fx | ||
| 384 | - .store | ||
| 385 | - .release_by_tag(&fx.repo.id, "v1.0") | ||
| 386 | - .await | ||
| 387 | - .expect("read") | ||
| 388 | - .expect("release"); | ||
| 389 | - assert_eq!(release.target_oid, tip); | ||
| 390 | - assert!(release.published_at.is_some()); | ||
| 391 | -} | ||
| 392 | 243 | ||
| 393 | #[tokio::test] | 244 | #[tokio::test] |
| 394 | -async fn an_existing_tag_is_used_as_it_is_and_never_moved() { | 245 | +async fn drafts_and_permissions_share_one_repo() { |
| 395 | let fx = fixture(None).await; | 246 | let fx = fixture(None).await; |
| 396 | - git(&fx.bare, &["tag", "v0.9", "refs/heads/main"]); | ||
| 397 | - let before = fx.tag_target("v0.9"); | ||
| 398 | - let reply = send( | ||
| 399 | - &fx.app, | ||
| 400 | - "POST", | ||
| 401 | - "/tahinli/proj/releases/new", | ||
| 402 | - Some(&fx.tahinli_cookie), | ||
| 403 | - Some(FORM_TYPE), | ||
| 404 | - form(&[ | ||
| 405 | - ("csrf", CSRF), | ||
| 406 | - ("tag", "v0.9"), | ||
| 407 | - ("target", "main"), | ||
| 408 | - ("title", "Old"), | ||
| 409 | - ]), | ||
| 410 | - ) | ||
| 411 | - .await; | ||
| 412 | - assert_eq!(reply.status, 303); | ||
| 413 | - assert_eq!(fx.tag_target("v0.9"), before); | ||
| 414 | -} | ||
| 415 | 247 | ||
| 416 | -#[tokio::test] | 248 | + // Drafts: readers see the published release only, owners see the draft too. |
| 417 | -async fn upload_then_download_round_trips_and_counts() { | 249 | + { |
| 418 | - let fx = fixture(None).await; | 250 | + let published = fx.release("v1.0", false).await; |
| 419 | - let rel = fx.release("v1.0", false).await; | 251 | + let draft = fx.release("v2.0", true).await; |
| 420 | - let payload = b"<html>not rendered</html>"; | 252 | + let draft_asset = fx |
| 421 | - let reply = fx | 253 | + .upload(&draft.id, "secret.bin", b"x", &fx.tahinli_cookie) |
| 422 | - .upload(&rel.id, "app.bin", payload, &fx.tahinli_cookie) | 254 | + .await; |
| 255 | + assert_eq!(draft_asset.status, 303, "writer uploads to a draft"); | ||
| 256 | + | ||
| 257 | + let list = send( | ||
| 258 | + &fx.app, | ||
| 259 | + "GET", | ||
| 260 | + "/tahinli/proj/releases", | ||
| 261 | + None, | ||
| 262 | + None, | ||
| 263 | + Vec::new(), | ||
| 264 | + ) | ||
| 423 | .await; | 265 | .await; |
| 424 | - assert_eq!(reply.status, 303, "body: {}", reply.text()); | 266 | + assert_eq!(list.status, 200); |
| 425 | - assert!( | 267 | + assert!(list.text().contains("Release v1.0")); |
| 426 | - reply | 268 | + assert!( |
| 427 | - .location | 269 | + !list.text().contains("Release v2.0"), |
| 428 | - .unwrap_or_default() | 270 | + "anonymous list hides drafts" |
| 429 | - .contains("notice=asset-uploaded") | 271 | + ); |
| 430 | - ); | ||
| 431 | 272 | ||
| 432 | - let row = fx | 273 | + let anon_draft = send( |
| 433 | - .store | 274 | + &fx.app, |
| 434 | - .release_asset(&rel.id, "app.bin") | 275 | + "GET", |
| 435 | - .await | 276 | + "/tahinli/proj/releases/tag/v2.0", |
| 436 | - .expect("read") | 277 | + None, |
| 437 | - .expect("asset row"); | 278 | + None, |
| 438 | - assert_eq!(row.size, payload.len() as u64); | 279 | + Vec::new(), |
| 439 | - let digest = { | 280 | + ) |
| 440 | - use sha2::{Digest, Sha256}; | 281 | + .await; |
| 441 | - hex::encode(Sha256::digest(payload)) | 282 | + assert_eq!(anon_draft.status, 404, "anonymous draft page"); |
| 442 | - }; | 283 | + let anon_download = send( |
| 443 | - assert_eq!(row.sha256, digest); | ||
| 444 | - | ||
| 445 | - for expected in 1..=2u64 { | ||
| 446 | - let got = send( | ||
| 447 | &fx.app, | 284 | &fx.app, |
| 448 | "GET", | 285 | "GET", |
| 449 | - "/tahinli/proj/releases/download/v1.0/app.bin", | 286 | + "/tahinli/proj/releases/download/v2.0/secret.bin", |
| 450 | None, | 287 | None, |
| 451 | None, | 288 | None, |
| 452 | Vec::new(), | 289 | Vec::new(), |
| 453 | ) | 290 | ) |
| 454 | .await; | 291 | .await; |
| 455 | - assert_eq!(got.status, 200); | ||
| 456 | - assert_eq!(got.body, payload); | ||
| 457 | assert_eq!( | 292 | assert_eq!( |
| 458 | - got.headers | 293 | + anon_download.status, 404, |
| 459 | - .get("content-disposition") | 294 | + "a draft asset is not downloadable by readers" |
| 460 | - .and_then(|v| v.to_str().ok()), | ||
| 461 | - Some("attachment; filename=\"app.bin\"") | ||
| 462 | ); | 295 | ); |
| 296 | + | ||
| 297 | + let writer_draft = send( | ||
| 298 | + &fx.app, | ||
| 299 | + "GET", | ||
| 300 | + "/tahinli/proj/releases/tag/v2.0", | ||
| 301 | + Some(&fx.tahinli_cookie), | ||
| 302 | + None, | ||
| 303 | + Vec::new(), | ||
| 304 | + ) | ||
| 305 | + .await; | ||
| 306 | + assert_eq!(writer_draft.status, 200, "the owner sees the draft"); | ||
| 307 | + let writer_list = send( | ||
| 308 | + &fx.app, | ||
| 309 | + "GET", | ||
| 310 | + "/tahinli/proj/releases", | ||
| 311 | + Some(&fx.tahinli_cookie), | ||
| 312 | + None, | ||
| 313 | + Vec::new(), | ||
| 314 | + ) | ||
| 315 | + .await; | ||
| 316 | + assert!(writer_list.text().contains("Release v2.0")); | ||
| 317 | + assert_eq!(published.tag, "v1.0"); | ||
| 318 | + } | ||
| 319 | + | ||
| 320 | + // Readers cannot create, edit, upload or delete. | ||
| 321 | + { | ||
| 322 | + let rel = fx.release("v3.0", false).await; | ||
| 323 | + | ||
| 324 | + let create = send( | ||
| 325 | + &fx.app, | ||
| 326 | + "POST", | ||
| 327 | + "/tahinli/proj/releases/new", | ||
| 328 | + Some(&fx.bob_cookie), | ||
| 329 | + Some(FORM_TYPE), | ||
| 330 | + form(&[ | ||
| 331 | + ("csrf", CSRF), | ||
| 332 | + ("tag", "v9"), | ||
| 333 | + ("target", "main"), | ||
| 334 | + ("title", "x"), | ||
| 335 | + ]), | ||
| 336 | + ) | ||
| 337 | + .await; | ||
| 338 | + assert_ne!(create.status, 303, "a reader cannot create a release"); | ||
| 339 | + assert!( | ||
| 340 | + fx.tag_target("v9").is_none(), | ||
| 341 | + "no tag is created for a refused release" | ||
| 342 | + ); | ||
| 343 | + | ||
| 344 | + let edit = send( | ||
| 345 | + &fx.app, | ||
| 346 | + "POST", | ||
| 347 | + &format!("/tahinli/proj/releases/{}/edit", rel.id), | ||
| 348 | + Some(&fx.bob_cookie), | ||
| 349 | + Some(FORM_TYPE), | ||
| 350 | + form(&[("csrf", CSRF), ("title", "hacked"), ("body", "")]), | ||
| 351 | + ) | ||
| 352 | + .await; | ||
| 353 | + assert_ne!(edit.status, 303, "a reader cannot edit"); | ||
| 463 | assert_eq!( | 354 | assert_eq!( |
| 464 | - got.headers | 355 | + fx.store |
| 465 | - .get("x-content-type-options") | 356 | + .release(&rel.id) |
| 466 | - .and_then(|v| v.to_str().ok()), | 357 | + .await |
| 467 | - Some("nosniff") | 358 | + .expect("read") |
| 359 | + .expect("row") | ||
| 360 | + .title, | ||
| 361 | + "Release v3.0" | ||
| 362 | + ); | ||
| 363 | + | ||
| 364 | + let upload = fx.upload(&rel.id, "a.bin", b"data", &fx.bob_cookie).await; | ||
| 365 | + assert_ne!(upload.status, 303, "a reader cannot upload"); | ||
| 366 | + assert!( | ||
| 367 | + fx.store | ||
| 368 | + .list_release_assets(&rel.id) | ||
| 369 | + .await | ||
| 370 | + .expect("assets") | ||
| 371 | + .is_empty() | ||
| 372 | + ); | ||
| 373 | + | ||
| 374 | + let delete = send( | ||
| 375 | + &fx.app, | ||
| 376 | + "POST", | ||
| 377 | + &format!("/tahinli/proj/releases/{}/delete", rel.id), | ||
| 378 | + Some(&fx.bob_cookie), | ||
| 379 | + Some(FORM_TYPE), | ||
| 380 | + form(&[("csrf", CSRF), ("confirm", "on")]), | ||
| 381 | + ) | ||
| 382 | + .await; | ||
| 383 | + assert_ne!(delete.status, 303, "a reader cannot delete"); | ||
| 384 | + assert!(fx.store.release(&rel.id).await.expect("read").is_some()); | ||
| 385 | + } | ||
| 386 | + | ||
| 387 | + // The repo home shows the latest release card and links to the releases tab. | ||
| 388 | + { | ||
| 389 | + fx.release("v0.9-beta", false).await; | ||
| 390 | + let home = send(&fx.app, "GET", "/tahinli/proj", None, None, Vec::new()).await; | ||
| 391 | + assert_eq!(home.status, 200); | ||
| 392 | + assert!(home.text().contains("Latest release")); | ||
| 393 | + assert!( | ||
| 394 | + home.text().contains("/tahinli/proj/releases"), | ||
| 395 | + "the tabs link to releases" | ||
| 468 | ); | 396 | ); |
| 469 | - let counted = fx | ||
| 470 | - .store | ||
| 471 | - .release_asset(&rel.id, "app.bin") | ||
| 472 | - .await | ||
| 473 | - .expect("read") | ||
| 474 | - .expect("row"); | ||
| 475 | - assert_eq!(counted.download_count, expected); | ||
| 476 | } | 397 | } |
| 477 | } | 398 | } |
| 478 | 399 | ||
| 479 | #[tokio::test] | 400 | #[tokio::test] |
| 480 | -async fn a_csrf_mismatch_on_upload_is_refused_and_stores_nothing() { | 401 | +async fn creating_a_release_takes_and_validates_its_tag_and_target() { |
| 481 | let fx = fixture(None).await; | 402 | let fx = fixture(None).await; |
| 482 | - let rel = fx.release("v1.0", false).await; | 403 | + |
| 483 | - let (content_type, body) = crate::pages::settings_profile::tests::multipart(&[ | 404 | + // A missing tag is created as a lightweight tag on the branch head. |
| 484 | - ("csrf", None, b"wrong"), | 405 | + { |
| 485 | - ("file", Some(("a.bin", "application/octet-stream")), b"data"), | 406 | + let reply = send( |
| 486 | - ]); | 407 | + &fx.app, |
| 487 | - let reply = send( | 408 | + "POST", |
| 488 | - &fx.app, | 409 | + "/tahinli/proj/releases/new", |
| 489 | - "POST", | 410 | + Some(&fx.tahinli_cookie), |
| 490 | - &format!("/tahinli/proj/releases/{}/assets", rel.id), | 411 | + Some(FORM_TYPE), |
| 491 | - Some(&fx.tahinli_cookie), | 412 | + form(&[ |
| 492 | - Some(&content_type), | 413 | + ("csrf", CSRF), |
| 493 | - body, | 414 | + ("tag", "v1.0"), |
| 494 | - ) | 415 | + ("target", "main"), |
| 495 | - .await; | 416 | + ("title", "First"), |
| 496 | - assert_ne!(reply.status, 303); | 417 | + ("body", "hello"), |
| 497 | - assert!( | 418 | + ]), |
| 498 | - fx.store | 419 | + ) |
| 499 | - .list_release_assets(&rel.id) | 420 | + .await; |
| 421 | + assert_eq!(reply.status, 303, "body: {}", reply.text()); | ||
| 422 | + assert_eq!( | ||
| 423 | + reply.location.as_deref(), | ||
| 424 | + Some("/tahinli/proj/releases/tag/v1.0") | ||
| 425 | + ); | ||
| 426 | + let tip = fx.main_tip(); | ||
| 427 | + assert_eq!(fx.tag_target("v1.0").as_deref(), Some(tip.as_str())); | ||
| 428 | + let release = fx | ||
| 429 | + .store | ||
| 430 | + .release_by_tag(&fx.repo.id, "v1.0") | ||
| 500 | .await | 431 | .await |
| 501 | - .expect("assets") | 432 | + .expect("read") |
| 502 | - .is_empty() | 433 | + .expect("release"); |
| 503 | - ); | 434 | + assert_eq!(release.target_oid, tip); |
| 504 | -} | 435 | + assert!(release.published_at.is_some()); |
| 436 | + } | ||
| 505 | 437 | ||
| 506 | -#[tokio::test] | 438 | + // An existing tag is used as it is and never moved. |
| 507 | -async fn an_upload_over_the_size_cap_is_refused_and_leaves_no_blob() { | 439 | + { |
| 508 | - let fx = fixture(Some(1)).await; | 440 | + git(&fx.bare, &["tag", "v0.9", "refs/heads/main"]); |
| 509 | - let rel = fx.release("v1.0", false).await; | 441 | + let before = fx.tag_target("v0.9"); |
| 510 | - let big = vec![7u8; 1024 * 1024 + 4096]; | 442 | + let reply = send( |
| 511 | - let reply = fx | 443 | + &fx.app, |
| 512 | - .upload(&rel.id, "big.bin", &big, &fx.tahinli_cookie) | 444 | + "POST", |
| 445 | + "/tahinli/proj/releases/new", | ||
| 446 | + Some(&fx.tahinli_cookie), | ||
| 447 | + Some(FORM_TYPE), | ||
| 448 | + form(&[ | ||
| 449 | + ("csrf", CSRF), | ||
| 450 | + ("tag", "v0.9"), | ||
| 451 | + ("target", "main"), | ||
| 452 | + ("title", "Old"), | ||
| 453 | + ]), | ||
| 454 | + ) | ||
| 513 | .await; | 455 | .await; |
| 514 | - assert_eq!(reply.status, 303); | 456 | + assert_eq!(reply.status, 303, "existing tag"); |
| 515 | - assert!( | 457 | + assert_eq!(fx.tag_target("v0.9"), before, "existing tag is not moved"); |
| 516 | - reply | 458 | + } |
| 517 | - .location | 459 | + |
| 518 | - .unwrap_or_default() | 460 | + // Create refuses a bad tag or a missing target before touching git, and takes a commit id as |
| 519 | - .contains("notice=asset-too-large"), | 461 | + // a target. |
| 520 | - "the cap refuses the file" | 462 | + { |
| 521 | - ); | 463 | + let input = |tag: &str, target: &str| CreateRelease { |
| 522 | - assert!( | 464 | + tag: tag.to_owned(), |
| 523 | - fx.store | 465 | + target: Some(target.to_owned()), |
| 524 | - .list_release_assets(&rel.id) | 466 | + title: "x".to_owned(), |
| 467 | + body: String::new(), | ||
| 468 | + draft: false, | ||
| 469 | + prerelease: false, | ||
| 470 | + }; | ||
| 471 | + let refused = | ||
| 472 | + release_ops::create(&fx.state, &fx.repo, &fx.tahinli, input("a/b", "main")).await; | ||
| 473 | + assert!( | ||
| 474 | + matches!(refused, Err(ReleaseError::Invalid(_))), | ||
| 475 | + "bad tag: {refused:?}" | ||
| 476 | + ); | ||
| 477 | + assert!(fx.tag_target("a/b").is_none(), "bad tag"); | ||
| 478 | + | ||
| 479 | + let tip = fx.main_tip(); | ||
| 480 | + let made = release_ops::create(&fx.state, &fx.repo, &fx.tahinli, input("v7", &tip)) | ||
| 525 | .await | 481 | .await |
| 526 | - .expect("assets") | 482 | + .expect("a commit id works as a target"); |
| 527 | - .is_empty() | 483 | + assert_eq!(made.target_oid, tip, "commit id target"); |
| 528 | - ); | 484 | + assert_eq!( |
| 529 | - let assets_dir = fx.state.config.data_dir.join("blobs").join("assets"); | 485 | + fx.tag_target("v7").as_deref(), |
| 530 | - let leftovers = std::fs::read_dir(&assets_dir) | 486 | + Some(tip.as_str()), |
| 531 | - .map(|entries| entries.count()) | 487 | + "commit id target" |
| 532 | - .unwrap_or(0); | 488 | + ); |
| 533 | - assert_eq!(leftovers, 0, "no partial blob is left behind"); | ||
| 534 | -} | ||
| 535 | 489 | ||
| 536 | -#[tokio::test] | 490 | + let missing = release_ops::create( |
| 537 | -async fn deleting_a_release_keeps_the_tag_and_removes_its_assets() { | 491 | + &fx.state, |
| 538 | - let fx = fixture(None).await; | 492 | + &fx.repo, |
| 539 | - git(&fx.bare, &["tag", "v1.0", "refs/heads/main"]); | 493 | + &fx.tahinli, |
| 540 | - let rel = fx.release("v1.0", false).await; | 494 | + input("v8", "1111111111111111111111111111111111111111"), |
| 541 | - let up = fx | 495 | + ) |
| 542 | - .upload(&rel.id, "gone.bin", b"bytes", &fx.tahinli_cookie) | ||
| 543 | .await; | 496 | .await; |
| 544 | - assert_eq!(up.status, 303); | 497 | + assert!( |
| 545 | - let asset = fx | 498 | + matches!(missing, Err(ReleaseError::Invalid(_))), |
| 546 | - .store | 499 | + "missing target: {missing:?}" |
| 547 | - .release_asset(&rel.id, "gone.bin") | 500 | + ); |
| 548 | - .await | 501 | + assert!(fx.tag_target("v8").is_none(), "missing target"); |
| 549 | - .expect("read") | 502 | + } |
| 550 | - .expect("row"); | ||
| 551 | - let blob = crate::blobs::asset_key(&asset.id); | ||
| 552 | - assert!(fx.state.blobs.read(&blob).await.expect("read").is_some()); | ||
| 553 | - | ||
| 554 | - let reply = send( | ||
| 555 | - &fx.app, | ||
| 556 | - "POST", | ||
| 557 | - &format!("/tahinli/proj/releases/{}/delete", rel.id), | ||
| 558 | - Some(&fx.tahinli_cookie), | ||
| 559 | - Some(FORM_TYPE), | ||
| 560 | - form(&[("csrf", CSRF), ("confirm", "on")]), | ||
| 561 | - ) | ||
| 562 | - .await; | ||
| 563 | - assert_eq!(reply.status, 303); | ||
| 564 | - assert!(fx.store.release(&rel.id).await.expect("read").is_none()); | ||
| 565 | - assert!(fx.tag_target("v1.0").is_some(), "the git tag survives"); | ||
| 566 | - assert!( | ||
| 567 | - fx.state.blobs.read(&blob).await.expect("read").is_none(), | ||
| 568 | - "the blob is gone" | ||
| 569 | - ); | ||
| 570 | - let download = send( | ||
| 571 | - &fx.app, | ||
| 572 | - "GET", | ||
| 573 | - "/tahinli/proj/releases/download/v1.0/gone.bin", | ||
| 574 | - None, | ||
| 575 | - None, | ||
| 576 | - Vec::new(), | ||
| 577 | - ) | ||
| 578 | - .await; | ||
| 579 | - assert_eq!(download.status, 404); | ||
| 580 | } | 503 | } |
| 581 | 504 | ||
| 582 | #[tokio::test] | 505 | #[tokio::test] |
| 583 | -async fn the_latest_release_card_shows_on_the_repo_home() { | 506 | +async fn retagging_rules_hold_for_published_and_draft_releases() { |
| 584 | let fx = fixture(None).await; | 507 | let fx = fixture(None).await; |
| 585 | - fx.release("v1.0", false).await; | ||
| 586 | - fx.release("v0.9-beta", false).await; | ||
| 587 | - let home = send(&fx.app, "GET", "/tahinli/proj", None, None, Vec::new()).await; | ||
| 588 | - assert_eq!(home.status, 200); | ||
| 589 | - assert!(home.text().contains("Latest release")); | ||
| 590 | - assert!( | ||
| 591 | - home.text().contains("/tahinli/proj/releases"), | ||
| 592 | - "the tabs link to releases" | ||
| 593 | - ); | ||
| 594 | -} | ||
| 595 | 508 | ||
| 596 | -/// The release's own fields, unchanged, with no tag change. | 509 | + // A published release moves to a new tag and keeps the old git tag. |
| 597 | -fn keep(rel: &Release) -> EditRelease { | 510 | + { |
| 598 | - EditRelease { | 511 | + git(&fx.bare, &["tag", "v1.0", "refs/heads/main"]); |
| 599 | - title: rel.title.clone(), | 512 | + let rel = fx.release("v1.0", false).await; |
| 600 | - body: rel.body.clone(), | 513 | + let edit = EditRelease { |
| 601 | - draft: rel.draft, | 514 | + tag: Some("v1.1".to_owned()), |
| 602 | - prerelease: rel.prerelease, | 515 | + ..keep(&rel) |
| 603 | - tag: None, | 516 | + }; |
| 604 | - target: None, | 517 | + let moved = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &rel, edit) |
| 518 | + .await | ||
| 519 | + .expect("retag"); | ||
| 520 | + assert_eq!(moved.tag, "v1.1"); | ||
| 521 | + assert_eq!(moved.target_oid, fx.main_tip()); | ||
| 522 | + assert_eq!( | ||
| 523 | + fx.tag_target("v1.1").as_deref(), | ||
| 524 | + Some(fx.main_tip().as_str()) | ||
| 525 | + ); | ||
| 526 | + assert!(fx.tag_target("v1.0").is_some(), "the old git tag is kept"); | ||
| 527 | + assert!( | ||
| 528 | + fx.store | ||
| 529 | + .release_by_tag(&fx.repo.id, "v1.0") | ||
| 530 | + .await | ||
| 531 | + .expect("read") | ||
| 532 | + .is_none(), | ||
| 533 | + "the old tag no longer names a release" | ||
| 534 | + ); | ||
| 605 | } | 535 | } |
| 606 | -} | ||
| 607 | 536 | ||
| 608 | -/// Adds a commit on top of `main` and points branch `next` at it. Returns the new commit. | 537 | + // A published tag cannot be moved in place, but a draft can. |
| 609 | -fn second_commit(fx: &Fixture) -> String { | 538 | + { |
| 610 | - let tree = git(&fx.bare, &["rev-parse", "refs/heads/main^{tree}"]); | 539 | + git(&fx.bare, &["tag", "v2.0", "refs/heads/main"]); |
| 611 | - let next = git( | 540 | + let next = second_commit(&fx); |
| 612 | - &fx.bare, | 541 | + let published = fx.release("v2.0", false).await; |
| 613 | - &["commit-tree", &tree, "-p", &fx.main_tip(), "-m", "two"], | 542 | + let move_published = EditRelease { |
| 614 | - ); | 543 | + target: Some("next".to_owned()), |
| 615 | - git(&fx.bare, &["update-ref", "refs/heads/next", &next]); | 544 | + ..keep(&published) |
| 616 | - next | 545 | + }; |
| 546 | + let refused = | ||
| 547 | + release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &published, move_published).await; | ||
| 548 | + assert!( | ||
| 549 | + matches!(refused, Err(ReleaseError::Invalid(_))), | ||
| 550 | + "{refused:?}" | ||
| 551 | + ); | ||
| 552 | + assert_eq!( | ||
| 553 | + fx.tag_target("v2.0").as_deref(), | ||
| 554 | + Some(fx.main_tip().as_str()), | ||
| 555 | + "published tag stays put" | ||
| 556 | + ); | ||
| 557 | + | ||
| 558 | + let draft = fx | ||
| 559 | + .store | ||
| 560 | + .create_release(&forge_core::releases::NewRelease { | ||
| 561 | + repo_id: &fx.repo.id, | ||
| 562 | + tag: "v3.0", | ||
| 563 | + target_oid: &fx.main_tip(), | ||
| 564 | + title: "draft", | ||
| 565 | + body: "", | ||
| 566 | + draft: true, | ||
| 567 | + prerelease: false, | ||
| 568 | + author_id: &fx.tahinli.id, | ||
| 569 | + }) | ||
| 570 | + .await | ||
| 571 | + .expect("draft"); | ||
| 572 | + git(&fx.bare, &["tag", "v3.0", "refs/heads/main"]); | ||
| 573 | + let move_draft = EditRelease { | ||
| 574 | + target: Some("next".to_owned()), | ||
| 575 | + ..keep(&draft) | ||
| 576 | + }; | ||
| 577 | + let moved = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &draft, move_draft) | ||
| 578 | + .await | ||
| 579 | + .expect("draft moves"); | ||
| 580 | + assert_eq!(moved.target_oid, next); | ||
| 581 | + assert_eq!(fx.tag_target("v3.0"), Some(next)); | ||
| 582 | + } | ||
| 583 | + | ||
| 584 | + // Retagging adopts an existing tag and refuses clashes and bad names. | ||
| 585 | + { | ||
| 586 | + git(&fx.bare, &["tag", "v0.9", "refs/heads/main"]); | ||
| 587 | + let next = second_commit(&fx); | ||
| 588 | + let draft = fx.release("v5", true).await; | ||
| 589 | + | ||
| 590 | + let clash_target = EditRelease { | ||
| 591 | + tag: Some("v0.9".to_owned()), | ||
| 592 | + target: Some("next".to_owned()), | ||
| 593 | + ..keep(&draft) | ||
| 594 | + }; | ||
| 595 | + let refused = | ||
| 596 | + release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &draft, clash_target).await; | ||
| 597 | + assert!( | ||
| 598 | + matches!(refused, Err(ReleaseError::Invalid(_))), | ||
| 599 | + "{refused:?}" | ||
| 600 | + ); | ||
| 601 | + | ||
| 602 | + let adopt = EditRelease { | ||
| 603 | + tag: Some("v0.9".to_owned()), | ||
| 604 | + ..keep(&draft) | ||
| 605 | + }; | ||
| 606 | + let adopted = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &draft, adopt) | ||
| 607 | + .await | ||
| 608 | + .expect("adopt"); | ||
| 609 | + assert_eq!(adopted.tag, "v0.9"); | ||
| 610 | + assert_eq!(adopted.target_oid, fx.main_tip()); | ||
| 611 | + assert_ne!(adopted.target_oid, next); | ||
| 612 | + | ||
| 613 | + let other = fx.release("v6", false).await; | ||
| 614 | + let taken = EditRelease { | ||
| 615 | + tag: Some("v0.9".to_owned()), | ||
| 616 | + ..keep(&other) | ||
| 617 | + }; | ||
| 618 | + let clash = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &other, taken).await; | ||
| 619 | + assert!(matches!(clash, Err(ReleaseError::Conflict(_))), "{clash:?}"); | ||
| 620 | + | ||
| 621 | + let bad_name = EditRelease { | ||
| 622 | + tag: Some("a/b".to_owned()), | ||
| 623 | + ..keep(&other) | ||
| 624 | + }; | ||
| 625 | + let invalid = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &other, bad_name).await; | ||
| 626 | + assert!( | ||
| 627 | + matches!(invalid, Err(ReleaseError::Invalid(_))), | ||
| 628 | + "{invalid:?}" | ||
| 629 | + ); | ||
| 630 | + } | ||
| 617 | } | 631 | } |
| 618 | 632 | ||
| 619 | #[tokio::test] | 633 | #[tokio::test] |
| 620 | -async fn a_second_upload_under_the_same_name_replaces_the_file() { | 634 | +async fn the_edit_page_and_its_form_follow_the_publish_state() { |
| 621 | let fx = fixture(None).await; | 635 | let fx = fixture(None).await; |
| 622 | - let rel = fx.release("v1.0", false).await; | 636 | + |
| 623 | - let first = fx | 637 | + // The edit page shows tag and target controls, and hides the target once published. |
| 624 | - .upload(&rel.id, "app.bin", b"first", &fx.tahinli_cookie) | 638 | + { |
| 639 | + let draft = fx.release("v1.0", true).await; | ||
| 640 | + let published = fx.release("v2.0", false).await; | ||
| 641 | + | ||
| 642 | + let page = send( | ||
| 643 | + &fx.app, | ||
| 644 | + "GET", | ||
| 645 | + &format!("/tahinli/proj/releases/{}/edit", draft.id), | ||
| 646 | + Some(&fx.tahinli_cookie), | ||
| 647 | + None, | ||
| 648 | + Vec::new(), | ||
| 649 | + ) | ||
| 625 | .await; | 650 | .await; |
| 626 | - assert_eq!(first.status, 303); | 651 | + assert_eq!(page.status, 200, "body: {}", page.text()); |
| 627 | - let before = fx | 652 | + let html = page.text(); |
| 628 | - .store | 653 | + assert!(html.contains("name=\"tag\" value=\"v1.0\""), "{html}"); |
| 629 | - .release_asset(&rel.id, "app.bin") | 654 | + assert!(html.contains("name=\"target\""), "a draft shows the target"); |
| 630 | - .await | ||
| 631 | - .expect("read") | ||
| 632 | - .expect("row"); | ||
| 633 | - fx.store | ||
| 634 | - .count_asset_download(&before.id) | ||
| 635 | - .await | ||
| 636 | - .expect("count"); | ||
| 637 | 655 | ||
| 638 | - let second = fx | 656 | + let page = send( |
| 639 | - .upload(&rel.id, "app.bin", b"second bytes", &fx.tahinli_cookie) | 657 | + &fx.app, |
| 658 | + "GET", | ||
| 659 | + &format!("/tahinli/proj/releases/{}/edit", published.id), | ||
| 660 | + Some(&fx.tahinli_cookie), | ||
| 661 | + None, | ||
| 662 | + Vec::new(), | ||
| 663 | + ) | ||
| 640 | .await; | 664 | .await; |
| 641 | - assert!( | 665 | + let html = page.text(); |
| 642 | - second | 666 | + assert!(html.contains("name=\"tag\" value=\"v2.0\""), "{html}"); |
| 643 | - .location | 667 | + assert!( |
| 644 | - .unwrap_or_default() | 668 | + !html.contains("name=\"target\""), |
| 645 | - .contains("notice=asset-replaced"), | 669 | + "published hides the target" |
| 646 | - "a same-name upload replaces instead of refusing" | 670 | + ); |
| 647 | - ); | 671 | + assert!( |
| 648 | - let assets = fx.store.list_release_assets(&rel.id).await.expect("assets"); | 672 | + html.contains("Published releases keep their commit"), |
| 649 | - assert_eq!(assets.len(), 1, "still one asset of that name"); | 673 | + "{html}" |
| 650 | - let after = &assets[0]; | 674 | + ); |
| 651 | - assert_ne!(after.id, before.id, "the new file has its own blob"); | 675 | + } |
| 652 | - assert_eq!(after.size, 12); | 676 | + |
| 653 | - assert_eq!(after.download_count, 1, "the download counter survives"); | 677 | + // A published release can be retagged through the form and keeps its commit. |
| 654 | - assert!( | 678 | + { |
| 655 | - fx.state | 679 | + git(&fx.bare, &["tag", "v4.0", "refs/heads/main"]); |
| 656 | - .blobs | 680 | + let published = fx.release("v4.0", false).await; |
| 657 | - .read(&asset_key(&before.id)) | 681 | + let reply = send( |
| 682 | + &fx.app, | ||
| 683 | + "POST", | ||
| 684 | + &format!("/tahinli/proj/releases/{}/edit", published.id), | ||
| 685 | + Some(&fx.tahinli_cookie), | ||
| 686 | + Some(FORM_TYPE), | ||
| 687 | + form(&[ | ||
| 688 | + ("csrf", CSRF), | ||
| 689 | + ("tag", "v4.1"), | ||
| 690 | + ("target", ""), | ||
| 691 | + ("title", "Release v4.0"), | ||
| 692 | + ("body", "notes"), | ||
| 693 | + ]), | ||
| 694 | + ) | ||
| 695 | + .await; | ||
| 696 | + assert_eq!(reply.status, 303, "body: {}", reply.text()); | ||
| 697 | + assert!(reply.location.unwrap_or_default().contains("notice=saved")); | ||
| 698 | + let moved = fx | ||
| 699 | + .store | ||
| 700 | + .release(&published.id) | ||
| 658 | .await | 701 | .await |
| 659 | .expect("read") | 702 | .expect("read") |
| 660 | - .is_none(), | 703 | + .expect("row"); |
| 661 | - "the replaced blob is deleted" | 704 | + assert_eq!(moved.tag, "v4.1"); |
| 662 | - ); | 705 | + assert_eq!(fx.tag_target("v4.1"), Some(fx.main_tip())); |
| 663 | - let got = send( | 706 | + assert_eq!( |
| 664 | - &fx.app, | 707 | + fx.tag_target("v4.0"), |
| 665 | - "GET", | 708 | + Some(fx.main_tip()), |
| 666 | - "/tahinli/proj/releases/download/v1.0/app.bin", | 709 | + "old git tag kept" |
| 667 | - None, | 710 | + ); |
| 668 | - None, | 711 | + } |
| 669 | - Vec::new(), | ||
| 670 | - ) | ||
| 671 | - .await; | ||
| 672 | - assert_eq!(got.body, b"second bytes"); | ||
| 673 | -} | ||
| 674 | 712 | ||
| 675 | -#[tokio::test] | 713 | + // A published target move is refused on the page, and the typed values stay. |
| 676 | -async fn a_refused_replacement_keeps_the_old_file() { | 714 | + { |
| 677 | - let fx = fixture(Some(1)).await; | 715 | + git(&fx.bare, &["tag", "v5.0", "refs/heads/main"]); |
| 678 | - let rel = fx.release("v1.0", false).await; | 716 | + second_commit(&fx); |
| 679 | - fx.upload(&rel.id, "app.bin", b"keep me", &fx.tahinli_cookie) | 717 | + fx.release("v5.0", false).await; |
| 680 | - .await; | 718 | + let published = fx |
| 681 | - let big = vec![7u8; 1024 * 1024 + 4096]; | 719 | + .store |
| 682 | - let reply = fx | 720 | + .release_by_tag(&fx.repo.id, "v5.0") |
| 683 | - .upload(&rel.id, "app.bin", &big, &fx.tahinli_cookie) | 721 | + .await |
| 722 | + .expect("read") | ||
| 723 | + .expect("row"); | ||
| 724 | + let reply = send( | ||
| 725 | + &fx.app, | ||
| 726 | + "POST", | ||
| 727 | + &format!("/tahinli/proj/releases/{}/edit", published.id), | ||
| 728 | + Some(&fx.tahinli_cookie), | ||
| 729 | + Some(FORM_TYPE), | ||
| 730 | + form(&[ | ||
| 731 | + ("csrf", CSRF), | ||
| 732 | + ("tag", "v5.0"), | ||
| 733 | + ("target", "next"), | ||
| 734 | + ("title", "Typed title"), | ||
| 735 | + ("body", "notes"), | ||
| 736 | + ]), | ||
| 737 | + ) | ||
| 684 | .await; | 738 | .await; |
| 685 | - assert!( | 739 | + assert_eq!( |
| 686 | - reply | 740 | + reply.status, |
| 687 | - .location | 741 | + 200, |
| 688 | - .unwrap_or_default() | 742 | + "a refused save re-renders: {}", |
| 689 | - .contains("notice=asset-too-large") | 743 | + reply.text() |
| 690 | - ); | 744 | + ); |
| 691 | - let row = fx | 745 | + let html = reply.text(); |
| 692 | - .store | 746 | + assert!(html.contains("pick a new tag"), "{html}"); |
| 693 | - .release_asset(&rel.id, "app.bin") | 747 | + assert!( |
| 694 | - .await | 748 | + html.contains("Typed title"), |
| 695 | - .expect("read") | 749 | + "the typed title stays: {html}" |
| 696 | - .expect("row"); | 750 | + ); |
| 697 | - assert_eq!(row.size, 7); | 751 | + assert_eq!(fx.tag_target("v5.0"), Some(fx.main_tip())); |
| 698 | - let got = send( | 752 | + } |
| 699 | - &fx.app, | ||
| 700 | - "GET", | ||
| 701 | - "/tahinli/proj/releases/download/v1.0/app.bin", | ||
| 702 | - None, | ||
| 703 | - None, | ||
| 704 | - Vec::new(), | ||
| 705 | - ) | ||
| 706 | - .await; | ||
| 707 | - assert_eq!(got.body, b"keep me"); | ||
| 708 | } | 753 | } |
| 709 | 754 | ||
| 710 | #[tokio::test] | 755 | #[tokio::test] |
| 711 | -async fn a_published_release_moves_to_a_new_tag_and_keeps_the_old_git_tag() { | 756 | +async fn asset_upload_replace_download_delete_and_csrf_rules() { |
| 712 | let fx = fixture(None).await; | 757 | let fx = fixture(None).await; |
| 713 | - git(&fx.bare, &["tag", "v1.0", "refs/heads/main"]); | 758 | + |
| 714 | - let rel = fx.release("v1.0", false).await; | 759 | + // Upload then download round-trips the bytes and counts each download. |
| 715 | - let edit = EditRelease { | 760 | + { |
| 716 | - tag: Some("v1.1".to_owned()), | 761 | + let rel = fx.release("v1.0", false).await; |
| 717 | - ..keep(&rel) | 762 | + let payload = b"<html>not rendered</html>"; |
| 718 | - }; | 763 | + let reply = fx |
| 719 | - let moved = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &rel, edit) | 764 | + .upload(&rel.id, "app.bin", payload, &fx.tahinli_cookie) |
| 720 | - .await | 765 | + .await; |
| 721 | - .expect("retag"); | 766 | + assert_eq!(reply.status, 303, "body: {}", reply.text()); |
| 722 | - assert_eq!(moved.tag, "v1.1"); | 767 | + assert!( |
| 723 | - assert_eq!(moved.target_oid, fx.main_tip()); | 768 | + reply |
| 724 | - assert_eq!( | 769 | + .location |
| 725 | - fx.tag_target("v1.1").as_deref(), | 770 | + .unwrap_or_default() |
| 726 | - Some(fx.main_tip().as_str()) | 771 | + .contains("notice=asset-uploaded") |
| 727 | - ); | 772 | + ); |
| 728 | - assert!(fx.tag_target("v1.0").is_some(), "the old git tag is kept"); | 773 | + |
| 729 | - assert!( | 774 | + let row = fx |
| 730 | - fx.store | 775 | + .store |
| 731 | - .release_by_tag(&fx.repo.id, "v1.0") | 776 | + .release_asset(&rel.id, "app.bin") |
| 732 | .await | 777 | .await |
| 733 | .expect("read") | 778 | .expect("read") |
| 734 | - .is_none() | 779 | + .expect("asset row"); |
| 735 | - ); | 780 | + assert_eq!(row.size, payload.len() as u64); |
| 736 | -} | 781 | + let digest = { |
| 782 | + use sha2::{Digest, Sha256}; | ||
| 783 | + hex::encode(Sha256::digest(payload)) | ||
| 784 | + }; | ||
| 785 | + assert_eq!(row.sha256, digest); | ||
| 786 | + | ||
| 787 | + for expected in 1..=2u64 { | ||
| 788 | + let got = send( | ||
| 789 | + &fx.app, | ||
| 790 | + "GET", | ||
| 791 | + "/tahinli/proj/releases/download/v1.0/app.bin", | ||
| 792 | + None, | ||
| 793 | + None, | ||
| 794 | + Vec::new(), | ||
| 795 | + ) | ||
| 796 | + .await; | ||
| 797 | + assert_eq!(got.status, 200); | ||
| 798 | + assert_eq!(got.body, payload); | ||
| 799 | + assert_eq!( | ||
| 800 | + got.headers | ||
| 801 | + .get("content-disposition") | ||
| 802 | + .and_then(|v| v.to_str().ok()), | ||
| 803 | + Some("attachment; filename=\"app.bin\"") | ||
| 804 | + ); | ||
| 805 | + assert_eq!( | ||
| 806 | + got.headers | ||
| 807 | + .get("x-content-type-options") | ||
| 808 | + .and_then(|v| v.to_str().ok()), | ||
| 809 | + Some("nosniff") | ||
| 810 | + ); | ||
| 811 | + let counted = fx | ||
| 812 | + .store | ||
| 813 | + .release_asset(&rel.id, "app.bin") | ||
| 814 | + .await | ||
| 815 | + .expect("read") | ||
| 816 | + .expect("row"); | ||
| 817 | + assert_eq!(counted.download_count, expected, "download counter"); | ||
| 818 | + } | ||
| 819 | + } | ||
| 737 | 820 | ||
| 738 | -#[tokio::test] | 821 | + // A same-name upload replaces the file and keeps its download counter. |
| 739 | -async fn a_published_tag_cannot_be_moved_in_place_but_a_draft_can() { | 822 | + { |
| 740 | - let fx = fixture(None).await; | 823 | + let rel = fx.release("v1.1", false).await; |
| 741 | - git(&fx.bare, &["tag", "v2.0", "refs/heads/main"]); | 824 | + let first = fx |
| 742 | - let next = second_commit(&fx); | 825 | + .upload(&rel.id, "app.bin", b"first", &fx.tahinli_cookie) |
| 743 | - let published = fx.release("v2.0", false).await; | 826 | + .await; |
| 744 | - let move_published = EditRelease { | 827 | + assert_eq!(first.status, 303); |
| 745 | - target: Some("next".to_owned()), | 828 | + let before = fx |
| 746 | - ..keep(&published) | 829 | + .store |
| 747 | - }; | 830 | + .release_asset(&rel.id, "app.bin") |
| 748 | - let refused = | 831 | + .await |
| 749 | - release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &published, move_published).await; | 832 | + .expect("read") |
| 750 | - assert!( | 833 | + .expect("row"); |
| 751 | - matches!(refused, Err(ReleaseError::Invalid(_))), | 834 | + fx.store |
| 752 | - "{refused:?}" | 835 | + .count_asset_download(&before.id) |
| 753 | - ); | 836 | + .await |
| 754 | - assert_eq!( | 837 | + .expect("count"); |
| 755 | - fx.tag_target("v2.0").as_deref(), | ||
| 756 | - Some(fx.main_tip().as_str()) | ||
| 757 | - ); | ||
| 758 | 838 | ||
| 759 | - let draft = fx | 839 | + let second = fx |
| 760 | - .store | 840 | + .upload(&rel.id, "app.bin", b"second bytes", &fx.tahinli_cookie) |
| 761 | - .create_release(&forge_core::releases::NewRelease { | 841 | + .await; |
| 762 | - repo_id: &fx.repo.id, | 842 | + assert!( |
| 763 | - tag: "v3.0", | 843 | + second |
| 764 | - target_oid: &fx.main_tip(), | 844 | + .location |
| 765 | - title: "draft", | 845 | + .unwrap_or_default() |
| 766 | - body: "", | 846 | + .contains("notice=asset-replaced"), |
| 767 | - draft: true, | 847 | + "a same-name upload replaces instead of refusing" |
| 768 | - prerelease: false, | 848 | + ); |
| 769 | - author_id: &fx.tahinli.id, | 849 | + let assets = fx.store.list_release_assets(&rel.id).await.expect("assets"); |
| 770 | - }) | 850 | + assert_eq!(assets.len(), 1, "still one asset of that name"); |
| 771 | - .await | 851 | + let after = &assets[0]; |
| 772 | - .expect("draft"); | 852 | + assert_ne!(after.id, before.id, "the new file has its own blob"); |
| 773 | - git(&fx.bare, &["tag", "v3.0", "refs/heads/main"]); | 853 | + assert_eq!(after.size, 12); |
| 774 | - let move_draft = EditRelease { | 854 | + assert_eq!(after.download_count, 1, "the download counter survives"); |
| 775 | - target: Some("next".to_owned()), | 855 | + assert!( |
| 776 | - ..keep(&draft) | 856 | + fx.state |
| 777 | - }; | 857 | + .blobs |
| 778 | - let moved = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &draft, move_draft) | 858 | + .read(&asset_key(&before.id)) |
| 779 | - .await | 859 | + .await |
| 780 | - .expect("draft moves"); | 860 | + .expect("read") |
| 781 | - assert_eq!(moved.target_oid, next); | 861 | + .is_none(), |
| 782 | - assert_eq!(fx.tag_target("v3.0"), Some(next)); | 862 | + "the replaced blob is deleted" |
| 783 | -} | 863 | + ); |
| 864 | + let got = send( | ||
| 865 | + &fx.app, | ||
| 866 | + "GET", | ||
| 867 | + "/tahinli/proj/releases/download/v1.1/app.bin", | ||
| 868 | + None, | ||
| 869 | + None, | ||
| 870 | + Vec::new(), | ||
| 871 | + ) | ||
| 872 | + .await; | ||
| 873 | + assert_eq!(got.body, b"second bytes"); | ||
| 874 | + } | ||
| 784 | 875 | ||
| 785 | -#[tokio::test] | 876 | + // Replacing by asset name keeps the name, whatever the new file is called. |
| 786 | -async fn retagging_adopts_an_existing_tag_and_refuses_clashes() { | 877 | + { |
| 787 | - let fx = fixture(None).await; | 878 | + let rel = fx.release("v1.2", false).await; |
| 788 | - git(&fx.bare, &["tag", "v0.9", "refs/heads/main"]); | 879 | + let first = fx |
| 789 | - let next = second_commit(&fx); | 880 | + .upload(&rel.id, "app.bin", b"first", &fx.tahinli_cookie) |
| 790 | - let draft = fx.release("v5", true).await; | 881 | + .await; |
| 791 | - | 882 | + assert_eq!(first.status, 303); |
| 792 | - let clash_target = EditRelease { | ||
| 793 | - tag: Some("v0.9".to_owned()), | ||
| 794 | - target: Some("next".to_owned()), | ||
| 795 | - ..keep(&draft) | ||
| 796 | - }; | ||
| 797 | - let refused = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &draft, clash_target).await; | ||
| 798 | - assert!( | ||
| 799 | - matches!(refused, Err(ReleaseError::Invalid(_))), | ||
| 800 | - "{refused:?}" | ||
| 801 | - ); | ||
| 802 | 883 | ||
| 803 | - let adopt = EditRelease { | 884 | + let page = send( |
| 804 | - tag: Some("v0.9".to_owned()), | 885 | + &fx.app, |
| 805 | - ..keep(&draft) | 886 | + "GET", |
| 806 | - }; | 887 | + &format!("/tahinli/proj/releases/{}/edit", rel.id), |
| 807 | - let adopted = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &draft, adopt) | 888 | + Some(&fx.tahinli_cookie), |
| 808 | - .await | 889 | + None, |
| 809 | - .expect("adopt"); | 890 | + Vec::new(), |
| 810 | - assert_eq!(adopted.tag, "v0.9"); | 891 | + ) |
| 811 | - assert_eq!(adopted.target_oid, fx.main_tip()); | 892 | + .await; |
| 812 | - assert_ne!(adopted.target_oid, next); | 893 | + assert!( |
| 813 | - | 894 | + page.text().contains("name=\"name\" value=\"app.bin\""), |
| 814 | - let other = fx.release("v6", false).await; | 895 | + "each asset has a replace form" |
| 815 | - let taken = EditRelease { | 896 | + ); |
| 816 | - tag: Some("v0.9".to_owned()), | ||
| 817 | - ..keep(&other) | ||
| 818 | - }; | ||
| 819 | - let clash = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &other, taken).await; | ||
| 820 | - assert!(matches!(clash, Err(ReleaseError::Conflict(_))), "{clash:?}"); | ||
| 821 | - | ||
| 822 | - let bad_name = EditRelease { | ||
| 823 | - tag: Some("a/b".to_owned()), | ||
| 824 | - ..keep(&other) | ||
| 825 | - }; | ||
| 826 | - let invalid = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &other, bad_name).await; | ||
| 827 | - assert!( | ||
| 828 | - matches!(invalid, Err(ReleaseError::Invalid(_))), | ||
| 829 | - "{invalid:?}" | ||
| 830 | - ); | ||
| 831 | -} | ||
| 832 | 897 | ||
| 833 | -/// Create refuses a bad tag or a missing target before touching git, and takes a commit id as a | 898 | + let (content_type, body) = crate::pages::settings_profile::tests::multipart(&[ |
| 834 | -/// target. | 899 | + ("csrf", None, CSRF.as_bytes()), |
| 835 | -#[tokio::test] | 900 | + ("name", None, "app.bin".as_bytes()), |
| 836 | -async fn create_validates_its_tag_and_target() { | 901 | + ( |
| 837 | - let fx = fixture(None).await; | 902 | + "file", |
| 838 | - let input = |tag: &str, target: &str| CreateRelease { | 903 | + Some(("renamed-build.zip", "application/octet-stream")), |
| 839 | - tag: tag.to_owned(), | 904 | + "second bytes".as_bytes(), |
| 840 | - target: Some(target.to_owned()), | 905 | + ), |
| 841 | - title: "x".to_owned(), | 906 | + ]); |
| 842 | - body: String::new(), | 907 | + let reply = send( |
| 843 | - draft: false, | 908 | + &fx.app, |
| 844 | - prerelease: false, | 909 | + "POST", |
| 845 | - }; | 910 | + &format!("/tahinli/proj/releases/{}/assets", rel.id), |
| 846 | - let refused = release_ops::create(&fx.state, &fx.repo, &fx.tahinli, input("a/b", "main")).await; | 911 | + Some(&fx.tahinli_cookie), |
| 847 | - assert!( | 912 | + Some(&content_type), |
| 848 | - matches!(refused, Err(ReleaseError::Invalid(_))), | 913 | + body, |
| 849 | - "bad tag: {refused:?}" | 914 | + ) |
| 850 | - ); | 915 | + .await; |
| 851 | - assert!(fx.tag_target("a/b").is_none(), "bad tag"); | 916 | + assert_eq!(reply.status, 303, "body: {}", reply.text()); |
| 917 | + assert!( | ||
| 918 | + reply | ||
| 919 | + .location | ||
| 920 | + .unwrap_or_default() | ||
| 921 | + .contains("notice=asset-replaced") | ||
| 922 | + ); | ||
| 923 | + let assets = fx.store.list_release_assets(&rel.id).await.expect("assets"); | ||
| 924 | + assert_eq!(assets.len(), 1, "still one asset"); | ||
| 925 | + assert_eq!(assets[0].name, "app.bin"); | ||
| 926 | + assert_eq!(assets[0].size, 12); | ||
| 927 | + let got = send( | ||
| 928 | + &fx.app, | ||
| 929 | + "GET", | ||
| 930 | + "/tahinli/proj/releases/download/v1.2/app.bin", | ||
| 931 | + None, | ||
| 932 | + None, | ||
| 933 | + Vec::new(), | ||
| 934 | + ) | ||
| 935 | + .await; | ||
| 936 | + assert_eq!(got.body, b"second bytes"); | ||
| 937 | + } | ||
| 852 | 938 | ||
| 853 | - let tip = fx.main_tip(); | 939 | + // A wrong CSRF token on upload is refused and stores nothing. |
| 854 | - let made = release_ops::create(&fx.state, &fx.repo, &fx.tahinli, input("v7", &tip)) | 940 | + { |
| 855 | - .await | 941 | + let rel = fx.release("v1.3", false).await; |
| 856 | - .expect("a commit id works as a target"); | 942 | + let (content_type, body) = crate::pages::settings_profile::tests::multipart(&[ |
| 857 | - assert_eq!(made.target_oid, tip, "commit id target"); | 943 | + ("csrf", None, b"wrong"), |
| 858 | - assert_eq!( | 944 | + ("file", Some(("a.bin", "application/octet-stream")), b"data"), |
| 859 | - fx.tag_target("v7").as_deref(), | 945 | + ]); |
| 860 | - Some(tip.as_str()), | 946 | + let reply = send( |
| 861 | - "commit id target" | 947 | + &fx.app, |
| 862 | - ); | 948 | + "POST", |
| 949 | + &format!("/tahinli/proj/releases/{}/assets", rel.id), | ||
| 950 | + Some(&fx.tahinli_cookie), | ||
| 951 | + Some(&content_type), | ||
| 952 | + body, | ||
| 953 | + ) | ||
| 954 | + .await; | ||
| 955 | + assert_ne!(reply.status, 303, "csrf mismatch"); | ||
| 956 | + assert!( | ||
| 957 | + fx.store | ||
| 958 | + .list_release_assets(&rel.id) | ||
| 959 | + .await | ||
| 960 | + .expect("assets") | ||
| 961 | + .is_empty() | ||
| 962 | + ); | ||
| 963 | + } | ||
| 863 | 964 | ||
| 864 | - let missing = release_ops::create( | 965 | + // Deleting a release keeps its git tag and removes its assets and blobs. |
| 865 | - &fx.state, | 966 | + { |
| 866 | - &fx.repo, | 967 | + git(&fx.bare, &["tag", "v1.4", "refs/heads/main"]); |
| 867 | - &fx.tahinli, | 968 | + let rel = fx.release("v1.4", false).await; |
| 868 | - input("v8", "1111111111111111111111111111111111111111"), | 969 | + let up = fx |
| 869 | - ) | 970 | + .upload(&rel.id, "gone.bin", b"bytes", &fx.tahinli_cookie) |
| 870 | - .await; | 971 | + .await; |
| 871 | - assert!( | 972 | + assert_eq!(up.status, 303); |
| 872 | - matches!(missing, Err(ReleaseError::Invalid(_))), | 973 | + let asset = fx |
| 873 | - "missing target: {missing:?}" | 974 | + .store |
| 874 | - ); | 975 | + .release_asset(&rel.id, "gone.bin") |
| 875 | - assert!(fx.tag_target("v8").is_none(), "missing target"); | 976 | + .await |
| 876 | -} | 977 | + .expect("read") |
| 978 | + .expect("row"); | ||
| 979 | + let blob = crate::blobs::asset_key(&asset.id); | ||
| 980 | + assert!(fx.state.blobs.read(&blob).await.expect("read").is_some()); | ||
| 877 | 981 | ||
| 878 | -#[tokio::test] | 982 | + let reply = send( |
| 879 | -async fn the_edit_page_shows_tag_and_target_controls_and_hides_the_target_once_published() { | 983 | + &fx.app, |
| 880 | - let fx = fixture(None).await; | 984 | + "POST", |
| 881 | - let draft = fx.release("v1.0", true).await; | 985 | + &format!("/tahinli/proj/releases/{}/delete", rel.id), |
| 882 | - let published = fx.release("v2.0", false).await; | 986 | + Some(&fx.tahinli_cookie), |
| 883 | - | 987 | + Some(FORM_TYPE), |
| 884 | - let page = send( | 988 | + form(&[("csrf", CSRF), ("confirm", "on")]), |
| 885 | - &fx.app, | 989 | + ) |
| 886 | - "GET", | 990 | + .await; |
| 887 | - &format!("/tahinli/proj/releases/{}/edit", draft.id), | 991 | + assert_eq!(reply.status, 303); |
| 888 | - Some(&fx.tahinli_cookie), | 992 | + assert!(fx.store.release(&rel.id).await.expect("read").is_none()); |
| 889 | - None, | 993 | + assert!(fx.tag_target("v1.4").is_some(), "the git tag survives"); |
| 890 | - Vec::new(), | 994 | + assert!( |
| 891 | - ) | 995 | + fx.state.blobs.read(&blob).await.expect("read").is_none(), |
| 892 | - .await; | 996 | + "the blob is gone" |
| 893 | - assert_eq!(page.status, 200, "body: {}", page.text()); | 997 | + ); |
| 894 | - let html = page.text(); | 998 | + let download = send( |
| 895 | - assert!(html.contains("name=\"tag\" value=\"v1.0\""), "{html}"); | 999 | + &fx.app, |
| 896 | - assert!(html.contains("name=\"target\""), "a draft shows the target"); | 1000 | + "GET", |
| 897 | - | 1001 | + "/tahinli/proj/releases/download/v1.4/gone.bin", |
| 898 | - let page = send( | 1002 | + None, |
| 899 | - &fx.app, | 1003 | + None, |
| 900 | - "GET", | 1004 | + Vec::new(), |
| 901 | - &format!("/tahinli/proj/releases/{}/edit", published.id), | 1005 | + ) |
| 902 | - Some(&fx.tahinli_cookie), | 1006 | + .await; |
| 903 | - None, | 1007 | + assert_eq!(download.status, 404, "a deleted asset does not download"); |
| 904 | - Vec::new(), | 1008 | + } |
| 905 | - ) | ||
| 906 | - .await; | ||
| 907 | - let html = page.text(); | ||
| 908 | - assert!(html.contains("name=\"tag\" value=\"v2.0\""), "{html}"); | ||
| 909 | - assert!( | ||
| 910 | - !html.contains("name=\"target\""), | ||
| 911 | - "published hides the target" | ||
| 912 | - ); | ||
| 913 | - assert!( | ||
| 914 | - html.contains("Published releases keep their commit"), | ||
| 915 | - "{html}" | ||
| 916 | - ); | ||
| 917 | } | 1009 | } |
| 918 | 1010 | ||
| 919 | #[tokio::test] | 1011 | #[tokio::test] |
| 920 | -async fn a_published_release_can_be_retagged_through_the_form_and_keeps_its_commit() { | 1012 | +async fn the_size_cap_refuses_new_and_replacement_uploads() { |
| 921 | - let fx = fixture(None).await; | 1013 | + let fx = fixture(Some(1)).await; |
| 922 | - git(&fx.bare, &["tag", "v2.0", "refs/heads/main"]); | 1014 | + let big = vec![7u8; 1024 * 1024 + 4096]; |
| 923 | - let published = fx.release("v2.0", false).await; | ||
| 924 | - let reply = send( | ||
| 925 | - &fx.app, | ||
| 926 | - "POST", | ||
| 927 | - &format!("/tahinli/proj/releases/{}/edit", published.id), | ||
| 928 | - Some(&fx.tahinli_cookie), | ||
| 929 | - Some(FORM_TYPE), | ||
| 930 | - form(&[ | ||
| 931 | - ("csrf", CSRF), | ||
| 932 | - ("tag", "v2.1"), | ||
| 933 | - ("target", ""), | ||
| 934 | - ("title", "Release v2.0"), | ||
| 935 | - ("body", "notes"), | ||
| 936 | - ]), | ||
| 937 | - ) | ||
| 938 | - .await; | ||
| 939 | - assert_eq!(reply.status, 303, "body: {}", reply.text()); | ||
| 940 | - assert!(reply.location.unwrap_or_default().contains("notice=saved")); | ||
| 941 | - let moved = fx | ||
| 942 | - .store | ||
| 943 | - .release(&published.id) | ||
| 944 | - .await | ||
| 945 | - .expect("read") | ||
| 946 | - .expect("row"); | ||
| 947 | - assert_eq!(moved.tag, "v2.1"); | ||
| 948 | - assert_eq!(fx.tag_target("v2.1"), Some(fx.main_tip())); | ||
| 949 | - assert_eq!( | ||
| 950 | - fx.tag_target("v2.0"), | ||
| 951 | - Some(fx.main_tip()), | ||
| 952 | - "old git tag kept" | ||
| 953 | - ); | ||
| 954 | -} | ||
| 955 | 1015 | ||
| 956 | -#[tokio::test] | 1016 | + // Runs first: no blob may exist yet, so the blob directory must stay empty. |
| 957 | -async fn a_published_target_move_is_refused_on_the_page_and_the_typed_values_stay() { | 1017 | + { |
| 958 | - let fx = fixture(None).await; | 1018 | + let rel = fx.release("v1.0", false).await; |
| 959 | - git(&fx.bare, &["tag", "v2.0", "refs/heads/main"]); | 1019 | + let reply = fx |
| 960 | - second_commit(&fx); | 1020 | + .upload(&rel.id, "big.bin", &big, &fx.tahinli_cookie) |
| 961 | - let published = fx.release("v2.0", false).await; | 1021 | + .await; |
| 962 | - let reply = send( | 1022 | + assert_eq!(reply.status, 303); |
| 963 | - &fx.app, | 1023 | + assert!( |
| 964 | - "POST", | 1024 | + reply |
| 965 | - &format!("/tahinli/proj/releases/{}/edit", published.id), | 1025 | + .location |
| 966 | - Some(&fx.tahinli_cookie), | 1026 | + .unwrap_or_default() |
| 967 | - Some(FORM_TYPE), | 1027 | + .contains("notice=asset-too-large"), |
| 968 | - form(&[ | 1028 | + "the cap refuses the file" |
| 969 | - ("csrf", CSRF), | 1029 | + ); |
| 970 | - ("tag", "v2.0"), | 1030 | + assert!( |
| 971 | - ("target", "next"), | 1031 | + fx.store |
| 972 | - ("title", "Typed title"), | 1032 | + .list_release_assets(&rel.id) |
| 973 | - ("body", "notes"), | 1033 | + .await |
| 974 | - ]), | 1034 | + .expect("assets") |
| 975 | - ) | 1035 | + .is_empty() |
| 976 | - .await; | 1036 | + ); |
| 977 | - assert_eq!( | 1037 | + let assets_dir = fx.state.config.data_dir.join("blobs").join("assets"); |
| 978 | - reply.status, | 1038 | + let leftovers = std::fs::read_dir(&assets_dir) |
| 979 | - 200, | 1039 | + .map(|entries| entries.count()) |
| 980 | - "a refused save re-renders: {}", | 1040 | + .unwrap_or(0); |
| 981 | - reply.text() | 1041 | + assert_eq!(leftovers, 0, "no partial blob is left behind"); |
| 982 | - ); | 1042 | + } |
| 983 | - let html = reply.text(); | ||
| 984 | - assert!(html.contains("pick a new tag"), "{html}"); | ||
| 985 | - assert!( | ||
| 986 | - html.contains("Typed title"), | ||
| 987 | - "the typed title stays: {html}" | ||
| 988 | - ); | ||
| 989 | - assert_eq!(fx.tag_target("v2.0"), Some(fx.main_tip())); | ||
| 990 | -} | ||
| 991 | 1043 | ||
| 992 | -#[tokio::test] | 1044 | + // A refused replacement keeps the old file. |
| 993 | -async fn replace_keeps_the_asset_name_whatever_the_new_file_is_called() { | 1045 | + { |
| 994 | - let fx = fixture(None).await; | 1046 | + let rel = fx.release("v1.1", false).await; |
| 995 | - let rel = fx.release("v1.0", false).await; | 1047 | + fx.upload(&rel.id, "app.bin", b"keep me", &fx.tahinli_cookie) |
| 996 | - let first = fx | 1048 | + .await; |
| 997 | - .upload(&rel.id, "app.bin", b"first", &fx.tahinli_cookie) | 1049 | + let reply = fx |
| 1050 | + .upload(&rel.id, "app.bin", &big, &fx.tahinli_cookie) | ||
| 1051 | + .await; | ||
| 1052 | + assert!( | ||
| 1053 | + reply | ||
| 1054 | + .location | ||
| 1055 | + .unwrap_or_default() | ||
| 1056 | + .contains("notice=asset-too-large"), | ||
| 1057 | + "an oversized replacement is refused" | ||
| 1058 | + ); | ||
| 1059 | + let row = fx | ||
| 1060 | + .store | ||
| 1061 | + .release_asset(&rel.id, "app.bin") | ||
| 1062 | + .await | ||
| 1063 | + .expect("read") | ||
| 1064 | + .expect("row"); | ||
| 1065 | + assert_eq!(row.size, 7, "the old size stays"); | ||
| 1066 | + let got = send( | ||
| 1067 | + &fx.app, | ||
| 1068 | + "GET", | ||
| 1069 | + "/tahinli/proj/releases/download/v1.1/app.bin", | ||
| 1070 | + None, | ||
| 1071 | + None, | ||
| 1072 | + Vec::new(), | ||
| 1073 | + ) | ||
| 998 | .await; | 1074 | .await; |
| 999 | - assert_eq!(first.status, 303); | 1075 | + assert_eq!(got.body, b"keep me", "the old bytes stay"); |
| 1000 | - | 1076 | + } |
| 1001 | - let page = send( | ||
| 1002 | - &fx.app, | ||
| 1003 | - "GET", | ||
| 1004 | - &format!("/tahinli/proj/releases/{}/edit", rel.id), | ||
| 1005 | - Some(&fx.tahinli_cookie), | ||
| 1006 | - None, | ||
| 1007 | - Vec::new(), | ||
| 1008 | - ) | ||
| 1009 | - .await; | ||
| 1010 | - assert!( | ||
| 1011 | - page.text().contains("name=\"name\" value=\"app.bin\""), | ||
| 1012 | - "each asset has a replace form" | ||
| 1013 | - ); | ||
| 1014 | - | ||
| 1015 | - let (content_type, body) = crate::pages::settings_profile::tests::multipart(&[ | ||
| 1016 | - ("csrf", None, CSRF.as_bytes()), | ||
| 1017 | - ("name", None, "app.bin".as_bytes()), | ||
| 1018 | - ( | ||
| 1019 | - "file", | ||
| 1020 | - Some(("renamed-build.zip", "application/octet-stream")), | ||
| 1021 | - "second bytes".as_bytes(), | ||
| 1022 | - ), | ||
| 1023 | - ]); | ||
| 1024 | - let reply = send( | ||
| 1025 | - &fx.app, | ||
| 1026 | - "POST", | ||
| 1027 | - &format!("/tahinli/proj/releases/{}/assets", rel.id), | ||
| 1028 | - Some(&fx.tahinli_cookie), | ||
| 1029 | - Some(&content_type), | ||
| 1030 | - body, | ||
| 1031 | - ) | ||
| 1032 | - .await; | ||
| 1033 | - assert_eq!(reply.status, 303, "body: {}", reply.text()); | ||
| 1034 | - assert!( | ||
| 1035 | - reply | ||
| 1036 | - .location | ||
| 1037 | - .unwrap_or_default() | ||
| 1038 | - .contains("notice=asset-replaced") | ||
| 1039 | - ); | ||
| 1040 | - let assets = fx.store.list_release_assets(&rel.id).await.expect("assets"); | ||
| 1041 | - assert_eq!(assets.len(), 1, "still one asset"); | ||
| 1042 | - assert_eq!(assets[0].name, "app.bin"); | ||
| 1043 | - assert_eq!(assets[0].size, 12); | ||
| 1044 | - let got = send( | ||
| 1045 | - &fx.app, | ||
| 1046 | - "GET", | ||
| 1047 | - "/tahinli/proj/releases/download/v1.0/app.bin", | ||
| 1048 | - None, | ||
| 1049 | - None, | ||
| 1050 | - Vec::new(), | ||
| 1051 | - ) | ||
| 1052 | - .await; | ||
| 1053 | - assert_eq!(got.body, b"second bytes"); | ||
| 1054 | } | 1077 | } |
| 1055 | 1078 | ||
| 1056 | /// The live streams of the release list and a release page: a change from another session wakes | 1079 | /// The live streams of the release list and a release page: a change from another session wakes |
crates/forge-web/src/pages/repo.rsmodified+25 -46
| @@ -635,10 +635,15 @@ mod tests { | |||
| 635 | (status, String::from_utf8_lossy(&bytes).into_owned()) | 635 | (status, String::from_utf8_lossy(&bytes).into_owned()) |
| 636 | } | 636 | } |
| 637 | 637 | ||
| 638 | + // The read-only tests below share one fixture: they only read the demo repo, so each scenario | ||
| 639 | + // sees the same pristine contents. | ||
| 640 | + | ||
| 638 | #[tokio::test] | 641 | #[tokio::test] |
| 639 | - async fn home_renders_tree_clone_popover_and_readme() { | 642 | + async fn demo_pages_render_from_the_repo_contents() { |
| 640 | let dir = tempfile::tempdir().unwrap(); | 643 | let dir = tempfile::tempdir().unwrap(); |
| 641 | let (router, _) = fixture(dir.path()).await; | 644 | let (router, _) = fixture(dir.path()).await; |
| 645 | + | ||
| 646 | + // Home renders the README, the tree, the clone popover and the archive links. | ||
| 642 | let (status, body) = get(&router, "/tahinli/demo").await; | 647 | let (status, body) = get(&router, "/tahinli/demo").await; |
| 643 | assert_eq!(status, 200); | 648 | assert_eq!(status, 200); |
| 644 | assert!(body.contains("Hello from the README"), "README rendered"); | 649 | assert!(body.contains("Hello from the README"), "README rendered"); |
| @@ -658,12 +663,8 @@ mod tests { | |||
| 658 | ); | 663 | ); |
| 659 | assert!(body.contains("/tahinli/demo/archive/main.tar.gz")); | 664 | assert!(body.contains("/tahinli/demo/archive/main.tar.gz")); |
| 660 | assert!(body.contains("/tahinli/demo/branches")); | 665 | assert!(body.contains("/tahinli/demo/branches")); |
| 661 | - } | ||
| 662 | 666 | ||
| 663 | - #[tokio::test] | 667 | + // Trees list their entries and link to subtrees and blobs. |
| 664 | - async fn tree_lists_entries() { | ||
| 665 | - let dir = tempfile::tempdir().unwrap(); | ||
| 666 | - let (router, _) = fixture(dir.path()).await; | ||
| 667 | let (status, body) = get(&router, "/tahinli/demo/tree/main/src").await; | 668 | let (status, body) = get(&router, "/tahinli/demo/tree/main/src").await; |
| 668 | assert_eq!(status, 200); | 669 | assert_eq!(status, 200); |
| 669 | assert!(body.contains("main.rs")); | 670 | assert!(body.contains("main.rs")); |
| @@ -671,24 +672,16 @@ mod tests { | |||
| 671 | let (status, body) = get(&router, "/tahinli/demo/tree/main").await; | 672 | let (status, body) = get(&router, "/tahinli/demo/tree/main").await; |
| 672 | assert_eq!(status, 200); | 673 | assert_eq!(status, 200); |
| 673 | assert!(body.contains("/tahinli/demo/tree/main/docs")); | 674 | assert!(body.contains("/tahinli/demo/tree/main/docs")); |
| 674 | - } | ||
| 675 | 675 | ||
| 676 | - #[tokio::test] | 676 | + // Rust source is highlighted with line anchors. |
| 677 | - async fn blob_highlights_rust_source() { | ||
| 678 | - let dir = tempfile::tempdir().unwrap(); | ||
| 679 | - let (router, _) = fixture(dir.path()).await; | ||
| 680 | let (status, body) = get(&router, "/tahinli/demo/blob/main/src/main.rs").await; | 677 | let (status, body) = get(&router, "/tahinli/demo/blob/main/src/main.rs").await; |
| 681 | assert_eq!(status, 200); | 678 | assert_eq!(status, 200); |
| 682 | assert!(body.contains("id=\"L1\"")); | 679 | assert!(body.contains("id=\"L1\"")); |
| 683 | assert!(body.contains("href=\"#L2\"")); | 680 | assert!(body.contains("href=\"#L2\"")); |
| 684 | assert!(body.contains("<span class="), "syntax classes applied"); | 681 | assert!(body.contains("<span class="), "syntax classes applied"); |
| 685 | assert!(body.contains("/assets/highlight.css?v=")); | 682 | assert!(body.contains("/assets/highlight.css?v=")); |
| 686 | - } | ||
| 687 | 683 | ||
| 688 | - #[tokio::test] | 684 | + // Markdown renders unless the viewer asks for the plain source. |
| 689 | - async fn markdown_blob_renders_unless_plain() { | ||
| 690 | - let dir = tempfile::tempdir().unwrap(); | ||
| 691 | - let (router, _) = fixture(dir.path()).await; | ||
| 692 | let (status, body) = get(&router, "/tahinli/demo/blob/main/docs/guide.md").await; | 685 | let (status, body) = get(&router, "/tahinli/demo/blob/main/docs/guide.md").await; |
| 693 | assert_eq!(status, 200); | 686 | assert_eq!(status, 200); |
| 694 | assert!( | 687 | assert!( |
| @@ -699,19 +692,29 @@ mod tests { | |||
| 699 | assert_eq!(status, 200); | 692 | assert_eq!(status, 200); |
| 700 | assert!(body.contains("file-table"), "source shown as lines"); | 693 | assert!(body.contains("file-table"), "source shown as lines"); |
| 701 | assert!(!body.contains("markdown-body"), "not rendered"); | 694 | assert!(!body.contains("markdown-body"), "not rendered"); |
| 702 | - } | ||
| 703 | 695 | ||
| 704 | - #[tokio::test] | 696 | + // Binary blobs show their size instead of content. |
| 705 | - async fn binary_blob_shows_size() { | ||
| 706 | - let dir = tempfile::tempdir().unwrap(); | ||
| 707 | - let (router, _) = fixture(dir.path()).await; | ||
| 708 | let (status, body) = get(&router, "/tahinli/demo/blob/main/data.bin").await; | 697 | let (status, body) = get(&router, "/tahinli/demo/blob/main/data.bin").await; |
| 709 | assert_eq!(status, 200); | 698 | assert_eq!(status, 200); |
| 710 | assert!(body.contains("Binary file, 3 bytes")); | 699 | assert!(body.contains("Binary file, 3 bytes")); |
| 700 | + | ||
| 701 | + // Fixed segment routes win over the repo catch-all. | ||
| 702 | + let (status, body) = get(&router, "/tahinli/demo/branches").await; | ||
| 703 | + assert_eq!(status, 200); | ||
| 704 | + assert!(body.contains("default"), "default branch marked"); | ||
| 705 | + let (status, body) = get(&router, "/tahinli/demo/tags").await; | ||
| 706 | + assert_eq!(status, 200); | ||
| 707 | + assert!(body.contains("Nothing here yet")); | ||
| 708 | + let (status, body) = get(&router, "/tahinli/demo/tree/main").await; | ||
| 709 | + assert_eq!(status, 200); | ||
| 710 | + assert!(body.contains("README.md")); | ||
| 711 | + let (status, body) = get(&router, "/tahinli/demo").await; | ||
| 712 | + assert_eq!(status, 200); | ||
| 713 | + assert!(body.contains("Hello from the README")); | ||
| 711 | } | 714 | } |
| 712 | 715 | ||
| 713 | #[tokio::test] | 716 | #[tokio::test] |
| 714 | - async fn unknown_refs_and_paths_are_404() { | 717 | + async fn missing_paths_and_private_repos_are_404_for_anonymous() { |
| 715 | let dir = tempfile::tempdir().unwrap(); | 718 | let dir = tempfile::tempdir().unwrap(); |
| 716 | let (router, _) = fixture(dir.path()).await; | 719 | let (router, _) = fixture(dir.path()).await; |
| 717 | for uri in [ | 720 | for uri in [ |
| @@ -724,12 +727,6 @@ mod tests { | |||
| 724 | let (status, _) = get(&router, uri).await; | 727 | let (status, _) = get(&router, uri).await; |
| 725 | assert_eq!(status, 404, "{uri}"); | 728 | assert_eq!(status, 404, "{uri}"); |
| 726 | } | 729 | } |
| 727 | - } | ||
| 728 | - | ||
| 729 | - #[tokio::test] | ||
| 730 | - async fn private_repo_is_404_for_anonymous() { | ||
| 731 | - let dir = tempfile::tempdir().unwrap(); | ||
| 732 | - let (router, _) = fixture(dir.path()).await; | ||
| 733 | for uri in [ | 730 | for uri in [ |
| 734 | "/tahinli/secret", | 731 | "/tahinli/secret", |
| 735 | "/tahinli/secret/tree/main", | 732 | "/tahinli/secret/tree/main", |
| @@ -742,24 +739,6 @@ mod tests { | |||
| 742 | } | 739 | } |
| 743 | } | 740 | } |
| 744 | 741 | ||
| 745 | - #[tokio::test] | ||
| 746 | - async fn fixed_segment_routes_win_over_the_repo_catch_all() { | ||
| 747 | - let dir = tempfile::tempdir().unwrap(); | ||
| 748 | - let (router, _) = fixture(dir.path()).await; | ||
| 749 | - let (status, body) = get(&router, "/tahinli/demo/branches").await; | ||
| 750 | - assert_eq!(status, 200); | ||
| 751 | - assert!(body.contains("default"), "default branch marked"); | ||
| 752 | - let (status, body) = get(&router, "/tahinli/demo/tags").await; | ||
| 753 | - assert_eq!(status, 200); | ||
| 754 | - assert!(body.contains("Nothing here yet")); | ||
| 755 | - let (status, body) = get(&router, "/tahinli/demo/tree/main").await; | ||
| 756 | - assert_eq!(status, 200); | ||
| 757 | - assert!(body.contains("README.md")); | ||
| 758 | - let (status, body) = get(&router, "/tahinli/demo").await; | ||
| 759 | - assert_eq!(status, 200); | ||
| 760 | - assert!(body.contains("Hello from the README")); | ||
| 761 | - } | ||
| 762 | - | ||
| 763 | const ZERO_OID: &str = "0000000000000000000000000000000000000000"; | 742 | const ZERO_OID: &str = "0000000000000000000000000000000000000000"; |
| 764 | const OID: &str = "1111111111111111111111111111111111111111"; | 743 | const OID: &str = "1111111111111111111111111111111111111111"; |
| 765 | const BANNER: &str = "had recent pushes"; | 744 | const BANNER: &str = "had recent pushes"; |
crates/forge-web/src/pages/repo_secrets.rsmodified+35 -33
| @@ -493,10 +493,15 @@ mod tests { | |||
| 493 | } | 493 | } |
| 494 | } | 494 | } |
| 495 | 495 | ||
| 496 | + /// Refusals share one fixture and must leave the store empty: a write collaborator and an | ||
| 497 | + /// outsider get 404, anonymous users never see the page, and a POST without a valid CSRF | ||
| 498 | + /// token is forbidden. | ||
| 496 | #[tokio::test] | 499 | #[tokio::test] |
| 497 | - async fn non_admin_gets_404_and_anonymous_is_sent_to_login() { | 500 | + async fn refusals_store_nothing_and_leave_the_page_alone() { |
| 498 | let dir = tempfile::tempdir().expect("tempdir"); | 501 | let dir = tempfile::tempdir().expect("tempdir"); |
| 499 | let (store, app, repo) = fixture(dir.path()).await; | 502 | let (store, app, repo) = fixture(dir.path()).await; |
| 503 | + | ||
| 504 | + // A write collaborator is not an admin; an outsider gets 404 too. | ||
| 500 | let (status, _, _) = send(&app, "GET", PAGE, Some("bob-token"), &[], None).await; | 505 | let (status, _, _) = send(&app, "GET", PAGE, Some("bob-token"), &[], None).await; |
| 501 | assert_eq!(status, 404, "a write collaborator is not an admin"); | 506 | assert_eq!(status, 404, "a write collaborator is not an admin"); |
| 502 | let (status, _, _) = send(&app, "GET", PAGE, Some("carol-token"), &[], None).await; | 507 | let (status, _, _) = send(&app, "GET", PAGE, Some("carol-token"), &[], None).await; |
| @@ -510,7 +515,7 @@ mod tests { | |||
| 510 | None, | 515 | None, |
| 511 | ) | 516 | ) |
| 512 | .await; | 517 | .await; |
| 513 | - assert_eq!(status, 404); | 518 | + assert_eq!(status, 404, "a collaborator cannot set a secret"); |
| 514 | let (status, _, _) = send( | 519 | let (status, _, _) = send( |
| 515 | &app, | 520 | &app, |
| 516 | "POST", | 521 | "POST", |
| @@ -520,16 +525,12 @@ mod tests { | |||
| 520 | None, | 525 | None, |
| 521 | ) | 526 | ) |
| 522 | .await; | 527 | .await; |
| 523 | - assert_eq!(status, 404); | 528 | + assert_eq!(status, 404, "an outsider cannot delete a secret"); |
| 524 | assert!(names(&store, &repo).await.is_empty(), "nothing was stored"); | 529 | assert!(names(&store, &repo).await.is_empty(), "nothing was stored"); |
| 525 | let (status, _, _) = send(&app, "GET", PAGE, None, &[], None).await; | 530 | let (status, _, _) = send(&app, "GET", PAGE, None, &[], None).await; |
| 526 | assert_ne!(status, 200, "anonymous users do not see the page"); | 531 | assert_ne!(status, 200, "anonymous users do not see the page"); |
| 527 | - } | ||
| 528 | 532 | ||
| 529 | - #[tokio::test] | 533 | + // A POST without a valid CSRF token is forbidden and stores nothing. |
| 530 | - async fn post_without_a_valid_csrf_token_is_forbidden() { | ||
| 531 | - let dir = tempfile::tempdir().expect("tempdir"); | ||
| 532 | - let (store, app, repo) = fixture(dir.path()).await; | ||
| 533 | let (status, _, _) = send( | 534 | let (status, _, _) = send( |
| 534 | &app, | 535 | &app, |
| 535 | "POST", | 536 | "POST", |
| @@ -539,7 +540,7 @@ mod tests { | |||
| 539 | Some("wrong-token"), | 540 | Some("wrong-token"), |
| 540 | ) | 541 | ) |
| 541 | .await; | 542 | .await; |
| 542 | - assert_eq!(status, 403); | 543 | + assert_eq!(status, 403, "a wrong csrf token"); |
| 543 | let (status, _, _) = send( | 544 | let (status, _, _) = send( |
| 544 | &app, | 545 | &app, |
| 545 | "POST", | 546 | "POST", |
| @@ -549,12 +550,17 @@ mod tests { | |||
| 549 | Some(""), | 550 | Some(""), |
| 550 | ) | 551 | ) |
| 551 | .await; | 552 | .await; |
| 552 | - assert_eq!(status, 403); | 553 | + assert_eq!(status, 403, "an empty csrf token on delete"); |
| 553 | - assert!(names(&store, &repo).await.is_empty()); | 554 | + assert!( |
| 555 | + names(&store, &repo).await.is_empty(), | ||
| 556 | + "csrf refusals store nothing" | ||
| 557 | + ); | ||
| 554 | } | 558 | } |
| 555 | 559 | ||
| 560 | + /// The value never comes back in any response, and the page follows the owner's language. | ||
| 561 | + /// Runs in one fixture: the English checks come before the language is switched. | ||
| 556 | #[tokio::test] | 562 | #[tokio::test] |
| 557 | - async fn value_never_appears_in_any_response() { | 563 | + async fn values_never_leak_and_the_page_follows_the_language() { |
| 558 | let dir = tempfile::tempdir().expect("tempdir"); | 564 | let dir = tempfile::tempdir().expect("tempdir"); |
| 559 | let (store, app, repo) = fixture(dir.path()).await; | 565 | let (store, app, repo) = fixture(dir.path()).await; |
| 560 | let (status, location, body) = send( | 566 | let (status, location, body) = send( |
| @@ -592,6 +598,23 @@ mod tests { | |||
| 592 | "no echo after a validation error" | 598 | "no echo after a validation error" |
| 593 | ); | 599 | ); |
| 594 | assert_eq!(names(&store, &repo).await.len(), 1); | 600 | assert_eq!(names(&store, &repo).await.len(), 1); |
| 601 | + | ||
| 602 | + // Turkish viewers see the Turkish heading. | ||
| 603 | + let tahinli = store | ||
| 604 | + .user_by_username("tahinli") | ||
| 605 | + .await | ||
| 606 | + .expect("lookup") | ||
| 607 | + .expect("tahinli"); | ||
| 608 | + store | ||
| 609 | + .set_ui_prefs(&tahinli.id, "auto", "tr") | ||
| 610 | + .await | ||
| 611 | + .expect("set language"); | ||
| 612 | + let (status, _, body) = send(&app, "GET", PAGE, Some("tahinli-token"), &[], None).await; | ||
| 613 | + assert_eq!(status, 200); | ||
| 614 | + assert!( | ||
| 615 | + body.contains("Gizli anahtar ekle veya güncelle"), | ||
| 616 | + "Turkish heading" | ||
| 617 | + ); | ||
| 595 | } | 618 | } |
| 596 | 619 | ||
| 597 | #[tokio::test] | 620 | #[tokio::test] |
| @@ -692,25 +715,4 @@ mod tests { | |||
| 692 | .await; | 715 | .await; |
| 693 | assert!(location.is_some_and(|l| l.contains("notice=missing"))); | 716 | assert!(location.is_some_and(|l| l.contains("notice=missing"))); |
| 694 | } | 717 | } |
| 695 | - | ||
| 696 | - #[tokio::test] | ||
| 697 | - async fn turkish_viewer_sees_turkish_heading() { | ||
| 698 | - let dir = tempfile::tempdir().expect("tempdir"); | ||
| 699 | - let (store, app, _repo) = fixture(dir.path()).await; | ||
| 700 | - let tahinli = store | ||
| 701 | - .user_by_username("tahinli") | ||
| 702 | - .await | ||
| 703 | - .expect("lookup") | ||
| 704 | - .expect("tahinli"); | ||
| 705 | - store | ||
| 706 | - .set_ui_prefs(&tahinli.id, "auto", "tr") | ||
| 707 | - .await | ||
| 708 | - .expect("set language"); | ||
| 709 | - let (status, _, body) = send(&app, "GET", PAGE, Some("tahinli-token"), &[], None).await; | ||
| 710 | - assert_eq!(status, 200); | ||
| 711 | - assert!( | ||
| 712 | - body.contains("Gizli anahtar ekle veya güncelle"), | ||
| 713 | - "Turkish heading" | ||
| 714 | - ); | ||
| 715 | - } | ||
| 716 | } | 718 | } |