Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(environment/views): Add get-by-uuid action #4875

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions api/environments/migrations/0037_add_uuid_field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 3.2.23 on 2024-11-27 08:46

from django.db import migrations, models
import uuid

from core.migration_helpers import AddDefaultUUIDs


class Migration(migrations.Migration):
dependencies = [
("environments", "0036_add_is_creating_field"),
]

operations = [
migrations.AddField(
model_name="environment",
name="uuid",
field=models.UUIDField(editable=False, null=True),
),
migrations.RunPython(
AddDefaultUUIDs("environments", "environment"),
reverse_code=migrations.RunPython.noop,
),
migrations.AlterField(
model_name="environment",
name="uuid",
field=models.UUIDField(default=uuid.uuid4, editable=False, unique=True),
),
]
6 changes: 5 additions & 1 deletion api/environments/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import typing
import uuid
from copy import deepcopy

from core.models import abstract_base_auditable_model_factory
Expand Down Expand Up @@ -63,14 +64,16 @@
class Environment(
LifecycleModel,
abstract_base_auditable_model_factory(
change_details_excluded_fields=["updated_at"]
change_details_excluded_fields=["updated_at"],
historical_records_excluded_fields=["uuid"],
),
SoftDeleteObject,
):
history_record_class_path = "environments.models.HistoricalEnvironment"
related_object_type = RelatedObjectType.ENVIRONMENT

name = models.CharField(max_length=2000)
uuid = models.UUIDField(default=uuid.uuid4, unique=True, editable=False)
created_date = models.DateTimeField("DateCreated", auto_now_add=True)
description = models.TextField(null=True, blank=True, max_length=20000)
project = models.ForeignKey(
Expand Down Expand Up @@ -178,6 +181,7 @@ def clone(
"""
clone = deepcopy(self)
clone.id = None
clone.uuid = uuid.uuid4()
clone.name = name
clone.api_key = api_key if api_key else create_hash()
clone.is_creating = True
Expand Down
1 change: 1 addition & 0 deletions api/environments/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Meta:
model = Environment
fields = (
"id",
"uuid",
"name",
"api_key",
"description",
Expand Down
22 changes: 20 additions & 2 deletions api/environments/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import logging

from common.environments.permissions import TAG_SUPPORTED_PERMISSIONS
from common.environments.permissions import (
TAG_SUPPORTED_PERMISSIONS,
VIEW_ENVIRONMENT,
)
from django.db.models import Count, Q
from django.utils.decorators import method_decorator
from drf_yasg import openapi
from drf_yasg.utils import no_body, swagger_auto_schema
from rest_framework import mixins, status, viewsets
from rest_framework.decorators import action
from rest_framework.exceptions import ValidationError
from rest_framework.exceptions import PermissionDenied, ValidationError
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
Expand Down Expand Up @@ -142,6 +146,20 @@ def perform_create(self, serializer):
user=self.request.user, environment=environment, admin=True
)

@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()
environment = get_object_or_404(qs, uuid=uuid)
if not request.user.has_environment_permission(VIEW_ENVIRONMENT, environment):
raise PermissionDenied()

serializer = self.get_serializer(environment)
return Response(serializer.data)

@action(detail=True, methods=["GET"], url_path="trait-keys")
def trait_keys(self, request, *args, **kwargs):
keys = [
Expand Down
37 changes: 37 additions & 0 deletions api/tests/unit/environments/test_unit_environments_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,43 @@ def test_retrieve_environment(
)


def test_get_by_uuid_returns_environment(
staff_client: APIClient,
environment: Environment,
with_environment_permissions: WithEnvironmentPermissionsCallable,
) -> None:
# Given
with_environment_permissions([VIEW_ENVIRONMENT])

url = reverse(
"api-v1:environments:environment-get-by-uuid",
args=[environment.uuid],
)

# When
response = staff_client.get(url)

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


def test_get_by_uuid_returns_403_for_user_without_permission(
staff_client: APIClient, environment: Environment
) -> None:
# Given
url = reverse(
"api-v1:environments:environment-get-by-uuid",
args=[environment.uuid],
)

# When
response = staff_client.get(url)

# Then
assert response.status_code == status.HTTP_403_FORBIDDEN


def test_user_with_view_environment_permission_can_retrieve_environment(
staff_client: APIClient,
environment: Environment,
Expand Down
Loading