use super::*;
#[test]
fn charset_is_rfc6749_3_3() {
assert!(Scope::new("read").is_ok());
assert!(Scope::new("urn:example:channel=HBO&level=5").is_ok());
assert!(Scope::new("!#[]~").is_ok());
assert!(Scope::new("").is_err(), "empty token");
assert!(
Scope::new("has space").is_err(),
"space is the delimiter, not scope content"
);
assert!(Scope::new("dq\"uote").is_err(), "0x22 excluded");
assert!(Scope::new("back\\slash").is_err(), "0x5C excluded");
assert!(Scope::new("caf\u{e9}").is_err(), "non-ASCII excluded");
}
#[test]
fn parse_dedupes_orders_and_roundtrips() {
let set = ScopeSet::parse("write read read").unwrap();
assert_eq!(set.len(), 2);
assert_eq!(set.to_string(), "read write");
let json = serde_json::to_string(&set).unwrap();
assert_eq!(json, "\"read write\"");
let back: ScopeSet = serde_json::from_str(&json).unwrap();
assert_eq!(back, set);
}
#[test]
fn subset_semantics() {
let all = ScopeSet::parse("a b c").unwrap();
let some = ScopeSet::parse("a c").unwrap();
let other = ScopeSet::parse("a d").unwrap();
assert!(some.is_subset(&all));
assert!(!other.is_subset(&all));
assert!(ScopeSet::empty().is_subset(&all));
}
#[test]
fn the_serialized_scope_string_is_sorted_deduplicated_and_space_delimited() {
let s = ScopeSet::parse("write read admin").unwrap();
assert_eq!(
serde_json::to_string(&s).unwrap(),
r#""admin read write""#,
"the wire form is sorted, because a BTreeSet was and every stored record was written by one"
);
let dup = ScopeSet::parse("read read write read").unwrap();
assert_eq!(serde_json::to_string(&dup).unwrap(), r#""read write""#);
let numeric = ScopeSet::parse("2 10 1 20 100 0").unwrap();
assert_eq!(
serde_json::to_string(&numeric).unwrap(),
r#""0 1 10 100 2 20""#
);
let prefix = ScopeSet::parse("aaa a aa").unwrap();
assert_eq!(serde_json::to_string(&prefix).unwrap(), r#""a aa aaa""#);
let mixed = ScopeSet::parse("read Read READ").unwrap();
assert_eq!(
serde_json::to_string(&mixed).unwrap(),
r#""READ Read read""#
);
assert_eq!(serde_json::to_string(&ScopeSet::empty()).unwrap(), r#""""#);
let stored: ScopeSet = serde_json::from_str(r#""admin read write""#).unwrap();
assert_eq!(stored, ScopeSet::parse("write admin read").unwrap());
}