diff --git a/crates/forge-web/src/pages/releases_tests.rs b/crates/forge-web/src/pages/releases_tests.rs index 958dc87..b541979 100644 --- a/crates/forge-web/src/pages/releases_tests.rs +++ b/crates/forge-web/src/pages/releases_tests.rs @@ -214,843 +214,866 @@ impl Fixture { } } -#[tokio::test] -async fn drafts_are_hidden_from_readers_and_visible_to_writers() { - let fx = fixture(None).await; - let published = fx.release("v1.0", false).await; - let draft = fx.release("v2.0", true).await; - let draft_asset = fx - .upload(&draft.id, "secret.bin", b"x", &fx.tahinli_cookie) - .await; - assert_eq!(draft_asset.status, 303, "writer uploads to a draft"); - - let list = send( - &fx.app, - "GET", - "/tahinli/proj/releases", - None, - None, - Vec::new(), - ) - .await; - assert_eq!(list.status, 200); - assert!(list.text().contains("Release v1.0")); - assert!( - !list.text().contains("Release v2.0"), - "anonymous list hides drafts" - ); - - let anon_draft = send( - &fx.app, - "GET", - "/tahinli/proj/releases/tag/v2.0", - None, - None, - Vec::new(), - ) - .await; - assert_eq!(anon_draft.status, 404); - let anon_download = send( - &fx.app, - "GET", - "/tahinli/proj/releases/download/v2.0/secret.bin", - None, - None, - Vec::new(), - ) - .await; - assert_eq!( - anon_download.status, 404, - "a draft asset is not downloadable by readers" - ); - - let writer_draft = send( - &fx.app, - "GET", - "/tahinli/proj/releases/tag/v2.0", - Some(&fx.tahinli_cookie), - None, - Vec::new(), - ) - .await; - assert_eq!(writer_draft.status, 200, "the owner sees the draft"); - let writer_list = send( - &fx.app, - "GET", - "/tahinli/proj/releases", - Some(&fx.tahinli_cookie), - None, - Vec::new(), - ) - .await; - assert!(writer_list.text().contains("Release v2.0")); - assert_eq!(published.tag, "v1.0"); +/// The release's own fields, unchanged, with no tag change. +fn keep(rel: &Release) -> EditRelease { + EditRelease { + title: rel.title.clone(), + body: rel.body.clone(), + draft: rel.draft, + prerelease: rel.prerelease, + tag: None, + target: None, + } } -#[tokio::test] -async fn readers_cannot_create_edit_upload_or_delete() { - let fx = fixture(None).await; - let rel = fx.release("v1.0", false).await; - - let create = send( - &fx.app, - "POST", - "/tahinli/proj/releases/new", - Some(&fx.bob_cookie), - Some(FORM_TYPE), - form(&[ - ("csrf", CSRF), - ("tag", "v9"), - ("target", "main"), - ("title", "x"), - ]), - ) - .await; - assert_ne!(create.status, 303, "a reader cannot create a release"); - assert!( - fx.tag_target("v9").is_none(), - "no tag is created for a refused release" - ); - - let edit = send( - &fx.app, - "POST", - &format!("/tahinli/proj/releases/{}/edit", rel.id), - Some(&fx.bob_cookie), - Some(FORM_TYPE), - form(&[("csrf", CSRF), ("title", "hacked"), ("body", "")]), - ) - .await; - assert_ne!(edit.status, 303); - assert_eq!( - fx.store - .release(&rel.id) - .await - .expect("read") - .expect("row") - .title, - "Release v1.0" - ); - - let upload = fx.upload(&rel.id, "a.bin", b"data", &fx.bob_cookie).await; - assert_ne!(upload.status, 303); - assert!( - fx.store - .list_release_assets(&rel.id) - .await - .expect("assets") - .is_empty() +/// Adds a commit on top of `main` and points branch `next` at it. Returns the new commit. +fn second_commit(fx: &Fixture) -> String { + let tree = git(&fx.bare, &["rev-parse", "refs/heads/main^{tree}"]); + let next = git( + &fx.bare, + &["commit-tree", &tree, "-p", &fx.main_tip(), "-m", "two"], ); - - let delete = send( - &fx.app, - "POST", - &format!("/tahinli/proj/releases/{}/delete", rel.id), - Some(&fx.bob_cookie), - Some(FORM_TYPE), - form(&[("csrf", CSRF), ("confirm", "on")]), - ) - .await; - assert_ne!(delete.status, 303); - assert!(fx.store.release(&rel.id).await.expect("read").is_some()); + git(&fx.bare, &["update-ref", "refs/heads/next", &next]); + next } -#[tokio::test] -async fn a_missing_tag_is_created_as_a_lightweight_tag_on_the_branch_head() { - let fx = fixture(None).await; - let reply = send( - &fx.app, - "POST", - "/tahinli/proj/releases/new", - Some(&fx.tahinli_cookie), - Some(FORM_TYPE), - form(&[ - ("csrf", CSRF), - ("tag", "v1.0"), - ("target", "main"), - ("title", "First"), - ("body", "hello"), - ]), - ) - .await; - assert_eq!(reply.status, 303, "body: {}", reply.text()); - assert_eq!( - reply.location.as_deref(), - Some("/tahinli/proj/releases/tag/v1.0") - ); - let tip = fx.main_tip(); - assert_eq!(fx.tag_target("v1.0").as_deref(), Some(tip.as_str())); - let release = fx - .store - .release_by_tag(&fx.repo.id, "v1.0") - .await - .expect("read") - .expect("release"); - assert_eq!(release.target_oid, tip); - assert!(release.published_at.is_some()); -} +// Each test below builds one fixture and runs its scenarios in order. Scenarios use their own +// tags and releases so they do not interfere, and the blob-count check in the size-cap test runs +// first, before any blob exists in its fixture. #[tokio::test] -async fn an_existing_tag_is_used_as_it_is_and_never_moved() { +async fn drafts_and_permissions_share_one_repo() { let fx = fixture(None).await; - git(&fx.bare, &["tag", "v0.9", "refs/heads/main"]); - let before = fx.tag_target("v0.9"); - let reply = send( - &fx.app, - "POST", - "/tahinli/proj/releases/new", - Some(&fx.tahinli_cookie), - Some(FORM_TYPE), - form(&[ - ("csrf", CSRF), - ("tag", "v0.9"), - ("target", "main"), - ("title", "Old"), - ]), - ) - .await; - assert_eq!(reply.status, 303); - assert_eq!(fx.tag_target("v0.9"), before); -} -#[tokio::test] -async fn upload_then_download_round_trips_and_counts() { - let fx = fixture(None).await; - let rel = fx.release("v1.0", false).await; - let payload = b"not rendered"; - let reply = fx - .upload(&rel.id, "app.bin", payload, &fx.tahinli_cookie) + // Drafts: readers see the published release only, owners see the draft too. + { + let published = fx.release("v1.0", false).await; + let draft = fx.release("v2.0", true).await; + let draft_asset = fx + .upload(&draft.id, "secret.bin", b"x", &fx.tahinli_cookie) + .await; + assert_eq!(draft_asset.status, 303, "writer uploads to a draft"); + + let list = send( + &fx.app, + "GET", + "/tahinli/proj/releases", + None, + None, + Vec::new(), + ) .await; - assert_eq!(reply.status, 303, "body: {}", reply.text()); - assert!( - reply - .location - .unwrap_or_default() - .contains("notice=asset-uploaded") - ); + assert_eq!(list.status, 200); + assert!(list.text().contains("Release v1.0")); + assert!( + !list.text().contains("Release v2.0"), + "anonymous list hides drafts" + ); - let row = fx - .store - .release_asset(&rel.id, "app.bin") - .await - .expect("read") - .expect("asset row"); - assert_eq!(row.size, payload.len() as u64); - let digest = { - use sha2::{Digest, Sha256}; - hex::encode(Sha256::digest(payload)) - }; - assert_eq!(row.sha256, digest); - - for expected in 1..=2u64 { - let got = send( + let anon_draft = send( + &fx.app, + "GET", + "/tahinli/proj/releases/tag/v2.0", + None, + None, + Vec::new(), + ) + .await; + assert_eq!(anon_draft.status, 404, "anonymous draft page"); + let anon_download = send( &fx.app, "GET", - "/tahinli/proj/releases/download/v1.0/app.bin", + "/tahinli/proj/releases/download/v2.0/secret.bin", None, None, Vec::new(), ) .await; - assert_eq!(got.status, 200); - assert_eq!(got.body, payload); assert_eq!( - got.headers - .get("content-disposition") - .and_then(|v| v.to_str().ok()), - Some("attachment; filename=\"app.bin\"") + anon_download.status, 404, + "a draft asset is not downloadable by readers" ); + + let writer_draft = send( + &fx.app, + "GET", + "/tahinli/proj/releases/tag/v2.0", + Some(&fx.tahinli_cookie), + None, + Vec::new(), + ) + .await; + assert_eq!(writer_draft.status, 200, "the owner sees the draft"); + let writer_list = send( + &fx.app, + "GET", + "/tahinli/proj/releases", + Some(&fx.tahinli_cookie), + None, + Vec::new(), + ) + .await; + assert!(writer_list.text().contains("Release v2.0")); + assert_eq!(published.tag, "v1.0"); + } + + // Readers cannot create, edit, upload or delete. + { + let rel = fx.release("v3.0", false).await; + + let create = send( + &fx.app, + "POST", + "/tahinli/proj/releases/new", + Some(&fx.bob_cookie), + Some(FORM_TYPE), + form(&[ + ("csrf", CSRF), + ("tag", "v9"), + ("target", "main"), + ("title", "x"), + ]), + ) + .await; + assert_ne!(create.status, 303, "a reader cannot create a release"); + assert!( + fx.tag_target("v9").is_none(), + "no tag is created for a refused release" + ); + + let edit = send( + &fx.app, + "POST", + &format!("/tahinli/proj/releases/{}/edit", rel.id), + Some(&fx.bob_cookie), + Some(FORM_TYPE), + form(&[("csrf", CSRF), ("title", "hacked"), ("body", "")]), + ) + .await; + assert_ne!(edit.status, 303, "a reader cannot edit"); assert_eq!( - got.headers - .get("x-content-type-options") - .and_then(|v| v.to_str().ok()), - Some("nosniff") + fx.store + .release(&rel.id) + .await + .expect("read") + .expect("row") + .title, + "Release v3.0" + ); + + let upload = fx.upload(&rel.id, "a.bin", b"data", &fx.bob_cookie).await; + assert_ne!(upload.status, 303, "a reader cannot upload"); + assert!( + fx.store + .list_release_assets(&rel.id) + .await + .expect("assets") + .is_empty() + ); + + let delete = send( + &fx.app, + "POST", + &format!("/tahinli/proj/releases/{}/delete", rel.id), + Some(&fx.bob_cookie), + Some(FORM_TYPE), + form(&[("csrf", CSRF), ("confirm", "on")]), + ) + .await; + assert_ne!(delete.status, 303, "a reader cannot delete"); + assert!(fx.store.release(&rel.id).await.expect("read").is_some()); + } + + // The repo home shows the latest release card and links to the releases tab. + { + fx.release("v0.9-beta", false).await; + let home = send(&fx.app, "GET", "/tahinli/proj", None, None, Vec::new()).await; + assert_eq!(home.status, 200); + assert!(home.text().contains("Latest release")); + assert!( + home.text().contains("/tahinli/proj/releases"), + "the tabs link to releases" ); - let counted = fx - .store - .release_asset(&rel.id, "app.bin") - .await - .expect("read") - .expect("row"); - assert_eq!(counted.download_count, expected); } } #[tokio::test] -async fn a_csrf_mismatch_on_upload_is_refused_and_stores_nothing() { +async fn creating_a_release_takes_and_validates_its_tag_and_target() { let fx = fixture(None).await; - let rel = fx.release("v1.0", false).await; - let (content_type, body) = crate::pages::settings_profile::tests::multipart(&[ - ("csrf", None, b"wrong"), - ("file", Some(("a.bin", "application/octet-stream")), b"data"), - ]); - let reply = send( - &fx.app, - "POST", - &format!("/tahinli/proj/releases/{}/assets", rel.id), - Some(&fx.tahinli_cookie), - Some(&content_type), - body, - ) - .await; - assert_ne!(reply.status, 303); - assert!( - fx.store - .list_release_assets(&rel.id) + + // A missing tag is created as a lightweight tag on the branch head. + { + let reply = send( + &fx.app, + "POST", + "/tahinli/proj/releases/new", + Some(&fx.tahinli_cookie), + Some(FORM_TYPE), + form(&[ + ("csrf", CSRF), + ("tag", "v1.0"), + ("target", "main"), + ("title", "First"), + ("body", "hello"), + ]), + ) + .await; + assert_eq!(reply.status, 303, "body: {}", reply.text()); + assert_eq!( + reply.location.as_deref(), + Some("/tahinli/proj/releases/tag/v1.0") + ); + let tip = fx.main_tip(); + assert_eq!(fx.tag_target("v1.0").as_deref(), Some(tip.as_str())); + let release = fx + .store + .release_by_tag(&fx.repo.id, "v1.0") .await - .expect("assets") - .is_empty() - ); -} + .expect("read") + .expect("release"); + assert_eq!(release.target_oid, tip); + assert!(release.published_at.is_some()); + } -#[tokio::test] -async fn an_upload_over_the_size_cap_is_refused_and_leaves_no_blob() { - let fx = fixture(Some(1)).await; - let rel = fx.release("v1.0", false).await; - let big = vec![7u8; 1024 * 1024 + 4096]; - let reply = fx - .upload(&rel.id, "big.bin", &big, &fx.tahinli_cookie) + // An existing tag is used as it is and never moved. + { + git(&fx.bare, &["tag", "v0.9", "refs/heads/main"]); + let before = fx.tag_target("v0.9"); + let reply = send( + &fx.app, + "POST", + "/tahinli/proj/releases/new", + Some(&fx.tahinli_cookie), + Some(FORM_TYPE), + form(&[ + ("csrf", CSRF), + ("tag", "v0.9"), + ("target", "main"), + ("title", "Old"), + ]), + ) .await; - assert_eq!(reply.status, 303); - assert!( - reply - .location - .unwrap_or_default() - .contains("notice=asset-too-large"), - "the cap refuses the file" - ); - assert!( - fx.store - .list_release_assets(&rel.id) + assert_eq!(reply.status, 303, "existing tag"); + assert_eq!(fx.tag_target("v0.9"), before, "existing tag is not moved"); + } + + // Create refuses a bad tag or a missing target before touching git, and takes a commit id as + // a target. + { + let input = |tag: &str, target: &str| CreateRelease { + tag: tag.to_owned(), + target: Some(target.to_owned()), + title: "x".to_owned(), + body: String::new(), + draft: false, + prerelease: false, + }; + let refused = + release_ops::create(&fx.state, &fx.repo, &fx.tahinli, input("a/b", "main")).await; + assert!( + matches!(refused, Err(ReleaseError::Invalid(_))), + "bad tag: {refused:?}" + ); + assert!(fx.tag_target("a/b").is_none(), "bad tag"); + + let tip = fx.main_tip(); + let made = release_ops::create(&fx.state, &fx.repo, &fx.tahinli, input("v7", &tip)) .await - .expect("assets") - .is_empty() - ); - let assets_dir = fx.state.config.data_dir.join("blobs").join("assets"); - let leftovers = std::fs::read_dir(&assets_dir) - .map(|entries| entries.count()) - .unwrap_or(0); - assert_eq!(leftovers, 0, "no partial blob is left behind"); -} + .expect("a commit id works as a target"); + assert_eq!(made.target_oid, tip, "commit id target"); + assert_eq!( + fx.tag_target("v7").as_deref(), + Some(tip.as_str()), + "commit id target" + ); -#[tokio::test] -async fn deleting_a_release_keeps_the_tag_and_removes_its_assets() { - let fx = fixture(None).await; - git(&fx.bare, &["tag", "v1.0", "refs/heads/main"]); - let rel = fx.release("v1.0", false).await; - let up = fx - .upload(&rel.id, "gone.bin", b"bytes", &fx.tahinli_cookie) + let missing = release_ops::create( + &fx.state, + &fx.repo, + &fx.tahinli, + input("v8", "1111111111111111111111111111111111111111"), + ) .await; - assert_eq!(up.status, 303); - let asset = fx - .store - .release_asset(&rel.id, "gone.bin") - .await - .expect("read") - .expect("row"); - let blob = crate::blobs::asset_key(&asset.id); - assert!(fx.state.blobs.read(&blob).await.expect("read").is_some()); - - let reply = send( - &fx.app, - "POST", - &format!("/tahinli/proj/releases/{}/delete", rel.id), - Some(&fx.tahinli_cookie), - Some(FORM_TYPE), - form(&[("csrf", CSRF), ("confirm", "on")]), - ) - .await; - assert_eq!(reply.status, 303); - assert!(fx.store.release(&rel.id).await.expect("read").is_none()); - assert!(fx.tag_target("v1.0").is_some(), "the git tag survives"); - assert!( - fx.state.blobs.read(&blob).await.expect("read").is_none(), - "the blob is gone" - ); - let download = send( - &fx.app, - "GET", - "/tahinli/proj/releases/download/v1.0/gone.bin", - None, - None, - Vec::new(), - ) - .await; - assert_eq!(download.status, 404); + assert!( + matches!(missing, Err(ReleaseError::Invalid(_))), + "missing target: {missing:?}" + ); + assert!(fx.tag_target("v8").is_none(), "missing target"); + } } #[tokio::test] -async fn the_latest_release_card_shows_on_the_repo_home() { +async fn retagging_rules_hold_for_published_and_draft_releases() { let fx = fixture(None).await; - fx.release("v1.0", false).await; - fx.release("v0.9-beta", false).await; - let home = send(&fx.app, "GET", "/tahinli/proj", None, None, Vec::new()).await; - assert_eq!(home.status, 200); - assert!(home.text().contains("Latest release")); - assert!( - home.text().contains("/tahinli/proj/releases"), - "the tabs link to releases" - ); -} -/// The release's own fields, unchanged, with no tag change. -fn keep(rel: &Release) -> EditRelease { - EditRelease { - title: rel.title.clone(), - body: rel.body.clone(), - draft: rel.draft, - prerelease: rel.prerelease, - tag: None, - target: None, + // A published release moves to a new tag and keeps the old git tag. + { + git(&fx.bare, &["tag", "v1.0", "refs/heads/main"]); + let rel = fx.release("v1.0", false).await; + let edit = EditRelease { + tag: Some("v1.1".to_owned()), + ..keep(&rel) + }; + let moved = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &rel, edit) + .await + .expect("retag"); + assert_eq!(moved.tag, "v1.1"); + assert_eq!(moved.target_oid, fx.main_tip()); + assert_eq!( + fx.tag_target("v1.1").as_deref(), + Some(fx.main_tip().as_str()) + ); + assert!(fx.tag_target("v1.0").is_some(), "the old git tag is kept"); + assert!( + fx.store + .release_by_tag(&fx.repo.id, "v1.0") + .await + .expect("read") + .is_none(), + "the old tag no longer names a release" + ); } -} -/// Adds a commit on top of `main` and points branch `next` at it. Returns the new commit. -fn second_commit(fx: &Fixture) -> String { - let tree = git(&fx.bare, &["rev-parse", "refs/heads/main^{tree}"]); - let next = git( - &fx.bare, - &["commit-tree", &tree, "-p", &fx.main_tip(), "-m", "two"], - ); - git(&fx.bare, &["update-ref", "refs/heads/next", &next]); - next + // A published tag cannot be moved in place, but a draft can. + { + git(&fx.bare, &["tag", "v2.0", "refs/heads/main"]); + let next = second_commit(&fx); + let published = fx.release("v2.0", false).await; + let move_published = EditRelease { + target: Some("next".to_owned()), + ..keep(&published) + }; + let refused = + release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &published, move_published).await; + assert!( + matches!(refused, Err(ReleaseError::Invalid(_))), + "{refused:?}" + ); + assert_eq!( + fx.tag_target("v2.0").as_deref(), + Some(fx.main_tip().as_str()), + "published tag stays put" + ); + + let draft = fx + .store + .create_release(&forge_core::releases::NewRelease { + repo_id: &fx.repo.id, + tag: "v3.0", + target_oid: &fx.main_tip(), + title: "draft", + body: "", + draft: true, + prerelease: false, + author_id: &fx.tahinli.id, + }) + .await + .expect("draft"); + git(&fx.bare, &["tag", "v3.0", "refs/heads/main"]); + let move_draft = EditRelease { + target: Some("next".to_owned()), + ..keep(&draft) + }; + let moved = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &draft, move_draft) + .await + .expect("draft moves"); + assert_eq!(moved.target_oid, next); + assert_eq!(fx.tag_target("v3.0"), Some(next)); + } + + // Retagging adopts an existing tag and refuses clashes and bad names. + { + git(&fx.bare, &["tag", "v0.9", "refs/heads/main"]); + let next = second_commit(&fx); + let draft = fx.release("v5", true).await; + + let clash_target = EditRelease { + tag: Some("v0.9".to_owned()), + target: Some("next".to_owned()), + ..keep(&draft) + }; + let refused = + release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &draft, clash_target).await; + assert!( + matches!(refused, Err(ReleaseError::Invalid(_))), + "{refused:?}" + ); + + let adopt = EditRelease { + tag: Some("v0.9".to_owned()), + ..keep(&draft) + }; + let adopted = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &draft, adopt) + .await + .expect("adopt"); + assert_eq!(adopted.tag, "v0.9"); + assert_eq!(adopted.target_oid, fx.main_tip()); + assert_ne!(adopted.target_oid, next); + + let other = fx.release("v6", false).await; + let taken = EditRelease { + tag: Some("v0.9".to_owned()), + ..keep(&other) + }; + let clash = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &other, taken).await; + assert!(matches!(clash, Err(ReleaseError::Conflict(_))), "{clash:?}"); + + let bad_name = EditRelease { + tag: Some("a/b".to_owned()), + ..keep(&other) + }; + let invalid = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &other, bad_name).await; + assert!( + matches!(invalid, Err(ReleaseError::Invalid(_))), + "{invalid:?}" + ); + } } #[tokio::test] -async fn a_second_upload_under_the_same_name_replaces_the_file() { +async fn the_edit_page_and_its_form_follow_the_publish_state() { let fx = fixture(None).await; - let rel = fx.release("v1.0", false).await; - let first = fx - .upload(&rel.id, "app.bin", b"first", &fx.tahinli_cookie) + + // The edit page shows tag and target controls, and hides the target once published. + { + let draft = fx.release("v1.0", true).await; + let published = fx.release("v2.0", false).await; + + let page = send( + &fx.app, + "GET", + &format!("/tahinli/proj/releases/{}/edit", draft.id), + Some(&fx.tahinli_cookie), + None, + Vec::new(), + ) .await; - assert_eq!(first.status, 303); - let before = fx - .store - .release_asset(&rel.id, "app.bin") - .await - .expect("read") - .expect("row"); - fx.store - .count_asset_download(&before.id) - .await - .expect("count"); + assert_eq!(page.status, 200, "body: {}", page.text()); + let html = page.text(); + assert!(html.contains("name=\"tag\" value=\"v1.0\""), "{html}"); + assert!(html.contains("name=\"target\""), "a draft shows the target"); - let second = fx - .upload(&rel.id, "app.bin", b"second bytes", &fx.tahinli_cookie) + let page = send( + &fx.app, + "GET", + &format!("/tahinli/proj/releases/{}/edit", published.id), + Some(&fx.tahinli_cookie), + None, + Vec::new(), + ) .await; - assert!( - second - .location - .unwrap_or_default() - .contains("notice=asset-replaced"), - "a same-name upload replaces instead of refusing" - ); - let assets = fx.store.list_release_assets(&rel.id).await.expect("assets"); - assert_eq!(assets.len(), 1, "still one asset of that name"); - let after = &assets[0]; - assert_ne!(after.id, before.id, "the new file has its own blob"); - assert_eq!(after.size, 12); - assert_eq!(after.download_count, 1, "the download counter survives"); - assert!( - fx.state - .blobs - .read(&asset_key(&before.id)) + let html = page.text(); + assert!(html.contains("name=\"tag\" value=\"v2.0\""), "{html}"); + assert!( + !html.contains("name=\"target\""), + "published hides the target" + ); + assert!( + html.contains("Published releases keep their commit"), + "{html}" + ); + } + + // A published release can be retagged through the form and keeps its commit. + { + git(&fx.bare, &["tag", "v4.0", "refs/heads/main"]); + let published = fx.release("v4.0", false).await; + let reply = send( + &fx.app, + "POST", + &format!("/tahinli/proj/releases/{}/edit", published.id), + Some(&fx.tahinli_cookie), + Some(FORM_TYPE), + form(&[ + ("csrf", CSRF), + ("tag", "v4.1"), + ("target", ""), + ("title", "Release v4.0"), + ("body", "notes"), + ]), + ) + .await; + assert_eq!(reply.status, 303, "body: {}", reply.text()); + assert!(reply.location.unwrap_or_default().contains("notice=saved")); + let moved = fx + .store + .release(&published.id) .await .expect("read") - .is_none(), - "the replaced blob is deleted" - ); - let got = send( - &fx.app, - "GET", - "/tahinli/proj/releases/download/v1.0/app.bin", - None, - None, - Vec::new(), - ) - .await; - assert_eq!(got.body, b"second bytes"); -} + .expect("row"); + assert_eq!(moved.tag, "v4.1"); + assert_eq!(fx.tag_target("v4.1"), Some(fx.main_tip())); + assert_eq!( + fx.tag_target("v4.0"), + Some(fx.main_tip()), + "old git tag kept" + ); + } -#[tokio::test] -async fn a_refused_replacement_keeps_the_old_file() { - let fx = fixture(Some(1)).await; - let rel = fx.release("v1.0", false).await; - fx.upload(&rel.id, "app.bin", b"keep me", &fx.tahinli_cookie) - .await; - let big = vec![7u8; 1024 * 1024 + 4096]; - let reply = fx - .upload(&rel.id, "app.bin", &big, &fx.tahinli_cookie) + // A published target move is refused on the page, and the typed values stay. + { + git(&fx.bare, &["tag", "v5.0", "refs/heads/main"]); + second_commit(&fx); + fx.release("v5.0", false).await; + let published = fx + .store + .release_by_tag(&fx.repo.id, "v5.0") + .await + .expect("read") + .expect("row"); + let reply = send( + &fx.app, + "POST", + &format!("/tahinli/proj/releases/{}/edit", published.id), + Some(&fx.tahinli_cookie), + Some(FORM_TYPE), + form(&[ + ("csrf", CSRF), + ("tag", "v5.0"), + ("target", "next"), + ("title", "Typed title"), + ("body", "notes"), + ]), + ) .await; - assert!( - reply - .location - .unwrap_or_default() - .contains("notice=asset-too-large") - ); - let row = fx - .store - .release_asset(&rel.id, "app.bin") - .await - .expect("read") - .expect("row"); - assert_eq!(row.size, 7); - let got = send( - &fx.app, - "GET", - "/tahinli/proj/releases/download/v1.0/app.bin", - None, - None, - Vec::new(), - ) - .await; - assert_eq!(got.body, b"keep me"); + assert_eq!( + reply.status, + 200, + "a refused save re-renders: {}", + reply.text() + ); + let html = reply.text(); + assert!(html.contains("pick a new tag"), "{html}"); + assert!( + html.contains("Typed title"), + "the typed title stays: {html}" + ); + assert_eq!(fx.tag_target("v5.0"), Some(fx.main_tip())); + } } #[tokio::test] -async fn a_published_release_moves_to_a_new_tag_and_keeps_the_old_git_tag() { +async fn asset_upload_replace_download_delete_and_csrf_rules() { let fx = fixture(None).await; - git(&fx.bare, &["tag", "v1.0", "refs/heads/main"]); - let rel = fx.release("v1.0", false).await; - let edit = EditRelease { - tag: Some("v1.1".to_owned()), - ..keep(&rel) - }; - let moved = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &rel, edit) - .await - .expect("retag"); - assert_eq!(moved.tag, "v1.1"); - assert_eq!(moved.target_oid, fx.main_tip()); - assert_eq!( - fx.tag_target("v1.1").as_deref(), - Some(fx.main_tip().as_str()) - ); - assert!(fx.tag_target("v1.0").is_some(), "the old git tag is kept"); - assert!( - fx.store - .release_by_tag(&fx.repo.id, "v1.0") + + // Upload then download round-trips the bytes and counts each download. + { + let rel = fx.release("v1.0", false).await; + let payload = b"not rendered"; + let reply = fx + .upload(&rel.id, "app.bin", payload, &fx.tahinli_cookie) + .await; + assert_eq!(reply.status, 303, "body: {}", reply.text()); + assert!( + reply + .location + .unwrap_or_default() + .contains("notice=asset-uploaded") + ); + + let row = fx + .store + .release_asset(&rel.id, "app.bin") .await .expect("read") - .is_none() - ); -} + .expect("asset row"); + assert_eq!(row.size, payload.len() as u64); + let digest = { + use sha2::{Digest, Sha256}; + hex::encode(Sha256::digest(payload)) + }; + assert_eq!(row.sha256, digest); + + for expected in 1..=2u64 { + let got = send( + &fx.app, + "GET", + "/tahinli/proj/releases/download/v1.0/app.bin", + None, + None, + Vec::new(), + ) + .await; + assert_eq!(got.status, 200); + assert_eq!(got.body, payload); + assert_eq!( + got.headers + .get("content-disposition") + .and_then(|v| v.to_str().ok()), + Some("attachment; filename=\"app.bin\"") + ); + assert_eq!( + got.headers + .get("x-content-type-options") + .and_then(|v| v.to_str().ok()), + Some("nosniff") + ); + let counted = fx + .store + .release_asset(&rel.id, "app.bin") + .await + .expect("read") + .expect("row"); + assert_eq!(counted.download_count, expected, "download counter"); + } + } -#[tokio::test] -async fn a_published_tag_cannot_be_moved_in_place_but_a_draft_can() { - let fx = fixture(None).await; - git(&fx.bare, &["tag", "v2.0", "refs/heads/main"]); - let next = second_commit(&fx); - let published = fx.release("v2.0", false).await; - let move_published = EditRelease { - target: Some("next".to_owned()), - ..keep(&published) - }; - let refused = - release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &published, move_published).await; - assert!( - matches!(refused, Err(ReleaseError::Invalid(_))), - "{refused:?}" - ); - assert_eq!( - fx.tag_target("v2.0").as_deref(), - Some(fx.main_tip().as_str()) - ); + // A same-name upload replaces the file and keeps its download counter. + { + let rel = fx.release("v1.1", false).await; + let first = fx + .upload(&rel.id, "app.bin", b"first", &fx.tahinli_cookie) + .await; + assert_eq!(first.status, 303); + let before = fx + .store + .release_asset(&rel.id, "app.bin") + .await + .expect("read") + .expect("row"); + fx.store + .count_asset_download(&before.id) + .await + .expect("count"); - let draft = fx - .store - .create_release(&forge_core::releases::NewRelease { - repo_id: &fx.repo.id, - tag: "v3.0", - target_oid: &fx.main_tip(), - title: "draft", - body: "", - draft: true, - prerelease: false, - author_id: &fx.tahinli.id, - }) - .await - .expect("draft"); - git(&fx.bare, &["tag", "v3.0", "refs/heads/main"]); - let move_draft = EditRelease { - target: Some("next".to_owned()), - ..keep(&draft) - }; - let moved = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &draft, move_draft) - .await - .expect("draft moves"); - assert_eq!(moved.target_oid, next); - assert_eq!(fx.tag_target("v3.0"), Some(next)); -} + let second = fx + .upload(&rel.id, "app.bin", b"second bytes", &fx.tahinli_cookie) + .await; + assert!( + second + .location + .unwrap_or_default() + .contains("notice=asset-replaced"), + "a same-name upload replaces instead of refusing" + ); + let assets = fx.store.list_release_assets(&rel.id).await.expect("assets"); + assert_eq!(assets.len(), 1, "still one asset of that name"); + let after = &assets[0]; + assert_ne!(after.id, before.id, "the new file has its own blob"); + assert_eq!(after.size, 12); + assert_eq!(after.download_count, 1, "the download counter survives"); + assert!( + fx.state + .blobs + .read(&asset_key(&before.id)) + .await + .expect("read") + .is_none(), + "the replaced blob is deleted" + ); + let got = send( + &fx.app, + "GET", + "/tahinli/proj/releases/download/v1.1/app.bin", + None, + None, + Vec::new(), + ) + .await; + assert_eq!(got.body, b"second bytes"); + } -#[tokio::test] -async fn retagging_adopts_an_existing_tag_and_refuses_clashes() { - let fx = fixture(None).await; - git(&fx.bare, &["tag", "v0.9", "refs/heads/main"]); - let next = second_commit(&fx); - let draft = fx.release("v5", true).await; - - let clash_target = EditRelease { - tag: Some("v0.9".to_owned()), - target: Some("next".to_owned()), - ..keep(&draft) - }; - let refused = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &draft, clash_target).await; - assert!( - matches!(refused, Err(ReleaseError::Invalid(_))), - "{refused:?}" - ); + // Replacing by asset name keeps the name, whatever the new file is called. + { + let rel = fx.release("v1.2", false).await; + let first = fx + .upload(&rel.id, "app.bin", b"first", &fx.tahinli_cookie) + .await; + assert_eq!(first.status, 303); - let adopt = EditRelease { - tag: Some("v0.9".to_owned()), - ..keep(&draft) - }; - let adopted = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &draft, adopt) - .await - .expect("adopt"); - assert_eq!(adopted.tag, "v0.9"); - assert_eq!(adopted.target_oid, fx.main_tip()); - assert_ne!(adopted.target_oid, next); - - let other = fx.release("v6", false).await; - let taken = EditRelease { - tag: Some("v0.9".to_owned()), - ..keep(&other) - }; - let clash = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &other, taken).await; - assert!(matches!(clash, Err(ReleaseError::Conflict(_))), "{clash:?}"); - - let bad_name = EditRelease { - tag: Some("a/b".to_owned()), - ..keep(&other) - }; - let invalid = release_ops::update(&fx.state, &fx.repo, &fx.tahinli, &other, bad_name).await; - assert!( - matches!(invalid, Err(ReleaseError::Invalid(_))), - "{invalid:?}" - ); -} + let page = send( + &fx.app, + "GET", + &format!("/tahinli/proj/releases/{}/edit", rel.id), + Some(&fx.tahinli_cookie), + None, + Vec::new(), + ) + .await; + assert!( + page.text().contains("name=\"name\" value=\"app.bin\""), + "each asset has a replace form" + ); -/// Create refuses a bad tag or a missing target before touching git, and takes a commit id as a -/// target. -#[tokio::test] -async fn create_validates_its_tag_and_target() { - let fx = fixture(None).await; - let input = |tag: &str, target: &str| CreateRelease { - tag: tag.to_owned(), - target: Some(target.to_owned()), - title: "x".to_owned(), - body: String::new(), - draft: false, - prerelease: false, - }; - let refused = release_ops::create(&fx.state, &fx.repo, &fx.tahinli, input("a/b", "main")).await; - assert!( - matches!(refused, Err(ReleaseError::Invalid(_))), - "bad tag: {refused:?}" - ); - assert!(fx.tag_target("a/b").is_none(), "bad tag"); + let (content_type, body) = crate::pages::settings_profile::tests::multipart(&[ + ("csrf", None, CSRF.as_bytes()), + ("name", None, "app.bin".as_bytes()), + ( + "file", + Some(("renamed-build.zip", "application/octet-stream")), + "second bytes".as_bytes(), + ), + ]); + let reply = send( + &fx.app, + "POST", + &format!("/tahinli/proj/releases/{}/assets", rel.id), + Some(&fx.tahinli_cookie), + Some(&content_type), + body, + ) + .await; + assert_eq!(reply.status, 303, "body: {}", reply.text()); + assert!( + reply + .location + .unwrap_or_default() + .contains("notice=asset-replaced") + ); + let assets = fx.store.list_release_assets(&rel.id).await.expect("assets"); + assert_eq!(assets.len(), 1, "still one asset"); + assert_eq!(assets[0].name, "app.bin"); + assert_eq!(assets[0].size, 12); + let got = send( + &fx.app, + "GET", + "/tahinli/proj/releases/download/v1.2/app.bin", + None, + None, + Vec::new(), + ) + .await; + assert_eq!(got.body, b"second bytes"); + } - let tip = fx.main_tip(); - let made = release_ops::create(&fx.state, &fx.repo, &fx.tahinli, input("v7", &tip)) - .await - .expect("a commit id works as a target"); - assert_eq!(made.target_oid, tip, "commit id target"); - assert_eq!( - fx.tag_target("v7").as_deref(), - Some(tip.as_str()), - "commit id target" - ); + // A wrong CSRF token on upload is refused and stores nothing. + { + let rel = fx.release("v1.3", false).await; + let (content_type, body) = crate::pages::settings_profile::tests::multipart(&[ + ("csrf", None, b"wrong"), + ("file", Some(("a.bin", "application/octet-stream")), b"data"), + ]); + let reply = send( + &fx.app, + "POST", + &format!("/tahinli/proj/releases/{}/assets", rel.id), + Some(&fx.tahinli_cookie), + Some(&content_type), + body, + ) + .await; + assert_ne!(reply.status, 303, "csrf mismatch"); + assert!( + fx.store + .list_release_assets(&rel.id) + .await + .expect("assets") + .is_empty() + ); + } - let missing = release_ops::create( - &fx.state, - &fx.repo, - &fx.tahinli, - input("v8", "1111111111111111111111111111111111111111"), - ) - .await; - assert!( - matches!(missing, Err(ReleaseError::Invalid(_))), - "missing target: {missing:?}" - ); - assert!(fx.tag_target("v8").is_none(), "missing target"); -} + // Deleting a release keeps its git tag and removes its assets and blobs. + { + git(&fx.bare, &["tag", "v1.4", "refs/heads/main"]); + let rel = fx.release("v1.4", false).await; + let up = fx + .upload(&rel.id, "gone.bin", b"bytes", &fx.tahinli_cookie) + .await; + assert_eq!(up.status, 303); + let asset = fx + .store + .release_asset(&rel.id, "gone.bin") + .await + .expect("read") + .expect("row"); + let blob = crate::blobs::asset_key(&asset.id); + assert!(fx.state.blobs.read(&blob).await.expect("read").is_some()); -#[tokio::test] -async fn the_edit_page_shows_tag_and_target_controls_and_hides_the_target_once_published() { - let fx = fixture(None).await; - let draft = fx.release("v1.0", true).await; - let published = fx.release("v2.0", false).await; - - let page = send( - &fx.app, - "GET", - &format!("/tahinli/proj/releases/{}/edit", draft.id), - Some(&fx.tahinli_cookie), - None, - Vec::new(), - ) - .await; - assert_eq!(page.status, 200, "body: {}", page.text()); - let html = page.text(); - assert!(html.contains("name=\"tag\" value=\"v1.0\""), "{html}"); - assert!(html.contains("name=\"target\""), "a draft shows the target"); - - let page = send( - &fx.app, - "GET", - &format!("/tahinli/proj/releases/{}/edit", published.id), - Some(&fx.tahinli_cookie), - None, - Vec::new(), - ) - .await; - let html = page.text(); - assert!(html.contains("name=\"tag\" value=\"v2.0\""), "{html}"); - assert!( - !html.contains("name=\"target\""), - "published hides the target" - ); - assert!( - html.contains("Published releases keep their commit"), - "{html}" - ); + let reply = send( + &fx.app, + "POST", + &format!("/tahinli/proj/releases/{}/delete", rel.id), + Some(&fx.tahinli_cookie), + Some(FORM_TYPE), + form(&[("csrf", CSRF), ("confirm", "on")]), + ) + .await; + assert_eq!(reply.status, 303); + assert!(fx.store.release(&rel.id).await.expect("read").is_none()); + assert!(fx.tag_target("v1.4").is_some(), "the git tag survives"); + assert!( + fx.state.blobs.read(&blob).await.expect("read").is_none(), + "the blob is gone" + ); + let download = send( + &fx.app, + "GET", + "/tahinli/proj/releases/download/v1.4/gone.bin", + None, + None, + Vec::new(), + ) + .await; + assert_eq!(download.status, 404, "a deleted asset does not download"); + } } #[tokio::test] -async fn a_published_release_can_be_retagged_through_the_form_and_keeps_its_commit() { - let fx = fixture(None).await; - git(&fx.bare, &["tag", "v2.0", "refs/heads/main"]); - let published = fx.release("v2.0", false).await; - let reply = send( - &fx.app, - "POST", - &format!("/tahinli/proj/releases/{}/edit", published.id), - Some(&fx.tahinli_cookie), - Some(FORM_TYPE), - form(&[ - ("csrf", CSRF), - ("tag", "v2.1"), - ("target", ""), - ("title", "Release v2.0"), - ("body", "notes"), - ]), - ) - .await; - assert_eq!(reply.status, 303, "body: {}", reply.text()); - assert!(reply.location.unwrap_or_default().contains("notice=saved")); - let moved = fx - .store - .release(&published.id) - .await - .expect("read") - .expect("row"); - assert_eq!(moved.tag, "v2.1"); - assert_eq!(fx.tag_target("v2.1"), Some(fx.main_tip())); - assert_eq!( - fx.tag_target("v2.0"), - Some(fx.main_tip()), - "old git tag kept" - ); -} +async fn the_size_cap_refuses_new_and_replacement_uploads() { + let fx = fixture(Some(1)).await; + let big = vec![7u8; 1024 * 1024 + 4096]; -#[tokio::test] -async fn a_published_target_move_is_refused_on_the_page_and_the_typed_values_stay() { - let fx = fixture(None).await; - git(&fx.bare, &["tag", "v2.0", "refs/heads/main"]); - second_commit(&fx); - let published = fx.release("v2.0", false).await; - let reply = send( - &fx.app, - "POST", - &format!("/tahinli/proj/releases/{}/edit", published.id), - Some(&fx.tahinli_cookie), - Some(FORM_TYPE), - form(&[ - ("csrf", CSRF), - ("tag", "v2.0"), - ("target", "next"), - ("title", "Typed title"), - ("body", "notes"), - ]), - ) - .await; - assert_eq!( - reply.status, - 200, - "a refused save re-renders: {}", - reply.text() - ); - let html = reply.text(); - assert!(html.contains("pick a new tag"), "{html}"); - assert!( - html.contains("Typed title"), - "the typed title stays: {html}" - ); - assert_eq!(fx.tag_target("v2.0"), Some(fx.main_tip())); -} + // Runs first: no blob may exist yet, so the blob directory must stay empty. + { + let rel = fx.release("v1.0", false).await; + let reply = fx + .upload(&rel.id, "big.bin", &big, &fx.tahinli_cookie) + .await; + assert_eq!(reply.status, 303); + assert!( + reply + .location + .unwrap_or_default() + .contains("notice=asset-too-large"), + "the cap refuses the file" + ); + assert!( + fx.store + .list_release_assets(&rel.id) + .await + .expect("assets") + .is_empty() + ); + let assets_dir = fx.state.config.data_dir.join("blobs").join("assets"); + let leftovers = std::fs::read_dir(&assets_dir) + .map(|entries| entries.count()) + .unwrap_or(0); + assert_eq!(leftovers, 0, "no partial blob is left behind"); + } -#[tokio::test] -async fn replace_keeps_the_asset_name_whatever_the_new_file_is_called() { - let fx = fixture(None).await; - let rel = fx.release("v1.0", false).await; - let first = fx - .upload(&rel.id, "app.bin", b"first", &fx.tahinli_cookie) + // A refused replacement keeps the old file. + { + let rel = fx.release("v1.1", false).await; + fx.upload(&rel.id, "app.bin", b"keep me", &fx.tahinli_cookie) + .await; + let reply = fx + .upload(&rel.id, "app.bin", &big, &fx.tahinli_cookie) + .await; + assert!( + reply + .location + .unwrap_or_default() + .contains("notice=asset-too-large"), + "an oversized replacement is refused" + ); + let row = fx + .store + .release_asset(&rel.id, "app.bin") + .await + .expect("read") + .expect("row"); + assert_eq!(row.size, 7, "the old size stays"); + let got = send( + &fx.app, + "GET", + "/tahinli/proj/releases/download/v1.1/app.bin", + None, + None, + Vec::new(), + ) .await; - assert_eq!(first.status, 303); - - let page = send( - &fx.app, - "GET", - &format!("/tahinli/proj/releases/{}/edit", rel.id), - Some(&fx.tahinli_cookie), - None, - Vec::new(), - ) - .await; - assert!( - page.text().contains("name=\"name\" value=\"app.bin\""), - "each asset has a replace form" - ); - - let (content_type, body) = crate::pages::settings_profile::tests::multipart(&[ - ("csrf", None, CSRF.as_bytes()), - ("name", None, "app.bin".as_bytes()), - ( - "file", - Some(("renamed-build.zip", "application/octet-stream")), - "second bytes".as_bytes(), - ), - ]); - let reply = send( - &fx.app, - "POST", - &format!("/tahinli/proj/releases/{}/assets", rel.id), - Some(&fx.tahinli_cookie), - Some(&content_type), - body, - ) - .await; - assert_eq!(reply.status, 303, "body: {}", reply.text()); - assert!( - reply - .location - .unwrap_or_default() - .contains("notice=asset-replaced") - ); - let assets = fx.store.list_release_assets(&rel.id).await.expect("assets"); - assert_eq!(assets.len(), 1, "still one asset"); - assert_eq!(assets[0].name, "app.bin"); - assert_eq!(assets[0].size, 12); - let got = send( - &fx.app, - "GET", - "/tahinli/proj/releases/download/v1.0/app.bin", - None, - None, - Vec::new(), - ) - .await; - assert_eq!(got.body, b"second bytes"); + assert_eq!(got.body, b"keep me", "the old bytes stay"); + } } /// The live streams of the release list and a release page: a change from another session wakes diff --git a/crates/forge-web/src/pages/repo.rs b/crates/forge-web/src/pages/repo.rs index 7c51cb2..1f22639 100644 --- a/crates/forge-web/src/pages/repo.rs +++ b/crates/forge-web/src/pages/repo.rs @@ -635,10 +635,15 @@ mod tests { (status, String::from_utf8_lossy(&bytes).into_owned()) } + // The read-only tests below share one fixture: they only read the demo repo, so each scenario + // sees the same pristine contents. + #[tokio::test] - async fn home_renders_tree_clone_popover_and_readme() { + async fn demo_pages_render_from_the_repo_contents() { let dir = tempfile::tempdir().unwrap(); let (router, _) = fixture(dir.path()).await; + + // Home renders the README, the tree, the clone popover and the archive links. let (status, body) = get(&router, "/tahinli/demo").await; assert_eq!(status, 200); assert!(body.contains("Hello from the README"), "README rendered"); @@ -658,12 +663,8 @@ mod tests { ); assert!(body.contains("/tahinli/demo/archive/main.tar.gz")); assert!(body.contains("/tahinli/demo/branches")); - } - #[tokio::test] - async fn tree_lists_entries() { - let dir = tempfile::tempdir().unwrap(); - let (router, _) = fixture(dir.path()).await; + // Trees list their entries and link to subtrees and blobs. let (status, body) = get(&router, "/tahinli/demo/tree/main/src").await; assert_eq!(status, 200); assert!(body.contains("main.rs")); @@ -671,24 +672,16 @@ mod tests { let (status, body) = get(&router, "/tahinli/demo/tree/main").await; assert_eq!(status, 200); assert!(body.contains("/tahinli/demo/tree/main/docs")); - } - #[tokio::test] - async fn blob_highlights_rust_source() { - let dir = tempfile::tempdir().unwrap(); - let (router, _) = fixture(dir.path()).await; + // Rust source is highlighted with line anchors. let (status, body) = get(&router, "/tahinli/demo/blob/main/src/main.rs").await; assert_eq!(status, 200); assert!(body.contains("id=\"L1\"")); assert!(body.contains("href=\"#L2\"")); assert!(body.contains("