Skip to content

Commit

Permalink
feat(org/view): Add api to get organisation by uuid (#4878)
Browse files Browse the repository at this point in the history
  • Loading branch information
gagantrivedi authored Dec 3, 2024
1 parent 9046930 commit 06ed466
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions api/organisations/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Meta:
model = Organisation
fields = (
"id",
"uuid",
"name",
"created_date",
"webhook_notification_email",
Expand Down
13 changes: 12 additions & 1 deletion api/organisations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from rest_framework.authentication import BasicAuthentication
from rest_framework.decorators import action, api_view, authentication_classes
from rest_framework.exceptions import ValidationError
from rest_framework.generics import ListAPIView
from rest_framework.generics import ListAPIView, get_object_or_404
from rest_framework.permissions import IsAuthenticated
from rest_framework.request import Request
from rest_framework.response import Response
Expand Down Expand Up @@ -124,6 +124,17 @@ def create(self, request, **kwargs):
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@action(
detail=False,
url_path=r"get-by-uuid/(?P<uuid>[0-9a-f-]+)",
methods=["get"],
)
def get_by_uuid(self, request, uuid):
qs = self.get_queryset()
organisation = get_object_or_404(qs, uuid=uuid)
serializer = self.get_serializer(organisation)
return Response(serializer.data)

@action(detail=True, permission_classes=[IsAuthenticated])
def projects(self, request, pk):
organisation = self.get_object()
Expand Down
36 changes: 36 additions & 0 deletions api/tests/unit/organisations/test_unit_organisations_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,42 @@ def test_should_return_organisation_list_when_requested(
assert response.data["results"][0]["name"] == organisation.name


def test_get_by_uuid_returns_organisation(
admin_client: APIClient,
organisation: Organisation,
) -> None:
# Given
url = reverse(
"api-v1:organisations:organisation-get-by-uuid",
args=[organisation.uuid],
)

# When
response = admin_client.get(url)

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


def test_get_by_uuid_returns_404_for_organisation_that_does_not_belong_to_the_user(
admin_client: APIClient,
organisation: Organisation,
) -> None:
# Given
different_org = Organisation.objects.create(name="Different org")
url = reverse(
"api-v1:organisations:organisation-get-by-uuid",
args=[different_org.uuid],
)

# When
response = admin_client.get(url)

# Then
assert response.status_code == status.HTTP_404_NOT_FOUND


def test_non_superuser_can_create_new_organisation_by_default(
staff_client: APIClient,
staff_user: FFAdminUser,
Expand Down

0 comments on commit 06ed466

Please sign in to comment.