feat: manga metadata with status, authors, genres, tags, and search filters (0.15.0)
Adds first-class manga metadata across the stack: - **Status** (ongoing / completed), **alternative titles**, normalized **multi-author** support, **curated genres** (13 seeded), and **free-form user tags** (case-insensitive, globally shared). Each is modelled as its own table joined to mangas; `mangas.author` is backfilled into `authors` + `manga_authors` and dropped. - New endpoints: `PATCH /v1/mangas/:id` (three-state `description`), `POST/DELETE /v1/mangas/:id/tags[/:tag_id]`, `GET /v1/genres`, `GET /v1/tags?search=`. - `GET /v1/mangas` now returns `MangaCard` (with authors + genres batched in) and supports `?status=`, `?author_id=`, `?genre_id=`, `?tag_id=` filters — AND across facets, with empty-array no-op semantics for the unnest primitive. - `GET /v1/mangas/:id` returns the enriched `MangaDetail` with tags. - Frontend: reusable `Chip` component; manga detail page renders authors as chips linking to `/authors/:id` (Phase 2), a status badge, alt titles, genres, and tags with inline add/remove (only the attacher sees remove); upload form supports multi-author / multi-genre / alt titles / status; search page gets a collapsible URL-synced filter panel with keyboard-navigable tag autocomplete. - 126 backend tests (incl. AND-across-facets primitive, case-insens author/tag de-dup, transactional create rollback, PATCH semantics for missing / null / set on description); 72 frontend tests + svelte-check clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
35
backend/tests/api_genres.rs
Normal file
35
backend/tests/api_genres.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
mod common;
|
||||
|
||||
use axum::http::StatusCode;
|
||||
use sqlx::PgPool;
|
||||
use tower::ServiceExt;
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn lists_seeded_genres(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::get("/api/v1/genres"))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
let body = common::body_json(resp).await;
|
||||
let items = body.as_array().expect("genres returned as a flat array");
|
||||
let names: Vec<&str> = items
|
||||
.iter()
|
||||
.map(|g| g["name"].as_str().unwrap())
|
||||
.collect();
|
||||
// The migration seeds a curated vocabulary; spot-check a few
|
||||
// common ones so accidental removals fail loudly.
|
||||
for expected in ["Action", "Comedy", "Romance", "Sci-Fi"] {
|
||||
assert!(
|
||||
names.contains(&expected),
|
||||
"expected seeded genre {expected:?} in {names:?}"
|
||||
);
|
||||
}
|
||||
// Every genre must carry an id so the create/patch endpoints have
|
||||
// something to reference.
|
||||
for g in items {
|
||||
assert!(g["id"].as_str().is_some());
|
||||
}
|
||||
}
|
||||
@@ -158,7 +158,11 @@ async fn create_then_list_roundtrip(pool: PgPool) {
|
||||
"/api/v1/mangas",
|
||||
MultipartBuilder::new().add_json(
|
||||
"metadata",
|
||||
json!({ "title": "Berserk", "author": "Kentaro Miura", "description": null }),
|
||||
json!({
|
||||
"title": "Berserk",
|
||||
"authors": ["Kentaro Miura"],
|
||||
"description": null,
|
||||
}),
|
||||
),
|
||||
&cookie,
|
||||
))
|
||||
@@ -167,14 +171,23 @@ async fn create_then_list_roundtrip(pool: PgPool) {
|
||||
assert_eq!(created.status(), StatusCode::CREATED);
|
||||
let body = common::body_json(created).await;
|
||||
assert_eq!(body["title"], "Berserk");
|
||||
assert_eq!(body["author"], "Kentaro Miura");
|
||||
let authors = body["authors"].as_array().unwrap();
|
||||
assert_eq!(authors.len(), 1);
|
||||
assert_eq!(authors[0]["name"], "Kentaro Miura");
|
||||
assert!(body["id"].as_str().is_some());
|
||||
// Status defaults to ongoing; tags and genres start empty.
|
||||
assert_eq!(body["status"], "ongoing");
|
||||
assert_eq!(body["genres"], json!([]));
|
||||
assert_eq!(body["tags"], json!([]));
|
||||
|
||||
let listed = h.app.oneshot(common::get("/api/v1/mangas")).await.unwrap();
|
||||
let listed_body = common::body_json(listed).await;
|
||||
let items = listed_body["items"].as_array().unwrap();
|
||||
assert_eq!(items.len(), 1);
|
||||
assert_eq!(items[0]["title"], "Berserk");
|
||||
// List endpoint returns the card shape: authors + genres included.
|
||||
let authors = items[0]["authors"].as_array().unwrap();
|
||||
assert_eq!(authors[0]["name"], "Kentaro Miura");
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
@@ -193,13 +206,15 @@ async fn search_filters_by_title_and_author(pool: PgPool) {
|
||||
.oneshot(common::post_multipart_with_cookie(
|
||||
"/api/v1/mangas",
|
||||
MultipartBuilder::new()
|
||||
.add_json("metadata", json!({ "title": title, "author": author })),
|
||||
.add_json("metadata", json!({ "title": title, "authors": [author] })),
|
||||
&cookie,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
// Searching by author name still works — the list query joins
|
||||
// authors so 'miura' resolves through the manga_authors table.
|
||||
let resp = h
|
||||
.app
|
||||
.clone()
|
||||
|
||||
568
backend/tests/api_mangas_metadata.rs
Normal file
568
backend/tests/api_mangas_metadata.rs
Normal file
@@ -0,0 +1,568 @@
|
||||
mod common;
|
||||
|
||||
use axum::http::StatusCode;
|
||||
use serde_json::{json, Value};
|
||||
use sqlx::PgPool;
|
||||
use tower::ServiceExt;
|
||||
use uuid::Uuid;
|
||||
|
||||
use common::MultipartBuilder;
|
||||
|
||||
async fn genre_id_named(app: &axum::Router, name: &str) -> String {
|
||||
let resp = app
|
||||
.clone()
|
||||
.oneshot(common::get("/api/v1/genres"))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
let body = common::body_json(resp).await;
|
||||
body.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.find(|g| g["name"].as_str() == Some(name))
|
||||
.and_then(|g| g["id"].as_str().map(str::to_string))
|
||||
.unwrap_or_else(|| panic!("expected seeded genre {name}"))
|
||||
}
|
||||
|
||||
async fn create_manga(
|
||||
app: &axum::Router,
|
||||
cookie: &str,
|
||||
metadata: Value,
|
||||
) -> Value {
|
||||
let resp = app
|
||||
.clone()
|
||||
.oneshot(common::post_multipart_with_cookie(
|
||||
"/api/v1/mangas",
|
||||
MultipartBuilder::new().add_json("metadata", metadata),
|
||||
cookie,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
resp.status(),
|
||||
StatusCode::CREATED,
|
||||
"create_manga failed: {:?}",
|
||||
resp.status()
|
||||
);
|
||||
common::body_json(resp).await
|
||||
}
|
||||
|
||||
fn id_of(body: &Value) -> Uuid {
|
||||
Uuid::parse_str(body["id"].as_str().unwrap()).unwrap()
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn create_returns_enriched_detail_with_defaults(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
|
||||
let body = create_manga(&h.app, &cookie, json!({ "title": "Solo Manga" })).await;
|
||||
assert_eq!(body["title"], "Solo Manga");
|
||||
assert_eq!(body["status"], "ongoing");
|
||||
assert_eq!(body["alt_titles"], json!([]));
|
||||
assert_eq!(body["authors"], json!([]));
|
||||
assert_eq!(body["genres"], json!([]));
|
||||
assert_eq!(body["tags"], json!([]));
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn create_with_full_metadata_roundtrips(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
let action_id = genre_id_named(&h.app, "Action").await;
|
||||
let fantasy_id = genre_id_named(&h.app, "Fantasy").await;
|
||||
|
||||
let body = create_manga(
|
||||
&h.app,
|
||||
&cookie,
|
||||
json!({
|
||||
"title": "Berserk",
|
||||
"status": "completed",
|
||||
"authors": ["Kentaro Miura", "Studio Gaga"],
|
||||
"alt_titles": ["ベルセルク"],
|
||||
"genre_ids": [action_id, fantasy_id],
|
||||
"description": "Guts wields a big sword."
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(body["status"], "completed");
|
||||
assert_eq!(body["alt_titles"], json!(["ベルセルク"]));
|
||||
let authors: Vec<&str> = body["authors"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|a| a["name"].as_str().unwrap())
|
||||
.collect();
|
||||
assert_eq!(authors, vec!["Kentaro Miura", "Studio Gaga"]);
|
||||
let genres: Vec<&str> = body["genres"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|g| g["name"].as_str().unwrap())
|
||||
.collect();
|
||||
assert_eq!(genres, vec!["Action", "Fantasy"]);
|
||||
|
||||
// GET /mangas/:id returns the same shape.
|
||||
let id = id_of(&body);
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::get(&format!("/api/v1/mangas/{id}")))
|
||||
.await
|
||||
.unwrap();
|
||||
let detail = common::body_json(resp).await;
|
||||
assert_eq!(detail["status"], "completed");
|
||||
assert_eq!(detail["authors"].as_array().unwrap().len(), 2);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn invalid_status_rejected_with_422(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::post_multipart_with_cookie(
|
||||
"/api/v1/mangas",
|
||||
MultipartBuilder::new()
|
||||
.add_json("metadata", json!({ "title": "Foo", "status": "hiatus" })),
|
||||
&cookie,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::UNPROCESSABLE_ENTITY);
|
||||
let body = common::body_json(resp).await;
|
||||
assert_eq!(body["error"]["code"], "validation_failed");
|
||||
assert!(body["error"]["details"]["status"].is_string());
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn unknown_genre_id_rejected_with_422(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
let unknown = Uuid::new_v4();
|
||||
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::post_multipart_with_cookie(
|
||||
"/api/v1/mangas",
|
||||
MultipartBuilder::new().add_json(
|
||||
"metadata",
|
||||
json!({ "title": "Foo", "genre_ids": [unknown.to_string()] }),
|
||||
),
|
||||
&cookie,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::UNPROCESSABLE_ENTITY);
|
||||
let body = common::body_json(resp).await;
|
||||
assert_eq!(body["error"]["code"], "validation_failed");
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn author_dedups_case_insensitively(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
|
||||
let a = create_manga(
|
||||
&h.app,
|
||||
&cookie,
|
||||
json!({ "title": "Berserk", "authors": ["Kentaro Miura"] }),
|
||||
)
|
||||
.await;
|
||||
let b = create_manga(
|
||||
&h.app,
|
||||
&cookie,
|
||||
json!({ "title": "Berserk Prelude", "authors": ["kentaro miura"] }),
|
||||
)
|
||||
.await;
|
||||
|
||||
// Both mangas resolve the same author id even though the casing differed.
|
||||
let id_a = a["authors"][0]["id"].as_str().unwrap();
|
||||
let id_b = b["authors"][0]["id"].as_str().unwrap();
|
||||
assert_eq!(id_a, id_b);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn filter_by_status_returns_only_matches(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
|
||||
let _ongoing = create_manga(&h.app, &cookie, json!({ "title": "Ongoing One" })).await;
|
||||
let _done = create_manga(
|
||||
&h.app,
|
||||
&cookie,
|
||||
json!({ "title": "Wrapped Up", "status": "completed" }),
|
||||
)
|
||||
.await;
|
||||
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::get("/api/v1/mangas?status=completed"))
|
||||
.await
|
||||
.unwrap();
|
||||
let body = common::body_json(resp).await;
|
||||
let titles: Vec<&str> = body["items"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|m| m["title"].as_str().unwrap())
|
||||
.collect();
|
||||
assert_eq!(titles, vec!["Wrapped Up"]);
|
||||
assert_eq!(body["page"]["total"], 1);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn filter_by_multiple_genres_is_and(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
let action = genre_id_named(&h.app, "Action").await;
|
||||
let fantasy = genre_id_named(&h.app, "Fantasy").await;
|
||||
let comedy = genre_id_named(&h.app, "Comedy").await;
|
||||
|
||||
let _both = create_manga(
|
||||
&h.app,
|
||||
&cookie,
|
||||
json!({ "title": "Action+Fantasy", "genre_ids": [action.clone(), fantasy.clone()] }),
|
||||
)
|
||||
.await;
|
||||
let _action_only = create_manga(
|
||||
&h.app,
|
||||
&cookie,
|
||||
json!({ "title": "Action Only", "genre_ids": [action.clone()] }),
|
||||
)
|
||||
.await;
|
||||
let _other = create_manga(
|
||||
&h.app,
|
||||
&cookie,
|
||||
json!({ "title": "Comedy Only", "genre_ids": [comedy] }),
|
||||
)
|
||||
.await;
|
||||
|
||||
let url = format!("/api/v1/mangas?genre_id={action},{fantasy}");
|
||||
let resp = h.app.oneshot(common::get(&url)).await.unwrap();
|
||||
let body = common::body_json(resp).await;
|
||||
let titles: Vec<&str> = body["items"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|m| m["title"].as_str().unwrap())
|
||||
.collect();
|
||||
// Only the manga tagged with BOTH genres matches — pure AND.
|
||||
assert_eq!(titles, vec!["Action+Fantasy"]);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn filter_by_author_id_matches_only_works_by_that_author(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
|
||||
let miura_manga = create_manga(
|
||||
&h.app,
|
||||
&cookie,
|
||||
json!({ "title": "Berserk", "authors": ["Kentaro Miura"] }),
|
||||
)
|
||||
.await;
|
||||
let miura_id = miura_manga["authors"][0]["id"].as_str().unwrap();
|
||||
let _oda = create_manga(
|
||||
&h.app,
|
||||
&cookie,
|
||||
json!({ "title": "One Piece", "authors": ["Eiichiro Oda"] }),
|
||||
)
|
||||
.await;
|
||||
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::get(&format!("/api/v1/mangas?author_id={miura_id}")))
|
||||
.await
|
||||
.unwrap();
|
||||
let body = common::body_json(resp).await;
|
||||
let titles: Vec<&str> = body["items"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|m| m["title"].as_str().unwrap())
|
||||
.collect();
|
||||
assert_eq!(titles, vec!["Berserk"]);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn patch_updates_status_authors_and_genres(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
let drama = genre_id_named(&h.app, "Drama").await;
|
||||
|
||||
let created = create_manga(
|
||||
&h.app,
|
||||
&cookie,
|
||||
json!({ "title": "WIP", "authors": ["Old Name"] }),
|
||||
)
|
||||
.await;
|
||||
let id = id_of(&created);
|
||||
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::patch_json_with_cookie(
|
||||
&format!("/api/v1/mangas/{id}"),
|
||||
json!({
|
||||
"status": "completed",
|
||||
"authors": ["Old Name", "New Coauthor"],
|
||||
"genre_ids": [drama]
|
||||
}),
|
||||
&cookie,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
let body = common::body_json(resp).await;
|
||||
assert_eq!(body["status"], "completed");
|
||||
let names: Vec<&str> = body["authors"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|a| a["name"].as_str().unwrap())
|
||||
.collect();
|
||||
assert_eq!(names, vec!["Old Name", "New Coauthor"]);
|
||||
assert_eq!(body["genres"][0]["name"], "Drama");
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn patch_404_on_unknown_id(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::patch_json_with_cookie(
|
||||
&format!("/api/v1/mangas/{}", Uuid::new_v4()),
|
||||
json!({ "status": "completed" }),
|
||||
&cookie,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn filter_by_unknown_uuid_returns_empty_not_error(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
let _ = create_manga(&h.app, &cookie, json!({ "title": "Anything" })).await;
|
||||
|
||||
let unknown = Uuid::new_v4();
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::get(&format!("/api/v1/mangas?genre_id={unknown}")))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
let body = common::body_json(resp).await;
|
||||
assert_eq!(body["items"], json!([]));
|
||||
assert_eq!(body["page"]["total"], 0);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn filter_combining_status_author_genre_and_tag_is_and(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
let action = genre_id_named(&h.app, "Action").await;
|
||||
|
||||
// The "winner" matches every facet; the other rows each miss at
|
||||
// least one so the combined filter must reject them.
|
||||
let winner = create_manga(
|
||||
&h.app,
|
||||
&cookie,
|
||||
json!({
|
||||
"title": "Winner",
|
||||
"status": "completed",
|
||||
"authors": ["Solo Author"],
|
||||
"genre_ids": [action.clone()],
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
let solo_author_id = winner["authors"][0]["id"].as_str().unwrap().to_string();
|
||||
let winner_id = id_of(&winner);
|
||||
|
||||
let _missing_status = create_manga(
|
||||
&h.app,
|
||||
&cookie,
|
||||
json!({
|
||||
"title": "Wrong Status",
|
||||
"authors": ["Solo Author"],
|
||||
"genre_ids": [action.clone()],
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
let _missing_author = create_manga(
|
||||
&h.app,
|
||||
&cookie,
|
||||
json!({
|
||||
"title": "Wrong Author",
|
||||
"status": "completed",
|
||||
"authors": ["Other"],
|
||||
"genre_ids": [action.clone()],
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
let _missing_genre = create_manga(
|
||||
&h.app,
|
||||
&cookie,
|
||||
json!({
|
||||
"title": "Missing Genre",
|
||||
"status": "completed",
|
||||
"authors": ["Solo Author"],
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
|
||||
// Attach a tag to the winner so we can hit all four facets at once.
|
||||
let tag_attach = h
|
||||
.app
|
||||
.clone()
|
||||
.oneshot(common::post_json_with_cookie(
|
||||
&format!("/api/v1/mangas/{winner_id}/tags"),
|
||||
json!({ "name": "Pinned" }),
|
||||
&cookie,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
let tag_id = common::body_json(tag_attach).await["id"].as_str().unwrap().to_string();
|
||||
|
||||
let url = format!(
|
||||
"/api/v1/mangas?status=completed&author_id={solo_author_id}&genre_id={action}&tag_id={tag_id}"
|
||||
);
|
||||
let resp = h.app.oneshot(common::get(&url)).await.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
let body = common::body_json(resp).await;
|
||||
let titles: Vec<&str> = body["items"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|m| m["title"].as_str().unwrap())
|
||||
.collect();
|
||||
assert_eq!(titles, vec!["Winner"]);
|
||||
assert_eq!(body["page"]["total"], 1);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn duplicate_genre_ids_accepted_not_treated_as_unknown(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
let action = genre_id_named(&h.app, "Action").await;
|
||||
|
||||
let body = create_manga(
|
||||
&h.app,
|
||||
&cookie,
|
||||
json!({
|
||||
"title": "Dup Genres",
|
||||
"genre_ids": [action.clone(), action]
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
// The repeated id resolves to a single attachment (the join table
|
||||
// de-dupes via the composite PK + `ON CONFLICT DO NOTHING`), so
|
||||
// the response carries one genre, not two.
|
||||
let names: Vec<&str> = body["genres"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|g| g["name"].as_str().unwrap())
|
||||
.collect();
|
||||
assert_eq!(names, vec!["Action"]);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn patch_explicit_null_description_clears_it(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
let created = create_manga(
|
||||
&h.app,
|
||||
&cookie,
|
||||
json!({ "title": "With desc", "description": "Original" }),
|
||||
)
|
||||
.await;
|
||||
let id = id_of(&created);
|
||||
assert_eq!(created["description"], "Original");
|
||||
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::patch_json_with_cookie(
|
||||
&format!("/api/v1/mangas/{id}"),
|
||||
json!({ "description": null }),
|
||||
&cookie,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
let body = common::body_json(resp).await;
|
||||
assert!(body["description"].is_null(), "expected description cleared");
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn patch_omitting_description_leaves_it_alone(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
let created = create_manga(
|
||||
&h.app,
|
||||
&cookie,
|
||||
json!({ "title": "With desc", "description": "Keep me" }),
|
||||
)
|
||||
.await;
|
||||
let id = id_of(&created);
|
||||
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::patch_json_with_cookie(
|
||||
&format!("/api/v1/mangas/{id}"),
|
||||
json!({ "status": "completed" }),
|
||||
&cookie,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
let body = common::body_json(resp).await;
|
||||
assert_eq!(body["description"], "Keep me");
|
||||
assert_eq!(body["status"], "completed");
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn patch_empty_alt_titles_clears_them(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
let created = create_manga(
|
||||
&h.app,
|
||||
&cookie,
|
||||
json!({ "title": "Has alts", "alt_titles": ["a", "b"] }),
|
||||
)
|
||||
.await;
|
||||
let id = id_of(&created);
|
||||
assert_eq!(created["alt_titles"], json!(["a", "b"]));
|
||||
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::patch_json_with_cookie(
|
||||
&format!("/api/v1/mangas/{id}"),
|
||||
json!({ "alt_titles": [] }),
|
||||
&cookie,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
let body = common::body_json(resp).await;
|
||||
assert_eq!(body["alt_titles"], json!([]));
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn patch_requires_authentication(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
let created = create_manga(&h.app, &cookie, json!({ "title": "Auth Check" })).await;
|
||||
let id = id_of(&created);
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::patch_json(
|
||||
&format!("/api/v1/mangas/{id}"),
|
||||
json!({ "status": "completed" }),
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
|
||||
}
|
||||
260
backend/tests/api_tags.rs
Normal file
260
backend/tests/api_tags.rs
Normal file
@@ -0,0 +1,260 @@
|
||||
mod common;
|
||||
|
||||
use axum::http::StatusCode;
|
||||
use serde_json::json;
|
||||
use sqlx::PgPool;
|
||||
use tower::ServiceExt;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn attach_creates_tag_and_links_to_manga(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
let manga_id = common::seed_manga_via_api(&h.app, &cookie, "Berserk").await;
|
||||
|
||||
let resp = h
|
||||
.app
|
||||
.clone()
|
||||
.oneshot(common::post_json_with_cookie(
|
||||
&format!("/api/v1/mangas/{manga_id}/tags"),
|
||||
json!({ "name": "Dark Fantasy" }),
|
||||
&cookie,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::CREATED);
|
||||
let body = common::body_json(resp).await;
|
||||
assert_eq!(body["name"], "Dark Fantasy");
|
||||
assert!(body["id"].as_str().is_some());
|
||||
assert!(body["added_by"].as_str().is_some());
|
||||
|
||||
// The tag is now visible on the manga detail response.
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::get(&format!("/api/v1/mangas/{manga_id}")))
|
||||
.await
|
||||
.unwrap();
|
||||
let detail = common::body_json(resp).await;
|
||||
let tags = detail["tags"].as_array().unwrap();
|
||||
assert_eq!(tags.len(), 1);
|
||||
assert_eq!(tags[0]["name"], "Dark Fantasy");
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn reattach_same_tag_is_idempotent_and_returns_200(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
let manga_id = common::seed_manga_via_api(&h.app, &cookie, "Berserk").await;
|
||||
|
||||
let make = || {
|
||||
common::post_json_with_cookie(
|
||||
&format!("/api/v1/mangas/{manga_id}/tags"),
|
||||
json!({ "name": "Gritty" }),
|
||||
&cookie,
|
||||
)
|
||||
};
|
||||
let first = h.app.clone().oneshot(make()).await.unwrap();
|
||||
assert_eq!(first.status(), StatusCode::CREATED);
|
||||
let second = h.app.oneshot(make()).await.unwrap();
|
||||
assert_eq!(second.status(), StatusCode::OK);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn tag_names_dedup_case_insensitively(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
let manga_id = common::seed_manga_via_api(&h.app, &cookie, "Berserk").await;
|
||||
|
||||
let first = h
|
||||
.app
|
||||
.clone()
|
||||
.oneshot(common::post_json_with_cookie(
|
||||
&format!("/api/v1/mangas/{manga_id}/tags"),
|
||||
json!({ "name": "Seinen" }),
|
||||
&cookie,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(first.status(), StatusCode::CREATED);
|
||||
let first_body = common::body_json(first).await;
|
||||
let first_id = first_body["id"].as_str().unwrap().to_string();
|
||||
|
||||
// Different casing on a second manga should resolve to the same tag id.
|
||||
let other_manga = common::seed_manga_via_api(&h.app, &cookie, "Other").await;
|
||||
let second = h
|
||||
.app
|
||||
.oneshot(common::post_json_with_cookie(
|
||||
&format!("/api/v1/mangas/{other_manga}/tags"),
|
||||
json!({ "name": "seinen" }),
|
||||
&cookie,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
let second_body = common::body_json(second).await;
|
||||
assert_eq!(second_body["id"].as_str().unwrap(), first_id);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn detach_only_by_attacher(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie_a) = common::register_user(&h.app).await;
|
||||
let (_, cookie_b) = common::register_user(&h.app).await;
|
||||
let manga_id = common::seed_manga_via_api(&h.app, &cookie_a, "Berserk").await;
|
||||
|
||||
let attach = h
|
||||
.app
|
||||
.clone()
|
||||
.oneshot(common::post_json_with_cookie(
|
||||
&format!("/api/v1/mangas/{manga_id}/tags"),
|
||||
json!({ "name": "Classic" }),
|
||||
&cookie_a,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
let attach_body = common::body_json(attach).await;
|
||||
let tag_id = attach_body["id"].as_str().unwrap();
|
||||
|
||||
// User B cannot remove user A's tag attachment.
|
||||
let resp = h
|
||||
.app
|
||||
.clone()
|
||||
.oneshot(common::delete_with_cookie(
|
||||
&format!("/api/v1/mangas/{manga_id}/tags/{tag_id}"),
|
||||
&cookie_b,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::FORBIDDEN);
|
||||
|
||||
// User A can.
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::delete_with_cookie(
|
||||
&format!("/api/v1/mangas/{manga_id}/tags/{tag_id}"),
|
||||
&cookie_a,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::NO_CONTENT);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn detach_404_when_not_attached(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
let manga_id = common::seed_manga_via_api(&h.app, &cookie, "Berserk").await;
|
||||
let unknown_tag = Uuid::new_v4();
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::delete_with_cookie(
|
||||
&format!("/api/v1/mangas/{manga_id}/tags/{unknown_tag}"),
|
||||
&cookie,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn attach_requires_authentication(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
let manga_id = common::seed_manga_via_api(&h.app, &cookie, "Berserk").await;
|
||||
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::post_json(
|
||||
&format!("/api/v1/mangas/{manga_id}/tags"),
|
||||
json!({ "name": "Anon" }),
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn attach_404_on_unknown_manga(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::post_json_with_cookie(
|
||||
&format!("/api/v1/mangas/{}/tags", Uuid::new_v4()),
|
||||
json!({ "name": "Anything" }),
|
||||
&cookie,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn filter_by_tag_id_matches_only_tagged_mangas(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
let tagged = common::seed_manga_via_api(&h.app, &cookie, "Tagged").await;
|
||||
let _untagged = common::seed_manga_via_api(&h.app, &cookie, "Untagged").await;
|
||||
|
||||
let attach = h
|
||||
.app
|
||||
.clone()
|
||||
.oneshot(common::post_json_with_cookie(
|
||||
&format!("/api/v1/mangas/{tagged}/tags"),
|
||||
json!({ "name": "Recommendation" }),
|
||||
&cookie,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
let tag_id = common::body_json(attach).await["id"].as_str().unwrap().to_string();
|
||||
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::get(&format!("/api/v1/mangas?tag_id={tag_id}")))
|
||||
.await
|
||||
.unwrap();
|
||||
let body = common::body_json(resp).await;
|
||||
let titles: Vec<&str> = body["items"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|m| m["title"].as_str().unwrap())
|
||||
.collect();
|
||||
assert_eq!(titles, vec!["Tagged"]);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn tag_autocomplete_returns_matches_ordered_by_similarity(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
let (_, cookie) = common::register_user(&h.app).await;
|
||||
let manga_id = common::seed_manga_via_api(&h.app, &cookie, "Berserk").await;
|
||||
|
||||
for name in ["Mystery", "Murder Mystery", "Comedy"] {
|
||||
let _ = h
|
||||
.app
|
||||
.clone()
|
||||
.oneshot(common::post_json_with_cookie(
|
||||
&format!("/api/v1/mangas/{manga_id}/tags"),
|
||||
json!({ "name": name }),
|
||||
&cookie,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::get("/api/v1/tags?search=myst"))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
let body = common::body_json(resp).await;
|
||||
let names: Vec<&str> = body
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|t| t["name"].as_str().unwrap())
|
||||
.collect();
|
||||
assert!(names.iter().any(|n| *n == "Mystery"));
|
||||
assert!(names.iter().any(|n| *n == "Murder Mystery"));
|
||||
assert!(!names.iter().any(|n| *n == "Comedy"));
|
||||
}
|
||||
Reference in New Issue
Block a user