Skip to content

Commit b8aac16

Browse files
authored
[AAP-18894] Update audit events endpoint to fix e2e tests (#572)
1 parent f71c1d7 commit b8aac16

File tree

2 files changed

+67
-68
lines changed

2 files changed

+67
-68
lines changed

src/aap_eda/api/views/rulebook.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,10 @@ def get_serializer_class(self):
249249
queryset=models.AuditAction.objects.order_by("id"),
250250
filterset_class=filters.AuditRuleActionFilter,
251251
ordering_fields=[
252-
"id",
253252
"name",
254253
"status",
255254
"url",
256255
"fired_at",
257-
"rule_fired_at",
258256
],
259257
rbac_action=Action.READ,
260258
url_path="(?P<id>[^/.]+)/actions",
@@ -289,14 +287,12 @@ def actions(self, _request, id):
289287
)
290288
@action(
291289
detail=False,
292-
queryset=models.AuditEvent.objects.order_by("id"),
290+
queryset=models.AuditEvent.objects.order_by("-received_at"),
293291
filterset_class=filters.AuditRuleEventFilter,
294292
ordering_fields=[
295-
"id",
296293
"source_name",
297294
"source_type",
298295
"received_at",
299-
"rule_fired_at",
300296
],
301297
rbac_resource_type=ResourceType.AUDIT_EVENT,
302298
rbac_action=Action.READ,
@@ -309,13 +305,13 @@ def events(self, _request, id):
309305
rule_fired_at=audit_rule.fired_at,
310306
)
311307

312-
eqs = models.AuditEvent.objects.none()
308+
audit_events = models.AuditEvent.objects.none()
313309
for audit_action in audit_actions:
314-
eqs = eqs.union(
315-
self.filter_queryset(audit_action.audit_events.all())
316-
).order_by("-received_at")
310+
audit_events |= audit_action.audit_events.all()
317311

318-
results = self.paginate_queryset(self.filter_queryset(eqs))
312+
filtered_audit_events = self.filter_queryset(audit_events.distinct())
313+
314+
results = self.paginate_queryset(filtered_audit_events)
319315
serializer = serializers.AuditEventSerializer(results, many=True)
320316

321317
return self.get_paginated_response(serializer.data)

tests/integration/api/test_rulebook.py

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ class InitData:
7676
audit_rule_2: models.AuditRule
7777
audit_action_1: models.AuditAction
7878
audit_action_2: models.AuditAction
79+
audit_action_3: models.AuditAction
7980
audit_event_1: models.AuditEvent
8081
audit_event_2: models.AuditEvent
81-
audit_event_4: models.AuditEvent
82+
audit_event_3: models.AuditEvent
8283

8384

8485
# ------------------------------------------
@@ -342,7 +343,7 @@ def test_list_audit_rules(client: APIClient, init_db):
342343

343344
assert len(audit_rules) == 2
344345
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"
346347
assert list(audit_rules[0]) == [
347348
"id",
348349
"name",
@@ -354,13 +355,13 @@ def test_list_audit_rules(client: APIClient, init_db):
354355

355356
@pytest.mark.django_db
356357
def test_list_audit_rules_filter_name(client: APIClient, init_db):
357-
filter_name = "test_action_1"
358+
filter_name = "rule with 1 action"
358359
response = client.get(f"{api_url_v1}/audit-rules/?name={filter_name}")
359360
assert response.status_code == status.HTTP_200_OK
360361
audit_rules = response.data["results"]
361362

362363
assert len(audit_rules) == 1
363-
assert audit_rules[0]["name"] == "test_action_1"
364+
assert audit_rules[0]["name"] == "rule with 1 action"
364365
assert list(audit_rules[0]) == [
365366
"id",
366367
"name",
@@ -443,8 +444,8 @@ def test_retrieve_audit_rule(client: APIClient, init_db):
443444
response = client.get(f"{api_url_v1}/audit-rules/{audit_rule_id}/")
444445

445446
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
448449

449450

450451
@pytest.mark.django_db
@@ -455,7 +456,7 @@ def test_retrieve_audit_rule_not_exist(client: APIClient):
455456

456457
@pytest.mark.django_db
457458
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
459460

460461
response = client.get(f"{api_url_v1}/audit-rules/{audit_rule_id}/actions/")
461462
assert response.status_code == status.HTTP_200_OK
@@ -467,8 +468,8 @@ def test_list_actions_from_audit_rule(client: APIClient, init_db):
467468

468469
@pytest.mark.django_db
469470
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
472473

473474
response = client.get(
474475
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):
477478

478479
filtered_actions = response.data["results"]
479480
assert len(filtered_actions) == 1
480-
assert filtered_actions[0]["name"] == "action-1"
481+
assert filtered_actions[0]["name"] == "print_event"
481482
assert list(filtered_actions[0]) == [
482483
"id",
483484
"name",
@@ -492,13 +493,13 @@ def test_list_actions_from_audit_rule_filter_name(client: APIClient, init_db):
492493

493494
@pytest.mark.parametrize(
494495
"ordering_field",
495-
["name", "status", "url", "fired_at", "rule_fired_at"],
496+
["name", "status", "url", "fired_at"],
496497
)
497498
@pytest.mark.django_db
498499
def test_list_actions_from_audit_rule_ordering(
499500
client: APIClient, init_db, ordering_field
500501
):
501-
audit_rule_id = init_db.audit_rule_1.id
502+
audit_rule_id = init_db.audit_rule_2.id
502503
response = client.get(
503504
f"{api_url_v1}/audit-rules/{audit_rule_id}/actions/?ordering="
504505
f"{ordering_field}"
@@ -507,7 +508,7 @@ def test_list_actions_from_audit_rule_ordering(
507508
actions = response.data["results"]
508509
assert len(actions) == 2
509510
assert actions[0][ordering_field] == getattr(
510-
init_db.audit_action_1, ordering_field
511+
init_db.audit_action_2, ordering_field
511512
)
512513

513514
response = client.get(
@@ -518,7 +519,7 @@ def test_list_actions_from_audit_rule_ordering(
518519
actions = response.data["results"]
519520
assert len(actions) == 2
520521
assert actions[0][ordering_field] == getattr(
521-
init_db.audit_action_2, ordering_field
522+
init_db.audit_action_3, ordering_field
522523
)
523524

524525

@@ -539,34 +540,34 @@ def test_list_actions_from_audit_rule_filter_name_non_existent(
539540

540541
@pytest.mark.django_db
541542
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
543544

544545
response = client.get(f"{api_url_v1}/audit-rules/{audit_rule_id}/events/")
545546
assert response.status_code == status.HTTP_200_OK
546547

547548
events = response.data["results"]
548-
assert len(events) == 4
549+
assert len(events) == 2
549550
assert events[0]["received_at"] > events[1]["received_at"]
550551

551552

552553
@pytest.mark.parametrize(
553554
"ordering_field",
554-
["source_name", "source_type", "received_at", "rule_fired_at"],
555+
["source_name", "source_type", "received_at"],
555556
)
556557
@pytest.mark.django_db
557558
def test_list_events_from_audit_rule_ordering(
558559
client: APIClient, init_db, ordering_field
559560
):
560-
audit_rule_id = init_db.audit_rule_1.id
561+
audit_rule_id = init_db.audit_rule_2.id
561562
response = client.get(
562563
f"{api_url_v1}/audit-rules/{audit_rule_id}/events/?ordering="
563564
f"{ordering_field}"
564565
)
565566
assert response.status_code == status.HTTP_200_OK
566567
events = response.data["results"]
567-
assert len(events) == 4
568+
assert len(events) == 2
568569
assert events[0][ordering_field] == getattr(
569-
init_db.audit_event_1, ordering_field
570+
init_db.audit_event_2, ordering_field
570571
)
571572

572573
response = client.get(
@@ -575,9 +576,9 @@ def test_list_events_from_audit_rule_ordering(
575576
)
576577
assert response.status_code == status.HTTP_200_OK
577578
events = response.data["results"]
578-
assert len(events) == 4
579+
assert len(events) == 2
579580
assert events[0][ordering_field] == getattr(
580-
init_db.audit_event_4, ordering_field
581+
init_db.audit_event_3, ordering_field
581582
)
582583

583584

@@ -682,16 +683,16 @@ def init_db():
682683
name=activation_2.name, activation=activation_2
683684
)
684685
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",
687688
rule_uuid=DUMMY_UUID,
688689
ruleset_uuid=DUMMY_UUID,
689690
ruleset_name="test-audit-ruleset-name-1",
690691
activation_instance=activation_instance,
691692
)
692693
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",
695696
rule_uuid=DUMMY_UUID,
696697
ruleset_uuid=DUMMY_UUID,
697698
ruleset_name="test-audit-ruleset-name-2",
@@ -700,53 +701,54 @@ def init_db():
700701

701702
action_1 = models.AuditAction.objects.create(
702703
id=str(uuid.uuid4()),
703-
name="action-1",
704+
name="debug",
704705
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",
708709
)
709710
action_2 = models.AuditAction.objects.create(
710711
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",
716725
)
717726
audit_event_1 = models.AuditEvent.objects.create(
718727
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",
723732
)
724733
audit_event_2 = models.AuditEvent.objects.create(
725734
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",
730739
)
731740
audit_event_3 = models.AuditEvent.objects.create(
732741
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",
744746
)
745747
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)
748750
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)
750752

751753
return InitData(
752754
activation=activation,
@@ -762,7 +764,8 @@ def init_db():
762764
audit_rule_2=audit_rule_2,
763765
audit_action_1=action_1,
764766
audit_action_2=action_2,
767+
audit_action_3=action_3,
765768
audit_event_1=audit_event_1,
766769
audit_event_2=audit_event_2,
767-
audit_event_4=audit_event_4,
770+
audit_event_3=audit_event_3,
768771
)

0 commit comments

Comments
 (0)