Skip to content

Commit

Permalink
feat: [AAP-22893] add filtering by activation instance name to audit …
Browse files Browse the repository at this point in the history
…rules list view
  • Loading branch information
Dostonbek1 committed Dec 16, 2024
1 parent 6085477 commit ecbe3d9
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 39 deletions.
14 changes: 7 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ cryptography = ">=42,<43"
kubernetes = "26.1.*"
podman = "4.9.*"
rq-scheduler = "^0.10"
django-ansible-base = { git = "https://github.com/ansible/django-ansible-base.git", branch = "devel", extras = [
django-ansible-base = { git = "https://github.com/ansible/django-ansible-base.git", tag = "2024.12.13", extras = [
"channel-auth",
"rbac",
"redis-client",
"resource-registry",
"jwt-consumer",
"rest-filters",
] }
jinja2 = ">=3.1.3,<3.2"
django-split-settings = "^1.2.0"
Expand Down
23 changes: 0 additions & 23 deletions src/aap_eda/api/views/rulebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
)
from rest_framework import status, viewsets
from rest_framework.decorators import action
from rest_framework.filters import OrderingFilter
from rest_framework.response import Response

from aap_eda.api import exceptions as api_exc, filters, serializers
Expand Down Expand Up @@ -152,15 +151,6 @@ class AuditRuleViewSet(
viewsets.ReadOnlyModelViewSet,
):
queryset = models.AuditRule.objects.all()
filter_backends = (defaultfilters.DjangoFilterBackend, OrderingFilter)
filterset_class = filters.AuditRuleFilter
ordering_fields = [
"id",
"name",
"status",
"activation_instance__name",
"fired_at",
]

def filter_queryset(self, queryset):
if queryset.model is models.AuditRule:
Expand Down Expand Up @@ -200,13 +190,6 @@ def get_serializer_class(self):
@action(
detail=False,
queryset=models.AuditAction.objects.order_by("id"),
filterset_class=filters.AuditRuleActionFilter,
ordering_fields=[
"name",
"status",
"url",
"fired_at",
],
rbac_action=Action.READ,
url_path="(?P<id>[^/.]+)/actions",
)
Expand Down Expand Up @@ -243,12 +226,6 @@ def actions(self, _request, id):
@action(
detail=False,
queryset=models.AuditEvent.objects.order_by("-received_at"),
filterset_class=filters.AuditRuleEventFilter,
ordering_fields=[
"source_name",
"source_type",
"received_at",
],
rbac_action=Action.READ,
url_path="(?P<id>[^/.]+)/events",
)
Expand Down
1 change: 1 addition & 0 deletions src/aap_eda/settings/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def _get_boolean(name: str, default=False) -> bool:
"ansible_base.rbac",
"ansible_base.resource_registry",
"ansible_base.jwt_consumer",
"ansible_base.rest_filters",
# Local apps
"aap_eda.api",
"aap_eda.core",
Expand Down
41 changes: 33 additions & 8 deletions tests/integration/api/test_rulebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def test_list_audit_rules_ordering(
ordering_field,
):
response = admin_client.get(
f"{api_url_v1}/audit-rules/?ordering={ordering_field}"
f"{api_url_v1}/audit-rules/?order={ordering_field}"
)
assert response.status_code == status.HTTP_200_OK
audit_rules = response.data["results"]
Expand All @@ -312,7 +312,7 @@ def test_list_audit_rules_ordering(
)

response = admin_client.get(
f"{api_url_v1}/audit-rules/?ordering=-{ordering_field}"
f"{api_url_v1}/audit-rules/?order=-{ordering_field}"
)
assert response.status_code == status.HTTP_200_OK
audit_rules = response.data["results"]
Expand All @@ -330,7 +330,7 @@ def test_list_audit_rules_ordering_activation_name(
):
ordering_field = "activation_instance__name"
response = admin_client.get(
f"{api_url_v1}/audit-rules/?ordering={ordering_field}"
f"{api_url_v1}/audit-rules/?order={ordering_field}"
)
assert response.status_code == status.HTTP_200_OK
audit_rules = response.data["results"]
Expand All @@ -341,7 +341,7 @@ def test_list_audit_rules_ordering_activation_name(
)

response = admin_client.get(
f"{api_url_v1}/audit-rules/?ordering=-{ordering_field}"
f"{api_url_v1}/audit-rules/?order=-{ordering_field}"
)
assert response.status_code == status.HTTP_200_OK
audit_rules = response.data["results"]
Expand All @@ -352,6 +352,31 @@ def test_list_audit_rules_ordering_activation_name(
)


@pytest.mark.django_db
def test_list_audit_rules_filter_activation_instance_name(
audit_rule_1: models.AuditRule,
audit_rule_2: models.AuditRule,
admin_client: APIClient,
):
filter_name = audit_rule_1.activation_instance.name
response = admin_client.get(
f"{api_url_v1}/audit-rules/?activation_instance__name={filter_name}"
)
assert response.status_code == status.HTTP_200_OK
audit_rules = response.data["results"]

assert len(audit_rules) == 1
assert audit_rules[0]["activation_instance"]["name"] == audit_rule_1.activation_instance.name
assert list(audit_rules[0]) == [
"id",
"name",
"status",
"activation_instance",
"organization",
"fired_at",
]


@pytest.mark.django_db
def test_retrieve_audit_rule(
audit_rule_1: models.AuditRule, admin_client: APIClient
Expand Down Expand Up @@ -426,7 +451,7 @@ def test_list_actions_from_audit_rule_ordering(
ordering_field,
):
response = admin_client.get(
f"{api_url_v1}/audit-rules/{audit_rule_2.id}/actions/?ordering="
f"{api_url_v1}/audit-rules/{audit_rule_2.id}/actions/?order="
f"{ordering_field}"
)
assert response.status_code == status.HTTP_200_OK
Expand All @@ -437,7 +462,7 @@ def test_list_actions_from_audit_rule_ordering(
)

response = admin_client.get(
f"{api_url_v1}/audit-rules/{audit_rule_2.id}/actions/?ordering="
f"{api_url_v1}/audit-rules/{audit_rule_2.id}/actions/?order="
f"-{ordering_field}"
)
assert response.status_code == status.HTTP_200_OK
Expand Down Expand Up @@ -495,7 +520,7 @@ def test_list_events_from_audit_rule_ordering(
ordering_field,
):
response = admin_client.get(
f"{api_url_v1}/audit-rules/{audit_rule_2.id}/events/?ordering="
f"{api_url_v1}/audit-rules/{audit_rule_2.id}/events/?order="
f"{ordering_field}"
)
assert response.status_code == status.HTTP_200_OK
Expand All @@ -504,7 +529,7 @@ def test_list_events_from_audit_rule_ordering(
assert events[0][ordering_field] == getattr(audit_event_2, ordering_field)

response = admin_client.get(
f"{api_url_v1}/audit-rules/{audit_rule_2.id}/events/?ordering="
f"{api_url_v1}/audit-rules/{audit_rule_2.id}/events/?order="
f"-{ordering_field}"
)
assert response.status_code == status.HTTP_200_OK
Expand Down

0 comments on commit ecbe3d9

Please sign in to comment.