33"""Diode NetBox Plugin - Tests."""
44from unittest import mock
55
6+ from django .contrib import messages
67from django .contrib .auth import get_user_model
78from django .contrib .auth .models import AnonymousUser
89from 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