use openidauthzen::*;
#[test]
fn subject_should_serialize_with_type_field_renamed() {
let subject = Subject {
subject_type: "user".to_owned(),
id: Some("alice@example.com".to_owned()),
properties: None,
};
let json = serde_json::to_value(&subject).unwrap();
assert_eq!(json["type"], "user");
assert!(json.get("subject_type").is_none());
}
#[test]
fn subject_should_deserialize_from_type_field() {
let json = r#"{"type": "user", "id": "alice@example.com"}"#;
let subject: Subject = serde_json::from_str(json).unwrap();
assert_eq!(subject.subject_type, "user");
assert_eq!(subject.id.as_deref(), Some("alice@example.com"));
}
#[test]
fn subject_should_omit_id_when_none() {
let subject = Subject {
subject_type: "user".to_owned(),
id: None,
properties: None,
};
let json = serde_json::to_value(&subject).unwrap();
assert!(json.get("id").is_none());
}
#[test]
fn subject_should_omit_properties_when_none() {
let subject = Subject {
subject_type: "user".to_owned(),
id: Some("alice@example.com".to_owned()),
properties: None,
};
let json = serde_json::to_value(&subject).unwrap();
assert!(json.get("properties").is_none());
}
#[test]
fn subject_should_include_id_when_present() {
let subject = Subject {
subject_type: "user".to_owned(),
id: Some("alice@example.com".to_owned()),
properties: None,
};
let json = serde_json::to_value(&subject).unwrap();
assert_eq!(json["id"], "alice@example.com");
}
#[test]
fn subject_should_include_properties_when_present() {
let subject = Subject {
subject_type: "user".to_owned(),
id: Some("alice@example.com".to_owned()),
properties: Some(serde_json::json!({"department": "Sales"})),
};
let json = serde_json::to_value(&subject).unwrap();
assert_eq!(json["properties"]["department"], "Sales");
}
#[test]
fn subject_should_ignore_unknown_fields_on_deserialize() {
let json = r#"{"type": "user", "id": "alice@example.com", "unknown_field": 42}"#;
let subject: Subject = serde_json::from_str(json).unwrap();
assert_eq!(subject.subject_type, "user");
}
#[test]
fn subject_should_roundtrip_spec_example() {
let json = r#"{
"type": "user",
"id": "alice@example.com",
"properties": {
"department": "Sales"
}
}"#;
let subject: Subject = serde_json::from_str(json).unwrap();
assert_eq!(subject.subject_type, "user");
assert_eq!(subject.id.as_deref(), Some("alice@example.com"));
assert_eq!(subject.properties.as_ref().unwrap()["department"], "Sales");
let roundtripped: Subject = serde_json::from_value(serde_json::to_value(&subject).unwrap()).unwrap();
assert_eq!(roundtripped, subject);
}
#[test]
fn resource_should_serialize_with_type_field_renamed() {
let resource = Resource {
resource_type: "book".to_owned(),
id: Some("123".to_owned()),
properties: None,
};
let json = serde_json::to_value(&resource).unwrap();
assert_eq!(json["type"], "book");
assert!(json.get("resource_type").is_none());
}
#[test]
fn resource_should_deserialize_from_type_field() {
let json = r#"{"type": "book", "id": "123"}"#;
let resource: Resource = serde_json::from_str(json).unwrap();
assert_eq!(resource.resource_type, "book");
assert_eq!(resource.id.as_deref(), Some("123"));
}
#[test]
fn resource_should_omit_id_when_none() {
let resource = Resource {
resource_type: "account".to_owned(),
id: None,
properties: None,
};
let json = serde_json::to_value(&resource).unwrap();
assert!(json.get("id").is_none());
}
#[test]
fn resource_should_omit_properties_when_none() {
let resource = Resource {
resource_type: "book".to_owned(),
id: Some("123".to_owned()),
properties: None,
};
let json = serde_json::to_value(&resource).unwrap();
assert!(json.get("properties").is_none());
}
#[test]
fn resource_should_roundtrip_spec_example() {
let json = r#"{
"type": "book",
"id": "123",
"properties": {
"library_record": {
"title": "AuthZEN in Action",
"isbn": "978-0593383322"
}
}
}"#;
let resource: Resource = serde_json::from_str(json).unwrap();
assert_eq!(resource.resource_type, "book");
assert_eq!(resource.id.as_deref(), Some("123"));
let roundtripped: Resource = serde_json::from_value(serde_json::to_value(&resource).unwrap()).unwrap();
assert_eq!(roundtripped, resource);
}
#[test]
fn action_should_serialize_name_field() {
let action = Action {
name: "can_read".to_owned(),
properties: None,
};
let json = serde_json::to_value(&action).unwrap();
assert_eq!(json["name"], "can_read");
}
#[test]
fn action_should_omit_properties_when_none() {
let action = Action {
name: "can_read".to_owned(),
properties: None,
};
let json = serde_json::to_value(&action).unwrap();
assert!(json.get("properties").is_none());
}
#[test]
fn action_should_include_properties_when_present() {
let action = Action {
name: "extend-loan".to_owned(),
properties: Some(serde_json::json!({"period": "2W"})),
};
let json = serde_json::to_value(&action).unwrap();
assert_eq!(json["properties"]["period"], "2W");
}
#[test]
fn action_should_roundtrip_spec_example() {
let json = r#"{"name": "can_read"}"#;
let action: Action = serde_json::from_str(json).unwrap();
assert_eq!(action.name, "can_read");
assert!(action.properties.is_none());
let roundtripped: Action = serde_json::from_value(serde_json::to_value(&action).unwrap()).unwrap();
assert_eq!(roundtripped, action);
}
#[test]
fn decision_should_serialize_permit() {
let decision = Decision {
decision: true,
context: None,
};
let json = serde_json::to_value(&decision).unwrap();
assert_eq!(json["decision"], true);
}
#[test]
fn decision_should_serialize_deny() {
let decision = Decision {
decision: false,
context: None,
};
let json = serde_json::to_value(&decision).unwrap();
assert_eq!(json["decision"], false);
}
#[test]
fn decision_should_omit_context_when_none() {
let decision = Decision {
decision: true,
context: None,
};
let json = serde_json::to_value(&decision).unwrap();
assert!(json.get("context").is_none());
}
#[test]
fn decision_should_include_context_when_present() {
let decision = Decision {
decision: false,
context: Some(serde_json::json!({
"reason_admin": {"403": "Request failed policy C076E82F"},
"reason_user": {"403": "Insufficient privileges. Contact your administrator"}
})),
};
let json = serde_json::to_value(&decision).unwrap();
assert!(json["context"]["reason_admin"].is_object());
assert!(json["context"]["reason_user"].is_object());
}
#[test]
fn context_should_serialize_as_json_object() {
let mut ctx: Context = serde_json::Map::new();
ctx.insert("time".to_owned(), serde_json::json!("1985-10-26T01:22-07:00"));
let json = serde_json::to_value(&ctx).unwrap();
assert_eq!(json["time"], "1985-10-26T01:22-07:00");
}
#[test]
fn context_should_deserialize_from_json_object() {
let json = r#"{"time": "1985-10-26T01:22-07:00"}"#;
let ctx: Context = serde_json::from_str(json).unwrap();
assert_eq!(ctx["time"], "1985-10-26T01:22-07:00");
}