Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(audit): add segment deleted audit log #3585

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/audit/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
FEATURE_UPDATED_MESSAGE = "Flag / Remote Config updated: %s"
SEGMENT_CREATED_MESSAGE = "New Segment created: %s"
SEGMENT_UPDATED_MESSAGE = "Segment updated: %s"
SEGMENT_DELETED_MESSAGE = "Segment deleted: %s"
FEATURE_SEGMENT_UPDATED_MESSAGE = (
"Segment rules updated for flag: %s in environment: %s"
)
Expand Down
9 changes: 8 additions & 1 deletion api/segments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
from django.db import models
from flag_engine.segments import constants

from audit.constants import SEGMENT_CREATED_MESSAGE, SEGMENT_UPDATED_MESSAGE
from audit.constants import (
SEGMENT_CREATED_MESSAGE,
SEGMENT_DELETED_MESSAGE,
SEGMENT_UPDATED_MESSAGE,
)
from audit.related_object_type import RelatedObjectType
from features.models import Feature
from projects.models import Project
Expand Down Expand Up @@ -82,6 +86,9 @@ def get_create_log_message(self, history_instance) -> typing.Optional[str]:
def get_update_log_message(self, history_instance) -> typing.Optional[str]:
return SEGMENT_UPDATED_MESSAGE % self.name

def get_delete_log_message(self, history_instance) -> typing.Optional[str]:
return SEGMENT_DELETED_MESSAGE % self.name

def _get_project(self):
return self.project

Expand Down
27 changes: 27 additions & 0 deletions api/tests/unit/segments/test_unit_segments_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from rest_framework import status
from rest_framework.test import APIClient

from audit.constants import SEGMENT_DELETED_MESSAGE
from audit.models import AuditLog
from audit.related_object_type import RelatedObjectType
from environments.models import Environment
Expand Down Expand Up @@ -179,6 +180,32 @@ def test_audit_log_created_when_segment_updated(project, segment, client):
)


@pytest.mark.parametrize(
"client",
[lazy_fixture("admin_master_api_key_client"), lazy_fixture("admin_client")],
)
def test_audit_log_created_when_segment_deleted(project, segment, client):
# Given
segment = Segment.objects.create(name="Test segment", project=project)
url = reverse(
"api-v1:projects:project-segments-detail",
args=[project.id, segment.id],
)

# When
res = client.delete(url, content_type="application/json")

# Then
assert res.status_code == status.HTTP_204_NO_CONTENT
assert (
AuditLog.objects.filter(
related_object_type=RelatedObjectType.SEGMENT.name,
log=SEGMENT_DELETED_MESSAGE % segment.name,
).count()
== 1
)


@pytest.mark.parametrize(
"client",
[lazy_fixture("admin_master_api_key_client"), lazy_fixture("admin_client")],
Expand Down
Loading