feat: edit/delete field definitions — audited, blocked when in use (#36)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -714,3 +714,167 @@ async fn edit_and_delete_authority(pool: PgPool) {
|
||||
.await;
|
||||
assert_eq!(deleted.status(), StatusCode::NO_CONTENT);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "../db/migrations")]
|
||||
async fn edit_and_delete_field_definition(pool: PgPool) {
|
||||
migrate_sessions(&db::Db::from_pool(pool.clone()))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
seed_user(&pool, "ed@example.com", "pw-editor-123", Role::Editor).await;
|
||||
|
||||
let app = build_app(state(pool));
|
||||
let cookie = login(&app, "ed@example.com", "pw-editor-123").await;
|
||||
|
||||
// create a field definition
|
||||
send(
|
||||
&app,
|
||||
&cookie,
|
||||
"POST",
|
||||
"/api/admin/field-definitions",
|
||||
Some(r#"{"key":"weight","data_type":"integer","vocabulary_id":null,"authority_kind":null,"required":false,"group":null,"labels":[{"lang":"sv","label":"Vikt"}]}"#),
|
||||
)
|
||||
.await;
|
||||
|
||||
// PATCH: update required + group + labels
|
||||
let patched = send(
|
||||
&app,
|
||||
&cookie,
|
||||
"PATCH",
|
||||
"/api/admin/field-definitions/weight",
|
||||
Some(r#"{"required":true,"group":"Mått","labels":[{"lang":"sv","label":"Vikt (g)"}]}"#),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(patched.status(), StatusCode::NO_CONTENT);
|
||||
|
||||
// PATCH unknown key → 404
|
||||
let missing = send(
|
||||
&app,
|
||||
&cookie,
|
||||
"PATCH",
|
||||
"/api/admin/field-definitions/nope",
|
||||
Some(r#"{"required":false,"group":null,"labels":[]}"#),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(missing.status(), StatusCode::NOT_FOUND);
|
||||
|
||||
// DELETE the (unreferenced) field definition
|
||||
let deleted = send(
|
||||
&app,
|
||||
&cookie,
|
||||
"DELETE",
|
||||
"/api/admin/field-definitions/weight",
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
assert_eq!(deleted.status(), StatusCode::NO_CONTENT);
|
||||
|
||||
// DELETE again → 404
|
||||
let again = send(
|
||||
&app,
|
||||
&cookie,
|
||||
"DELETE",
|
||||
"/api/admin/field-definitions/weight",
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
assert_eq!(again.status(), StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "../db/migrations")]
|
||||
async fn delete_field_definition_referenced_is_409(pool: PgPool) {
|
||||
migrate_sessions(&db::Db::from_pool(pool.clone()))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
seed_user(&pool, "ed@example.com", "pw-editor-123", Role::Editor).await;
|
||||
|
||||
let app = build_app(state(pool));
|
||||
let cookie = login(&app, "ed@example.com", "pw-editor-123").await;
|
||||
|
||||
// create a field definition
|
||||
send(
|
||||
&app,
|
||||
&cookie,
|
||||
"POST",
|
||||
"/api/admin/field-definitions",
|
||||
Some(r#"{"key":"weight","data_type":"integer","vocabulary_id":null,"authority_kind":null,"required":false,"group":null,"labels":[{"lang":"sv","label":"Vikt"}]}"#),
|
||||
)
|
||||
.await;
|
||||
|
||||
// create an object and set the field
|
||||
let obj = send(
|
||||
&app,
|
||||
&cookie,
|
||||
"POST",
|
||||
"/api/admin/objects",
|
||||
Some(r#"{"object_number":"T-2","object_name":"test","number_of_objects":1,"visibility":"draft"}"#),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(obj.status(), StatusCode::CREATED);
|
||||
|
||||
let obj_json: serde_json::Value =
|
||||
serde_json::from_slice(&obj.into_body().collect().await.unwrap().to_bytes()).unwrap();
|
||||
let obj_id = obj_json["id"].as_str().unwrap().to_owned();
|
||||
|
||||
let set = send(
|
||||
&app,
|
||||
&cookie,
|
||||
"PUT",
|
||||
&format!("/api/admin/objects/{obj_id}/fields"),
|
||||
Some(r#"{"weight":42}"#),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(set.status(), StatusCode::NO_CONTENT);
|
||||
|
||||
// delete the field definition — must be blocked
|
||||
let blocked = send(
|
||||
&app,
|
||||
&cookie,
|
||||
"DELETE",
|
||||
"/api/admin/field-definitions/weight",
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
assert_eq!(blocked.status(), StatusCode::CONFLICT);
|
||||
|
||||
let body: serde_json::Value =
|
||||
serde_json::from_slice(&blocked.into_body().collect().await.unwrap().to_bytes()).unwrap();
|
||||
assert_eq!(body["count"], 1);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "../db/migrations")]
|
||||
async fn field_definition_edit_delete_requires_auth(pool: PgPool) {
|
||||
migrate_sessions(&db::Db::from_pool(pool.clone()))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let app = build_app(state(pool));
|
||||
|
||||
let patch_resp = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("PATCH")
|
||||
.uri("/api/admin/field-definitions/weight")
|
||||
.header(header::CONTENT_TYPE, "application/json")
|
||||
.body(Body::from(r#"{"required":false,"group":null,"labels":[]}"#))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(patch_resp.status(), StatusCode::UNAUTHORIZED);
|
||||
|
||||
let delete_resp = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("DELETE")
|
||||
.uri("/api/admin/field-definitions/weight")
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(delete_resp.status(), StatusCode::UNAUTHORIZED);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user