Files
netbox-plugin-dns-bridge/netbox_dns_bridge/tests/test_views.py
T
sven 9f5e40ae48 Replace global IntegerKeyValueSetting with per-view CatalogZone model
Track the catalog zone SOA serial per view instead of a single global value.
The new CatalogZone model has a 1:1 FK to netbox_dns.models.View and its own
soa_serial field; each view's catalog zone now increments independently. The
existing global serial is preserved during migration by copying it into every
view's new CatalogZone row.

Change CatalogZoneMemberIdentifier.name from globally unique to unique per
catalog zone (UniqueConstraint(name, catalog_zone)) and add a catalog_zone FK
to enforce it.

Remove the dns-settings management command, which was a generic interface over
the now-removed IntegerKeyValueSetting model.

Increment both catalog zone serials when a zone moves between views: the old
view's catalog zone loses a member and the new view's gains one, so both must
be bumped. Also refresh the member identifier's catalog_zone FK on a view move
so it does not go stale. The pre_save handler now captures _old_view_id and the
post_save handler detects view changes, increments the old view's serial inside
on_commit (before the new view's), and re-creates the identifier.

AI generated, code manually verified.
2026-08-17 10:02:59 +02:00

106 lines
4.0 KiB
Python

"""Tests for views: CatzOverviewView, 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 TestCatzOverviewView:
def test_unauthenticated_redirects_to_login(self, client):
resp = client.get("/plugins/dns-bridge/catz/")
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/catz/")
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/catz/")
assert resp.status_code == 200
def test_context_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/catz/")
assert "catalog_zones" in resp.context
cz_list = list(resp.context["catalog_zones"])
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_context_no_catalog_zones_when_empty(self, client, superuser):
client.login(username="admin", password="testpass")
resp = client.get("/plugins/dns-bridge/catz/")
assert "catalog_zones" in resp.context
assert list(resp.context["catalog_zones"]) == []
@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"]