|
1 | 1 | // SPDX-License-Identifier: Apache-2.0 |
2 | 2 |
|
| 3 | +use std::collections::BTreeSet; |
| 4 | + |
3 | 5 | use registry_breg::compiler::{compile_project, module_digest, CompileProfile}; |
4 | | -use registry_breg::contract::{parse_module_yaml, parse_project_json, parse_project_yaml}; |
| 6 | +use registry_breg::contract::{ |
| 7 | + parse_module_json, parse_module_yaml, parse_project_json, parse_project_yaml, |
| 8 | +}; |
5 | 9 | use registry_breg::odrl_export::{export_odrl, PROFILE_IRI, VOCAB}; |
6 | 10 | use registry_breg::{CompiledRegistry, GeneratedArtifact}; |
7 | 11 | use serde_json::{json, Value}; |
@@ -146,6 +150,21 @@ fn registered_coverage_example_matches_minimal_project() { |
146 | 150 | ); |
147 | 151 | } |
148 | 152 |
|
| 153 | +#[test] |
| 154 | +fn registered_policy_example_matches_authoring_display_fixture() { |
| 155 | + let project = parse_project_yaml(include_bytes!( |
| 156 | + "../../../products/breg/fixtures/authoring-display/registry.yaml" |
| 157 | + )) |
| 158 | + .unwrap(); |
| 159 | + let registry = compile_project(&project, &[], CompileProfile::Authoring).unwrap(); |
| 160 | + let export = export_odrl(®istry, None).unwrap(); |
| 161 | + assert_eq!( |
| 162 | + artifact(&export.artifacts, "generated/odrl/policies.jsonld").bytes, |
| 163 | + include_bytes!("../../../products/breg/examples/formats/odrl-policies.jsonld") |
| 164 | + ); |
| 165 | + assert!(export.coverage["totals"]["exported"].as_u64().unwrap() > 0); |
| 166 | +} |
| 167 | + |
149 | 168 | #[test] |
150 | 169 | fn supported_reads_preserve_profile_conditions_and_projection_fields() { |
151 | 170 | let registry = compile_source(&supported_source()); |
@@ -212,6 +231,7 @@ fn supported_reads_preserve_profile_conditions_and_projection_fields() { |
212 | 231 | assert_eq!(policies[format!("{VOCAB}partial")], true); |
213 | 232 | assert!(policies["http://purl.org/dc/terms/description"].is_string()); |
214 | 233 | let policy = &policies["@graph"][0]; |
| 234 | + assert_eq!(policy[format!("{VOCAB}partial")], true); |
215 | 235 | assert_eq!(policy["odrl:profile"], json!([{"@id":PROFILE_IRI}])); |
216 | 236 | let permissions = policy["odrl:permission"] |
217 | 237 | .as_array() |
@@ -455,3 +475,134 @@ fn exact_selection_of_known_action_only_profile_is_empty_and_valid() { |
455 | 475 | ); |
456 | 476 | } |
457 | 477 | } |
| 478 | + |
| 479 | +#[test] |
| 480 | +fn exact_selection_accepts_profiles_from_modules_and_extensions() { |
| 481 | + let project = parse_project_json( |
| 482 | + &serde_json::to_vec(&json!({ |
| 483 | + "apiVersion": "id.registrystack.org/formats/breg/project/v1alpha1", |
| 484 | + "kind": "BRegProject", |
| 485 | + "project": {"id":"module-profile", "version":"1", "defaultLanguage":"en", |
| 486 | + "canonicalBaseIri":"https://authoring.example.test"}, |
| 487 | + "datasets": [{"id":"records"}], |
| 488 | + "recordTypes": [{ |
| 489 | + "id":"case-file", "primaryDataset":"records", "route":"case-files", |
| 490 | + "mutationMode":"mutable", |
| 491 | + "fields":[{"id":"code", "type":"string", "maximumLength":32, |
| 492 | + "classification":"internal"}] |
| 493 | + }], |
| 494 | + "modules": [{"id":"core", "version":"1"}] |
| 495 | + })) |
| 496 | + .unwrap(), |
| 497 | + ) |
| 498 | + .unwrap(); |
| 499 | + let profile = |id| { |
| 500 | + json!({ |
| 501 | + "id":id, "principalClaim":"sub", "operations":["get"], |
| 502 | + "readableFields":["code"], "requiredScopes":"unrestricted", |
| 503 | + "rowBoundaries":"unrestricted" |
| 504 | + }) |
| 505 | + }; |
| 506 | + let module = parse_module_json( |
| 507 | + &serde_json::to_vec(&json!({ |
| 508 | + "apiVersion":"id.registrystack.org/formats/breg/module/v1alpha1", |
| 509 | + "kind":"BRegModule", "id":"core", "version":"1", |
| 510 | + "recordTypes":[{ |
| 511 | + "id":"module-record", "primaryDataset":"records", "route":"module-records", |
| 512 | + "mutationMode":"mutable", |
| 513 | + "fields":[{"id":"code", "type":"string", "maximumLength":32, |
| 514 | + "classification":"internal"}], |
| 515 | + "accessProfiles":[profile("module-reader")] |
| 516 | + }], |
| 517 | + "extendRecordTypes":[{ |
| 518 | + "recordType":"case-file", "accessProfiles":[profile("extension-reader")] |
| 519 | + }] |
| 520 | + })) |
| 521 | + .unwrap(), |
| 522 | + ) |
| 523 | + .unwrap(); |
| 524 | + let registry = compile_project(&project, &[module], CompileProfile::Authoring).unwrap(); |
| 525 | + assert!(registry.access_profile_displays().is_empty()); |
| 526 | + for (id, record_type) in [ |
| 527 | + ("module-reader", "module-record"), |
| 528 | + ("extension-reader", "case-file"), |
| 529 | + ] { |
| 530 | + let export = export_odrl(®istry, Some(id)).expect("compiled profile is selectable"); |
| 531 | + assert_eq!(export.coverage["selectedAccessProfile"], id); |
| 532 | + assert_eq!( |
| 533 | + export.coverage["totals"], |
| 534 | + json!({"considered":1,"exported":1,"omitted":0}) |
| 535 | + ); |
| 536 | + assert_eq!(export.coverage["grants"][0]["recordType"], record_type); |
| 537 | + assert_eq!(export.coverage["grants"][0]["accessProfile"], id); |
| 538 | + } |
| 539 | + assert!(export_odrl(®istry, Some("module")).is_err()); |
| 540 | +} |
| 541 | + |
| 542 | +#[test] |
| 543 | +fn profile_declares_every_emitted_extension_and_adopts_read() { |
| 544 | + fn emitted_terms(value: &Value, terms: &mut BTreeSet<String>) { |
| 545 | + match value { |
| 546 | + Value::Object(object) => { |
| 547 | + for (key, value) in object { |
| 548 | + if key.starts_with(VOCAB) { |
| 549 | + terms.insert(key.clone()); |
| 550 | + } |
| 551 | + emitted_terms(value, terms); |
| 552 | + } |
| 553 | + } |
| 554 | + Value::Array(values) => { |
| 555 | + for value in values { |
| 556 | + emitted_terms(value, terms); |
| 557 | + } |
| 558 | + } |
| 559 | + Value::String(value) if value.starts_with(VOCAB) || value == ODRL_READ => { |
| 560 | + terms.insert(value.clone()); |
| 561 | + } |
| 562 | + _ => {} |
| 563 | + } |
| 564 | + } |
| 565 | + |
| 566 | + let registry = compile_source(&supported_source()); |
| 567 | + let export = export_odrl(®istry, None).unwrap(); |
| 568 | + let policies = document(&export, "generated/odrl/policies.jsonld"); |
| 569 | + let profile = document(&export, "generated/odrl/profile.jsonld"); |
| 570 | + let graph = profile["@graph"].as_array().unwrap(); |
| 571 | + let collection = graph |
| 572 | + .iter() |
| 573 | + .find(|node| node["@type"] == "skos:Collection") |
| 574 | + .expect("profile vocabulary is a machine-readable collection"); |
| 575 | + let expand = |value: &Value| { |
| 576 | + let id = value.as_str().unwrap(); |
| 577 | + id.strip_prefix("breg:").map_or_else( |
| 578 | + || id.replace("odrl:", "http://www.w3.org/ns/odrl/2/"), |
| 579 | + |term| format!("{VOCAB}{term}"), |
| 580 | + ) |
| 581 | + }; |
| 582 | + let members = collection["skos:member"] |
| 583 | + .as_array() |
| 584 | + .unwrap() |
| 585 | + .iter() |
| 586 | + .map(|member| expand(&member["@id"])) |
| 587 | + .collect::<BTreeSet<_>>(); |
| 588 | + let mut emitted = BTreeSet::new(); |
| 589 | + emitted_terms(&policies, &mut emitted); |
| 590 | + assert_eq!( |
| 591 | + emitted.len(), |
| 592 | + 14, |
| 593 | + "thirteen BReg terms and odrl:read are exercised" |
| 594 | + ); |
| 595 | + assert_eq!(members, emitted); |
| 596 | + for term in graph |
| 597 | + .iter() |
| 598 | + .filter(|node| node["@type"] != "skos:Collection") |
| 599 | + { |
| 600 | + assert_eq!(term["rdfs:isDefinedBy"]["@id"], PROFILE_IRI); |
| 601 | + assert!(members.contains(&expand(&term["@id"]))); |
| 602 | + } |
| 603 | + assert_eq!( |
| 604 | + graph.len(), |
| 605 | + 14, |
| 606 | + "one collection and thirteen term definitions" |
| 607 | + ); |
| 608 | +} |
0 commit comments