Skip to content

Commit

Permalink
fix: Feature id in mv-option request is undefined (#2751)
Browse files Browse the repository at this point in the history
  • Loading branch information
novakzaballa authored Sep 12, 2023
1 parent 9b4fdd0 commit 3c3b1d7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
6 changes: 6 additions & 0 deletions api/features/multivariate/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ def get_permissions(self):
)
]

def create(self, request, *args, **kwargs):
feature_pk = self.kwargs.get("feature_pk")
project_pk = self.kwargs.get("project_pk")
get_object_or_404(Feature.objects.filter(project__id=project_pk), pk=feature_pk)
return super().create(request, *args, **kwargs)

def get_queryset(self):
if getattr(self, "swagger_fake_view", False):
return MultivariateFeatureOption.objects.none()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
from django.urls import reverse
from pytest_lazyfixture import lazy_fixture
from rest_framework import status
from rest_framework.test import APIClient

from features.models import Feature
from organisations.models import Organisation
from projects.models import Project
from users.models import FFAdminUser


@pytest.mark.parametrize(
Expand Down Expand Up @@ -34,6 +40,68 @@ def test_can_create_mv_option(client, project, mv_option_50_percent, feature):
assert set(data.items()).issubset(set(response.json().items()))


@pytest.mark.parametrize(
"client, feature_id",
[
(lazy_fixture("admin_client"), "undefined"),
(lazy_fixture("admin_client"), "89809"),
],
)
def test_cannot_create_mv_option_when_feature_id_invalid(client, feature_id, project):
# Given
url = reverse(
"api-v1:projects:feature-mv-options-list",
args=[project, feature_id],
)

data = {
"type": "unicode",
"feature": feature_id,
"string_value": "bigger",
"default_percentage_allocation": 50,
}
# When
response = client.post(
url,
data=json.dumps(data),
content_type="application/json",
)
# Then
assert response.status_code == status.HTTP_404_NOT_FOUND


def test_cannot_create_mv_option_when_user_is_not_owner_of_the_feature(project):
# Given
new_user = FFAdminUser.objects.create(email="[email protected]")
organisation = Organisation.objects.create(name="Test Org")
new_project = Project.objects.create(name="Test project", organisation=organisation)
feature = Feature.objects.create(
name="New_feature",
project=new_project,
)
url = reverse(
"api-v1:projects:feature-mv-options-list",
args=[project, feature.id],
)

data = {
"type": "unicode",
"feature": feature.id,
"string_value": "bigger",
"default_percentage_allocation": 50,
}
client = APIClient()
client.force_authenticate(user=new_user)
# When
response = client.post(
url,
data=json.dumps(data),
content_type="application/json",
)
# Then
assert response.status_code == status.HTTP_403_FORBIDDEN


@pytest.mark.parametrize(
"client",
[lazy_fixture("admin_master_api_key_client"), lazy_fixture("admin_client")],
Expand Down
2 changes: 1 addition & 1 deletion frontend/common/stores/feature-list-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const controller = {
(flag.multivariate_options || []).map((v) =>
data
.post(
`${Project.api}projects/${projectId}/features/${flag.id}/mv-options/`,
`${Project.api}projects/${projectId}/features/${res.id}/mv-options/`,
{
...v,
feature: res.id,
Expand Down

3 comments on commit 3c3b1d7

@vercel
Copy link

@vercel vercel bot commented on 3c3b1d7 Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 3c3b1d7 Sep 12, 2023

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-flagsmith.vercel.app
docs-git-main-flagsmith.vercel.app
docs.bullet-train.io
docs.flagsmith.com

@vercel
Copy link

@vercel vercel bot commented on 3c3b1d7 Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.