1
Fork 0
mirror of https://github.com/thegeneralist01/archivr synced 2026-12-04 18:56:44 +01:00

chore: remove stale dead_code allows; default collection delete is a 400

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
TheGeneralist 2026-10-09 19:45:25 +02:00
parent ba9c21b480
commit b53e67e7f3
Signed by: thegeneralist01
SSH key fingerprint: SHA256:pp9qddbCNmVNoSjevdvQvM5z0DHN7LTa8qBMbcMq/R4
3 changed files with 28 additions and 2 deletions

View file

@ -2,7 +2,6 @@
//!
//! Callers still do their own coarse role check (`require_role(ROLE_ADMIN)`);
//! these guards add the target-aware rules on top.
#![allow(dead_code)] // consumed by the admin_users / credentials streams
use rusqlite::Connection;
use crate::auth::{ROLE_ADMIN, ROLE_OWNER};

View file

@ -3628,6 +3628,15 @@ async fn delete_collection_handler(
auth.require_role(ROLE_USER)?;
let mounted = mounted_archive(&state, &archive_id)?;
let conn = database::open_or_initialize(&mounted.archive_path)?;
// Same rule (and status) as adding/removing entries: the default collection is permanent.
// core also refuses, but as an anyhow error that would surface as a 500.
if database::get_collection_by_uid(&conn, &coll_uid)?
.is_some_and(|coll| coll.slug == "_default_")
{
return Err(ApiError::bad_request(
"cannot delete the default collection",
));
}
let deleted = database::delete_collection(&conn, &coll_uid)?;
if deleted {
Ok(StatusCode::NO_CONTENT)
@ -7495,6 +7504,25 @@ mod tests {
assert_eq!(response.status(), StatusCode::OK);
}
#[tokio::test]
async fn deleting_the_default_collection_is_a_400_not_a_500() {
let dir = tempfile::tempdir().unwrap();
let (registry, _, auth_path) = make_test_registry(&dir);
let session_cookie = make_test_session(&auth_path);
let router = app(registry, auth_path);
let list = router.clone().oneshot(Request::builder()
.uri("/api/archives/test/collections").header("cookie", &session_cookie)
.body(Body::empty()).unwrap()).await.unwrap();
let collections = body_json(list).await;
let default_uid = collections.as_array().unwrap().iter()
.find(|c| c["slug"] == "_default_").unwrap()["collection_uid"].as_str().unwrap().to_string();
let response = router.oneshot(Request::builder().method("DELETE")
.uri(format!("/api/archives/test/collections/{default_uid}"))
.header("cookie", &session_cookie).body(Body::empty()).unwrap()).await.unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_eq!(body_json(response).await["error"], "cannot delete the default collection");
}
// ── Task 2: list_entries / search_entries auth enforcement ───────────────
#[tokio::test]

View file

@ -1,7 +1,6 @@
//! Shared helpers for server tests (`#[cfg(test)]` only). Copied from
//! `routes.rs`'s `mod tests` so new stream modules can reuse them without
//! touching that file; the originals there are unchanged.
#![allow(dead_code)] // helpers are consumed by the stream modules' tests
use std::path::{Path, PathBuf};
use archivr_core::database;