Skip to content

Commit

Permalink
feat(tags/view): Add api to get tag by uuid (#3229)
Browse files Browse the repository at this point in the history
  • Loading branch information
gagantrivedi authored Jan 9, 2024
1 parent 91cef60 commit 6500451
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 8 deletions.
7 changes: 2 additions & 5 deletions api/projects/tags/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ def has_permission(self, request, view):
project_pk = view.kwargs.get("project_pk")
if not project_pk:
return False

project = Project.objects.get(pk=project_pk)

if request.user.is_project_admin(project):
return True

if view.action == "list" and request.user.has_project_permission(
VIEW_PROJECT, project
):
return True
if view.action in ["list", "get_by_uuid"]:
return request.user.has_project_permission(VIEW_PROJECT, project)

# move on to object specific permissions
return view.detail
Expand Down
2 changes: 1 addition & 1 deletion api/projects/tags/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
class TagSerializer(serializers.ModelSerializer):
class Meta:
model = Tag
fields = ("id", "label", "color", "description", "project")
fields = ("id", "label", "color", "description", "project", "uuid")
read_only_fields = ("project",)
18 changes: 16 additions & 2 deletions api/projects/tags/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.generics import get_object_or_404
from rest_framework.permissions import IsAuthenticated
from rest_framework.request import Request
from rest_framework.response import Response

from projects.permissions import VIEW_PROJECT

Expand All @@ -26,9 +29,20 @@ def get_queryset(self):
return queryset

def perform_create(self, serializer):
project_id = self.kwargs["project_pk"]
project_id = int(self.kwargs["project_pk"])
serializer.save(project_id=project_id)

def perform_update(self, serializer):
project_id = self.kwargs["project_pk"]
project_id = int(self.kwargs["project_pk"])
serializer.save(project_id=project_id)

@action(
detail=False,
url_path=r"get-by-uuid/(?P<uuid>[0-9a-f-]+)",
methods=["get"],
)
def get_by_uuid(self, request: Request, project_pk: int, uuid: str):
qs = self.get_queryset()
tag = get_object_or_404(qs, uuid=uuid)
serializer = self.get_serializer(tag)
return Response(serializer.data)
71 changes: 71 additions & 0 deletions api/tests/unit/projects/tags/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from typing import Callable

import pytest
from django.urls import reverse
from pytest_lazyfixture import lazy_fixture
from rest_framework import status
from rest_framework.test import APIClient

from projects.models import Project
from projects.permissions import VIEW_PROJECT
from projects.tags.models import Tag


@pytest.mark.parametrize(
"client",
[(lazy_fixture("admin_master_api_key_client")), (lazy_fixture("admin_client"))],
)
def test_get_tag_by_uuid(client: APIClient, project: Project, tag: Tag):
url = reverse("api-v1:projects:tags-get-by-uuid", args=[project.id, str(tag.uuid)])

# When
response = client.get(url)

# Then
assert response.status_code == status.HTTP_200_OK
assert response.json()["uuid"] == str(tag.uuid)


def test_get_tag_by_uuid__returns_403_for_user_without_permission(
staff_client: APIClient,
organisation_one_project_two: Project,
project: Project,
tag: Tag,
with_project_permissions: Callable[[list[str], int], None],
):
# Given
# user with view permission for a different project
with_project_permissions([VIEW_PROJECT], organisation_one_project_two.id)

url = reverse(
"api-v1:projects:tags-get-by-uuid",
args=[project.id, str(tag.uuid)],
)

# When
response = staff_client.get(url)

# Then
assert response.status_code == status.HTTP_403_FORBIDDEN


def test_get_tag_by__uuid_returns_200_for_user_with_view_project_permission(
staff_client: APIClient,
project: Project,
tag: Tag,
with_project_permissions: Callable[[list[str], int], None],
):
# Given
with_project_permissions([VIEW_PROJECT])

url = reverse(
"api-v1:projects:tags-get-by-uuid",
args=[project.id, str(tag.uuid)],
)

# When
response = staff_client.get(url)

# Then
assert response.status_code == status.HTTP_200_OK
assert response.json()["uuid"] == str(tag.uuid)

3 comments on commit 6500451

@vercel
Copy link

@vercel vercel bot commented on 6500451 Jan 9, 2024

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 6500451 Jan 9, 2024

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

@vercel
Copy link

@vercel vercel bot commented on 6500451 Jan 9, 2024

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.