new file: conftest.py

new file:   netbox_dns_bridge/tests/__init__.py
new file:   netbox_dns_bridge/tests/_helpers.py
new file:   netbox_dns_bridge/tests/conftest.py
new file:   netbox_dns_bridge/tests/test_bootstrap.py
new file:   netbox_dns_bridge/tests/test_catalog_zone_manager.py
new file:   netbox_dns_bridge/tests/test_dns_server.py
new file:   netbox_dns_bridge/tests/test_management_commands.py
new file:   netbox_dns_bridge/tests/test_models.py
new file:   netbox_dns_bridge/tests/test_notify_handler.py
new file:   netbox_dns_bridge/tests/test_request_handler_integration.py
new file:   netbox_dns_bridge/tests/test_request_handler_unit.py
new file:   netbox_dns_bridge/tests/test_signals.py
new file:   netbox_dns_bridge/tests/test_smoke.py
new file:   netbox_dns_bridge/tests/test_utils.py
new file:   netbox_dns_bridge/tests/test_views.py
modified:   pyproject.toml
Add some tests. #AI generated
This commit is contained in:
sven committed 2026-08-12 12:25:46 +02:00
1 parent e8762bf807
commit 4ad110ca53
17 files changed
+2680

No files matched your search

+102
View File
@@ -0,0 +1,102 @@
"""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_soa_serial(self, client, superuser, catz_serial):
client.login(username="admin", password="testpass")
resp = client.get("/plugins/dns-bridge/catz/")
assert "soa_serial" in resp.context
assert resp.context["soa_serial"] == catz_serial
def test_context_soa_serial_none_when_not_set(self, client, superuser):
from netbox_dns_bridge.models import IntegerKeyValueSetting
IntegerKeyValueSetting.objects.filter(key="catalog-zone-soa-serial").delete()
client.login(username="admin", password="testpass")
resp = client.get("/plugins/dns-bridge/catz/")
assert resp.context["soa_serial"] is None
@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"]