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

fix(feature-export): handle unsuccessful feature export download exception #5086

Merged
merged 2 commits into from
Feb 13, 2025
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
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
Loading