Skip to content

Commit 9fbac4c

Browse files
committed
test: add unit tests for branch_id field and related properties in Setting model
Signed-off-by: Michal Fiedorowicz <mfiedorowicz@netboxlabs.com>
1 parent 24b3c73 commit 9fbac4c

File tree

2 files changed

+103
-27
lines changed

2 files changed

+103
-27
lines changed

netbox_diode_plugin/tests/test_models.py

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env python
22
# Copyright 2025 NetBox Labs, Inc.
33
"""Diode NetBox Plugin - Tests."""
4+
from unittest import mock
5+
6+
from django.apps import apps
47
from django.core.exceptions import ValidationError
58
from django.test import TestCase
69

@@ -17,19 +20,98 @@ def test_validators(self):
1720
with self.assertRaises(ValidationError):
1821
setting.clean_fields()
1922

20-
2123
def test_str(self):
2224
"""Check Setting model string representation."""
2325
setting = Setting(diode_target="http://localhost:8080")
2426
self.assertEqual(str(setting), "")
2527

26-
2728
def test_absolute_url(self):
2829
"""Check Setting model absolute URL."""
2930
setting = Setting()
3031
self.assertEqual(setting.get_absolute_url(), "/netbox/plugins/diode/settings/")
3132

32-
def test_tags_disabled(self):
33-
"""Check Setting model has tags disabled."""
34-
setting = Setting(diode_target="http://localhost:8080")
35-
self.assertIsNone(setting.tags)
33+
def test_branch_id_field_exists(self):
34+
"""Check Setting model has branch_id field."""
35+
setting = Setting(diode_target="grpc://localhost:8080/diode")
36+
self.assertIsNone(setting.branch_id)
37+
38+
# Set branch_id
39+
setting.branch_id = 123
40+
self.assertEqual(setting.branch_id, 123)
41+
42+
def test_branch_property_returns_none_when_no_branch_id(self):
43+
"""Check branch property returns None when branch_id is not set."""
44+
setting = Setting(diode_target="grpc://localhost:8080/diode")
45+
self.assertIsNone(setting.branch)
46+
47+
def test_branch_property_returns_none_when_plugin_not_installed(self):
48+
"""Check branch property returns None when branching plugin is not installed."""
49+
setting = Setting(diode_target="grpc://localhost:8080/diode", branch_id=123)
50+
51+
# Mock the import to simulate plugin not being available
52+
with mock.patch.dict('sys.modules', {'netbox_branching.models': None}):
53+
self.assertIsNone(setting.branch)
54+
55+
def test_branch_property_returns_branch_when_available(self):
56+
"""Check branch property returns Branch object when available."""
57+
if not apps.is_installed("netbox_branching"):
58+
self.skipTest("netbox_branching plugin not installed")
59+
60+
from netbox_branching.models import Branch
61+
62+
# Create a test branch
63+
branch = Branch.objects.create(name="test-branch")
64+
65+
setting = Setting(diode_target="grpc://localhost:8080/diode", branch_id=branch.id)
66+
67+
# Check branch property returns the correct branch
68+
self.assertEqual(setting.branch.id, branch.id)
69+
self.assertEqual(setting.branch.name, "test-branch")
70+
71+
# Clean up
72+
branch.delete()
73+
74+
def test_branch_setter(self):
75+
"""Check branch setter updates branch_id."""
76+
if not apps.is_installed("netbox_branching"):
77+
self.skipTest("netbox_branching plugin not installed")
78+
79+
from netbox_branching.models import Branch
80+
81+
# Create a test branch
82+
branch = Branch.objects.create(name="test-branch-setter")
83+
84+
setting = Setting(diode_target="grpc://localhost:8080/diode")
85+
86+
# Use setter to assign branch
87+
setting.branch = branch
88+
self.assertEqual(setting.branch_id, branch.id)
89+
90+
# Set to None
91+
setting.branch = None
92+
self.assertIsNone(setting.branch_id)
93+
94+
# Clean up
95+
branch.delete()
96+
97+
def test_branch_schema_id_property(self):
98+
"""Check branch_schema_id property returns schema_id when branch is set."""
99+
if not apps.is_installed("netbox_branching"):
100+
self.skipTest("netbox_branching plugin not installed")
101+
102+
from netbox_branching.models import Branch
103+
104+
# Create a test branch
105+
branch = Branch.objects.create(name="test-branch-schema")
106+
107+
setting = Setting(diode_target="grpc://localhost:8080/diode", branch_id=branch.id)
108+
109+
# Check branch_schema_id returns the schema_id
110+
self.assertEqual(setting.branch_schema_id, branch.schema_id)
111+
112+
# Check it returns None when no branch
113+
setting.branch_id = None
114+
self.assertIsNone(setting.branch_schema_id)
115+
116+
# Clean up
117+
branch.delete()

netbox_diode_plugin/tests/test_views.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""Diode NetBox Plugin - Tests."""
44
from unittest import mock
55

6+
from django.contrib import messages
67
from django.contrib.auth import get_user_model
78
from django.contrib.auth.models import AnonymousUser
89
from django.contrib.messages.middleware import MessageMiddleware
@@ -158,8 +159,8 @@ def test_settings_update_post_redirects_to_login_page_for_unauthenticated_user(
158159
self.assertEqual(response.status_code, status.HTTP_302_FOUND)
159160
self.assertEqual(response.url, f"/netbox/login/?next={self.path}")
160161

161-
def test_settings_update_disallowed_on_get_method(self):
162-
"""Test that the accessing settings edit is not allowed with diode target override."""
162+
def test_settings_update_allowed_on_get_method_with_override(self):
163+
"""Test that accessing settings edit shows info message when diode target is overridden."""
163164
with mock.patch(
164165
"netbox_diode_plugin.views.get_plugin_config"
165166
) as mock_get_plugin_config:
@@ -173,7 +174,7 @@ def test_settings_update_disallowed_on_get_method(self):
173174
"netbox_diode_plugin.change_setting",
174175
)
175176

176-
request = self.request_factory.post(self.path)
177+
request = self.request_factory.get(self.path)
177178
request.user = user
178179
request.htmx = None
179180

@@ -185,25 +186,22 @@ def test_settings_update_disallowed_on_get_method(self):
185186
middleware.process_request(request)
186187
request.session.save()
187188

188-
setattr(request, "session", "session")
189-
messages = FallbackStorage(request)
190-
request._messages = messages
191-
192189
self.view.setup(request)
193190
response = self.view.get(request)
194191

195-
self.assertEqual(response.status_code, status.HTTP_302_FOUND)
196-
self.assertEqual(
197-
response.url, reverse("plugins:netbox_diode_plugin:settings")
198-
)
199-
self.assertEqual(len(request._messages._queued_messages), 1)
192+
self.assertEqual(response.status_code, status.HTTP_200_OK)
193+
194+
# Check that the message was added
195+
storage = messages.get_messages(request)
196+
message_list = list(storage)
197+
self.assertEqual(len(message_list), 1)
200198
self.assertEqual(
201-
str(request._messages._queued_messages[0]),
202-
"The Diode target is not allowed to be modified.",
199+
str(message_list[0]),
200+
"The Diode target field is disabled because it is overridden in the plugin configuration.",
203201
)
204202

205-
def test_settings_update_disallowed_on_post_method(self):
206-
"""Test that the updating settings is not allowed with diode target override."""
203+
def test_settings_update_allowed_on_post_method_with_override(self):
204+
"""Test that updating settings succeeds when diode target is overridden (field is disabled in form)."""
207205
with mock.patch(
208206
"netbox_diode_plugin.views.get_plugin_config"
209207
) as mock_get_plugin_config:
@@ -237,12 +235,8 @@ def test_settings_update_disallowed_on_post_method(self):
237235
self.view.setup(request)
238236
response = self.view.post(request)
239237

238+
# Should succeed and redirect to settings view
240239
self.assertEqual(response.status_code, status.HTTP_302_FOUND)
241240
self.assertEqual(
242241
response.url, reverse("plugins:netbox_diode_plugin:settings")
243242
)
244-
self.assertEqual(len(request._messages._queued_messages), 1)
245-
self.assertEqual(
246-
str(request._messages._queued_messages[0]),
247-
"The Diode target is not allowed to be modified.",
248-
)

0 commit comments

Comments
 (0)