use wasm_capability_contract::{CapabilityGrant, CapabilityScope};
const GRANTED_CAPABILITIES_JSON: &str = r#"[
{ "name": "some-capability", "scope": { "allowed": ["api.example.com"] } },
{ "name": "another-capability", "scope": { "allowed": ["method-a", "method-b"] } },
{ "name": "no-restrictions-capability", "scope": { "allowed": ["*"] } },
{ "name": "empty-scope-capability", "scope": { "allowed": [] } }
]"#;
#[test]
fn test_manifest_fragment_deserializes_into_grants_with_correct_scope() {
let grants: Vec<CapabilityGrant> = serde_json::from_str(GRANTED_CAPABILITIES_JSON)
.unwrap_or_else(|e| panic!("manifest fragment must deserialize: {e}"));
assert_eq!(grants.len(), 4, "expected exactly four grants");
let by_name = |name: &str| {
grants
.iter()
.find(|g| g.name == name)
.unwrap_or_else(|| panic!("expected a grant named '{name}'"))
};
assert_eq!(
by_name("some-capability").scope,
CapabilityScope {
allowed: vec!["api.example.com".to_string()]
}
);
assert_eq!(
by_name("another-capability").scope,
CapabilityScope {
allowed: vec!["method-a".to_string(), "method-b".to_string()]
}
);
assert_eq!(
by_name("no-restrictions-capability").scope,
CapabilityScope {
allowed: vec!["*".to_string()]
}
);
assert_eq!(
by_name("empty-scope-capability").scope,
CapabilityScope { allowed: vec![] }
);
}
#[test]
fn test_full_granted_capabilities_list_round_trips_unchanged() {
let grants: Vec<CapabilityGrant> = serde_json::from_str(GRANTED_CAPABILITIES_JSON)
.unwrap_or_else(|e| panic!("must deserialize: {e}"));
let json = serde_json::to_string(&grants).unwrap_or_else(|e| panic!("must serialize: {e}"));
let restored: Vec<CapabilityGrant> =
serde_json::from_str(&json).unwrap_or_else(|e| panic!("must re-deserialize: {e}"));
assert_eq!(restored, grants);
}