@@ -248,7 +248,9 @@ def test_deleteting_user(self):
248
248
self .assertEqual (historical_poll .history_user , None )
249
249
250
250
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
+ """
252
254
self .login ()
253
255
manager = Employee .objects .create ()
254
256
employee = Employee .objects .create (manager = manager )
@@ -259,6 +261,10 @@ def test_missing_one_to_one(self):
259
261
self .assertEqual (response .status_code , 200 )
260
262
261
263
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
+ """
262
268
request = RequestFactory ().post ('/' )
263
269
request .POST = {'_change_history' : True }
264
270
request .session = 'session'
@@ -269,12 +275,42 @@ def test_response_change(self):
269
275
poll .question = "how?"
270
276
poll .save ()
271
277
272
- admin = SimpleHistoryAdmin (Poll , 'admin' )
278
+ admin_site = AdminSite ()
279
+ admin = SimpleHistoryAdmin (Poll , admin_site )
273
280
274
- response = admin .response_change (request , poll )
281
+ with override_settings (SIMPLE_HISTORY_EDIT = True ):
282
+ response = admin .response_change (request , poll )
275
283
276
284
self .assertEqual (response .url , '/awesome/url/' )
277
285
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
+
278
314
def test_response_change_no_change_history (self ):
279
315
request = RequestFactory ().post ('/' )
280
316
request .session = 'session'
@@ -285,11 +321,12 @@ def test_response_change_no_change_history(self):
285
321
poll .question = "how?"
286
322
poll .save ()
287
323
288
- admin = SimpleHistoryAdmin (Poll , 'admin' )
324
+ admin_site = AdminSite ()
325
+ admin = SimpleHistoryAdmin (Poll , admin_site )
289
326
290
327
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'
293
330
response = admin .response_change (request , poll )
294
331
295
332
self .assertEqual (response , 'it was called' )
@@ -305,7 +342,6 @@ def test_history_form_view_without_getting_history(self):
305
342
poll .save ()
306
343
history = poll .history .all ()[0 ]
307
344
308
-
309
345
admin_site = AdminSite ()
310
346
admin = SimpleHistoryAdmin (Poll , admin_site )
311
347
@@ -315,8 +351,9 @@ def test_history_form_view_without_getting_history(self):
315
351
context = {
316
352
# Verify this is set for original object
317
353
'original' : poll ,
354
+ 'change_history' : False ,
318
355
319
- 'title' : 'Revert {}' .format (force_text (history )),
356
+ 'title' : 'Revert {}' .format (force_text (poll )),
320
357
'adminform' : ANY ,
321
358
'object_id' : poll .id ,
322
359
'is_popup' : False ,
@@ -363,16 +400,17 @@ def test_history_form_view_getting_history(self):
363
400
poll .save ()
364
401
history = poll .history .all ()[0 ]
365
402
366
-
367
403
admin_site = AdminSite ()
368
404
admin = SimpleHistoryAdmin (Poll , admin_site )
369
405
370
406
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 )
372
409
373
410
context = {
374
411
# Verify this is set for history object not poll object
375
412
'original' : history ,
413
+ 'change_history' : True ,
376
414
377
415
'title' : 'Revert {}' .format (force_text (history )),
378
416
'adminform' : ANY ,
@@ -408,3 +446,62 @@ def test_history_form_view_getting_history(self):
408
446
mock_render .assert_called_once_with (
409
447
request , template_name = admin .object_history_form_template ,
410
448
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