TEST: field locking test fixes 1

Signed-off-by: jokob-sk <jokob.sk@gmail.com>
This commit is contained in:
jokob-sk
2026-01-28 22:52:57 +11:00
parent b4348c18b6
commit 6388afbb1e
2 changed files with 54 additions and 58 deletions

View File

@@ -6,7 +6,7 @@
# GitHub recommends pinning actions to a commit SHA.
# To get a newer version, you will need to update the SHA.
# You can also reference a tag or branch, but the action may change without warning.
name: 🐳 📦 Publish Docker image
name: 🐳 🚀 Publish Docker image
on:
release:

View File

@@ -10,208 +10,206 @@ from server.db.authoritative_handler import can_overwrite_field
def test_locked_source_prevents_plugin_overwrite():
"""Field with LOCKED source should NOT be updated by plugins."""
result = can_overwrite_field(
field_name="devName",
current_value="ExistingName",
current_source="LOCKED",
plugin_prefix="ARPSCAN",
plugin_settings={"set_always": [], "set_empty": []},
field_value="New Name",
)
assert result is False, "LOCKED source should prevent plugin overwrites"
assert result is False
def test_user_source_prevents_plugin_overwrite():
"""Field with USER source should NOT be updated by plugins."""
result = can_overwrite_field(
field_name="devName",
current_value="UserName",
current_source="USER",
plugin_prefix="NBTSCAN",
plugin_settings={"set_always": [], "set_empty": []},
field_value="Plugin Discovered Name",
)
assert result is False, "USER source should prevent plugin overwrites"
assert result is False
def test_newdev_source_allows_plugin_overwrite():
"""Field with NEWDEV source should be updated by plugins."""
result = can_overwrite_field(
field_name="devName",
current_value="",
current_source="NEWDEV",
plugin_prefix="NBTSCAN",
plugin_settings={"set_always": [], "set_empty": []},
field_value="DiscoveredName",
)
assert result is True, "NEWDEV source should allow plugin overwrites"
assert result is True
def test_empty_current_source_allows_plugin_overwrite():
"""Field with empty source should be updated by plugins."""
result = can_overwrite_field(
field_name="devName",
current_value="",
current_source="",
plugin_prefix="NBTSCAN",
plugin_settings={"set_always": [], "set_empty": []},
field_value="DiscoveredName",
)
assert result is True, "Empty source should allow plugin overwrites"
assert result is True
def test_plugin_source_allows_same_plugin_overwrite_with_set_always():
"""Field owned by plugin can be updated by same plugin if SET_ALWAYS enabled."""
result = can_overwrite_field(
field_name="devName",
current_value="OldName",
current_source="NBTSCAN",
plugin_prefix="NBTSCAN",
plugin_settings={"set_always": ["devName"], "set_empty": []},
field_value="NewName",
)
assert result is True, "Same plugin with SET_ALWAYS should update its own fields"
assert result is True
def test_plugin_source_cannot_overwrite_without_authorization():
"""Plugin cannot update field it already owns without SET_ALWAYS/SET_EMPTY."""
result = can_overwrite_field(
field_name="devName",
current_value="OldName",
current_source="NBTSCAN",
plugin_prefix="NBTSCAN",
plugin_settings={"set_always": [], "set_empty": []},
field_value="NewName",
)
assert result is False, "Plugin cannot update owned field without SET_ALWAYS/SET_EMPTY"
assert result is False
def test_plugin_source_allows_different_plugin_overwrite_with_set_always():
"""Field owned by plugin can be overwritten by different plugin if SET_ALWAYS enabled."""
result = can_overwrite_field(
field_name="devVendor",
current_value="OldVendor",
current_source="ARPSCAN",
plugin_prefix="PIHOLEAPI",
plugin_settings={"set_always": ["devVendor"], "set_empty": []},
field_value="NewVendor",
)
assert result is True, "Different plugin with SET_ALWAYS should be able to overwrite"
assert result is True
def test_plugin_source_rejects_different_plugin_without_set_always():
"""Field owned by plugin should NOT be updated by different plugin without SET_ALWAYS."""
result = can_overwrite_field(
field_name="devVendor",
current_value="OldVendor",
current_source="ARPSCAN",
plugin_prefix="PIHOLEAPI",
plugin_settings={"set_always": [], "set_empty": []},
field_value="NewVendor",
)
assert result is False, "Different plugin without SET_ALWAYS should not overwrite plugin-owned fields"
assert result is False
def test_set_empty_allows_overwrite_on_empty_field():
"""SET_EMPTY allows overwriting when field is truly empty."""
result = can_overwrite_field(
field_name="devName",
current_source="NEWDEV",
current_value="",
current_source="ARPSCAN",
plugin_prefix="PIHOLEAPI",
plugin_settings={"set_always": [], "set_empty": ["devName"]},
field_value="DiscoveredName",
)
assert result is True, "SET_EMPTY should allow overwrite on NEWDEV source"
assert result is True
def test_set_empty_rejects_overwrite_on_non_empty_field():
"""SET_EMPTY should NOT allow overwriting non-empty plugin-owned fields."""
result = can_overwrite_field(
field_name="devName",
current_value="ExistingName",
current_source="ARPSCAN",
plugin_prefix="PIHOLEAPI",
plugin_settings={"set_always": [], "set_empty": ["devName"]},
field_value="NewName",
)
assert result is False, "SET_EMPTY should not allow overwrite on non-empty plugin field"
assert result is False
def test_empty_plugin_value_not_used():
"""Plugin must provide non-empty value for update to occur."""
result = can_overwrite_field(
field_name="devName",
current_value="",
current_source="NEWDEV",
plugin_prefix="NBTSCAN",
plugin_settings={"set_always": [], "set_empty": []},
field_value="",
)
assert result is False, "Empty plugin value should be rejected"
assert result is False
def test_whitespace_only_plugin_value_not_used():
"""Plugin providing whitespace-only value should be rejected."""
result = can_overwrite_field(
field_name="devName",
current_value="",
current_source="NEWDEV",
plugin_prefix="NBTSCAN",
plugin_settings={"set_always": [], "set_empty": []},
field_value=" ",
)
assert result is False, "Whitespace-only plugin value should be rejected"
assert result is False
def test_none_plugin_value_not_used():
"""Plugin providing None value should be rejected."""
result = can_overwrite_field(
field_name="devName",
current_value="",
current_source="NEWDEV",
plugin_prefix="NBTSCAN",
plugin_settings={"set_always": [], "set_empty": []},
field_value=None,
)
assert result is False, "None plugin value should be rejected"
assert result is False
def test_set_always_overrides_plugin_ownership():
"""SET_ALWAYS should allow overwriting any non-protected field."""
# Test 1: SET_ALWAYS overrides other plugin ownership
result = can_overwrite_field(
field_name="devVendor",
current_value="OldVendor",
current_source="ARPSCAN",
plugin_prefix="UNIFIAPI",
plugin_settings={"set_always": ["devVendor"], "set_empty": []},
field_value="NewVendor",
)
assert result is True, "SET_ALWAYS should override plugin ownership"
assert result is True
# Test 2: SET_ALWAYS does NOT override USER
result = can_overwrite_field(
field_name="devVendor",
current_value="UserVendor",
current_source="USER",
plugin_prefix="UNIFIAPI",
plugin_settings={"set_always": ["devVendor"], "set_empty": []},
field_value="NewVendor",
)
assert result is False, "SET_ALWAYS should not override USER source"
assert result is False
# Test 3: SET_ALWAYS does NOT override LOCKED
result = can_overwrite_field(
field_name="devVendor",
current_value="LockedVendor",
current_source="LOCKED",
plugin_prefix="UNIFIAPI",
plugin_settings={"set_always": ["devVendor"], "set_empty": []},
field_value="NewVendor",
)
assert result is False, "SET_ALWAYS should not override LOCKED source"
assert result is False
def test_multiple_plugins_set_always_scenarios():
"""Test SET_ALWAYS with multiple different plugins."""
# current_source, plugin_prefix, has_set_always
plugins_scenarios = [
("ARPSCAN", "ARPSCAN", False), # Same plugin, no SET_ALWAYS - BLOCKED
("ARPSCAN", "ARPSCAN", True), # Same plugin, WITH SET_ALWAYS - ALLOWED
("ARPSCAN", "NBTSCAN", False), # Different plugin, no SET_ALWAYS - BLOCKED
("ARPSCAN", "PIHOLEAPI", True), # Different plugin, PIHOLEAPI has SET_ALWAYS - ALLOWED
("ARPSCAN", "UNIFIAPI", True), # Different plugin, UNIFIAPI has SET_ALWAYS - ALLOWED
("ARPSCAN", "ARPSCAN", False),
("ARPSCAN", "ARPSCAN", True),
("ARPSCAN", "NBTSCAN", False),
("ARPSCAN", "PIHOLEAPI", True),
("ARPSCAN", "UNIFIAPI", True),
]
for current_source, plugin_prefix, has_set_always in plugins_scenarios:
result = can_overwrite_field(
field_name="devName",
current_value="ExistingName",
current_source=current_source,
plugin_prefix=plugin_prefix,
plugin_settings={"set_always": ["devName"] if has_set_always else [], "set_empty": []},
@@ -219,45 +217,43 @@ def test_multiple_plugins_set_always_scenarios():
)
if has_set_always:
assert result is True, f"Should allow with SET_ALWAYS: {current_source} -> {plugin_prefix}"
assert result is True
else:
assert result is False, f"Should reject without SET_ALWAYS: {current_source} -> {plugin_prefix}"
assert result is False
def test_different_fields_with_different_sources():
"""Test that each field respects its own source tracking."""
# Device has mixed sources
fields_sources = [
("devName", "USER"), # User-owned
("devVendor", "ARPSCAN"), # Plugin-owned
("devLastIP", "NEWDEV"), # Default
("devFQDN", "LOCKED"), # Locked
("devName", "USER", "UserValue"),
("devVendor", "ARPSCAN", "VendorX"),
("devLastIP", "NEWDEV", ""),
("devFQDN", "LOCKED", "locked.example.com"),
]
results = {}
for field_name, current_source in fields_sources:
for field_name, current_source, current_value in fields_sources:
results[field_name] = can_overwrite_field(
field_name=field_name,
current_value=current_value,
current_source=current_source,
plugin_prefix="NBTSCAN",
plugin_settings={"set_always": [], "set_empty": []},
field_value="NewValue",
)
# Verify each field's result based on its source
assert results["devName"] is False, "USER source should prevent overwrite"
assert results["devVendor"] is False, "Plugin source without SET_ALWAYS should prevent overwrite"
assert results["devLastIP"] is True, "NEWDEV source should allow overwrite"
assert results["devFQDN"] is False, "LOCKED source should prevent overwrite"
assert results["devName"] is False
assert results["devVendor"] is False
assert results["devLastIP"] is True
assert results["devFQDN"] is False
def test_set_empty_with_empty_string_source():
"""SET_EMPTY with empty string source should allow overwrite."""
result = can_overwrite_field(
field_name="devName",
current_value="",
current_source="",
plugin_prefix="PIHOLEAPI",
plugin_settings={"set_always": [], "set_empty": ["devName"]},
field_value="DiscoveredName",
)
assert result is True, "SET_EMPTY with empty source should allow overwrite"
assert result is True