Skip to content

Commit

Permalink
fix(audit): add segment deleted audit log (#3585)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewelwell authored Mar 8, 2024
1 parent 7babc38 commit e2b8a92
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
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

0 comments on commit e2b8a92

Please sign in to comment.