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

Respond to invalid api routes with JSON #11166 #11167

Merged
merged 4 commits into from
Aug 13, 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
7 changes: 7 additions & 0 deletions arches/app/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ def dispatch(self, request, *args, **kwargs):
return super(APIBase, self).dispatch(request, *args, **kwargs)


class API404(View):
def dispatch(self, request, *args, **kwargs):
return JSONErrorResponse(
_("Request failed"), _("Route not found"), status=HTTPStatus.NOT_FOUND
)


class GetFrontendI18NData(APIBase):
def get(self, request):
user_language = get_language()
Expand Down
1 change: 1 addition & 0 deletions arches/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@
api.TransformEdtfForTile.as_view(),
name="transform_edtf_for_tile",
),
re_path("^api", api.API404.as_view(), name="api_404"),
]

# This must be included in core to keep webpack happy, but cannot be appended when running a project.
Expand Down
1 change: 1 addition & 0 deletions releases/7.6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Arches 7.6.0 Release Notes
- 10558 Combine templates for Arches project and applications and move several dotfiles to root
- 10490 Fixes an issue where webpack receives multiple build calls when running in a container
- Handle missing ontologies in JSON-LD api response
- 11166 Invalid API routes now respond with JSON instead of HTML
- 10710 Workflow history API: return 400 (instead of 401) for attempts to update completed workflows
- 10083 Fix whatisthis command
- 9768 Filter out tiles created during resource creation from activity stream API
Expand Down
11 changes: 10 additions & 1 deletion tests/views/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

import json
import os

from arches.app.utils.i18n import LanguageSynchronizer
from tests import test_settings
from tests.base_test import ArchesTestCase
from django.urls import reverse
from django.core import management
from django.test.client import RequestFactory, Client
from django.test.client import RequestFactory
from django.test.utils import captured_stdout

from arches.app.views.api import APIBase
Expand Down Expand Up @@ -113,6 +115,13 @@ def test_api_base_view(self):
response = view(request)
self.assertEqual(request.GET.get("ver"), "2.1")

def test_api_404(self):
with self.assertLogs("django.request", level="WARNING"):
response = self.client.get(reverse("api_404"))
self.assertEqual(
set(json.loads(response.content)), {"message", "status", "success", "title"}
)

def test_api_resources_archesjson(self):
"""
Test that resources POST and PUT accept arches-json format data.
Expand Down