@@ -76,9 +76,10 @@ class InitData:
76
76
audit_rule_2 : models .AuditRule
77
77
audit_action_1 : models .AuditAction
78
78
audit_action_2 : models .AuditAction
79
+ audit_action_3 : models .AuditAction
79
80
audit_event_1 : models .AuditEvent
80
81
audit_event_2 : models .AuditEvent
81
- audit_event_4 : models .AuditEvent
82
+ audit_event_3 : models .AuditEvent
82
83
83
84
84
85
# ------------------------------------------
@@ -342,7 +343,7 @@ def test_list_audit_rules(client: APIClient, init_db):
342
343
343
344
assert len (audit_rules ) == 2
344
345
assert audit_rules [0 ]["fired_at" ] > audit_rules [1 ]["fired_at" ]
345
- assert audit_rules [0 ]["name" ] == "test_action_2 "
346
+ assert audit_rules [0 ]["name" ] == "rule with 2 actions/events "
346
347
assert list (audit_rules [0 ]) == [
347
348
"id" ,
348
349
"name" ,
@@ -354,13 +355,13 @@ def test_list_audit_rules(client: APIClient, init_db):
354
355
355
356
@pytest .mark .django_db
356
357
def test_list_audit_rules_filter_name (client : APIClient , init_db ):
357
- filter_name = "test_action_1 "
358
+ filter_name = "rule with 1 action "
358
359
response = client .get (f"{ api_url_v1 } /audit-rules/?name={ filter_name } " )
359
360
assert response .status_code == status .HTTP_200_OK
360
361
audit_rules = response .data ["results" ]
361
362
362
363
assert len (audit_rules ) == 1
363
- assert audit_rules [0 ]["name" ] == "test_action_1 "
364
+ assert audit_rules [0 ]["name" ] == "rule with 1 action "
364
365
assert list (audit_rules [0 ]) == [
365
366
"id" ,
366
367
"name" ,
@@ -443,8 +444,8 @@ def test_retrieve_audit_rule(client: APIClient, init_db):
443
444
response = client .get (f"{ api_url_v1 } /audit-rules/{ audit_rule_id } /" )
444
445
445
446
assert response .status_code == status .HTTP_200_OK
446
- assert response .data ["name" ] == "test_action_1"
447
- assert response .data ["ruleset_name" ] == "test-audit-ruleset-name-1"
447
+ assert response .data ["name" ] == init_db . audit_rule_1 . name
448
+ assert response .data ["ruleset_name" ] == init_db . audit_rule_1 . ruleset_name
448
449
449
450
450
451
@pytest .mark .django_db
@@ -455,7 +456,7 @@ def test_retrieve_audit_rule_not_exist(client: APIClient):
455
456
456
457
@pytest .mark .django_db
457
458
def test_list_actions_from_audit_rule (client : APIClient , init_db ):
458
- audit_rule_id = init_db .audit_rule_1 .id
459
+ audit_rule_id = init_db .audit_rule_2 .id
459
460
460
461
response = client .get (f"{ api_url_v1 } /audit-rules/{ audit_rule_id } /actions/" )
461
462
assert response .status_code == status .HTTP_200_OK
@@ -467,8 +468,8 @@ def test_list_actions_from_audit_rule(client: APIClient, init_db):
467
468
468
469
@pytest .mark .django_db
469
470
def test_list_actions_from_audit_rule_filter_name (client : APIClient , init_db ):
470
- filter_name = "action-1 "
471
- audit_rule_id = init_db .audit_rule_1 .id
471
+ filter_name = "print_event "
472
+ audit_rule_id = init_db .audit_rule_2 .id
472
473
473
474
response = client .get (
474
475
f"{ api_url_v1 } /audit-rules/{ audit_rule_id } /actions/?name={ filter_name } "
@@ -477,7 +478,7 @@ def test_list_actions_from_audit_rule_filter_name(client: APIClient, init_db):
477
478
478
479
filtered_actions = response .data ["results" ]
479
480
assert len (filtered_actions ) == 1
480
- assert filtered_actions [0 ]["name" ] == "action-1 "
481
+ assert filtered_actions [0 ]["name" ] == "print_event "
481
482
assert list (filtered_actions [0 ]) == [
482
483
"id" ,
483
484
"name" ,
@@ -492,13 +493,13 @@ def test_list_actions_from_audit_rule_filter_name(client: APIClient, init_db):
492
493
493
494
@pytest .mark .parametrize (
494
495
"ordering_field" ,
495
- ["name" , "status" , "url" , "fired_at" , "rule_fired_at" ],
496
+ ["name" , "status" , "url" , "fired_at" ],
496
497
)
497
498
@pytest .mark .django_db
498
499
def test_list_actions_from_audit_rule_ordering (
499
500
client : APIClient , init_db , ordering_field
500
501
):
501
- audit_rule_id = init_db .audit_rule_1 .id
502
+ audit_rule_id = init_db .audit_rule_2 .id
502
503
response = client .get (
503
504
f"{ api_url_v1 } /audit-rules/{ audit_rule_id } /actions/?ordering="
504
505
f"{ ordering_field } "
@@ -507,7 +508,7 @@ def test_list_actions_from_audit_rule_ordering(
507
508
actions = response .data ["results" ]
508
509
assert len (actions ) == 2
509
510
assert actions [0 ][ordering_field ] == getattr (
510
- init_db .audit_action_1 , ordering_field
511
+ init_db .audit_action_2 , ordering_field
511
512
)
512
513
513
514
response = client .get (
@@ -518,7 +519,7 @@ def test_list_actions_from_audit_rule_ordering(
518
519
actions = response .data ["results" ]
519
520
assert len (actions ) == 2
520
521
assert actions [0 ][ordering_field ] == getattr (
521
- init_db .audit_action_2 , ordering_field
522
+ init_db .audit_action_3 , ordering_field
522
523
)
523
524
524
525
@@ -539,34 +540,34 @@ def test_list_actions_from_audit_rule_filter_name_non_existent(
539
540
540
541
@pytest .mark .django_db
541
542
def test_list_events_from_audit_rule (client : APIClient , init_db ):
542
- audit_rule_id = init_db .audit_rule_1 .id
543
+ audit_rule_id = init_db .audit_rule_2 .id
543
544
544
545
response = client .get (f"{ api_url_v1 } /audit-rules/{ audit_rule_id } /events/" )
545
546
assert response .status_code == status .HTTP_200_OK
546
547
547
548
events = response .data ["results" ]
548
- assert len (events ) == 4
549
+ assert len (events ) == 2
549
550
assert events [0 ]["received_at" ] > events [1 ]["received_at" ]
550
551
551
552
552
553
@pytest .mark .parametrize (
553
554
"ordering_field" ,
554
- ["source_name" , "source_type" , "received_at" , "rule_fired_at" ],
555
+ ["source_name" , "source_type" , "received_at" ],
555
556
)
556
557
@pytest .mark .django_db
557
558
def test_list_events_from_audit_rule_ordering (
558
559
client : APIClient , init_db , ordering_field
559
560
):
560
- audit_rule_id = init_db .audit_rule_1 .id
561
+ audit_rule_id = init_db .audit_rule_2 .id
561
562
response = client .get (
562
563
f"{ api_url_v1 } /audit-rules/{ audit_rule_id } /events/?ordering="
563
564
f"{ ordering_field } "
564
565
)
565
566
assert response .status_code == status .HTTP_200_OK
566
567
events = response .data ["results" ]
567
- assert len (events ) == 4
568
+ assert len (events ) == 2
568
569
assert events [0 ][ordering_field ] == getattr (
569
- init_db .audit_event_1 , ordering_field
570
+ init_db .audit_event_2 , ordering_field
570
571
)
571
572
572
573
response = client .get (
@@ -575,9 +576,9 @@ def test_list_events_from_audit_rule_ordering(
575
576
)
576
577
assert response .status_code == status .HTTP_200_OK
577
578
events = response .data ["results" ]
578
- assert len (events ) == 4
579
+ assert len (events ) == 2
579
580
assert events [0 ][ordering_field ] == getattr (
580
- init_db .audit_event_4 , ordering_field
581
+ init_db .audit_event_3 , ordering_field
581
582
)
582
583
583
584
@@ -682,16 +683,16 @@ def init_db():
682
683
name = activation_2 .name , activation = activation_2
683
684
)
684
685
audit_rule_1 = models .AuditRule .objects .create (
685
- name = "test_action_1 " ,
686
- fired_at = "2023-03-23T01:36:36.835248Z " ,
686
+ name = "rule with 1 action " ,
687
+ fired_at = "2023-12-14T15:19:02.313122Z " ,
687
688
rule_uuid = DUMMY_UUID ,
688
689
ruleset_uuid = DUMMY_UUID ,
689
690
ruleset_name = "test-audit-ruleset-name-1" ,
690
691
activation_instance = activation_instance ,
691
692
)
692
693
audit_rule_2 = models .AuditRule .objects .create (
693
- name = "test_action_2 " ,
694
- fired_at = "2023-03-24T01:46:36.835248Z " ,
694
+ name = "rule with 2 actions/events " ,
695
+ fired_at = "2023-12-14T15:19:02.323704Z " ,
695
696
rule_uuid = DUMMY_UUID ,
696
697
ruleset_uuid = DUMMY_UUID ,
697
698
ruleset_name = "test-audit-ruleset-name-2" ,
@@ -700,53 +701,54 @@ def init_db():
700
701
701
702
action_1 = models .AuditAction .objects .create (
702
703
id = str (uuid .uuid4 ()),
703
- name = "action-1 " ,
704
+ name = "debug " ,
704
705
audit_rule = audit_rule_1 ,
705
- status = "pending " ,
706
- rule_fired_at = "2023-03-23T01:36:36.835248Z " ,
707
- fired_at = "2023-03-30T20:59:42.042148Z " ,
706
+ status = "successful " ,
707
+ rule_fired_at = "2023-12-14T15:19:02.313122Z " ,
708
+ fired_at = "2023-12-14T15:19:02.319506Z " ,
708
709
)
709
710
action_2 = models .AuditAction .objects .create (
710
711
id = str (uuid .uuid4 ()),
711
- name = "action-2" ,
712
- audit_rule = audit_rule_1 ,
713
- status = "pending" ,
714
- rule_fired_at = "2023-03-23T01:36:36.835248Z" ,
715
- fired_at = "2023-03-31T20:59:42.052148Z" ,
712
+ name = "debug" ,
713
+ audit_rule = audit_rule_2 ,
714
+ status = "successful" ,
715
+ rule_fired_at = "2023-12-14T15:19:02.323704Z" ,
716
+ fired_at = "2023-12-14T15:19:02.326503Z" ,
717
+ )
718
+ action_3 = models .AuditAction .objects .create (
719
+ id = str (uuid .uuid4 ()),
720
+ name = "print_event" ,
721
+ audit_rule = audit_rule_2 ,
722
+ status = "successful" ,
723
+ rule_fired_at = "2023-12-14T15:19:02.323704Z" ,
724
+ fired_at = "2023-12-14T15:19:02.327547Z" ,
716
725
)
717
726
audit_event_1 = models .AuditEvent .objects .create (
718
727
id = str (uuid .uuid4 ()),
719
- source_name = "event-1 " ,
720
- source_type = "type-1 " ,
721
- rule_fired_at = "2023-03-23T01:16:36.835248Z " ,
722
- received_at = "2023-03-30T20:59:42.042148Z " ,
728
+ source_name = "my test source " ,
729
+ source_type = "ansible.eda.range " ,
730
+ rule_fired_at = "2023-12-14T15:19:02.313122Z " ,
731
+ received_at = "2023-12-14T15:19:02.289549Z " ,
723
732
)
724
733
audit_event_2 = models .AuditEvent .objects .create (
725
734
id = str (uuid .uuid4 ()),
726
- source_name = "event-2 " ,
727
- source_type = "type-2 " ,
728
- rule_fired_at = "2023-03-23T01:28:36.835248Z " ,
729
- received_at = "2023-03-30T20:59:43.042148Z " ,
735
+ source_name = "my test source " ,
736
+ source_type = "ansible.eda.range " ,
737
+ rule_fired_at = "2023-12-14T15:19:02.323704Z " ,
738
+ received_at = "2023-12-14T15:19:02.313063Z " ,
730
739
)
731
740
audit_event_3 = models .AuditEvent .objects .create (
732
741
id = str (uuid .uuid4 ()),
733
- source_name = "event-3" ,
734
- source_type = "type-3" ,
735
- rule_fired_at = "2023-03-23T01:36:36.835248Z" ,
736
- received_at = "2023-03-30T20:59:44.042148Z" ,
737
- )
738
- audit_event_4 = models .AuditEvent .objects .create (
739
- id = str (uuid .uuid4 ()),
740
- source_name = "event-4" ,
741
- source_type = "type-4" ,
742
- rule_fired_at = "2023-03-23T01:37:36.835248Z" ,
743
- received_at = "2023-03-30T20:59:45.042148Z" ,
742
+ source_name = "my test source" ,
743
+ source_type = "ansible.eda.range" ,
744
+ rule_fired_at = "2023-12-14T15:19:02.323704Z" ,
745
+ received_at = "2023-12-14T15:19:02.321472Z" ,
744
746
)
745
747
audit_event_1 .audit_actions .add (action_1 )
746
- audit_event_1 .audit_actions .add (action_2 )
747
- audit_event_2 .audit_actions .add (action_1 )
748
+ audit_event_2 .audit_actions .add (action_2 )
749
+ audit_event_2 .audit_actions .add (action_3 )
748
750
audit_event_3 .audit_actions .add (action_2 )
749
- audit_event_4 .audit_actions .add (action_2 )
751
+ audit_event_3 .audit_actions .add (action_3 )
750
752
751
753
return InitData (
752
754
activation = activation ,
@@ -762,7 +764,8 @@ def init_db():
762
764
audit_rule_2 = audit_rule_2 ,
763
765
audit_action_1 = action_1 ,
764
766
audit_action_2 = action_2 ,
767
+ audit_action_3 = action_3 ,
765
768
audit_event_1 = audit_event_1 ,
766
769
audit_event_2 = audit_event_2 ,
767
- audit_event_4 = audit_event_4 ,
770
+ audit_event_3 = audit_event_3 ,
768
771
)
0 commit comments