Skip to content

Commit

Permalink
Merge pull request #11167 from archesproject/jtw/api-fallback
Browse files Browse the repository at this point in the history
Respond to invalid api routes with JSON #11166
  • Loading branch information
apeters authored Aug 13, 2024
2 parents f7f3525 + 5ebdbef commit 8e6b468
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
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 @@ -33,6 +33,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
- Updates Google Analytics syntax supporting Google Analytics 4 #11132
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

0 comments on commit 8e6b468

Please sign in to comment.