Skip to content

Commit

Permalink
fix(feature-export): handle unsuccessful feature export download exce…
Browse files Browse the repository at this point in the history
…ption (#5086)
  • Loading branch information
matthewelwell authored Feb 13, 2025
1 parent ecb6b0b commit a116f2c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
10 changes: 9 additions & 1 deletion api/features/import_export/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
from django.db.models import QuerySet
from django.http import Http404
from drf_yasg.utils import swagger_auto_schema
from rest_framework import permissions
from rest_framework import permissions, serializers
from rest_framework.decorators import api_view, permission_classes
from rest_framework.generics import ListAPIView, get_object_or_404
from rest_framework.request import Request
from rest_framework.response import Response

from environments.models import Environment

from .constants import SUCCESS
from .models import (
FeatureExport,
FeatureImport,
Expand Down Expand Up @@ -73,6 +74,13 @@ def feature_import(request: Request, environment_id: int) -> Response:
def download_feature_export(request: Request, feature_export_id: int) -> Response:
feature_export = get_object_or_404(FeatureExport, id=feature_export_id)

if feature_export.status != SUCCESS:
raise serializers.ValidationError(
{
"detail": f"Unable to download export with status '{feature_export.status}'."
}
)

response = Response(
json.loads(feature_export.data), content_type="application/json"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@

from environments.models import Environment
from environments.permissions.models import UserEnvironmentPermission
from features.import_export.constants import OVERWRITE_DESTRUCTIVE, PROCESSING
from features.import_export.constants import (
FAILED,
OVERWRITE_DESTRUCTIVE,
PROCESSING,
SUCCESS,
)
from features.import_export.models import (
FeatureExport,
FeatureImport,
Expand Down Expand Up @@ -136,6 +141,7 @@ def test_download_feature_export(
feature_export = FeatureExport.objects.create(
environment=environment,
data='[{"feature": "data"}]',
status=SUCCESS,
)
url = reverse("api-v1:features:download-feature-export", args=[feature_export.id])
# When
Expand Down Expand Up @@ -166,6 +172,29 @@ def test_download_feature_export_unauthorized(
assert response.status_code == 403


@pytest.mark.parametrize("status", (PROCESSING, FAILED))
def test_cannot_download_non_success_feature_export(
admin_client_new: APIClient,
environment: Environment,
status: str,
) -> None:
# Given
feature_export = FeatureExport.objects.create(
environment=environment,
status=status,
)
url = reverse("api-v1:features:download-feature-export", args=[feature_export.id])

# When
response = admin_client_new.get(url)

# Then
assert response.status_code == 400
assert response.json() == {
"detail": f"Unable to download export with status '{status}'."
}


def test_feature_import(
admin_client: APIClient,
environment: Environment,
Expand Down

0 comments on commit a116f2c

Please sign in to comment.