-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: admin action to delete all segments for project (#2646)
- Loading branch information
1 parent
7c92525
commit 4df1b80
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import typing | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
from django.contrib.admin import AdminSite | ||
|
||
from environments.models import Environment | ||
from features.models import Feature, FeatureSegment, FeatureState | ||
from projects.admin import ProjectAdmin | ||
from projects.models import Project | ||
from segments.models import EQUAL, Condition, Segment, SegmentRule | ||
|
||
if typing.TYPE_CHECKING: | ||
from django.contrib.auth.models import AbstractUser | ||
|
||
from organisations.models import Organisation | ||
|
||
|
||
def test_project_admin_delete_all_segments(organisation: "Organisation"): | ||
# Given | ||
project_1 = Project.objects.create(name="project_1", organisation=organisation) | ||
project_2 = Project.objects.create(name="project_2", organisation=organisation) | ||
|
||
for project in (project_1, project_2): | ||
segment = Segment.objects.create(name="segment", project=project) | ||
parent_rule = SegmentRule.objects.create( | ||
segment=segment, type=SegmentRule.ALL_RULE | ||
) | ||
child_rule = SegmentRule.objects.create( | ||
rule=parent_rule, type=SegmentRule.ANY_RULE | ||
) | ||
Condition.objects.create( | ||
rule=child_rule, property="foo", operator=EQUAL, value="bar" | ||
) | ||
|
||
environment = Environment.objects.create(name="test", project=project) | ||
feature = Feature.objects.create(name="test", project=project) | ||
|
||
feature_segment = FeatureSegment.objects.create( | ||
feature=feature, environment=environment, segment=segment | ||
) | ||
FeatureState.objects.create( | ||
feature=feature, environment=environment, feature_segment=feature_segment | ||
) | ||
|
||
project_admin = ProjectAdmin(Project, AdminSite()) | ||
|
||
# When | ||
project_admin.delete_all_segments( | ||
request=MagicMock(), queryset=Project.objects.filter(id=project_1.id) | ||
) | ||
|
||
# Then | ||
assert not project_1.segments.exists() | ||
assert not FeatureState.objects.filter( | ||
feature=feature, environment__project=project_1, feature_segment__isnull=False | ||
).exists() | ||
|
||
assert project_2.segments.exists() | ||
assert FeatureState.objects.filter( | ||
feature=feature, environment__project=project_2, feature_segment__isnull=False | ||
).exists() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"is_superuser, expected_result", ((True, True), (False, False)) | ||
) | ||
def test_project_admin_has_delete_all_segments_permission( | ||
is_superuser: bool, expected_result: bool, django_user_model: type["AbstractUser"] | ||
): | ||
# Given | ||
request = MagicMock(user=django_user_model(is_superuser=is_superuser)) | ||
|
||
# Then | ||
assert ( | ||
ProjectAdmin(Project, AdminSite()).has_delete_all_segments_permission( | ||
request=request | ||
) | ||
is expected_result | ||
) |
4df1b80
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
flagsmith-frontend-preview – ./frontend
flagsmith-frontend-production-native.vercel.app
flagsmith-frontend-preview-flagsmith.vercel.app
flagsmith-frontend-preview-git-main-flagsmith.vercel.app
4df1b80
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
flagsmith-frontend-staging – ./frontend
flagsmith-frontend-staging-git-main-flagsmith.vercel.app
flagsmith-frontend-staging-flagsmith.vercel.app
staging.flagsmith.com
flagsmith-staging-frontend.vercel.app
4df1b80
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs – ./docs
docs-git-main-flagsmith.vercel.app
docs.bullet-train.io
docs.flagsmith.com
docs-flagsmith.vercel.app