6
6
from django .contrib .auth import get_user_model
7
7
from django .contrib .auth .models import AnonymousUser
8
8
from django .contrib .messages .middleware import MessageMiddleware
9
+ from django .contrib .messages .storage .fallback import FallbackStorage
9
10
from django .contrib .sessions .middleware import SessionMiddleware
10
11
from django .core .cache import cache
11
12
from django .test import RequestFactory , TestCase
12
13
from django .urls import reverse
14
+ from rest_framework import status
13
15
14
16
from netbox_diode_plugin .models import Setting
15
17
from netbox_diode_plugin .reconciler .sdk .v1 import ingester_pb2 , reconciler_pb2
@@ -35,7 +37,7 @@ def test_returns_200_for_authenticated(self):
35
37
self .request .user .is_staff = True
36
38
37
39
response = self .view .get (self .request )
38
- self .assertEqual (response .status_code , 200 )
40
+ self .assertEqual (response .status_code , status . HTTP_200_OK )
39
41
40
42
def test_redirects_to_login_page_for_unauthenticated_user (self ):
41
43
"""Test that the view returns 200 for an authenticated user."""
@@ -44,7 +46,7 @@ def test_redirects_to_login_page_for_unauthenticated_user(self):
44
46
45
47
response = IngestionLogsView .as_view ()(self .request )
46
48
47
- self .assertEqual (response .status_code , 302 )
49
+ self .assertEqual (response .status_code , status . HTTP_302_FOUND )
48
50
self .assertEqual (response .url , f"/netbox/login/?next={ self .path } " )
49
51
50
52
def test_ingestion_logs_failed_to_retrieve (self ):
@@ -53,7 +55,7 @@ def test_ingestion_logs_failed_to_retrieve(self):
53
55
self .request .user .is_staff = True
54
56
55
57
response = self .view .get (self .request )
56
- self .assertEqual (response .status_code , 200 )
58
+ self .assertEqual (response .status_code , status . HTTP_200_OK )
57
59
self .assertIn (
58
60
"UNAVAILABLE: failed to connect to all addresses;" , str (response .content )
59
61
)
@@ -100,7 +102,7 @@ def test_ingestion_logs_retrieve_logs(self):
100
102
response = self .view .get (self .request )
101
103
mock_retrieve_ingestion_logs .assert_called ()
102
104
self .assertEqual (mock_retrieve_ingestion_logs .call_count , 2 )
103
- self .assertEqual (response .status_code , 200 )
105
+ self .assertEqual (response .status_code , status . HTTP_200_OK )
104
106
self .assertNotIn ("Server Error" , str (response .content ))
105
107
106
108
def test_cached_metrics (self ):
@@ -153,7 +155,7 @@ def test_cached_metrics(self):
153
155
response = self .view .get (self .request )
154
156
mock_retrieve_ingestion_logs .assert_called ()
155
157
self .assertEqual (mock_retrieve_ingestion_logs .call_count , 1 )
156
- self .assertEqual (response .status_code , 200 )
158
+ self .assertEqual (response .status_code , status . HTTP_200_OK )
157
159
self .assertNotIn ("Server Error" , str (response .content ))
158
160
159
161
@@ -173,7 +175,7 @@ def test_returns_200_for_authenticated(self):
173
175
self .request .user .is_staff = True
174
176
175
177
response = self .view .get (self .request )
176
- self .assertEqual (response .status_code , 200 )
178
+ self .assertEqual (response .status_code , status . HTTP_200_OK )
177
179
178
180
def test_redirects_to_login_page_for_unauthenticated_user (self ):
179
181
"""Test that the view returns 200 for an authenticated user."""
@@ -182,7 +184,7 @@ def test_redirects_to_login_page_for_unauthenticated_user(self):
182
184
183
185
response = SettingsView .as_view ()(self .request )
184
186
185
- self .assertEqual (response .status_code , 302 )
187
+ self .assertEqual (response .status_code , status . HTTP_302_FOUND )
186
188
self .assertEqual (response .url , f"/netbox/login/?next={ self .path } " )
187
189
188
190
def test_settings_created_if_not_found (self ):
@@ -194,7 +196,7 @@ def test_settings_created_if_not_found(self):
194
196
mock_get .side_effect = Setting .DoesNotExist
195
197
196
198
response = self .view .get (self .request )
197
- self .assertEqual (response .status_code , 200 )
199
+ self .assertEqual (response .status_code , status . HTTP_200_OK )
198
200
self .assertIn (
199
201
"grpc://localhost:8080/diode" , str (response .content )
200
202
)
@@ -218,7 +220,7 @@ def test_returns_200_for_authenticated(self):
218
220
self .view .setup (request )
219
221
220
222
response = self .view .get (request )
221
- self .assertEqual (response .status_code , 200 )
223
+ self .assertEqual (response .status_code , status . HTTP_200_OK )
222
224
223
225
def test_redirects_to_login_page_for_unauthenticated_user (self ):
224
226
"""Test that the view redirects an authenticated user to login page."""
@@ -227,7 +229,7 @@ def test_redirects_to_login_page_for_unauthenticated_user(self):
227
229
self .view .setup (request )
228
230
229
231
response = self .view .get (request )
230
- self .assertEqual (response .status_code , 302 )
232
+ self .assertEqual (response .status_code , status . HTTP_302_FOUND )
231
233
self .assertEqual (response .url , f"/netbox/login/?next={ self .path } " )
232
234
233
235
def test_settings_updated (self ):
@@ -241,7 +243,7 @@ def test_settings_updated(self):
241
243
self .view .setup (request )
242
244
243
245
response = self .view .get (request )
244
- self .assertEqual (response .status_code , 200 )
246
+ self .assertEqual (response .status_code , status . HTTP_200_OK )
245
247
self .assertIn ("grpc://localhost:8080/diode" , str (response .content ))
246
248
247
249
request = self .request_factory .post (self .path )
@@ -258,7 +260,7 @@ def test_settings_updated(self):
258
260
request .session .save ()
259
261
260
262
response = self .view .post (request )
261
- self .assertEqual (response .status_code , 302 )
263
+ self .assertEqual (response .status_code , status . HTTP_302_FOUND )
262
264
self .assertEqual (response .url , reverse ("plugins:netbox_diode_plugin:settings" ))
263
265
264
266
request = self .request_factory .get (self .path )
@@ -267,7 +269,7 @@ def test_settings_updated(self):
267
269
self .view .setup (request )
268
270
269
271
response = self .view .get (request )
270
- self .assertEqual (response .status_code , 200 )
272
+ self .assertEqual (response .status_code , status . HTTP_200_OK )
271
273
self .assertIn ("grpc://localhost:8090/diode" , str (response .content ))
272
274
273
275
def test_settings_update_post_redirects_to_login_page_for_unauthenticated_user (
@@ -280,5 +282,45 @@ def test_settings_update_post_redirects_to_login_page_for_unauthenticated_user(
280
282
request .POST = {"diode_target" : "grpc://localhost:8090/diode" }
281
283
282
284
response = self .view .post (request )
283
- self .assertEqual (response .status_code , 302 )
285
+ self .assertEqual (response .status_code , status . HTTP_302_FOUND )
284
286
self .assertEqual (response .url , f"/netbox/login/?next={ self .path } " )
287
+
288
+ def test_settings_update_disallowed (self ):
289
+ """Test that the Diode target cannot be overridden."""
290
+ with mock .patch ("netbox_diode_plugin.views.netbox_settings" ) as mock_settings :
291
+ mock_settings .PLUGINS_CONFIG = {
292
+ "netbox_diode_plugin" : {
293
+ "disallow_diode_target_override" : True
294
+ }
295
+ }
296
+
297
+ user = User .objects .create_user ("foo" , password = "pass" )
298
+ user .is_staff = True
299
+
300
+ request = self .request_factory .post (self .path )
301
+ request .user = user
302
+ request .htmx = None
303
+ request .POST = {"diode_target" : "grpc://localhost:8090/diode" }
304
+
305
+ middleware = SessionMiddleware (get_response = lambda request : None )
306
+ middleware .process_request (request )
307
+ request .session .save ()
308
+
309
+ middleware = MessageMiddleware (get_response = lambda request : None )
310
+ middleware .process_request (request )
311
+ request .session .save ()
312
+
313
+ setattr (request , 'session' , 'session' )
314
+ messages = FallbackStorage (request )
315
+ request ._messages = messages
316
+
317
+ self .view .setup (request )
318
+ response = self .view .post (request )
319
+
320
+ self .assertEqual (response .status_code , status .HTTP_302_FOUND )
321
+ self .assertEqual (response .url , reverse ("plugins:netbox_diode_plugin:settings" ))
322
+ self .assertEqual (len (request ._messages ._queued_messages ), 1 )
323
+ self .assertEqual (
324
+ str (request ._messages ._queued_messages [0 ]),
325
+ "The Diode target is not allowed to be overridden." ,
326
+ )
0 commit comments