Files
netbox-plugin-dns-bridge/netbox_dns_bridge/tests/test_views.py
T
sven 14805ed32b Move from hard-coded CatalogZone SOA values (refresh, retry, expire and minimum)
to DB driven to permit user modification:

modified:   netbox_dns_bridge/models.py
  Add 4 cols for values.

modified:   netbox_dns_bridge/migrations/0003_catalogzone_and_per_zone_identifier.py
  Adjust migration. This expects that the last migration the user has is 0002 (<=v1.5.8)

modified:   netbox_dns_bridge/jobs/notify.py
modified:   netbox_dns_bridge/management/commands/dns_transfer_endpoint/catalog_zone_manager.py
  Update to use new db values for SOA creation.

Change UI to permit editing for SOA values and disable member identifier editing.
modified:   netbox_dns_bridge/forms.py
modified:   netbox_dns_bridge/navigation.py
new file:   netbox_dns_bridge/templates/netbox_dns_bridge/catalog_zone_edit.html
new file:   netbox_dns_bridge/templates/netbox_dns_bridge/catalog_zone_list.html
new file:   netbox_dns_bridge/templates/netbox_dns_bridge/catalog_zone_members.html
deleted:    netbox_dns_bridge/templates/netbox_dns_bridge/catz_overview.html
modified:   netbox_dns_bridge/tables.py
modified:   netbox_dns_bridge/urls.py
modified:   netbox_dns_bridge/views.py
modified:   netbox_dns_bridge/filtersets.py
  AI created/modified: Left the UI update to the LLM.

modified:   netbox_dns_bridge/tests/test_catalog_zone_manager.py
modified:   netbox_dns_bridge/tests/test_models.py
modified:   netbox_dns_bridge/tests/test_notify.py
modified:   netbox_dns_bridge/tests/test_views.py
  AI created/modified: Updating tests
2026-08-17 13:20:17 +02:00

183 lines
7.3 KiB
Python

"""Tests for views: CatalogZoneMembersView, CatalogZoneListView, CatalogZoneEditView, NotifyOverviewView — auth gate and context."""
import pytest
from django.test import Client
from django.contrib.auth import get_user_model
@pytest.fixture
def client():
return Client()
@pytest.fixture
def superuser(db):
User = get_user_model()
user = User(username="admin", email="admin@test.local", is_superuser=True)
user.set_password("testpass")
user.save()
return user
@pytest.fixture
def regular_user(db):
User = get_user_model()
user = User(username="regular", email="reg@test.local")
user.set_password("testpass")
user.save()
return user
@pytest.mark.django_db
class TestCatalogZoneMembersView:
def test_unauthenticated_redirects_to_login(self, client):
resp = client.get("/plugins/dns-bridge/catalog-zone-members/")
assert resp.status_code in (302, 403)
def test_regular_user_forbidden(self, client, regular_user):
client.login(username="regular", password="testpass")
resp = client.get("/plugins/dns-bridge/catalog-zone-members/")
assert resp.status_code in (302, 403)
def test_superuser_can_access(self, client, superuser):
client.login(username="admin", password="testpass")
resp = client.get("/plugins/dns-bridge/catalog-zone-members/")
assert resp.status_code == 200
@pytest.mark.django_db
class TestCatalogZoneListView:
def test_unauthenticated_redirects_to_login(self, client):
resp = client.get("/plugins/dns-bridge/catalog-zones/")
assert resp.status_code in (302, 403)
def test_regular_user_forbidden(self, client, regular_user):
client.login(username="regular", password="testpass")
resp = client.get("/plugins/dns-bridge/catalog-zones/")
assert resp.status_code in (302, 403)
def test_superuser_can_access(self, client, superuser):
client.login(username="admin", password="testpass")
resp = client.get("/plugins/dns-bridge/catalog-zones/")
assert resp.status_code == 200
def test_list_contains_catalog_zones(self, client, superuser, catz_serial, make_zone):
zone = make_zone(name="ctx.test")
client.login(username="admin", password="testpass")
resp = client.get("/plugins/dns-bridge/catalog-zones/")
cz_list = list(resp.context["table"].data)
assert len(cz_list) >= 1
cz = next(c for c in cz_list if c.view_id == zone.view_id)
assert cz.soa_serial == catz_serial
def test_list_empty_when_no_catalog_zones(self, client, superuser):
client.login(username="admin", password="testpass")
resp = client.get("/plugins/dns-bridge/catalog-zones/")
cz_list = list(resp.context["table"].data)
assert cz_list == []
@pytest.mark.django_db
class TestCatalogZoneEditView:
def test_unauthenticated_redirects_to_login(self, client, make_view):
view = make_view(name="edit-cz-view")
from netbox_dns_bridge.models import CatalogZone
cz = CatalogZone.objects.create(view=view)
resp = client.get(f"/plugins/dns-bridge/catalog-zones/{cz.pk}/edit/")
assert resp.status_code in (302, 403)
def test_regular_user_forbidden(self, client, regular_user, make_view):
view = make_view(name="edit-cz-view-forbidden")
from netbox_dns_bridge.models import CatalogZone
cz = CatalogZone.objects.create(view=view)
client.login(username="regular", password="testpass")
resp = client.get(f"/plugins/dns-bridge/catalog-zones/{cz.pk}/edit/")
assert resp.status_code in (302, 403)
def test_superuser_can_get_edit_form(self, client, superuser, make_view):
view = make_view(name="edit-cz-view-get")
from netbox_dns_bridge.models import CatalogZone
cz = CatalogZone.objects.create(view=view)
client.login(username="admin", password="testpass")
resp = client.get(f"/plugins/dns-bridge/catalog-zones/{cz.pk}/edit/")
assert resp.status_code == 200
def test_superuser_can_update_soa_fields(self, client, superuser, make_view):
view = make_view(name="edit-cz-view-post")
from netbox_dns_bridge.models import CatalogZone
cz = CatalogZone.objects.create(view=view)
client.login(username="admin", password="testpass")
resp = client.post(
f"/plugins/dns-bridge/catalog-zones/{cz.pk}/edit/",
{
"soa_serial": 100,
"soa_refresh": 300,
"soa_retry": 100,
"soa_expire": 86400,
"soa_minimum": 3600,
},
)
cz.refresh_from_db()
assert cz.soa_serial == 100
assert cz.soa_refresh == 300
assert cz.soa_retry == 100
assert cz.soa_expire == 86400
assert cz.soa_minimum == 3600
def test_no_create_route(self, client, superuser):
client.login(username="admin", password="testpass")
resp = client.get("/plugins/dns-bridge/catalog-zones/add/")
assert resp.status_code == 404
def test_no_delete_route(self, client, superuser, make_view):
view = make_view(name="edit-cz-view-delete")
from netbox_dns_bridge.models import CatalogZone
cz = CatalogZone.objects.create(view=view)
client.login(username="admin", password="testpass")
resp = client.get(f"/plugins/dns-bridge/catalog-zones/{cz.pk}/delete/")
assert resp.status_code == 404
@pytest.mark.django_db
class TestNotifyOverviewView:
def test_unauthenticated_redirects_to_login(self, client):
resp = client.get("/plugins/dns-bridge/notify/")
assert resp.status_code in (302, 403)
def test_regular_user_forbidden(self, client, regular_user):
client.login(username="regular", password="testpass")
resp = client.get("/plugins/dns-bridge/notify/")
assert resp.status_code in (302, 403)
def test_superuser_can_access(self, client, superuser):
client.login(username="admin", password="testpass")
resp = client.get("/plugins/dns-bridge/notify/")
assert resp.status_code == 200
def test_context_contains_seen_clients(self, client, superuser, make_view):
from netbox_dns_bridge.models import SeenTransferClients
from django.utils import timezone
view = make_view(name="notify-view")
SeenTransferClients.objects.create(
source_ip="192.0.2.1", view=view, last_seen=timezone.now()
)
client.login(username="admin", password="testpass")
resp = client.get("/plugins/dns-bridge/notify/")
assert "seen_clients" in resp.context
clients = list(resp.context["seen_clients"])
assert len(clients) == 1
assert clients[0].source_ip == "192.0.2.1"
def test_seen_clients_ordered_by_ip(self, client, superuser, make_view):
from netbox_dns_bridge.models import SeenTransferClients
from django.utils import timezone
view = make_view(name="ord-notify-view")
now = timezone.now()
for ip in ["10.0.0.3", "10.0.0.1", "10.0.0.2"]:
SeenTransferClients.objects.create(source_ip=ip, view=view, last_seen=now)
client.login(username="admin", password="testpass")
resp = client.get("/plugins/dns-bridge/notify/")
ips = [c.source_ip for c in resp.context["seen_clients"]]
assert ips == ["10.0.0.1", "10.0.0.2", "10.0.0.3"]