mod common;
use std::collections::BTreeMap;
use common::{ACCOUNT_ID, INJECTABLE_BACKEND_CANARY, InjectableBackend, SPACE_ID};
use jmap_chat_server::{handle_ban_changes, handle_ban_set};
use jmap_server::HandlerOutput;
use jmap_types::JmapError;
use serde_json::{Value, json};
const BAN_ID: &str = "ban1";
fn seeded() -> InjectableBackend {
let backend = InjectableBackend::new();
backend.inner.insert_object_for_test(
"SpaceBan",
ACCOUNT_ID,
BAN_ID,
json!({
"id": BAN_ID,
"spaceId": SPACE_ID,
"userId": "target",
"bannedBy": ACCOUNT_ID,
"createdAt": "2026-01-01T00:00:00Z",
}),
);
backend
}
async fn set(backend: &InjectableBackend, body: Value) -> Result<Value, JmapError> {
let mut args = body;
args["accountId"] = json!(ACCOUNT_ID);
handle_ban_set(backend, &(), &BTreeMap::new(), args)
.await
.map(|HandlerOutput { primary, .. }| primary)
}
fn assert_set_error(
resp: &Value,
map: &str,
key: &str,
error_type: &str,
properties: Option<Value>,
) {
assert_eq!(resp[map][key]["type"], error_type, "{resp}");
if let Some(props) = properties {
assert_eq!(resp[map][key]["properties"], props, "{resp}");
}
}
fn create(body: Value) -> Value {
json!({ "create": { "b0": body } })
}
#[tokio::test]
async fn changes_without_since_state_is_invalid_arguments() {
let err = handle_ban_changes(&seeded(), &(), json!({ "accountId": ACCOUNT_ID }))
.await
.expect_err("missing sinceState");
assert_eq!(err.error_type.to_string(), "invalidArguments", "{err:?}");
}
#[tokio::test]
async fn set_with_stale_if_in_state_is_state_mismatch() {
let err = set(
&seeded(),
json!({ "ifInState": "not-the-state", "destroy": [BAN_ID] }),
)
.await
.expect_err("stale ifInState");
assert_eq!(err.error_type.to_string(), "stateMismatch", "{err:?}");
}
#[tokio::test]
async fn create_with_unknown_creation_reference_is_invalid_properties() {
let resp = set(
&seeded(),
create(json!({ "spaceId": "#nope", "userId": "target2" })),
)
.await
.expect("SpaceBan/set");
assert_eq!(resp["created"], Value::Null, "{resp}");
assert_set_error(
&resp,
"notCreated",
"b0",
"invalidProperties",
Some(json!(["spaceId"])),
);
}
#[tokio::test]
async fn create_supplying_banned_by_is_invalid_properties() {
let resp = set(
&seeded(),
create(json!({ "spaceId": SPACE_ID, "userId": "target2", "bannedBy": "someone" })),
)
.await
.expect("SpaceBan/set");
assert_eq!(resp["created"], Value::Null, "{resp}");
assert_set_error(
&resp,
"notCreated",
"b0",
"invalidProperties",
Some(json!(["bannedBy"])),
);
}
#[tokio::test]
async fn create_with_malformed_ids_is_invalid_properties() {
for (body, prop) in [
(
json!({ "spaceId": "bad id", "userId": "target2" }),
"spaceId",
),
(json!({ "spaceId": SPACE_ID, "userId": "bad id" }), "userId"),
] {
let resp = set(&seeded(), create(body)).await.expect("SpaceBan/set");
assert_eq!(resp["created"], Value::Null, "{prop}: {resp}");
assert_set_error(
&resp,
"notCreated",
"b0",
"invalidProperties",
Some(json!([prop])),
);
}
}
#[tokio::test]
async fn update_with_malformed_id_is_invalid_properties() {
let resp = set(
&seeded(),
json!({ "update": { "bad id": { "reason": "x" } } }),
)
.await
.expect("SpaceBan/set");
assert_eq!(resp["updated"], Value::Null, "{resp}");
assert_set_error(
&resp,
"notUpdated",
"bad id",
"invalidProperties",
Some(json!(["id"])),
);
}
#[tokio::test]
async fn update_of_immutable_space_id_is_invalid_properties() {
let resp = set(
&seeded(),
json!({ "update": { BAN_ID: { "spaceId": "s2", "reason": "x" } } }),
)
.await
.expect("SpaceBan/set");
assert_eq!(resp["updated"], Value::Null, "{resp}");
assert_set_error(
&resp,
"notUpdated",
BAN_ID,
"invalidProperties",
Some(json!(["spaceId"])),
);
}
#[tokio::test]
async fn update_with_invalid_patch_is_invalid_patch() {
for patch in [json!("spam"), json!({ "unknownProperty": 1 })] {
let resp = set(&seeded(), json!({ "update": { BAN_ID: patch.clone() } }))
.await
.expect("SpaceBan/set");
assert_eq!(resp["updated"], Value::Null, "{patch}: {resp}");
assert_set_error(&resp, "notUpdated", BAN_ID, "invalidPatch", None);
}
}
#[tokio::test]
async fn update_of_unknown_ban_is_not_found() {
let resp = set(
&seeded(),
json!({ "update": { "missing": { "reason": "x" } } }),
)
.await
.expect("SpaceBan/set");
assert_eq!(resp["updated"], Value::Null, "{resp}");
assert_set_error(&resp, "notUpdated", "missing", "notFound", None);
}
#[tokio::test]
async fn destroy_with_non_string_element_is_invalid_arguments() {
let err = set(&seeded(), json!({ "destroy": [BAN_ID, 7] }))
.await
.expect_err("non-string destroy element");
assert_eq!(err.error_type.to_string(), "invalidArguments", "{err:?}");
}
#[tokio::test]
async fn destroy_with_malformed_id_is_invalid_properties() {
let resp = set(&seeded(), json!({ "destroy": ["bad id"] }))
.await
.expect("SpaceBan/set");
assert_eq!(resp["destroyed"], Value::Null, "{resp}");
assert_set_error(
&resp,
"notDestroyed",
"bad id",
"invalidProperties",
Some(json!(["id"])),
);
}
#[tokio::test]
async fn backend_faults_fail_the_target_with_server_fail() {
let cases: [(&str, Value, &str, &str); 4] = [
(
"create",
create(json!({ "spaceId": SPACE_ID, "userId": "target2" })),
"notCreated",
"b0",
),
(
"get",
json!({ "update": { BAN_ID: { "reason": "x" } } }),
"notUpdated",
BAN_ID,
),
(
"get",
json!({ "destroy": [BAN_ID] }),
"notDestroyed",
BAN_ID,
),
(
"destroy",
json!({ "destroy": [BAN_ID] }),
"notDestroyed",
BAN_ID,
),
];
for (op, body, map, key) in cases {
let backend = seeded();
backend.inject("SpaceBan", op);
let resp = set(&backend, body).await.expect("SpaceBan/set");
assert_eq!(
resp[map][key]["type"], "serverFail",
"SpaceBan/{op}: {resp}"
);
assert!(
!resp.to_string().contains(INJECTABLE_BACKEND_CANARY),
"SpaceBan/{op} leaked backend text: {resp}"
);
}
}
#[tokio::test]
async fn update_with_identical_read_only_values_is_updated() {
let resp = set(
&seeded(),
json!({ "update": { BAN_ID: {
"id": BAN_ID,
"spaceId": SPACE_ID,
"userId": "target",
"bannedBy": ACCOUNT_ID,
"createdAt": "2026-01-01T00:00:00Z",
"reason": "x",
} } }),
)
.await
.expect("SpaceBan/set");
assert_eq!(resp["notUpdated"], Value::Null, "{resp}");
assert!(resp["updated"].get(BAN_ID).is_some(), "{resp}");
}