cargo-allow 0.1.3

Source-tree exception ledger and policy scanner for Rust repositories.
use crate::artifact_schema_support::{assert_enum_equals, parse_schema, required_schema_pointer};
use serde_json::Value;

#[test]
fn receipt_schema_locks_top_level_status_vocabulary() {
    let schema = parse_schema(
        "receipt",
        include_str!("../../../docs/schemas/receipt.schema.json"),
    );

    assert_enum_equals(
        "receipt status",
        &schema,
        "/properties/status/enum",
        allow_report::ARTIFACT_STATUSES,
    );
}

#[test]
fn receipt_schema_locks_check_command_producer() {
    let schema = parse_schema(
        "receipt",
        include_str!("../../../docs/schemas/receipt.schema.json"),
    );

    assert_eq!(
        schema
            .pointer("/properties/command/const")
            .and_then(Value::as_str),
        Some("check"),
        "receipt command producer"
    );
}

#[test]
fn receipt_schema_allows_optional_policy_baseline_debt_count() {
    let schema = parse_schema(
        "receipt",
        include_str!("../../../docs/schemas/receipt.schema.json"),
    );

    let count = required_schema_pointer(
        "receipt",
        &schema,
        "/$defs/counts/properties/policy_baseline_debt",
    );
    assert_eq!(
        count.get("type").and_then(Value::as_str),
        Some("integer"),
        "receipt policy_baseline_debt count type"
    );
    assert_eq!(
        count.get("minimum").and_then(Value::as_u64),
        Some(0),
        "receipt policy_baseline_debt count minimum"
    );
}

#[test]
fn receipt_schema_allows_optional_broken_evidence_link_count() {
    let schema = parse_schema(
        "receipt",
        include_str!("../../../docs/schemas/receipt.schema.json"),
    );

    let count = required_schema_pointer(
        "receipt",
        &schema,
        "/$defs/counts/properties/broken_evidence_links",
    );
    assert_eq!(
        count.get("type").and_then(Value::as_str),
        Some("integer"),
        "receipt broken_evidence_links count type"
    );
    assert_eq!(
        count.get("minimum").and_then(Value::as_u64),
        Some(0),
        "receipt broken_evidence_links count minimum"
    );
}

#[test]
fn receipt_schema_allows_optional_weak_evidence_reference_count() {
    let schema = parse_schema(
        "receipt",
        include_str!("../../../docs/schemas/receipt.schema.json"),
    );

    let count = required_schema_pointer(
        "receipt",
        &schema,
        "/$defs/counts/properties/weak_evidence_references",
    );
    assert_eq!(
        count.get("type").and_then(Value::as_str),
        Some("integer"),
        "receipt weak_evidence_references count type"
    );
    assert_eq!(
        count.get("minimum").and_then(Value::as_u64),
        Some(0),
        "receipt weak_evidence_references count minimum"
    );
}

#[test]
fn receipt_schema_allows_optional_policy_missing_evidence_count() {
    let schema = parse_schema(
        "receipt",
        include_str!("../../../docs/schemas/receipt.schema.json"),
    );

    let count = required_schema_pointer(
        "receipt",
        &schema,
        "/$defs/counts/properties/policy_missing_evidence",
    );
    assert_eq!(
        count.get("type").and_then(Value::as_str),
        Some("integer"),
        "receipt policy_missing_evidence count type"
    );
    assert_eq!(
        count.get("minimum").and_then(Value::as_u64),
        Some(0),
        "receipt policy_missing_evidence count minimum"
    );
}

#[test]
fn receipt_schema_allows_optional_source_inventory() {
    let schema = parse_schema(
        "receipt",
        include_str!("../../../docs/schemas/receipt.schema.json"),
    );

    let source_inventory =
        required_schema_pointer("receipt", &schema, "/properties/source_inventory/$ref");
    assert_eq!(
        source_inventory.as_str(),
        Some("#/$defs/source_inventory"),
        "receipt source_inventory should use the shared source inventory shape"
    );
    for pointer in [
        "/$defs/source_inventory/properties/findings",
        "/$defs/source_inventory_kind_row/properties/total",
        "/$defs/source_inventory_kind_row/properties/matched",
        "/$defs/source_inventory_kind_row/properties/new",
        "/$defs/source_inventory_kind_row/properties/review_items",
        "/$defs/source_inventory_family_row/properties/total",
        "/$defs/source_inventory_family_row/properties/matched",
        "/$defs/source_inventory_family_row/properties/new",
        "/$defs/source_inventory_family_row/properties/review_items",
    ] {
        let value = required_schema_pointer("receipt", &schema, pointer);
        assert_eq!(
            value.get("type").and_then(Value::as_str),
            Some("integer"),
            "{pointer} should be an integer"
        );
        assert_eq!(
            value.get("minimum").and_then(Value::as_u64),
            Some(0),
            "{pointer} minimum"
        );
    }
}