fix(admin): three review findings — audit no-op, 404, chapter priority (0.41.1)
- admin_safe_set_is_admin: short-circuit when target.is_admin == value,
before writing audit. PATCH {is_admin: true} on someone already admin
previously wrote a misleading "promote_user" row even though the UPDATE
was a no-op.
- list_chapters (/admin/mangas/:id/chapters): explicit exists() check on
manga_id, returns 404 instead of 200 [] for a typo'd / deleted manga.
- ChapterSyncState priority: the Failed branch now requires page_count = 0,
so a chapter with pages on disk AND a historical dead job (from a
re-download attempt that crashed) stays Synced. The old order
contradicted Synced's documented "downloaded at some point" contract.
Doc comments updated alongside the SQL.
Three new regression tests pin the behaviour.
This commit is contained in:
@@ -423,6 +423,56 @@ async fn http_list_chapters_returns_per_chapter_state(pool: PgPool) {
|
||||
assert_eq!(items[1]["sync_state"], "not_downloaded");
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn http_list_chapters_returns_404_for_unknown_manga(pool: PgPool) {
|
||||
// Regression: used to return 200 [] for a non-existent manga,
|
||||
// which silently rendered "No chapters." for a typo'd / deleted id.
|
||||
let h = common::harness(pool.clone());
|
||||
let (_admin, cookie) = seed_admin(&pool, &h.app).await;
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::get_with_cookie(
|
||||
&format!("/api/v1/admin/mangas/{}/chapters", Uuid::new_v4()),
|
||||
&cookie,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn chapter_state_synced_when_pages_present_even_with_dead_job(pool: PgPool) {
|
||||
// Regression: the old CASE prioritised the dead-job branch above
|
||||
// the page_count check, so a chapter with pages on disk AND a
|
||||
// historical dead job (e.g. from a re-download attempt that
|
||||
// crashed) flipped to Failed — contradicting Synced's "downloaded
|
||||
// at some point" contract.
|
||||
seed_source(&pool).await;
|
||||
let m = insert_manga(&pool, "M").await;
|
||||
let c = insert_chapter(&pool, m, 1, 12).await; // pages present
|
||||
insert_chapter_source(&pool, c, "ckey-1", false).await;
|
||||
insert_job(
|
||||
&pool,
|
||||
json!({
|
||||
"kind": "sync_chapter_content",
|
||||
"source_id": SOURCE_ID,
|
||||
"chapter_id": c.to_string(),
|
||||
"source_chapter_key": "ckey-1",
|
||||
}),
|
||||
"dead",
|
||||
)
|
||||
.await;
|
||||
|
||||
let rows = repo::admin_view::list_chapters_with_sync_state(&pool, m)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
rows[0].sync_state,
|
||||
mangalord::domain::ChapterSyncState::Synced,
|
||||
"pages on disk override historical dead-job noise"
|
||||
);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn http_list_mangas_requires_admin(pool: PgPool) {
|
||||
let h = common::harness(pool);
|
||||
|
||||
@@ -330,6 +330,37 @@ async fn promote_writes_audit_row(pool: PgPool) {
|
||||
assert_eq!(target, Some(b.id));
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn redundant_promote_does_not_write_audit_row(pool: PgPool) {
|
||||
// Regression: PATCH {is_admin: true} on someone already admin used
|
||||
// to UPDATE (no-op) and still INSERT a misleading "promote_user"
|
||||
// audit row. Should short-circuit without touching admin_audit.
|
||||
let h = common::harness(pool.clone());
|
||||
let (_a_name, a_cookie, _a_id) = seed_admin(&pool, &h.app).await;
|
||||
let (b_name, _b_cookie, _b_id) = seed_admin(&pool, &h.app).await; // already admin
|
||||
|
||||
let b = repo::user::find_by_username(&pool, &b_name)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let resp = h
|
||||
.app
|
||||
.oneshot(common::patch_json_with_cookie(
|
||||
&format!("/api/v1/admin/users/{}", b.id),
|
||||
json!({ "is_admin": true }),
|
||||
&a_cookie,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
||||
let (count,): (i64,) = sqlx::query_as("SELECT COUNT(*) FROM admin_audit")
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(count, 0, "no-op promote must not write audit row");
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn delete_writes_audit_row(pool: PgPool) {
|
||||
let h = common::harness(pool.clone());
|
||||
|
||||
Reference in New Issue
Block a user