Files
netbox-plugin-dns-bridge/netbox_dns_bridge/tests/test_models.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

171 lines
6.3 KiB
Python

"""Tests for plugin models: constraints, __str__, cascade behaviours."""
import pytest
from netbox_dns_bridge.models import (
CatalogZone,
CatalogZoneMemberIdentifier,
SeenTransferClients,
)
@pytest.mark.django_db
class TestCatalogZone:
def test_str_representation(self, make_view):
view = make_view(name="cz-str-view")
cz = CatalogZone.objects.create(view=view, soa_serial=42)
assert str(cz) == "cz-str-view: 42"
def test_default_soa_serial(self, make_view):
view = make_view(name="cz-default-view")
cz = CatalogZone.objects.create(view=view)
assert cz.soa_serial == 1
def test_one_to_one_relationship(self, make_view):
view = make_view(name="cz-oto-view")
cz = CatalogZone.objects.create(view=view, soa_serial=7)
assert view.catalog_zone == cz
def test_create_and_retrieve(self, make_view):
view = make_view(name="cz-retrieve-view")
CatalogZone.objects.create(view=view, soa_serial=99)
cz = CatalogZone.objects.get(view=view)
assert cz.soa_serial == 99
def test_ordering_by_view_name(self, make_view):
v1 = make_view(name="z-view")
v2 = make_view(name="a-view")
v3 = make_view(name="m-view")
CatalogZone.objects.create(view=v1, soa_serial=1)
CatalogZone.objects.create(view=v2, soa_serial=2)
CatalogZone.objects.create(view=v3, soa_serial=3)
names = list(
CatalogZone.objects.values_list("view__name", flat=True)
)
assert names == ["a-view", "m-view", "z-view"]
@pytest.mark.django_db
class TestCatalogZoneMemberIdentifier:
def test_str_representation(self, make_zone):
zone = make_zone(name="str.test")
m = CatalogZoneMemberIdentifier.objects.get(zone=zone)
assert str(m) == m.name
def test_unique_name_per_catalog_zone(self, make_zone, make_view):
view = make_view(name="shared-view")
zone1 = make_zone(name="uniq1.test", view=view)
zone2 = make_zone(name="uniq2.test", view=view)
ident1 = CatalogZoneMemberIdentifier.objects.get(zone=zone1)
ident1.name = "shared-name"
ident1.save()
ident2 = CatalogZoneMemberIdentifier.objects.get(zone=zone2)
from django.db import IntegrityError
with pytest.raises(IntegrityError):
ident2.name = "shared-name"
ident2.save()
def test_same_name_allowed_in_different_catalog_zones(self, make_zone, make_view):
view1 = make_view(name="view-a")
view2 = make_view(name="view-b")
zone1 = make_zone(name="diff1.test", view=view1)
zone2 = make_zone(name="diff2.test", view=view2)
ident1 = CatalogZoneMemberIdentifier.objects.get(zone=zone1)
ident1.name = "shared-name"
ident1.save()
ident2 = CatalogZoneMemberIdentifier.objects.get(zone=zone2)
ident2.name = "shared-name"
ident2.save()
def test_cascade_delete_when_zone_deleted(self, make_zone):
zone = make_zone(name="cascade.test")
m = CatalogZoneMemberIdentifier.objects.get(zone=zone)
assert CatalogZoneMemberIdentifier.objects.filter(pk=m.pk).exists()
zone.delete()
assert not CatalogZoneMemberIdentifier.objects.filter(pk=m.pk).exists()
def test_one_to_one_relationship(self, make_zone):
zone = make_zone(name="oto.test")
m = CatalogZoneMemberIdentifier.objects.get(zone=zone)
assert zone.catz_identifier == m
def test_ordering_by_name(self, make_zone, make_view):
view = make_view(name="ord-view")
z1 = make_zone(name="z1.test", view=view)
z2 = make_zone(name="z2.test", view=view)
z3 = make_zone(name="z3.test", view=view)
CatalogZoneMemberIdentifier.objects.filter(zone=z3).update(name="gamma")
CatalogZoneMemberIdentifier.objects.filter(zone=z1).update(name="alpha")
CatalogZoneMemberIdentifier.objects.filter(zone=z2).update(name="beta")
names = list(
CatalogZoneMemberIdentifier.objects.filter(
zone__in=[z1, z2, z3]
).values_list("name", flat=True)
)
assert names == ["alpha", "beta", "gamma"]
@pytest.mark.django_db
class TestSeenTransferClients:
def test_str_representation(self, make_view):
view = make_view(name="str-view")
client = SeenTransferClients.objects.create(
source_ip="192.0.2.1",
view=view,
last_seen="2026-01-01T00:00:00Z",
)
assert str(client) == f"192.0.2.1 ({view.name})"
def test_unique_ip_view_constraint(self, make_view):
from django.db import IntegrityError
view = make_view(name="uniq-view")
SeenTransferClients.objects.create(
source_ip="10.0.0.1",
view=view,
last_seen="2026-01-01T00:00:00Z",
)
with pytest.raises(IntegrityError):
SeenTransferClients.objects.create(
source_ip="10.0.0.1",
view=view,
last_seen="2026-01-02T00:00:00Z",
)
def test_same_ip_different_views_allowed(self, make_view):
v1 = make_view(name="v1")
v2 = make_view(name="v2")
SeenTransferClients.objects.create(
source_ip="10.0.0.5", view=v1, last_seen="2026-01-01T00:00:00Z"
)
SeenTransferClients.objects.create(
source_ip="10.0.0.5", view=v2, last_seen="2026-01-01T00:00:00Z"
)
assert SeenTransferClients.objects.filter(source_ip="10.0.0.5").count() == 2
def test_protect_on_view_delete(self, make_view):
from django.db.models import ProtectedError
view = make_view(name="protected-view")
SeenTransferClients.objects.create(
source_ip="10.0.0.9",
view=view,
last_seen="2026-01-01T00:00:00Z",
)
with pytest.raises(ProtectedError):
view.delete()
def test_ordering_by_source_ip(self, make_view):
from django.utils import timezone
v = make_view(name="ord-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=v, last_seen=now)
ips = list(SeenTransferClients.objects.values_list("source_ip", flat=True))
assert ips == ["10.0.0.1", "10.0.0.2", "10.0.0.3"]