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] | |
| 218 | -async fn drafts_are_hidden_from_readers_and_visible_to_writers() { | |
| 219 | - let fx = fixture(None).await; | |
| 220 | - let published = fx.release("v1.0", false).await; | |
| 221 | - let draft = fx.release("v2.0", true).await; | |
| 222 | - let draft_asset = fx | |
| 223 | - .upload(&draft.id, "secret.bin", b"x", &fx.tahinli_cookie) | |
| 224 | - .await; | |
| 225 | - assert_eq!(draft_asset.status, 303, "writer uploads to a draft"); | |
| 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"); | |
| 217 | +/// The release's own fields, unchanged, with no tag change. | |
| 218 | +fn keep(rel: &Release) -> EditRelease { | |
| 219 | + EditRelease { | |
| 220 | + title: rel.title.clone(), | |
| 221 | + body: rel.body.clone(), | |
| 222 | + draft: rel.draft, | |
| 223 | + prerelease: rel.prerelease, | |
| 224 | + tag: None, | |
| 225 | + target: None, | |
| 226 | + } | |
| 288 | 227 | } |
| 289 | 228 | |
| 290 | -#[tokio::test] | |
| 291 | -async fn readers_cannot_create_edit_upload_or_delete() { | |
| 292 | - let fx = fixture(None).await; | |
| 293 | - let rel = fx.release("v1.0", false).await; | |
| 294 | - | |
| 295 | - let create = send( | |
| 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() | |
| 229 | +/// Adds a commit on top of `main` and points branch `next` at it. Returns the new commit. | |
| 230 | +fn second_commit(fx: &Fixture) -> String { | |
| 231 | + let tree = git(&fx.bare, &["rev-parse", "refs/heads/main^{tree}"]); | |
| 232 | + let next = git( | |
| 233 | + &fx.bare, | |
| 234 | + &["commit-tree", &tree, "-p", &fx.main_tip(), "-m", "two"], | |
| 343 | 235 | ); |
| 344 | - | |
| 345 | - let delete = send( | |
| 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()); | |
| 236 | + git(&fx.bare, &["update-ref", "refs/heads/next", &next]); | |
| 237 | + next | |
| 356 | 238 | } |
| 357 | 239 | |
| 358 | -#[tokio::test] | |
| 359 | -async fn a_missing_tag_is_created_as_a_lightweight_tag_on_the_branch_head() { | |
| 360 | - let fx = fixture(None).await; | |
| 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 | -} | |
| 240 | +// Each test below builds one fixture and runs its scenarios in order. Scenarios use their own | |
| 241 | +// tags and releases so they do not interfere, and the blob-count check in the size-cap test runs | |
| 242 | +// first, before any blob exists in its fixture. | |
| 392 | 243 | |
| 393 | 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 | 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] | |
| 417 | -async fn upload_then_download_round_trips_and_counts() { | |
| 418 | - let fx = fixture(None).await; | |
| 419 | - let rel = fx.release("v1.0", false).await; | |
| 420 | - let payload = b"<html>not rendered</html>"; | |
| 421 | - let reply = fx | |
| 422 | - .upload(&rel.id, "app.bin", payload, &fx.tahinli_cookie) | |
| 248 | + // Drafts: readers see the published release only, owners see the draft too. | |
| 249 | + { | |
| 250 | + let published = fx.release("v1.0", false).await; | |
| 251 | + let draft = fx.release("v2.0", true).await; | |
| 252 | + let draft_asset = fx | |
| 253 | + .upload(&draft.id, "secret.bin", b"x", &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 | 265 | .await; |
| 424 | - assert_eq!(reply.status, 303, "body: {}", reply.text()); | |
| 425 | - assert!( | |
| 426 | - reply | |
| 427 | - .location | |
| 428 | - .unwrap_or_default() | |
| 429 | - .contains("notice=asset-uploaded") | |
| 430 | - ); | |
| 266 | + assert_eq!(list.status, 200); | |
| 267 | + assert!(list.text().contains("Release v1.0")); | |
| 268 | + assert!( | |
| 269 | + !list.text().contains("Release v2.0"), | |
| 270 | + "anonymous list hides drafts" | |
| 271 | + ); | |
| 431 | 272 | |
| 432 | - let row = fx | |
| 433 | - .store | |
| 434 | - .release_asset(&rel.id, "app.bin") | |
| 435 | - .await | |
| 436 | - .expect("read") | |
| 437 | - .expect("asset row"); | |
| 438 | - assert_eq!(row.size, payload.len() as u64); | |
| 439 | - let digest = { | |
| 440 | - use sha2::{Digest, Sha256}; | |
| 441 | - hex::encode(Sha256::digest(payload)) | |
| 442 | - }; | |
| 443 | - assert_eq!(row.sha256, digest); | |
| 444 | - | |
| 445 | - for expected in 1..=2u64 { | |
| 446 | - let got = send( | |
| 273 | + let anon_draft = send( | |
| 274 | + &fx.app, | |
| 275 | + "GET", | |
| 276 | + "/tahinli/proj/releases/tag/v2.0", | |
| 277 | + None, | |
| 278 | + None, | |
| 279 | + Vec::new(), | |
| 280 | + ) | |
| 281 | + .await; | |
| 282 | + assert_eq!(anon_draft.status, 404, "anonymous draft page"); | |
| 283 | + let anon_download = send( | |
| 447 | 284 | &fx.app, |
| 448 | 285 | "GET", |
| 449 | - "/tahinli/proj/releases/download/v1.0/app.bin", | |
| 286 | + "/tahinli/proj/releases/download/v2.0/secret.bin", | |
| 450 | 287 | None, |
| 451 | 288 | None, |
| 452 | 289 | Vec::new(), |
| 453 | 290 | ) |
| 454 | 291 | .await; |
| 455 | - assert_eq!(got.status, 200); | |
| 456 | - assert_eq!(got.body, payload); | |
| 457 | 292 | assert_eq!( |
| 458 | - got.headers | |
| 459 | - .get("content-disposition") | |
| 460 | - .and_then(|v| v.to_str().ok()), | |
| 461 | - Some("attachment; filename=\"app.bin\"") | |
| 293 | + anon_download.status, 404, | |
| 294 | + "a draft asset is not downloadable by readers" | |
| 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 | 354 | assert_eq!( |
| 464 | - got.headers | |
| 465 | - .get("x-content-type-options") | |
| 466 | - .and_then(|v| v.to_str().ok()), | |
| 467 | - Some("nosniff") | |
| 355 | + fx.store | |
| 356 | + .release(&rel.id) | |
| 357 | + .await | |
| 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 | 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 | 402 | let fx = fixture(None).await; |
| 482 | - let rel = fx.release("v1.0", false).await; | |
| 483 | - let (content_type, body) = crate::pages::settings_profile::tests::multipart(&[ | |
| 484 | - ("csrf", None, b"wrong"), | |
| 485 | - ("file", Some(("a.bin", "application/octet-stream")), b"data"), | |
| 486 | - ]); | |
| 487 | - let reply = send( | |
| 488 | - &fx.app, | |
| 489 | - "POST", | |
| 490 | - &format!("/tahinli/proj/releases/{}/assets", rel.id), | |
| 491 | - Some(&fx.tahinli_cookie), | |
| 492 | - Some(&content_type), | |
| 493 | - body, | |
| 494 | - ) | |
| 495 | - .await; | |
| 496 | - assert_ne!(reply.status, 303); | |
| 497 | - assert!( | |
| 498 | - fx.store | |
| 499 | - .list_release_assets(&rel.id) | |
| 403 | + | |
| 404 | + // A missing tag is created as a lightweight tag on the branch head. | |
| 405 | + { | |
| 406 | + let reply = send( | |
| 407 | + &fx.app, | |
| 408 | + "POST", | |
| 409 | + "/tahinli/proj/releases/new", | |
| 410 | + Some(&fx.tahinli_cookie), | |
| 411 | + Some(FORM_TYPE), | |
| 412 | + form(&[ | |
| 413 | + ("csrf", CSRF), | |
| 414 | + ("tag", "v1.0"), | |
| 415 | + ("target", "main"), | |
| 416 | + ("title", "First"), | |
| 417 | + ("body", "hello"), | |
| 418 | + ]), | |
| 419 | + ) | |
| 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 | 431 | .await |
| 501 | - .expect("assets") | |
| 502 | - .is_empty() | |
| 503 | - ); | |
| 504 | -} | |
| 432 | + .expect("read") | |
| 433 | + .expect("release"); | |
| 434 | + assert_eq!(release.target_oid, tip); | |
| 435 | + assert!(release.published_at.is_some()); | |
| 436 | + } | |
| 505 | 437 | |
| 506 | -#[tokio::test] | |
| 507 | -async fn an_upload_over_the_size_cap_is_refused_and_leaves_no_blob() { | |
| 508 | - let fx = fixture(Some(1)).await; | |
| 509 | - let rel = fx.release("v1.0", false).await; | |
| 510 | - let big = vec![7u8; 1024 * 1024 + 4096]; | |
| 511 | - let reply = fx | |
| 512 | - .upload(&rel.id, "big.bin", &big, &fx.tahinli_cookie) | |
| 438 | + // An existing tag is used as it is and never moved. | |
| 439 | + { | |
| 440 | + git(&fx.bare, &["tag", "v0.9", "refs/heads/main"]); | |
| 441 | + let before = fx.tag_target("v0.9"); | |
| 442 | + let reply = send( | |
| 443 | + &fx.app, | |
| 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 | 455 | .await; |
| 514 | - assert_eq!(reply.status, 303); | |
| 515 | - assert!( | |
| 516 | - reply | |
| 517 | - .location | |
| 518 | - .unwrap_or_default() | |
| 519 | - .contains("notice=asset-too-large"), | |
| 520 | - "the cap refuses the file" | |
| 521 | - ); | |
| 522 | - assert!( | |
| 523 | - fx.store | |
| 524 | - .list_release_assets(&rel.id) | |
| 456 | + assert_eq!(reply.status, 303, "existing tag"); | |
| 457 | + assert_eq!(fx.tag_target("v0.9"), before, "existing tag is not moved"); | |
| 458 | + } | |
| 459 | + | |
| 460 | + // Create refuses a bad tag or a missing target before touching git, and takes a commit id as | |
| 461 | + // a target. | |
| 462 | + { | |
| 463 | + let input = |tag: &str, target: &str| CreateRelease { | |
| 464 | + tag: tag.to_owned(), | |
| 465 | + target: Some(target.to_owned()), | |
| 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 | 481 | .await |
| 526 | - .expect("assets") | |
| 527 | - .is_empty() | |
| 528 | - ); | |
| 529 | - let assets_dir = fx.state.config.data_dir.join("blobs").join("assets"); | |
| 530 | - let leftovers = std::fs::read_dir(&assets_dir) | |
| 531 | - .map(|entries| entries.count()) | |
| 532 | - .unwrap_or(0); | |
| 533 | - assert_eq!(leftovers, 0, "no partial blob is left behind"); | |
| 534 | -} | |
| 482 | + .expect("a commit id works as a target"); | |
| 483 | + assert_eq!(made.target_oid, tip, "commit id target"); | |
| 484 | + assert_eq!( | |
| 485 | + fx.tag_target("v7").as_deref(), | |
| 486 | + Some(tip.as_str()), | |
| 487 | + "commit id target" | |
| 488 | + ); | |
| 535 | 489 | |
| 536 | -#[tokio::test] | |
| 537 | -async fn deleting_a_release_keeps_the_tag_and_removes_its_assets() { | |
| 538 | - let fx = fixture(None).await; | |
| 539 | - git(&fx.bare, &["tag", "v1.0", "refs/heads/main"]); | |
| 540 | - let rel = fx.release("v1.0", false).await; | |
| 541 | - let up = fx | |
| 542 | - .upload(&rel.id, "gone.bin", b"bytes", &fx.tahinli_cookie) | |
| 490 | + let missing = release_ops::create( | |
| 491 | + &fx.state, | |
| 492 | + &fx.repo, | |
| 493 | + &fx.tahinli, | |
| 494 | + input("v8", "1111111111111111111111111111111111111111"), | |
| 495 | + ) | |
| 543 | 496 | .await; |
| 544 | - assert_eq!(up.status, 303); | |
| 545 | - let asset = fx | |
| 546 | - .store | |
| 547 | - .release_asset(&rel.id, "gone.bin") | |
| 548 | - .await | |
| 549 | - .expect("read") | |
| 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); | |
| 497 | + assert!( | |
| 498 | + matches!(missing, Err(ReleaseError::Invalid(_))), | |
| 499 | + "missing target: {missing:?}" | |
| 500 | + ); | |
| 501 | + assert!(fx.tag_target("v8").is_none(), "missing target"); | |
| 502 | + } | |
| 580 | 503 | } |
| 581 | 504 | |
| 582 | 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 | 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. | |
| 597 | -fn keep(rel: &Release) -> EditRelease { | |
| 598 | - EditRelease { | |
| 599 | - title: rel.title.clone(), | |
| 600 | - body: rel.body.clone(), | |
| 601 | - draft: rel.draft, | |
| 602 | - prerelease: rel.prerelease, | |
| 603 | - tag: None, | |
| 604 | - target: None, | |
| 509 | + // A published release moves to a new tag and keeps the old git tag. | |
| 510 | + { | |
| 511 | + git(&fx.bare, &["tag", "v1.0", "refs/heads/main"]); | |
| 512 | + let rel = fx.release("v1.0", false).await; | |
| 513 | + let edit = EditRelease { | |
| 514 | + tag: Some("v1.1".to_owned()), | |
| 515 | + ..keep(&rel) | |
| 516 | + }; | |
| 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. | |
| 609 | -fn second_commit(fx: &Fixture) -> String { | |
| 610 | - let tree = git(&fx.bare, &["rev-parse", "refs/heads/main^{tree}"]); | |
| 611 | - let next = git( | |
| 612 | - &fx.bare, | |
| 613 | - &["commit-tree", &tree, "-p", &fx.main_tip(), "-m", "two"], | |
| 614 | - ); | |
| 615 | - git(&fx.bare, &["update-ref", "refs/heads/next", &next]); | |
| 616 | - next | |
| 537 | + // A published tag cannot be moved in place, but a draft can. | |
| 538 | + { | |
| 539 | + git(&fx.bare, &["tag", "v2.0", "refs/heads/main"]); | |
| 540 | + let next = second_commit(&fx); | |
| 541 | + let published = fx.release("v2.0", false).await; | |
| 542 | + let move_published = EditRelease { | |
| 543 | + target: Some("next".to_owned()), | |
| 544 | + ..keep(&published) | |
| 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 | 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 | 635 | let fx = fixture(None).await; |
| 622 | - let rel = fx.release("v1.0", false).await; | |
| 623 | - let first = fx | |
| 624 | - .upload(&rel.id, "app.bin", b"first", &fx.tahinli_cookie) | |
| 636 | + | |
| 637 | + // The edit page shows tag and target controls, and hides the target once published. | |
| 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 | 650 | .await; |
| 626 | - assert_eq!(first.status, 303); | |
| 627 | - let before = fx | |
| 628 | - .store | |
| 629 | - .release_asset(&rel.id, "app.bin") | |
| 630 | - .await | |
| 631 | - .expect("read") | |
| 632 | - .expect("row"); | |
| 633 | - fx.store | |
| 634 | - .count_asset_download(&before.id) | |
| 635 | - .await | |
| 636 | - .expect("count"); | |
| 651 | + assert_eq!(page.status, 200, "body: {}", page.text()); | |
| 652 | + let html = page.text(); | |
| 653 | + assert!(html.contains("name=\"tag\" value=\"v1.0\""), "{html}"); | |
| 654 | + assert!(html.contains("name=\"target\""), "a draft shows the target"); | |
| 637 | 655 | |
| 638 | - let second = fx | |
| 639 | - .upload(&rel.id, "app.bin", b"second bytes", &fx.tahinli_cookie) | |
| 656 | + let page = send( | |
| 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 | 664 | .await; |
| 641 | - assert!( | |
| 642 | - second | |
| 643 | - .location | |
| 644 | - .unwrap_or_default() | |
| 645 | - .contains("notice=asset-replaced"), | |
| 646 | - "a same-name upload replaces instead of refusing" | |
| 647 | - ); | |
| 648 | - let assets = fx.store.list_release_assets(&rel.id).await.expect("assets"); | |
| 649 | - assert_eq!(assets.len(), 1, "still one asset of that name"); | |
| 650 | - let after = &assets[0]; | |
| 651 | - assert_ne!(after.id, before.id, "the new file has its own blob"); | |
| 652 | - assert_eq!(after.size, 12); | |
| 653 | - assert_eq!(after.download_count, 1, "the download counter survives"); | |
| 654 | - assert!( | |
| 655 | - fx.state | |
| 656 | - .blobs | |
| 657 | - .read(&asset_key(&before.id)) | |
| 665 | + let html = page.text(); | |
| 666 | + assert!(html.contains("name=\"tag\" value=\"v2.0\""), "{html}"); | |
| 667 | + assert!( | |
| 668 | + !html.contains("name=\"target\""), | |
| 669 | + "published hides the target" | |
| 670 | + ); | |
| 671 | + assert!( | |
| 672 | + html.contains("Published releases keep their commit"), | |
| 673 | + "{html}" | |
| 674 | + ); | |
| 675 | + } | |
| 676 | + | |
| 677 | + // A published release can be retagged through the form and keeps its commit. | |
| 678 | + { | |
| 679 | + git(&fx.bare, &["tag", "v4.0", "refs/heads/main"]); | |
| 680 | + let published = fx.release("v4.0", false).await; | |
| 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 | 701 | .await |
| 659 | 702 | .expect("read") |
| 660 | - .is_none(), | |
| 661 | - "the replaced blob is deleted" | |
| 662 | - ); | |
| 663 | - let got = send( | |
| 664 | - &fx.app, | |
| 665 | - "GET", | |
| 666 | - "/tahinli/proj/releases/download/v1.0/app.bin", | |
| 667 | - None, | |
| 668 | - None, | |
| 669 | - Vec::new(), | |
| 670 | - ) | |
| 671 | - .await; | |
| 672 | - assert_eq!(got.body, b"second bytes"); | |
| 673 | -} | |
| 703 | + .expect("row"); | |
| 704 | + assert_eq!(moved.tag, "v4.1"); | |
| 705 | + assert_eq!(fx.tag_target("v4.1"), Some(fx.main_tip())); | |
| 706 | + assert_eq!( | |
| 707 | + fx.tag_target("v4.0"), | |
| 708 | + Some(fx.main_tip()), | |
| 709 | + "old git tag kept" | |
| 710 | + ); | |
| 711 | + } | |
| 674 | 712 | |
| 675 | -#[tokio::test] | |
| 676 | -async fn a_refused_replacement_keeps_the_old_file() { | |
| 677 | - let fx = fixture(Some(1)).await; | |
| 678 | - let rel = fx.release("v1.0", false).await; | |
| 679 | - fx.upload(&rel.id, "app.bin", b"keep me", &fx.tahinli_cookie) | |
| 680 | - .await; | |
| 681 | - let big = vec![7u8; 1024 * 1024 + 4096]; | |
| 682 | - let reply = fx | |
| 683 | - .upload(&rel.id, "app.bin", &big, &fx.tahinli_cookie) | |
| 713 | + // A published target move is refused on the page, and the typed values stay. | |
| 714 | + { | |
| 715 | + git(&fx.bare, &["tag", "v5.0", "refs/heads/main"]); | |
| 716 | + second_commit(&fx); | |
| 717 | + fx.release("v5.0", false).await; | |
| 718 | + let published = fx | |
| 719 | + .store | |
| 720 | + .release_by_tag(&fx.repo.id, "v5.0") | |
| 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 | 738 | .await; |
| 685 | - assert!( | |
| 686 | - reply | |
| 687 | - .location | |
| 688 | - .unwrap_or_default() | |
| 689 | - .contains("notice=asset-too-large") | |
| 690 | - ); | |
| 691 | - let row = fx | |
| 692 | - .store | |
| 693 | - .release_asset(&rel.id, "app.bin") | |
| 694 | - .await | |
| 695 | - .expect("read") | |
| 696 | - .expect("row"); | |
| 697 | - assert_eq!(row.size, 7); | |
| 698 | - let got = send( | |
| 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"); | |
| 739 | + assert_eq!( | |
| 740 | + reply.status, | |
| 741 | + 200, | |
| 742 | + "a refused save re-renders: {}", | |
| 743 | + reply.text() | |
| 744 | + ); | |
| 745 | + let html = reply.text(); | |
| 746 | + assert!(html.contains("pick a new tag"), "{html}"); | |
| 747 | + assert!( | |
| 748 | + html.contains("Typed title"), | |
| 749 | + "the typed title stays: {html}" | |
| 750 | + ); | |
| 751 | + assert_eq!(fx.tag_target("v5.0"), Some(fx.main_tip())); | |
| 752 | + } | |
| 708 | 753 | } |
| 709 | 754 | |
| 710 | 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 | 757 | let fx = fixture(None).await; |
| 713 | - git(&fx.bare, &["tag", "v1.0", "refs/heads/main"]); | |
| 714 | - let rel = fx.release("v1.0", false).await; | |
| 715 | - let edit = EditRelease { | |
| 716 | - tag: Some("v1.1".to_owned()), | |
| 717 | - ..keep(&rel) | |
| 718 | - }; | |
| 719 | - let moved = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &rel, edit) | |
| 720 | - .await | |
| 721 | - .expect("retag"); | |
| 722 | - assert_eq!(moved.tag, "v1.1"); | |
| 723 | - assert_eq!(moved.target_oid, fx.main_tip()); | |
| 724 | - assert_eq!( | |
| 725 | - fx.tag_target("v1.1").as_deref(), | |
| 726 | - Some(fx.main_tip().as_str()) | |
| 727 | - ); | |
| 728 | - assert!(fx.tag_target("v1.0").is_some(), "the old git tag is kept"); | |
| 729 | - assert!( | |
| 730 | - fx.store | |
| 731 | - .release_by_tag(&fx.repo.id, "v1.0") | |
| 758 | + | |
| 759 | + // Upload then download round-trips the bytes and counts each download. | |
| 760 | + { | |
| 761 | + let rel = fx.release("v1.0", false).await; | |
| 762 | + let payload = b"<html>not rendered</html>"; | |
| 763 | + let reply = fx | |
| 764 | + .upload(&rel.id, "app.bin", payload, &fx.tahinli_cookie) | |
| 765 | + .await; | |
| 766 | + assert_eq!(reply.status, 303, "body: {}", reply.text()); | |
| 767 | + assert!( | |
| 768 | + reply | |
| 769 | + .location | |
| 770 | + .unwrap_or_default() | |
| 771 | + .contains("notice=asset-uploaded") | |
| 772 | + ); | |
| 773 | + | |
| 774 | + let row = fx | |
| 775 | + .store | |
| 776 | + .release_asset(&rel.id, "app.bin") | |
| 732 | 777 | .await |
| 733 | 778 | .expect("read") |
| 734 | - .is_none() | |
| 735 | - ); | |
| 736 | -} | |
| 779 | + .expect("asset row"); | |
| 780 | + assert_eq!(row.size, payload.len() as u64); | |
| 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] | |
| 739 | -async fn a_published_tag_cannot_be_moved_in_place_but_a_draft_can() { | |
| 740 | - let fx = fixture(None).await; | |
| 741 | - git(&fx.bare, &["tag", "v2.0", "refs/heads/main"]); | |
| 742 | - let next = second_commit(&fx); | |
| 743 | - let published = fx.release("v2.0", false).await; | |
| 744 | - let move_published = EditRelease { | |
| 745 | - target: Some("next".to_owned()), | |
| 746 | - ..keep(&published) | |
| 747 | - }; | |
| 748 | - let refused = | |
| 749 | - release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &published, move_published).await; | |
| 750 | - assert!( | |
| 751 | - matches!(refused, Err(ReleaseError::Invalid(_))), | |
| 752 | - "{refused:?}" | |
| 753 | - ); | |
| 754 | - assert_eq!( | |
| 755 | - fx.tag_target("v2.0").as_deref(), | |
| 756 | - Some(fx.main_tip().as_str()) | |
| 757 | - ); | |
| 821 | + // A same-name upload replaces the file and keeps its download counter. | |
| 822 | + { | |
| 823 | + let rel = fx.release("v1.1", false).await; | |
| 824 | + let first = fx | |
| 825 | + .upload(&rel.id, "app.bin", b"first", &fx.tahinli_cookie) | |
| 826 | + .await; | |
| 827 | + assert_eq!(first.status, 303); | |
| 828 | + let before = fx | |
| 829 | + .store | |
| 830 | + .release_asset(&rel.id, "app.bin") | |
| 831 | + .await | |
| 832 | + .expect("read") | |
| 833 | + .expect("row"); | |
| 834 | + fx.store | |
| 835 | + .count_asset_download(&before.id) | |
| 836 | + .await | |
| 837 | + .expect("count"); | |
| 758 | 838 | |
| 759 | - let draft = fx | |
| 760 | - .store | |
| 761 | - .create_release(&forge_core::releases::NewRelease { | |
| 762 | - repo_id: &fx.repo.id, | |
| 763 | - tag: "v3.0", | |
| 764 | - target_oid: &fx.main_tip(), | |
| 765 | - title: "draft", | |
| 766 | - body: "", | |
| 767 | - draft: true, | |
| 768 | - prerelease: false, | |
| 769 | - author_id: &fx.tahinli.id, | |
| 770 | - }) | |
| 771 | - .await | |
| 772 | - .expect("draft"); | |
| 773 | - git(&fx.bare, &["tag", "v3.0", "refs/heads/main"]); | |
| 774 | - let move_draft = EditRelease { | |
| 775 | - target: Some("next".to_owned()), | |
| 776 | - ..keep(&draft) | |
| 777 | - }; | |
| 778 | - let moved = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &draft, move_draft) | |
| 779 | - .await | |
| 780 | - .expect("draft moves"); | |
| 781 | - assert_eq!(moved.target_oid, next); | |
| 782 | - assert_eq!(fx.tag_target("v3.0"), Some(next)); | |
| 783 | -} | |
| 839 | + let second = fx | |
| 840 | + .upload(&rel.id, "app.bin", b"second bytes", &fx.tahinli_cookie) | |
| 841 | + .await; | |
| 842 | + assert!( | |
| 843 | + second | |
| 844 | + .location | |
| 845 | + .unwrap_or_default() | |
| 846 | + .contains("notice=asset-replaced"), | |
| 847 | + "a same-name upload replaces instead of refusing" | |
| 848 | + ); | |
| 849 | + let assets = fx.store.list_release_assets(&rel.id).await.expect("assets"); | |
| 850 | + assert_eq!(assets.len(), 1, "still one asset of that name"); | |
| 851 | + let after = &assets[0]; | |
| 852 | + assert_ne!(after.id, before.id, "the new file has its own blob"); | |
| 853 | + assert_eq!(after.size, 12); | |
| 854 | + assert_eq!(after.download_count, 1, "the download counter survives"); | |
| 855 | + assert!( | |
| 856 | + fx.state | |
| 857 | + .blobs | |
| 858 | + .read(&asset_key(&before.id)) | |
| 859 | + .await | |
| 860 | + .expect("read") | |
| 861 | + .is_none(), | |
| 862 | + "the replaced blob is deleted" | |
| 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] | |
| 786 | -async fn retagging_adopts_an_existing_tag_and_refuses_clashes() { | |
| 787 | - let fx = fixture(None).await; | |
| 788 | - git(&fx.bare, &["tag", "v0.9", "refs/heads/main"]); | |
| 789 | - let next = second_commit(&fx); | |
| 790 | - let draft = fx.release("v5", true).await; | |
| 791 | - | |
| 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 | - ); | |
| 876 | + // Replacing by asset name keeps the name, whatever the new file is called. | |
| 877 | + { | |
| 878 | + let rel = fx.release("v1.2", false).await; | |
| 879 | + let first = fx | |
| 880 | + .upload(&rel.id, "app.bin", b"first", &fx.tahinli_cookie) | |
| 881 | + .await; | |
| 882 | + assert_eq!(first.status, 303); | |
| 802 | 883 | |
| 803 | - let adopt = EditRelease { | |
| 804 | - tag: Some("v0.9".to_owned()), | |
| 805 | - ..keep(&draft) | |
| 806 | - }; | |
| 807 | - let adopted = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &draft, adopt) | |
| 808 | - .await | |
| 809 | - .expect("adopt"); | |
| 810 | - assert_eq!(adopted.tag, "v0.9"); | |
| 811 | - assert_eq!(adopted.target_oid, fx.main_tip()); | |
| 812 | - assert_ne!(adopted.target_oid, next); | |
| 813 | - | |
| 814 | - let other = fx.release("v6", false).await; | |
| 815 | - let taken = EditRelease { | |
| 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 | -} | |
| 884 | + let page = send( | |
| 885 | + &fx.app, | |
| 886 | + "GET", | |
| 887 | + &format!("/tahinli/proj/releases/{}/edit", rel.id), | |
| 888 | + Some(&fx.tahinli_cookie), | |
| 889 | + None, | |
| 890 | + Vec::new(), | |
| 891 | + ) | |
| 892 | + .await; | |
| 893 | + assert!( | |
| 894 | + page.text().contains("name=\"name\" value=\"app.bin\""), | |
| 895 | + "each asset has a replace form" | |
| 896 | + ); | |
| 832 | 897 | |
| 833 | -/// Create refuses a bad tag or a missing target before touching git, and takes a commit id as a | |
| 834 | -/// target. | |
| 835 | -#[tokio::test] | |
| 836 | -async fn create_validates_its_tag_and_target() { | |
| 837 | - let fx = fixture(None).await; | |
| 838 | - let input = |tag: &str, target: &str| CreateRelease { | |
| 839 | - tag: tag.to_owned(), | |
| 840 | - target: Some(target.to_owned()), | |
| 841 | - title: "x".to_owned(), | |
| 842 | - body: String::new(), | |
| 843 | - draft: false, | |
| 844 | - prerelease: false, | |
| 845 | - }; | |
| 846 | - let refused = release_ops::create(&fx.state, &fx.repo, &fx.tahinli, input("a/b", "main")).await; | |
| 847 | - assert!( | |
| 848 | - matches!(refused, Err(ReleaseError::Invalid(_))), | |
| 849 | - "bad tag: {refused:?}" | |
| 850 | - ); | |
| 851 | - assert!(fx.tag_target("a/b").is_none(), "bad tag"); | |
| 898 | + let (content_type, body) = crate::pages::settings_profile::tests::multipart(&[ | |
| 899 | + ("csrf", None, CSRF.as_bytes()), | |
| 900 | + ("name", None, "app.bin".as_bytes()), | |
| 901 | + ( | |
| 902 | + "file", | |
| 903 | + Some(("renamed-build.zip", "application/octet-stream")), | |
| 904 | + "second bytes".as_bytes(), | |
| 905 | + ), | |
| 906 | + ]); | |
| 907 | + let reply = send( | |
| 908 | + &fx.app, | |
| 909 | + "POST", | |
| 910 | + &format!("/tahinli/proj/releases/{}/assets", rel.id), | |
| 911 | + Some(&fx.tahinli_cookie), | |
| 912 | + Some(&content_type), | |
| 913 | + body, | |
| 914 | + ) | |
| 915 | + .await; | |
| 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(); | |
| 854 | - let made = release_ops::create(&fx.state, &fx.repo, &fx.tahinli, input("v7", &tip)) | |
| 855 | - .await | |
| 856 | - .expect("a commit id works as a target"); | |
| 857 | - assert_eq!(made.target_oid, tip, "commit id target"); | |
| 858 | - assert_eq!( | |
| 859 | - fx.tag_target("v7").as_deref(), | |
| 860 | - Some(tip.as_str()), | |
| 861 | - "commit id target" | |
| 862 | - ); | |
| 939 | + // A wrong CSRF token on upload is refused and stores nothing. | |
| 940 | + { | |
| 941 | + let rel = fx.release("v1.3", false).await; | |
| 942 | + let (content_type, body) = crate::pages::settings_profile::tests::multipart(&[ | |
| 943 | + ("csrf", None, b"wrong"), | |
| 944 | + ("file", Some(("a.bin", "application/octet-stream")), b"data"), | |
| 945 | + ]); | |
| 946 | + let reply = send( | |
| 947 | + &fx.app, | |
| 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( | |
| 865 | - &fx.state, | |
| 866 | - &fx.repo, | |
| 867 | - &fx.tahinli, | |
| 868 | - input("v8", "1111111111111111111111111111111111111111"), | |
| 869 | - ) | |
| 870 | - .await; | |
| 871 | - assert!( | |
| 872 | - matches!(missing, Err(ReleaseError::Invalid(_))), | |
| 873 | - "missing target: {missing:?}" | |
| 874 | - ); | |
| 875 | - assert!(fx.tag_target("v8").is_none(), "missing target"); | |
| 876 | -} | |
| 965 | + // Deleting a release keeps its git tag and removes its assets and blobs. | |
| 966 | + { | |
| 967 | + git(&fx.bare, &["tag", "v1.4", "refs/heads/main"]); | |
| 968 | + let rel = fx.release("v1.4", false).await; | |
| 969 | + let up = fx | |
| 970 | + .upload(&rel.id, "gone.bin", b"bytes", &fx.tahinli_cookie) | |
| 971 | + .await; | |
| 972 | + assert_eq!(up.status, 303); | |
| 973 | + let asset = fx | |
| 974 | + .store | |
| 975 | + .release_asset(&rel.id, "gone.bin") | |
| 976 | + .await | |
| 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] | |
| 879 | -async fn the_edit_page_shows_tag_and_target_controls_and_hides_the_target_once_published() { | |
| 880 | - let fx = fixture(None).await; | |
| 881 | - let draft = fx.release("v1.0", true).await; | |
| 882 | - let published = fx.release("v2.0", false).await; | |
| 883 | - | |
| 884 | - let page = send( | |
| 885 | - &fx.app, | |
| 886 | - "GET", | |
| 887 | - &format!("/tahinli/proj/releases/{}/edit", draft.id), | |
| 888 | - Some(&fx.tahinli_cookie), | |
| 889 | - None, | |
| 890 | - Vec::new(), | |
| 891 | - ) | |
| 892 | - .await; | |
| 893 | - assert_eq!(page.status, 200, "body: {}", page.text()); | |
| 894 | - let html = page.text(); | |
| 895 | - assert!(html.contains("name=\"tag\" value=\"v1.0\""), "{html}"); | |
| 896 | - assert!(html.contains("name=\"target\""), "a draft shows the target"); | |
| 897 | - | |
| 898 | - let page = send( | |
| 899 | - &fx.app, | |
| 900 | - "GET", | |
| 901 | - &format!("/tahinli/proj/releases/{}/edit", published.id), | |
| 902 | - Some(&fx.tahinli_cookie), | |
| 903 | - None, | |
| 904 | - Vec::new(), | |
| 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 | - ); | |
| 982 | + let reply = send( | |
| 983 | + &fx.app, | |
| 984 | + "POST", | |
| 985 | + &format!("/tahinli/proj/releases/{}/delete", rel.id), | |
| 986 | + Some(&fx.tahinli_cookie), | |
| 987 | + Some(FORM_TYPE), | |
| 988 | + form(&[("csrf", CSRF), ("confirm", "on")]), | |
| 989 | + ) | |
| 990 | + .await; | |
| 991 | + assert_eq!(reply.status, 303); | |
| 992 | + assert!(fx.store.release(&rel.id).await.expect("read").is_none()); | |
| 993 | + assert!(fx.tag_target("v1.4").is_some(), "the git tag survives"); | |
| 994 | + assert!( | |
| 995 | + fx.state.blobs.read(&blob).await.expect("read").is_none(), | |
| 996 | + "the blob is gone" | |
| 997 | + ); | |
| 998 | + let download = send( | |
| 999 | + &fx.app, | |
| 1000 | + "GET", | |
| 1001 | + "/tahinli/proj/releases/download/v1.4/gone.bin", | |
| 1002 | + None, | |
| 1003 | + None, | |
| 1004 | + Vec::new(), | |
| 1005 | + ) | |
| 1006 | + .await; | |
| 1007 | + assert_eq!(download.status, 404, "a deleted asset does not download"); | |
| 1008 | + } | |
| 917 | 1009 | } |
| 918 | 1010 | |
| 919 | 1011 | #[tokio::test] |
| 920 | -async fn a_published_release_can_be_retagged_through_the_form_and_keeps_its_commit() { | |
| 921 | - let fx = fixture(None).await; | |
| 922 | - git(&fx.bare, &["tag", "v2.0", "refs/heads/main"]); | |
| 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 | -} | |
| 1012 | +async fn the_size_cap_refuses_new_and_replacement_uploads() { | |
| 1013 | + let fx = fixture(Some(1)).await; | |
| 1014 | + let big = vec![7u8; 1024 * 1024 + 4096]; | |
| 955 | 1015 | |
| 956 | -#[tokio::test] | |
| 957 | -async fn a_published_target_move_is_refused_on_the_page_and_the_typed_values_stay() { | |
| 958 | - let fx = fixture(None).await; | |
| 959 | - git(&fx.bare, &["tag", "v2.0", "refs/heads/main"]); | |
| 960 | - second_commit(&fx); | |
| 961 | - let published = fx.release("v2.0", false).await; | |
| 962 | - let reply = send( | |
| 963 | - &fx.app, | |
| 964 | - "POST", | |
| 965 | - &format!("/tahinli/proj/releases/{}/edit", published.id), | |
| 966 | - Some(&fx.tahinli_cookie), | |
| 967 | - Some(FORM_TYPE), | |
| 968 | - form(&[ | |
| 969 | - ("csrf", CSRF), | |
| 970 | - ("tag", "v2.0"), | |
| 971 | - ("target", "next"), | |
| 972 | - ("title", "Typed title"), | |
| 973 | - ("body", "notes"), | |
| 974 | - ]), | |
| 975 | - ) | |
| 976 | - .await; | |
| 977 | - assert_eq!( | |
| 978 | - reply.status, | |
| 979 | - 200, | |
| 980 | - "a refused save re-renders: {}", | |
| 981 | - reply.text() | |
| 982 | - ); | |
| 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 | -} | |
| 1016 | + // Runs first: no blob may exist yet, so the blob directory must stay empty. | |
| 1017 | + { | |
| 1018 | + let rel = fx.release("v1.0", false).await; | |
| 1019 | + let reply = fx | |
| 1020 | + .upload(&rel.id, "big.bin", &big, &fx.tahinli_cookie) | |
| 1021 | + .await; | |
| 1022 | + assert_eq!(reply.status, 303); | |
| 1023 | + assert!( | |
| 1024 | + reply | |
| 1025 | + .location | |
| 1026 | + .unwrap_or_default() | |
| 1027 | + .contains("notice=asset-too-large"), | |
| 1028 | + "the cap refuses the file" | |
| 1029 | + ); | |
| 1030 | + assert!( | |
| 1031 | + fx.store | |
| 1032 | + .list_release_assets(&rel.id) | |
| 1033 | + .await | |
| 1034 | + .expect("assets") | |
| 1035 | + .is_empty() | |
| 1036 | + ); | |
| 1037 | + let assets_dir = fx.state.config.data_dir.join("blobs").join("assets"); | |
| 1038 | + let leftovers = std::fs::read_dir(&assets_dir) | |
| 1039 | + .map(|entries| entries.count()) | |
| 1040 | + .unwrap_or(0); | |
| 1041 | + assert_eq!(leftovers, 0, "no partial blob is left behind"); | |
| 1042 | + } | |
| 991 | 1043 | |
| 992 | -#[tokio::test] | |
| 993 | -async fn replace_keeps_the_asset_name_whatever_the_new_file_is_called() { | |
| 994 | - let fx = fixture(None).await; | |
| 995 | - let rel = fx.release("v1.0", false).await; | |
| 996 | - let first = fx | |
| 997 | - .upload(&rel.id, "app.bin", b"first", &fx.tahinli_cookie) | |
| 1044 | + // A refused replacement keeps the old file. | |
| 1045 | + { | |
| 1046 | + let rel = fx.release("v1.1", false).await; | |
| 1047 | + fx.upload(&rel.id, "app.bin", b"keep me", &fx.tahinli_cookie) | |
| 1048 | + .await; | |
| 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 | 1074 | .await; |
| 999 | - assert_eq!(first.status, 303); | |
| 1000 | - | |
| 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"); | |
| 1075 | + assert_eq!(got.body, b"keep me", "the old bytes stay"); | |
| 1076 | + } | |
| 1054 | 1077 | } |
| 1055 | 1078 | |
| 1056 | 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 | 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 | 641 | #[tokio::test] |
| 639 | - async fn home_renders_tree_clone_popover_and_readme() { | |
| 642 | + async fn demo_pages_render_from_the_repo_contents() { | |
| 640 | 643 | let dir = tempfile::tempdir().unwrap(); |
| 641 | 644 | let (router, _) = fixture(dir.path()).await; |
| 645 | + | |
| 646 | + // Home renders the README, the tree, the clone popover and the archive links. | |
| 642 | 647 | let (status, body) = get(&router, "/tahinli/demo").await; |
| 643 | 648 | assert_eq!(status, 200); |
| 644 | 649 | assert!(body.contains("Hello from the README"), "README rendered"); |
| @@ -658,12 +663,8 @@ mod tests { | ||
| 658 | 663 | ); |
| 659 | 664 | assert!(body.contains("/tahinli/demo/archive/main.tar.gz")); |
| 660 | 665 | assert!(body.contains("/tahinli/demo/branches")); |
| 661 | - } | |
| 662 | 666 | |
| 663 | - #[tokio::test] | |
| 664 | - async fn tree_lists_entries() { | |
| 665 | - let dir = tempfile::tempdir().unwrap(); | |
| 666 | - let (router, _) = fixture(dir.path()).await; | |
| 667 | + // Trees list their entries and link to subtrees and blobs. | |
| 667 | 668 | let (status, body) = get(&router, "/tahinli/demo/tree/main/src").await; |
| 668 | 669 | assert_eq!(status, 200); |
| 669 | 670 | assert!(body.contains("main.rs")); |
| @@ -671,24 +672,16 @@ mod tests { | ||
| 671 | 672 | let (status, body) = get(&router, "/tahinli/demo/tree/main").await; |
| 672 | 673 | assert_eq!(status, 200); |
| 673 | 674 | assert!(body.contains("/tahinli/demo/tree/main/docs")); |
| 674 | - } | |
| 675 | 675 | |
| 676 | - #[tokio::test] | |
| 677 | - async fn blob_highlights_rust_source() { | |
| 678 | - let dir = tempfile::tempdir().unwrap(); | |
| 679 | - let (router, _) = fixture(dir.path()).await; | |
| 676 | + // Rust source is highlighted with line anchors. | |
| 680 | 677 | let (status, body) = get(&router, "/tahinli/demo/blob/main/src/main.rs").await; |
| 681 | 678 | assert_eq!(status, 200); |
| 682 | 679 | assert!(body.contains("id=\"L1\"")); |
| 683 | 680 | assert!(body.contains("href=\"#L2\"")); |
| 684 | 681 | assert!(body.contains("<span class="), "syntax classes applied"); |
| 685 | 682 | assert!(body.contains("/assets/highlight.css?v=")); |
| 686 | - } | |
| 687 | 683 | |
| 688 | - #[tokio::test] | |
| 689 | - async fn markdown_blob_renders_unless_plain() { | |
| 690 | - let dir = tempfile::tempdir().unwrap(); | |
| 691 | - let (router, _) = fixture(dir.path()).await; | |
| 684 | + // Markdown renders unless the viewer asks for the plain source. | |
| 692 | 685 | let (status, body) = get(&router, "/tahinli/demo/blob/main/docs/guide.md").await; |
| 693 | 686 | assert_eq!(status, 200); |
| 694 | 687 | assert!( |
| @@ -699,19 +692,29 @@ mod tests { | ||
| 699 | 692 | assert_eq!(status, 200); |
| 700 | 693 | assert!(body.contains("file-table"), "source shown as lines"); |
| 701 | 694 | assert!(!body.contains("markdown-body"), "not rendered"); |
| 702 | - } | |
| 703 | 695 | |
| 704 | - #[tokio::test] | |
| 705 | - async fn binary_blob_shows_size() { | |
| 706 | - let dir = tempfile::tempdir().unwrap(); | |
| 707 | - let (router, _) = fixture(dir.path()).await; | |
| 696 | + // Binary blobs show their size instead of content. | |
| 708 | 697 | let (status, body) = get(&router, "/tahinli/demo/blob/main/data.bin").await; |
| 709 | 698 | assert_eq!(status, 200); |
| 710 | 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 | 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 | 718 | let dir = tempfile::tempdir().unwrap(); |
| 716 | 719 | let (router, _) = fixture(dir.path()).await; |
| 717 | 720 | for uri in [ |
| @@ -724,12 +727,6 @@ mod tests { | ||
| 724 | 727 | let (status, _) = get(&router, uri).await; |
| 725 | 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 | 730 | for uri in [ |
| 734 | 731 | "/tahinli/secret", |
| 735 | 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 | 742 | const ZERO_OID: &str = "0000000000000000000000000000000000000000"; |
| 764 | 743 | const OID: &str = "1111111111111111111111111111111111111111"; |
| 765 | 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 | 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 | 501 | let dir = tempfile::tempdir().expect("tempdir"); |
| 499 | 502 | let (store, app, repo) = fixture(dir.path()).await; |
| 503 | + | |
| 504 | + // A write collaborator is not an admin; an outsider gets 404 too. | |
| 500 | 505 | let (status, _, _) = send(&app, "GET", PAGE, Some("bob-token"), &[], None).await; |
| 501 | 506 | assert_eq!(status, 404, "a write collaborator is not an admin"); |
| 502 | 507 | let (status, _, _) = send(&app, "GET", PAGE, Some("carol-token"), &[], None).await; |
| @@ -510,7 +515,7 @@ mod tests { | ||
| 510 | 515 | None, |
| 511 | 516 | ) |
| 512 | 517 | .await; |
| 513 | - assert_eq!(status, 404); | |
| 518 | + assert_eq!(status, 404, "a collaborator cannot set a secret"); | |
| 514 | 519 | let (status, _, _) = send( |
| 515 | 520 | &app, |
| 516 | 521 | "POST", |
| @@ -520,16 +525,12 @@ mod tests { | ||
| 520 | 525 | None, |
| 521 | 526 | ) |
| 522 | 527 | .await; |
| 523 | - assert_eq!(status, 404); | |
| 528 | + assert_eq!(status, 404, "an outsider cannot delete a secret"); | |
| 524 | 529 | assert!(names(&store, &repo).await.is_empty(), "nothing was stored"); |
| 525 | 530 | let (status, _, _) = send(&app, "GET", PAGE, None, &[], None).await; |
| 526 | 531 | assert_ne!(status, 200, "anonymous users do not see the page"); |
| 527 | - } | |
| 528 | 532 | |
| 529 | - #[tokio::test] | |
| 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 | + // A POST without a valid CSRF token is forbidden and stores nothing. | |
| 533 | 534 | let (status, _, _) = send( |
| 534 | 535 | &app, |
| 535 | 536 | "POST", |
| @@ -539,7 +540,7 @@ mod tests { | ||
| 539 | 540 | Some("wrong-token"), |
| 540 | 541 | ) |
| 541 | 542 | .await; |
| 542 | - assert_eq!(status, 403); | |
| 543 | + assert_eq!(status, 403, "a wrong csrf token"); | |
| 543 | 544 | let (status, _, _) = send( |
| 544 | 545 | &app, |
| 545 | 546 | "POST", |
| @@ -549,12 +550,17 @@ mod tests { | ||
| 549 | 550 | Some(""), |
| 550 | 551 | ) |
| 551 | 552 | .await; |
| 552 | - assert_eq!(status, 403); | |
| 553 | - assert!(names(&store, &repo).await.is_empty()); | |
| 553 | + assert_eq!(status, 403, "an empty csrf token on delete"); | |
| 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 | 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 | 564 | let dir = tempfile::tempdir().expect("tempdir"); |
| 559 | 565 | let (store, app, repo) = fixture(dir.path()).await; |
| 560 | 566 | let (status, location, body) = send( |
| @@ -592,6 +598,23 @@ mod tests { | ||
| 592 | 598 | "no echo after a validation error" |
| 593 | 599 | ); |
| 594 | 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 | 620 | #[tokio::test] |
| @@ -692,25 +715,4 @@ mod tests { | ||
| 692 | 715 | .await; |
| 693 | 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 | } |