Enhance HostsManager to prevent modification and movement of default system entries; add is_default_entry method to HostEntry and update sorting methods to prioritize default entries.
This commit is contained in:
parent
8c1cd2047e
commit
3084650c27
6 changed files with 122 additions and 45 deletions
|
@ -326,9 +326,9 @@ class TestHostsManagerApp:
|
|||
|
||||
app.action_sort_by_ip()
|
||||
|
||||
# Check that entries are sorted
|
||||
assert app.hosts_file.entries[0].ip_address == "10.0.0.1"
|
||||
assert app.hosts_file.entries[1].ip_address == "127.0.0.1"
|
||||
# Check that entries are sorted with default entries on top
|
||||
assert app.hosts_file.entries[0].ip_address == "127.0.0.1" # Default entry first
|
||||
assert app.hosts_file.entries[1].ip_address == "10.0.0.1" # Then sorted non-defaults
|
||||
assert app.hosts_file.entries[2].ip_address == "192.168.1.1"
|
||||
|
||||
assert app.sort_column == "ip"
|
||||
|
|
|
@ -321,7 +321,7 @@ class TestHostsManager:
|
|||
manager.edit_mode = True
|
||||
|
||||
hosts_file = HostsFile()
|
||||
entry = HostEntry("127.0.0.1", ["localhost"], is_active=True)
|
||||
entry = HostEntry("192.168.1.1", ["router"], is_active=True) # Non-default entry
|
||||
hosts_file.entries.append(entry)
|
||||
|
||||
success, message = manager.toggle_entry(hosts_file, 0)
|
||||
|
@ -363,7 +363,7 @@ class TestHostsManager:
|
|||
manager.edit_mode = True
|
||||
|
||||
hosts_file = HostsFile()
|
||||
entry1 = HostEntry("127.0.0.1", ["localhost"])
|
||||
entry1 = HostEntry("10.0.0.1", ["test1"]) # Non-default entries
|
||||
entry2 = HostEntry("192.168.1.1", ["router"])
|
||||
hosts_file.entries.extend([entry1, entry2])
|
||||
|
||||
|
@ -372,7 +372,7 @@ class TestHostsManager:
|
|||
assert success
|
||||
assert "moved up" in message
|
||||
assert hosts_file.entries[0].hostnames[0] == "router"
|
||||
assert hosts_file.entries[1].hostnames[0] == "localhost"
|
||||
assert hosts_file.entries[1].hostnames[0] == "test1"
|
||||
|
||||
def test_move_entry_up_invalid_index(self):
|
||||
"""Test moving entry up with invalid index."""
|
||||
|
@ -396,7 +396,7 @@ class TestHostsManager:
|
|||
manager.edit_mode = True
|
||||
|
||||
hosts_file = HostsFile()
|
||||
entry1 = HostEntry("127.0.0.1", ["localhost"])
|
||||
entry1 = HostEntry("10.0.0.1", ["test1"]) # Non-default entries
|
||||
entry2 = HostEntry("192.168.1.1", ["router"])
|
||||
hosts_file.entries.extend([entry1, entry2])
|
||||
|
||||
|
@ -405,7 +405,7 @@ class TestHostsManager:
|
|||
assert success
|
||||
assert "moved down" in message
|
||||
assert hosts_file.entries[0].hostnames[0] == "router"
|
||||
assert hosts_file.entries[1].hostnames[0] == "localhost"
|
||||
assert hosts_file.entries[1].hostnames[0] == "test1"
|
||||
|
||||
def test_move_entry_down_invalid_index(self):
|
||||
"""Test moving entry down with invalid index."""
|
||||
|
@ -429,7 +429,7 @@ class TestHostsManager:
|
|||
manager.edit_mode = True
|
||||
|
||||
hosts_file = HostsFile()
|
||||
entry = HostEntry("127.0.0.1", ["localhost"])
|
||||
entry = HostEntry("10.0.0.1", ["test"]) # Non-default entry
|
||||
hosts_file.entries.append(entry)
|
||||
|
||||
success, message = manager.update_entry(
|
||||
|
@ -449,7 +449,7 @@ class TestHostsManager:
|
|||
manager.edit_mode = True
|
||||
|
||||
hosts_file = HostsFile()
|
||||
entry = HostEntry("127.0.0.1", ["localhost"])
|
||||
entry = HostEntry("127.0.0.1", ["localhost"]) # Default entry - cannot be modified
|
||||
hosts_file.entries.append(entry)
|
||||
|
||||
success, message = manager.update_entry(
|
||||
|
@ -457,7 +457,7 @@ class TestHostsManager:
|
|||
)
|
||||
|
||||
assert not success
|
||||
assert "Invalid entry data" in message
|
||||
assert "Cannot modify default system entries" in message
|
||||
|
||||
@patch('tempfile.NamedTemporaryFile')
|
||||
@patch('subprocess.run')
|
||||
|
|
|
@ -224,20 +224,21 @@ class TestHostsFile:
|
|||
assert inactive_entries[0] == inactive_entry
|
||||
|
||||
def test_sort_by_ip(self):
|
||||
"""Test sorting entries by IP address."""
|
||||
"""Test sorting entries by IP address with default entries on top."""
|
||||
hosts_file = HostsFile()
|
||||
entry1 = HostEntry(ip_address="192.168.1.1", hostnames=["router"])
|
||||
entry2 = HostEntry(ip_address="127.0.0.1", hostnames=["localhost"])
|
||||
entry2 = HostEntry(ip_address="127.0.0.1", hostnames=["localhost"]) # Default entry
|
||||
entry3 = HostEntry(ip_address="10.0.0.1", hostnames=["test"])
|
||||
|
||||
|
||||
hosts_file.add_entry(entry1)
|
||||
hosts_file.add_entry(entry2)
|
||||
hosts_file.add_entry(entry3)
|
||||
|
||||
|
||||
hosts_file.sort_by_ip()
|
||||
|
||||
assert hosts_file.entries[0].ip_address == "10.0.0.1"
|
||||
assert hosts_file.entries[1].ip_address == "127.0.0.1"
|
||||
|
||||
# Default entries should come first, then sorted non-default entries
|
||||
assert hosts_file.entries[0].ip_address == "127.0.0.1" # Default entry first
|
||||
assert hosts_file.entries[1].ip_address == "10.0.0.1" # Then sorted non-defaults
|
||||
assert hosts_file.entries[2].ip_address == "192.168.1.1"
|
||||
|
||||
def test_sort_by_hostname(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue