1
1
import datetime
2
2
from datetime import timedelta
3
- from unittest .mock import patch
3
+ from unittest .mock import Mock , patch
4
4
5
5
from ddt import data , ddt , unpack
6
6
from django .contrib .auth .models import AnonymousUser
@@ -96,7 +96,7 @@ def test_create_access_log_ignores_attempt_to_override_standard_fields(
96
96
# the standard fields should be set the same as any other access logs
97
97
self ._check_common_fields (log , AccessLogModelTestCase .super_user )
98
98
# we logged a warning for each attempt to override a field
99
- self .assertEquals (patched_warning .call_count , 4 )
99
+ self .assertEqual (patched_warning .call_count , 4 )
100
100
101
101
def test_basic_create_auth_log_from_request (self ):
102
102
request = self ._create_request (
@@ -412,7 +412,7 @@ def test_create_project_history_log_ignores_attempt_to_override_standard_fields(
412
412
# the standard fields should be set the same as any other project history logs
413
413
self ._check_common_fields (log , user , asset )
414
414
# we logged a warning for each attempt to override a field
415
- self .assertEquals (patched_warning .call_count , 3 )
415
+ self .assertEqual (patched_warning .call_count , 3 )
416
416
417
417
@data (
418
418
# source, asset_uid, ip_address, subtype
@@ -434,12 +434,125 @@ def test_create_project_history_log_requires_metadata_fields(
434
434
'asset_uid' : asset_uid ,
435
435
'log_subtype' : subtype ,
436
436
}
437
- # remove whatever we set to None
438
- # filtered = { k:v for k,v in metadata.items() if v is not None }
439
437
440
438
with self .assertRaises (ValidationError ):
441
439
ProjectHistoryLog .objects .create (
442
440
object_id = asset .id ,
443
441
metadata = metadata ,
444
442
user = user ,
445
443
)
444
+
445
+ # remove key
446
+ filtered = {k : v for k , v in metadata .items () if v is not None }
447
+ with self .assertRaises (ValidationError ):
448
+ ProjectHistoryLog .objects .create (
449
+ object_id = asset .id ,
450
+ metadata = filtered ,
451
+ user = user ,
452
+ )
453
+
454
+ def test_create_from_related_request_object_created (self ):
455
+ factory = RequestFactory ()
456
+ request = factory .post ('/' )
457
+ request .user = User .objects .get (username = 'someuser' )
458
+ request .resolver_match = Mock ()
459
+ request .resolver_match .kwargs = {'parent_lookup_asset' : 'a12345' }
460
+ # if an object has been created, only `updated_data` will be set
461
+ request .updated_data = {
462
+ 'object_id' : 1 ,
463
+ 'field_1' : 'a' ,
464
+ 'field_2' : 'b' ,
465
+ }
466
+ ProjectHistoryLog .create_from_related_request (
467
+ request ,
468
+ label = 'fieldname' ,
469
+ add_action = AuditAction .CREATE ,
470
+ delete_action = AuditAction .DELETE ,
471
+ modify_action = AuditAction .UPDATE ,
472
+ )
473
+ log = ProjectHistoryLog .objects .first ()
474
+ self .assertEqual (log .action , AuditAction .CREATE )
475
+ self .assertEqual (log .object_id , 1 )
476
+ # metadata should contain all additional fields that were stored in updated_data
477
+ # under the given label
478
+ self .assertDictEqual (
479
+ log .metadata ['fieldname' ], {'field_1' : 'a' , 'field_2' : 'b' }
480
+ )
481
+ self .assertEqual (log .metadata ['asset_uid' ], 'a12345' )
482
+
483
+ def test_create_from_related_request_object_deleted (self ):
484
+ factory = RequestFactory ()
485
+ request = factory .post ('/' )
486
+ request .user = User .objects .get (username = 'someuser' )
487
+ request .resolver_match = Mock ()
488
+ request .resolver_match .kwargs = {'parent_lookup_asset' : 'a12345' }
489
+ # if an object has been created, only `initial_data` will be set
490
+ request .initial_data = {
491
+ 'object_id' : 1 ,
492
+ 'field_1' : 'a' ,
493
+ 'field_2' : 'b' ,
494
+ }
495
+ ProjectHistoryLog .create_from_related_request (
496
+ request ,
497
+ label = 'label' ,
498
+ add_action = AuditAction .CREATE ,
499
+ delete_action = AuditAction .DELETE ,
500
+ modify_action = AuditAction .UPDATE ,
501
+ )
502
+ log = ProjectHistoryLog .objects .first ()
503
+ self .assertEqual (log .action , AuditAction .DELETE )
504
+ self .assertEqual (log .object_id , 1 )
505
+ # metadata should contain all additional fields that were stored in updated_data
506
+ # under the given label
507
+ self .assertDictEqual (log .metadata ['label' ], {'field_1' : 'a' , 'field_2' : 'b' })
508
+ self .assertEqual (log .metadata ['asset_uid' ], 'a12345' )
509
+
510
+ def test_create_from_related_request_object_modified (self ):
511
+ factory = RequestFactory ()
512
+ request = factory .post ('/' )
513
+ request .user = User .objects .get (username = 'someuser' )
514
+ request .resolver_match = Mock ()
515
+ request .resolver_match .kwargs = {'parent_lookup_asset' : 'a12345' }
516
+ # if an object has been modified, both `initial_data`
517
+ # and `updated_data` should be filled
518
+ request .initial_data = {
519
+ 'object_id' : 1 ,
520
+ 'field_1' : 'a' ,
521
+ 'field_2' : 'b' ,
522
+ }
523
+ request .updated_data = {
524
+ 'object_id' : 1 ,
525
+ 'field_1' : 'new_field1' ,
526
+ 'field_2' : 'new_field2' ,
527
+ }
528
+ ProjectHistoryLog .create_from_related_request (
529
+ request ,
530
+ label = 'label' ,
531
+ add_action = AuditAction .CREATE ,
532
+ delete_action = AuditAction .DELETE ,
533
+ modify_action = AuditAction .UPDATE ,
534
+ )
535
+ log = ProjectHistoryLog .objects .first ()
536
+ self .assertEqual (log .action , AuditAction .UPDATE )
537
+ self .assertEqual (log .object_id , 1 )
538
+ # we should use the updated data for the log
539
+ self .assertDictEqual (
540
+ log .metadata ['label' ], {'field_1' : 'new_field1' , 'field_2' : 'new_field2' }
541
+ )
542
+ self .assertEqual (log .metadata ['asset_uid' ], 'a12345' )
543
+
544
+ def test_create_from_related_request_no_log_created_if_no_data (self ):
545
+ factory = RequestFactory ()
546
+ request = factory .post ('/' )
547
+ request .user = User .objects .get (username = 'someuser' )
548
+ request .resolver_match = Mock ()
549
+ request .resolver_match .kwargs = {'parent_lookup_asset' : 'a12345' }
550
+ # no `initial_data` or `updated_data` present
551
+ ProjectHistoryLog .create_from_related_request (
552
+ request ,
553
+ label = 'label' ,
554
+ add_action = AuditAction .CREATE ,
555
+ delete_action = AuditAction .DELETE ,
556
+ modify_action = AuditAction .UPDATE ,
557
+ )
558
+ self .assertEqual (ProjectHistoryLog .objects .count (), 0 )
0 commit comments