The `metadata` JSON part in the manga-create and chapter-upload handlers was read with `Field::bytes()`, buffering the whole part before any check — a client could send a multi-hundred-MB metadata blob (up to the 200 MiB request limit) as one allocation. Image parts were already streamed with a cap; metadata was the last unbounded read. Cap it at a shared upload::MAX_METADATA_BYTES (64 KiB) via the read_capped streaming loop (413 on overflow), and remove the now-unused read_field_bytes helper so the unbounded path can't return. Tests: manga + chapter oversized-metadata parts are rejected with 413. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
385 lines
14 KiB
Rust
385 lines
14 KiB
Rust
mod common;
|
|
|
|
use axum::http::StatusCode;
|
|
use serde_json::json;
|
|
use sqlx::PgPool;
|
|
use tower::ServiceExt;
|
|
use uuid::Uuid;
|
|
#[allow(unused_imports)]
|
|
use serde_json as _;
|
|
|
|
use common::MultipartBuilder;
|
|
|
|
async fn seed_manga(h: &common::Harness, cookie: &str, title: &str) -> Uuid {
|
|
common::seed_manga_via_api(&h.app, cookie, title).await
|
|
}
|
|
|
|
async fn seed_chapter(
|
|
pool: &PgPool,
|
|
manga_id: Uuid,
|
|
number: i32,
|
|
title: Option<&str>,
|
|
) -> Uuid {
|
|
// Historical seed — uploaded_by remains NULL, mirroring the
|
|
// pre-Phase-5 rows in the production DB.
|
|
mangalord::repo::chapter::create(pool, manga_id, number, title, None)
|
|
.await
|
|
.unwrap()
|
|
.id
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn list_chapters_is_empty_initially(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let manga_id = seed_manga(&h, &cookie, "Berserk").await;
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!("/api/v1/mangas/{manga_id}/chapters")))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
let body = common::body_json(resp).await;
|
|
assert_eq!(body["items"], json!([]));
|
|
assert_eq!(body["page"]["limit"], 50);
|
|
assert_eq!(body["page"]["offset"], 0);
|
|
assert!(body["page"]["total"].is_null());
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn uncrawled_chapter_reports_zero_storage(pool: PgPool) {
|
|
// A page-less chapter (page_count = 0, no page rows) is "uncrawled"
|
|
// — its size_bytes is 0, which the frontend renders as an em-dash.
|
|
let h = common::harness(pool.clone());
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let manga_id = seed_manga(&h, &cookie, "Berserk").await;
|
|
seed_chapter(&pool, manga_id, 1, Some("Unfetched")).await;
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!("/api/v1/mangas/{manga_id}/chapters")))
|
|
.await
|
|
.unwrap();
|
|
let body = common::body_json(resp).await;
|
|
assert_eq!(body["items"][0]["page_count"].as_i64().unwrap(), 0);
|
|
assert_eq!(body["items"][0]["size_bytes"].as_i64().unwrap(), 0);
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn chapter_with_unmeasured_page_reports_null_storage(pool: PgPool) {
|
|
// A crawled chapter whose pages predate the size backfill (size_bytes
|
|
// IS NULL) must report size_bytes = null — "unknown" — so the frontend
|
|
// shows an em-dash, not a misleading "0 B".
|
|
let h = common::harness(pool.clone());
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let manga_id = seed_manga(&h, &cookie, "Berserk").await;
|
|
let chapter_id = seed_chapter(&pool, manga_id, 1, Some("Crawled")).await;
|
|
// One measured page, one unmeasured (NULL) → whole chapter is unknown.
|
|
sqlx::query(
|
|
"INSERT INTO pages (chapter_id, page_number, storage_key, content_type, size_bytes) \
|
|
VALUES ($1, 1, 'k/1.jpg', 'image/jpeg', 100)",
|
|
)
|
|
.bind(chapter_id)
|
|
.execute(&pool)
|
|
.await
|
|
.unwrap();
|
|
sqlx::query(
|
|
"INSERT INTO pages (chapter_id, page_number, storage_key, content_type) \
|
|
VALUES ($1, 2, 'k/2.jpg', 'image/jpeg')",
|
|
)
|
|
.bind(chapter_id)
|
|
.execute(&pool)
|
|
.await
|
|
.unwrap();
|
|
|
|
let resp = h
|
|
.app
|
|
.clone()
|
|
.oneshot(common::get(&format!("/api/v1/mangas/{manga_id}/chapters")))
|
|
.await
|
|
.unwrap();
|
|
let body = common::body_json(resp).await;
|
|
assert!(
|
|
body["items"][0]["size_bytes"].is_null(),
|
|
"unmeasured page should make the chapter size unknown (null), got {}",
|
|
body["items"][0]["size_bytes"]
|
|
);
|
|
|
|
// The manga detail total is likewise unknown (null), not a silent
|
|
// undercount of just the measured page.
|
|
let detail = h
|
|
.app
|
|
.oneshot(common::get(&format!("/api/v1/mangas/{manga_id}")))
|
|
.await
|
|
.unwrap();
|
|
let detail = common::body_json(detail).await;
|
|
assert!(
|
|
detail["chapter_storage_bytes"].is_null(),
|
|
"manga total should be unknown when a page is unmeasured, got {}",
|
|
detail["chapter_storage_bytes"]
|
|
);
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn list_chapters_returned_in_number_order(pool: PgPool) {
|
|
let h = common::harness(pool.clone());
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let manga_id = seed_manga(&h, &cookie, "Berserk").await;
|
|
|
|
seed_chapter(&pool, manga_id, 3, Some("The Black Swordsman")).await;
|
|
seed_chapter(&pool, manga_id, 1, Some("The Brand")).await;
|
|
seed_chapter(&pool, manga_id, 2, None).await;
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!("/api/v1/mangas/{manga_id}/chapters")))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
let body = common::body_json(resp).await;
|
|
let numbers: Vec<i64> = body["items"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|c| c["number"].as_i64().unwrap())
|
|
.collect();
|
|
assert_eq!(numbers, vec![1, 2, 3]);
|
|
assert_eq!(body["items"][1]["title"], serde_json::Value::Null);
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn list_interleaves_uploaded_and_crawled_chapters_by_number(pool: PgPool) {
|
|
// Mixed source: crawled chapters carry a `source_index`; an uploaded
|
|
// chapter has none. The uploaded chapter must interleave by NUMBER, not sort
|
|
// after every crawled chapter (the old `source_index DESC NULLS LAST` bug
|
|
// dumped uploaded chapter 2 to the end, giving [1, 3, 2]).
|
|
let h = common::harness(pool.clone());
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let manga_id = seed_manga(&h, &cookie, "Berserk").await;
|
|
|
|
// Two crawled chapters (1 and 3) with DOM positions — newest-first, so
|
|
// chapter 3 is at source_index 0 and chapter 1 at source_index 1.
|
|
let c1 = seed_chapter(&pool, manga_id, 1, Some("Crawled One")).await;
|
|
let c3 = seed_chapter(&pool, manga_id, 3, Some("Crawled Three")).await;
|
|
sqlx::query("UPDATE chapters SET source_index = 1 WHERE id = $1")
|
|
.bind(c1)
|
|
.execute(&pool)
|
|
.await
|
|
.unwrap();
|
|
sqlx::query("UPDATE chapters SET source_index = 0 WHERE id = $1")
|
|
.bind(c3)
|
|
.execute(&pool)
|
|
.await
|
|
.unwrap();
|
|
// Uploaded chapter 2 — no source_index.
|
|
seed_chapter(&pool, manga_id, 2, Some("Uploaded Two")).await;
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!("/api/v1/mangas/{manga_id}/chapters")))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
let body = common::body_json(resp).await;
|
|
let numbers: Vec<i64> = body["items"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|c| c["number"].as_i64().unwrap())
|
|
.collect();
|
|
assert_eq!(numbers, vec![1, 2, 3], "uploaded chapter 2 must slot between 1 and 3");
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn list_chapters_returns_404_for_unknown_manga(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let unknown = Uuid::nil();
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!("/api/v1/mangas/{unknown}/chapters")))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
let body = common::body_json(resp).await;
|
|
assert_eq!(body["error"]["code"], "not_found");
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn get_chapter_by_id(pool: PgPool) {
|
|
let h = common::harness(pool.clone());
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let manga_id = seed_manga(&h, &cookie, "Berserk").await;
|
|
let chapter_id = seed_chapter(&pool, manga_id, 1, Some("The Brand")).await;
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!(
|
|
"/api/v1/mangas/{manga_id}/chapters/{chapter_id}"
|
|
)))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
let body = common::body_json(resp).await;
|
|
assert_eq!(body["number"], 1);
|
|
assert_eq!(body["title"], "The Brand");
|
|
assert_eq!(body["page_count"], 0);
|
|
assert_eq!(body["id"], chapter_id.to_string());
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn get_chapter_unknown_id_is_404(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let manga_id = seed_manga(&h, &cookie, "Berserk").await;
|
|
let unknown_chapter = Uuid::new_v4();
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!(
|
|
"/api/v1/mangas/{manga_id}/chapters/{unknown_chapter}"
|
|
)))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
let body = common::body_json(resp).await;
|
|
assert_eq!(body["error"]["code"], "not_found");
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn get_chapter_unknown_manga_is_404(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let unknown_manga = Uuid::nil();
|
|
let unknown_chapter = Uuid::new_v4();
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!(
|
|
"/api/v1/mangas/{unknown_manga}/chapters/{unknown_chapter}"
|
|
)))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
}
|
|
|
|
/// Cross-manga isolation: a chapter id belonging to manga A must not
|
|
/// resolve when accessed via manga B's URL. The (manga_id, id) scoping
|
|
/// in `find_by_id_in_manga` enforces this.
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn get_chapter_from_wrong_manga_is_404(pool: PgPool) {
|
|
let h = common::harness(pool.clone());
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let manga_a = seed_manga(&h, &cookie, "Berserk").await;
|
|
let manga_b = seed_manga(&h, &cookie, "Vagabond").await;
|
|
let chapter_id = seed_chapter(&pool, manga_a, 1, Some("Episode 1")).await;
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!(
|
|
"/api/v1/mangas/{manga_b}/chapters/{chapter_id}"
|
|
)))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn list_pages_empty_for_chapter_without_upload(pool: PgPool) {
|
|
let h = common::harness(pool.clone());
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let manga_id = seed_manga(&h, &cookie, "Berserk").await;
|
|
let chapter_id = seed_chapter(&pool, manga_id, 1, None).await;
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!(
|
|
"/api/v1/mangas/{manga_id}/chapters/{chapter_id}/pages"
|
|
)))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
let body = common::body_json(resp).await;
|
|
assert_eq!(body["pages"], json!([]));
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn list_pages_returns_404_for_unknown_chapter(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let manga_id = seed_manga(&h, &cookie, "Berserk").await;
|
|
let unknown_chapter = Uuid::new_v4();
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!(
|
|
"/api/v1/mangas/{manga_id}/chapters/{unknown_chapter}/pages"
|
|
)))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn non_owner_can_upload_chapter(pool: PgPool) {
|
|
// Contract lock: chapter upload is INTENTIONALLY open to any
|
|
// authenticated user, not just the manga's creator. A user who did not
|
|
// create the manga can still contribute a chapter (community
|
|
// contributions), unlike manga-record edits which gate on ownership.
|
|
// If this ever needs to become owner-only, that's a deliberate change —
|
|
// this test (and the doc comment on `api::chapters::create`) should be
|
|
// updated together, not silently broken.
|
|
let h = common::harness(pool);
|
|
|
|
// Owner creates the manga.
|
|
let (_owner, owner_cookie) = common::register_user(&h.app).await;
|
|
let manga_id = seed_manga(&h, &owner_cookie, "Berserk").await;
|
|
|
|
// A different, non-owner user uploads a chapter to it.
|
|
let (_other, other_cookie) = common::register_user(&h.app).await;
|
|
let resp = h
|
|
.app
|
|
.clone()
|
|
.oneshot(common::post_multipart_with_cookie(
|
|
&format!("/api/v1/mangas/{manga_id}/chapters"),
|
|
MultipartBuilder::new()
|
|
.add_json("metadata", json!({ "number": 1, "title": "Contributed" }))
|
|
.add_file("page", "1.png", "image/png", &common::fake_png_bytes()),
|
|
&other_cookie,
|
|
))
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
resp.status(),
|
|
StatusCode::CREATED,
|
|
"a non-owner authenticated user must be able to upload a chapter"
|
|
);
|
|
let body = common::body_json(resp).await;
|
|
assert_eq!(body["number"], 1);
|
|
assert_eq!(body["title"], "Contributed");
|
|
assert_eq!(body["page_count"], 1);
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn chapter_upload_rejects_oversized_metadata_part(pool: PgPool) {
|
|
// The chapter `metadata` JSON part is capped as bytes arrive, just like the
|
|
// manga one, so a client can't buffer a huge JSON blob in memory. A ~200 KiB
|
|
// title blows the metadata cap before any page is staged.
|
|
let h = common::harness(pool);
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let manga_id = seed_manga(&h, &cookie, "Berserk").await;
|
|
|
|
let huge = "A".repeat(200 * 1024);
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::post_multipart_with_cookie(
|
|
&format!("/api/v1/mangas/{manga_id}/chapters"),
|
|
MultipartBuilder::new()
|
|
.add_json("metadata", json!({ "number": 1, "title": huge }))
|
|
.add_file("page", "1.png", "image/png", &common::fake_png_bytes()),
|
|
&cookie,
|
|
))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE);
|
|
}
|