Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions tests/utils/test_autotailor.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,98 @@ def test_get_datastream_uri():
uri = t._get_datastream_uri()
assert uri.startswith("file://")
assert "relative/path/to/ds.xml" in uri


def test_datastream_validator():
"""Test that DataStreamValidator properly validates IDs."""
ds_path = pathlib.Path(__file__).parent.joinpath("data_stream.xml")
validator = autotailor.DataStreamValidator(str(ds_path))

# Test valid profile validation
validator.validate_profile("xccdf_com.example.www_profile_P1")

# Test valid value validation
validator.validate_value("xccdf_com.example.www_value_V1")
validator.validate_value("xccdf_com.example.www_value_V2")

# Test valid rule validation
validator.validate_rule("xccdf_com.example.www_rule_R1")
validator.validate_rule("xccdf_com.example.www_rule_R2")
validator.validate_rule("xccdf_com.example.www_rule_R3")
validator.validate_rule("xccdf_com.example.www_rule_R4")

# Test valid group validation
validator.validate_group("xccdf_com.example.www_group_G34")

# Test invalid profile
with pytest.raises(ValueError) as e:
validator.validate_profile("xccdf_com.example.www_profile_INVALID")
assert "Profile ID 'xccdf_com.example.www_profile_INVALID' does not exist" in str(e.value)

# Test invalid value with suggestion
with pytest.raises(ValueError) as e:
validator.validate_value("xccdf_com.example.www_value_V3")
assert "Value ID 'xccdf_com.example.www_value_V3' does not exist" in str(e.value)

# Test invalid rule with suggestion
with pytest.raises(ValueError) as e:
validator.validate_rule("xccdf_com.example.www_rule_R5")
assert "Rule ID 'xccdf_com.example.www_rule_R5' does not exist" in str(e.value)

# Test invalid group
with pytest.raises(ValueError) as e:
validator.validate_group("xccdf_com.example.www_group_INVALID")
assert "Group ID 'xccdf_com.example.www_group_INVALID' does not exist" in str(e.value)


def test_profile_with_validator():
"""Test that Profile uses validator to check IDs."""
ds_path = pathlib.Path(__file__).parent.joinpath("data_stream.xml")
validator = autotailor.DataStreamValidator(str(ds_path))

p = autotailor.Profile(validator=validator)
p.reverse_dns = "com.example.www"

# Test valid variable change works
p.add_value_change("V1", "30")

# Test invalid variable name fails
with pytest.raises(ValueError) as e:
p.add_value_change("INVALID_VAR", "test")
assert "Value ID 'xccdf_com.example.www_value_INVALID_VAR' does not exist" in str(e.value)

# Test valid rule selection works
p.select_rule("R1")

# Test invalid rule selection fails
with pytest.raises(ValueError) as e:
p.select_rule("INVALID_RULE")
assert "Rule ID 'xccdf_com.example.www_rule_INVALID_RULE' does not exist" in str(e.value)

# Test valid base profile validation
p.validate_base_profile("P1")

# Test invalid base profile fails
with pytest.raises(ValueError) as e:
p.validate_base_profile("INVALID_PROFILE")
assert "Profile ID 'xccdf_com.example.www_profile_INVALID_PROFILE' does not exist" in str(e.value)


def test_validator_suggestions():
"""Test that validator provides helpful suggestions for typos."""
ds_path = pathlib.Path(__file__).parent.joinpath("data_stream.xml")
validator = autotailor.DataStreamValidator(str(ds_path))

# Test suggestion for value with typo (V11 instead of V1)
with pytest.raises(ValueError) as e:
validator.validate_value("xccdf_com.example.www_value_V11")
error_msg = str(e.value)
assert "Did you mean one of these?" in error_msg
assert "xccdf_com.example.www_value_V1" in error_msg

# Test suggestion for rule with typo (R11 instead of R1)
with pytest.raises(ValueError) as e:
validator.validate_rule("xccdf_com.example.www_rule_R11")
error_msg = str(e.value)
assert "Did you mean one of these?" in error_msg
assert "xccdf_com.example.www_rule_R1" in error_msg
Loading
Loading