@@ -248,7 +248,9 @@ def test_deleteting_user(self):
248248 self .assertEqual (historical_poll .history_user , None )
249249
250250 def test_missing_one_to_one (self ):
251- """A relation to a missing one-to-one model should still show history"""
251+ """
252+ A relation to a missing one-to-one model should still show history
253+ """
252254 self .login ()
253255 manager = Employee .objects .create ()
254256 employee = Employee .objects .create (manager = manager )
@@ -259,6 +261,10 @@ def test_missing_one_to_one(self):
259261 self .assertEqual (response .status_code , 200 )
260262
261263 def test_response_change (self ):
264+ """
265+ Test the response_change method that it works with a _change_history
266+ in the POST and settings.SIMPLE_HISTORY_EDIT set to True
267+ """
262268 request = RequestFactory ().post ('/' )
263269 request .POST = {'_change_history' : True }
264270 request .session = 'session'
@@ -269,12 +275,42 @@ def test_response_change(self):
269275 poll .question = "how?"
270276 poll .save ()
271277
272- admin = SimpleHistoryAdmin (Poll , 'admin' )
278+ admin_site = AdminSite ()
279+ admin = SimpleHistoryAdmin (Poll , admin_site )
273280
274- response = admin .response_change (request , poll )
281+ with override_settings (SIMPLE_HISTORY_EDIT = True ):
282+ response = admin .response_change (request , poll )
275283
276284 self .assertEqual (response .url , '/awesome/url/' )
277285
286+ def test_response_change_change_history_setting_off (self ):
287+ """
288+ Test the response_change method that it works with a _change_history
289+ in the POST and settings.SIMPLE_HISTORY_EDIT set to False
290+ """
291+ request = RequestFactory ().post ('/' )
292+ request .POST = {'_change_history' : True }
293+ request .session = 'session'
294+ request ._messages = FallbackStorage (request )
295+ request .path = '/awesome/url/'
296+ request .user = self .user
297+
298+ poll = Poll .objects .create (question = "why?" , pub_date = today )
299+ poll .question = "how?"
300+ poll .save ()
301+
302+ admin_site = AdminSite ()
303+ admin = SimpleHistoryAdmin (Poll , admin_site )
304+
305+ response = admin .response_change (request , poll )
306+
307+ with patch (
308+ 'simple_history.admin.ModelAdmin.response_change' ) as m_admin :
309+ m_admin .return_value = 'it was called'
310+ response = admin .response_change (request , poll )
311+
312+ self .assertEqual (response , 'it was called' )
313+
278314 def test_response_change_no_change_history (self ):
279315 request = RequestFactory ().post ('/' )
280316 request .session = 'session'
@@ -285,11 +321,12 @@ def test_response_change_no_change_history(self):
285321 poll .question = "how?"
286322 poll .save ()
287323
288- admin = SimpleHistoryAdmin (Poll , 'admin' )
324+ admin_site = AdminSite ()
325+ admin = SimpleHistoryAdmin (Poll , admin_site )
289326
290327 with patch (
291- 'simple_history.admin.ModelAdmin.response_change' ) as mock_admin :
292- mock_admin .return_value = 'it was called'
328+ 'simple_history.admin.ModelAdmin.response_change' ) as m_admin :
329+ m_admin .return_value = 'it was called'
293330 response = admin .response_change (request , poll )
294331
295332 self .assertEqual (response , 'it was called' )
@@ -305,7 +342,6 @@ def test_history_form_view_without_getting_history(self):
305342 poll .save ()
306343 history = poll .history .all ()[0 ]
307344
308-
309345 admin_site = AdminSite ()
310346 admin = SimpleHistoryAdmin (Poll , admin_site )
311347
@@ -315,8 +351,9 @@ def test_history_form_view_without_getting_history(self):
315351 context = {
316352 # Verify this is set for original object
317353 'original' : poll ,
354+ 'change_history' : False ,
318355
319- 'title' : 'Revert {}' .format (force_text (history )),
356+ 'title' : 'Revert {}' .format (force_text (poll )),
320357 'adminform' : ANY ,
321358 'object_id' : poll .id ,
322359 'is_popup' : False ,
@@ -363,16 +400,17 @@ def test_history_form_view_getting_history(self):
363400 poll .save ()
364401 history = poll .history .all ()[0 ]
365402
366-
367403 admin_site = AdminSite ()
368404 admin = SimpleHistoryAdmin (Poll , admin_site )
369405
370406 with patch ('simple_history.admin.render' ) as mock_render :
371- admin .history_form_view (request , poll .id , history .pk )
407+ with override_settings (SIMPLE_HISTORY_EDIT = True ):
408+ admin .history_form_view (request , poll .id , history .pk )
372409
373410 context = {
374411 # Verify this is set for history object not poll object
375412 'original' : history ,
413+ 'change_history' : True ,
376414
377415 'title' : 'Revert {}' .format (force_text (history )),
378416 'adminform' : ANY ,
@@ -408,3 +446,62 @@ def test_history_form_view_getting_history(self):
408446 mock_render .assert_called_once_with (
409447 request , template_name = admin .object_history_form_template ,
410448 dictionary = context , current_app = admin_site .name )
449+
450+ def test_history_form_view_getting_history_with_setting_off (self ):
451+ request = RequestFactory ().post ('/' )
452+ request .session = 'session'
453+ request ._messages = FallbackStorage (request )
454+ request .user = self .user
455+ request .POST = {'_change_history' : True }
456+
457+ poll = Poll .objects .create (question = "why?" , pub_date = today )
458+ poll .question = "how?"
459+ poll .save ()
460+ history = poll .history .all ()[0 ]
461+
462+ admin_site = AdminSite ()
463+ admin = SimpleHistoryAdmin (Poll , admin_site )
464+
465+ with patch ('simple_history.admin.render' ) as mock_render :
466+ with override_settings (SIMPLE_HISTORY_EDIT = False ):
467+ admin .history_form_view (request , poll .id , history .pk )
468+
469+ context = {
470+ # Verify this is set for history object not poll object
471+ 'original' : poll ,
472+ 'change_history' : False ,
473+
474+ 'title' : 'Revert {}' .format (force_text (poll )),
475+ 'adminform' : ANY ,
476+ 'object_id' : poll .id ,
477+ 'is_popup' : False ,
478+ 'media' : ANY ,
479+ 'errors' : [
480+ '<ul class="errorlist"><li>This field is required.</li></ul>' ,
481+ '<ul class="errorlist"><li>This field is required.</li></ul>'
482+ ],
483+ 'app_label' : 'tests' ,
484+ 'original_opts' : ANY ,
485+ 'changelist_url' : '/admin/tests/poll/' ,
486+ 'change_url' : '/admin/tests/poll/1/' ,
487+ 'history_url' : '/admin/tests/poll/1/history/' ,
488+ 'add' : False ,
489+ 'change' : True ,
490+ 'has_add_permission' : admin .has_add_permission (request ),
491+ 'has_change_permission' : admin .has_change_permission (
492+ request , poll ),
493+ 'has_delete_permission' : admin .has_delete_permission (
494+ request , poll ),
495+ 'has_file_field' : True ,
496+ 'has_absolute_url' : False ,
497+ 'form_url' : '' ,
498+ 'opts' : ANY ,
499+ 'content_type_id' : ANY ,
500+ 'save_as' : admin .save_as ,
501+ 'save_on_top' : admin .save_on_top ,
502+ 'root_path' : getattr (admin_site , 'root_path' , None ),
503+ }
504+
505+ mock_render .assert_called_once_with (
506+ request , template_name = admin .object_history_form_template ,
507+ dictionary = context , current_app = admin_site .name )
0 commit comments