mod common;
use std::collections::BTreeMap;
use common::MemoryBackend;
use jmap_tasks_server::{
handle_task_get, handle_task_list_get, handle_task_list_set, handle_task_set,
};
use jmap_types::Id;
use serde_json::{Value, json};
fn my_rights() -> Value {
json!({
"mayReadItems": true,
"mayWriteAll": true,
"mayWriteOwn": true,
"mayUpdatePrivate": true,
"mayRSVP": true,
"mayAdmin": true,
"mayDelete": true
})
}
fn seeded_backend() -> MemoryBackend<()> {
let acc = Id::from("acc1");
let backend = MemoryBackend::<()>::default().with_account(&acc);
backend.seed_object(
&acc,
"TaskList",
&Id::from("tl1"),
json!({
"id": "tl1",
"name": "Chores",
"sortOrder": 0,
"isSubscribed": true,
"myRights": my_rights()
}),
);
backend.seed_object(
&acc,
"Task",
&Id::from("t1"),
json!({
"id": "t1",
"@type": "Task",
"uid": "t1-uid",
"title": "Review"
}),
);
backend
}
async fn task_set(backend: &MemoryBackend<()>, id: &str, patch: Value) -> Value {
let args = json!({ "accountId": "acc1", "update": { (id): patch } });
handle_task_set(backend, &(), &BTreeMap::new(), args, &[])
.await
.expect("/set must succeed at the top level")
.primary
}
async fn task_get(backend: &MemoryBackend<()>) -> Value {
let args = json!({ "accountId": "acc1", "ids": ["t1"] });
handle_task_get(backend, &(), args, &[])
.await
.expect("/get must succeed")
.primary["list"][0]
.clone()
}
async fn list_set(backend: &MemoryBackend<()>, id: &str, patch: Value) -> Value {
let args = json!({ "accountId": "acc1", "update": { (id): patch } });
handle_task_list_set(backend, &(), args, &[])
.await
.expect("/set must succeed at the top level")
.primary
}
async fn list_get(backend: &MemoryBackend<()>) -> Value {
let args = json!({ "accountId": "acc1", "ids": ["tl1"] });
handle_task_list_get(backend, &(), args, &[])
.await
.expect("/get must succeed")
.primary["list"][0]
.clone()
}
fn assert_noop(resp: &Value, id: &str) {
assert!(resp["notUpdated"].is_null(), "{resp}");
assert_eq!(resp["updated"], json!({ (id): null }), "{resp}");
assert_eq!(resp["oldState"], resp["newState"], "{resp}");
}
#[tokio::test]
async fn task_identical_id_is_a_noop() {
let backend = seeded_backend();
let before = task_get(&backend).await;
let resp = task_set(&backend, "t1", json!({ "id": "t1" })).await;
assert_noop(&resp, "t1");
assert_eq!(task_get(&backend).await, before);
}
#[tokio::test]
async fn task_identical_id_with_change_applies() {
let backend = seeded_backend();
let resp = task_set(&backend, "t1", json!({ "id": "t1", "title": "Done" })).await;
assert!(resp["notUpdated"].is_null(), "{resp}");
assert_eq!(task_get(&backend).await["title"], "Done");
}
#[tokio::test]
async fn task_changed_id_is_invalid_properties() {
let backend = seeded_backend();
let resp = task_set(&backend, "t1", json!({ "id": "t2", "title": "Done" })).await;
assert_eq!(
resp["notUpdated"]["t1"]["type"], "invalidProperties",
"{resp}"
);
assert_eq!(
resp["notUpdated"]["t1"]["properties"],
json!(["id"]),
"{resp}"
);
assert_eq!(task_get(&backend).await["title"], "Review");
}
#[tokio::test]
async fn task_empty_patch_is_a_noop() {
let backend = seeded_backend();
let before = task_get(&backend).await;
let resp = task_set(&backend, "t1", json!({})).await;
assert_noop(&resp, "t1");
assert_eq!(task_get(&backend).await, before);
}
#[tokio::test]
async fn task_empty_patch_of_missing_id_is_not_found() {
let backend = seeded_backend();
let resp = task_set(&backend, "t9", json!({})).await;
assert_eq!(resp["notUpdated"]["t9"]["type"], "notFound", "{resp}");
}
#[tokio::test]
async fn task_changed_server_set_value_of_missing_id_is_not_found() {
let backend = seeded_backend();
for patch in [
json!({ "id": "t8" }),
json!({ "baseTaskId": "t1", "title": "x" }),
json!({ "isOrigin": false }),
] {
let resp = task_set(&backend, "t9", patch.clone()).await;
assert_eq!(
resp["notUpdated"]["t9"]["type"], "notFound",
"{patch}: {resp}"
);
}
}
#[tokio::test]
async fn task_list_identical_server_set_values_are_a_noop() {
let backend = seeded_backend();
let before = list_get(&backend).await;
let resp = list_set(
&backend,
"tl1",
json!({ "id": "tl1", "myRights": my_rights() }),
)
.await;
assert_noop(&resp, "tl1");
assert_eq!(list_get(&backend).await, before);
}
#[tokio::test]
async fn task_list_identical_id_with_change_applies() {
let backend = seeded_backend();
let resp = list_set(&backend, "tl1", json!({ "id": "tl1", "name": "Errands" })).await;
assert!(resp["notUpdated"].is_null(), "{resp}");
assert_eq!(list_get(&backend).await["name"], "Errands");
}
#[tokio::test]
async fn task_list_changed_my_rights_is_invalid_properties() {
let backend = seeded_backend();
let mut rights = my_rights();
rights["mayDelete"] = json!(false);
let resp = list_set(&backend, "tl1", json!({ "myRights": rights })).await;
assert_eq!(
resp["notUpdated"]["tl1"]["type"], "invalidProperties",
"{resp}"
);
assert_eq!(
resp["notUpdated"]["tl1"]["properties"],
json!(["myRights"]),
"{resp}"
);
}
#[tokio::test]
async fn task_list_empty_patch_is_a_noop() {
let backend = seeded_backend();
let before = list_get(&backend).await;
let resp = list_set(&backend, "tl1", json!({})).await;
assert_noop(&resp, "tl1");
assert_eq!(list_get(&backend).await, before);
}