use serde_json::{Value, json};
use super::Grants;
const TABLE: &str = r#"
grant_format = 1
[[clients]]
actor = "client:reader"
[[clients.conversations]]
prefix = "s-"
read = true
[[clients]]
actor = "client:writer"
[[clients.conversations]]
ids = ["s-1"]
append = true
[[clients]]
actor = "client:both"
[[clients.conversations]]
prefix = "s-"
read = true
append = true
"#;
fn refused(grants: &Grants, actor: &str, route: &str, body: Value) -> bool {
let payload = body.as_object().expect("an object");
let refusal = grants.allowed(actor, route, payload, false);
if let Some(fail) = &refusal {
assert_eq!((fail.status.as_u16(), fail.code), (403, "forbidden"));
}
refusal.is_some()
}
#[test]
fn write_records_needs_append_on_its_conversation() {
let grants: Grants = toml::from_str(TABLE).unwrap();
let route = "conversation/write_records";
let body = |id: &str| json!({"conversation_id": id, "draft": "d", "records": []});
assert!(refused(&grants, "client:reader", route, body("s-1")));
assert!(!refused(&grants, "client:writer", route, body("s-1")));
assert!(!refused(&grants, "client:both", route, body("s-2")));
assert!(refused(&grants, "client:writer", route, body("s-2")));
assert!(refused(&grants, "client:both", route, body("t-1")));
assert!(refused(&grants, "client:unknown", route, body("s-1")));
assert!(refused(
&grants,
"client:both",
route,
json!({"draft": "d"})
));
assert!(refused(
&grants,
"client:both",
route,
json!({"conversation_id": 7})
));
let read = "conversation/read_turns";
assert!(!refused(&grants, "client:reader", read, body("s-1")));
assert!(refused(&grants, "client:writer", read, body("s-1")));
let append = "conversation/append_turn";
assert!(refused(&grants, "client:reader", append, body("s-1")));
assert!(!refused(&grants, "client:writer", append, body("s-1")));
}
#[test]
fn read_records_needs_read_on_its_conversation() {
let grants: Grants = toml::from_str(TABLE).unwrap();
let route = "conversation/read_records";
let body = |id: &str| json!({"conversation_id": id});
assert!(!refused(&grants, "client:reader", route, body("s-1")));
assert!(!refused(&grants, "client:both", route, body("s-9")));
assert!(refused(&grants, "client:writer", route, body("s-1")));
assert!(refused(&grants, "client:reader", route, body("t-1")));
assert!(refused(&grants, "client:unknown", route, body("s-1")));
for body in [
json!({}),
json!({"conversation_id": 7}),
json!({"conversations": ["s-1"]}),
json!({"conversation_id": "t-1", "conversations": ["s-1"]}),
json!({"conversation_id": "t-1", "continuation": "s-1"}),
] {
assert!(refused(&grants, "client:reader", route, body));
}
}
#[test]
fn search_records_needs_read_on_every_conversation_it_lists() {
let grants: Grants = toml::from_str(TABLE).unwrap();
let route = "conversation/search_records";
let body = |ids: &[&str]| json!({"query": "x", "conversations": ids});
assert!(!refused(&grants, "client:reader", route, body(&["s-1"])));
assert!(!refused(
&grants,
"client:both",
route,
body(&["s-1", "s-9"])
));
assert!(!refused(&grants, "client:reader", route, body(&[])));
assert!(!refused(&grants, "client:writer", route, body(&[])));
assert!(refused(&grants, "client:unknown", route, body(&[])));
assert!(refused(&grants, "client:writer", route, body(&["s-1"])));
assert!(refused(
&grants,
"client:reader",
route,
body(&["s-1", "t-1"])
));
for actor in ["client:reader", "client:both"] {
for body in [
json!({"query": "x"}),
json!({"query": "x", "conversations": null}),
json!({"query": "x", "conversations": "s-1"}),
json!({"query": "x", "conversations": ["s-1", 7]}),
json!({"query": "x", "conversation_id": "s-1"}),
] {
assert!(refused(&grants, actor, route, body));
}
}
}
#[test]
fn sweep_records_is_refused_to_every_grant_but_the_operators() {
let table = format!("{TABLE}[[clients]]\nactor = \"client:ops\"\noperator = true\n");
let grants: Grants = toml::from_str(&table).unwrap();
let whole = |actor: &str, body: Value| {
let payload = body.as_object().expect("an object");
let refusal = grants.allowed(actor, "store/sweep_records", payload, true);
refusal.is_some()
};
assert!(!whole("client:ops", json!({})));
assert!(!whole("client:ops", json!({"write": true})));
for actor in [
"client:reader",
"client:writer",
"client:both",
"client:unknown",
] {
assert!(whole(actor, json!({})));
assert!(whole(actor, json!({"conversation_id": "s-1"})));
assert!(whole(actor, json!({"conversations": ["s-1"]})));
}
}