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 error encoding on install_v2 endpoint #5133

Merged
merged 1 commit into from
Oct 7, 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
25 changes: 25 additions & 0 deletions engine/apps/grafana_plugin/tests/test_install_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from unittest.mock import patch

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

from apps.grafana_plugin.views.sync_v2 import SyncException
from common.api_helpers.errors import INVALID_SELF_HOSTED_ID


@pytest.mark.django_db
def test_install_v2_error_encoding(make_organization_and_user_with_plugin_token, make_user_auth_headers):
organization, user, token = make_organization_and_user_with_plugin_token()
client = APIClient()

auth_headers = make_user_auth_headers(user, token)

exc = SyncException(INVALID_SELF_HOSTED_ID)

with patch("apps.grafana_plugin.views.InstallV2View.do_sync", side_effect=exc):
response = client.post(reverse("grafana-plugin:install-v2"), format="json", **auth_headers)
assert response.data["code"] == INVALID_SELF_HOSTED_ID.code
assert response.data["message"] == INVALID_SELF_HOSTED_ID.message
assert response.status_code == status.HTTP_400_BAD_REQUEST
7 changes: 5 additions & 2 deletions engine/apps/grafana_plugin/views/install_v2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from dataclasses import asdict
from dataclasses import asdict, is_dataclass

from django.conf import settings
from rest_framework import status
Expand All @@ -23,7 +23,10 @@ def post(self, request: Request) -> Response:
try:
organization = self.do_sync(request)
except SyncException as e:
return Response(data=e.error_data, status=status.HTTP_400_BAD_REQUEST)
return Response(
data=asdict(e.error_data) if is_dataclass(e.error_data) else e.error_data,
status=status.HTTP_400_BAD_REQUEST,
)

organization.revoke_plugin()
provisioned_data = organization.provision_plugin()
Expand Down
Loading