This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from mdn/query_reject_1243221
fix bug 1243221 - Reject query parameters reserved by JSON API v1.0
- Loading branch information
Showing
24 changed files
with
261 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/v1/raw/view-feature-update-parts-with-changeset-2-request-headers.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/v1/raw/view-feature-update-parts-with-changeset-3-request-headers.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/v2/raw/view-feature-update-parts-with-changeset-2-request-headers.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/v2/raw/view-feature-update-parts-with-changeset-2-response-body.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/v2/raw/view-feature-update-parts-with-changeset-3-request-headers.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/v2/raw/view-feature-update-parts-with-changeset-3-response-body.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# -*- coding: utf-8 -*- | ||
"""API exceptions and exception handler.""" | ||
|
||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
from rest_framework.exceptions import APIException | ||
from rest_framework.status import HTTP_400_BAD_REQUEST | ||
from rest_framework.views import exception_handler as base_handler | ||
|
||
|
||
class InvalidQueryParam(APIException): | ||
"""The request contained a bad query parameter. | ||
JSON API v1.0 defines a structure (source/parameter) for specifying which | ||
query parameter was bad. | ||
http://jsonapi.org/format/1.0/#error-objects | ||
""" | ||
|
||
detail_fmt = _('Query parameter "%(query_param)s" is invalid.') | ||
status_code = HTTP_400_BAD_REQUEST | ||
|
||
def __init__(self, query_param): | ||
"""Initialize the exception with the bad query parameter.""" | ||
self.parameter = query_param | ||
self.detail = self.detail_fmt % {'query_param': query_param} | ||
|
||
|
||
class NotImplementedQueryParam(InvalidQueryParam): | ||
"""Request contained an query parameter that is not implemented. | ||
JSON API v1.0 requires a 400 when the service doesn't support a documented | ||
query parameter, such as 'include': | ||
http://jsonapi.org/format/1.0/#fetching-includes | ||
""" | ||
|
||
detail_fmt = _('Query parameter "%(query_param)s" is not implemented.') | ||
|
||
|
||
def handler(exc, context): | ||
""" | ||
Return the response that should be used for any given exception. | ||
If the v2 API is called, use the JSON API v1.0 exception handler. | ||
Otherwise, use the default Django REST Framework handler. | ||
""" | ||
response = base_handler(exc, context) | ||
if response is not None: | ||
response.exc = exc | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Tests for API exceptions.""" | ||
|
||
from rest_framework.exceptions import ParseError | ||
|
||
from ..exceptions import handler | ||
from .base import TestCase | ||
|
||
|
||
class TestExceptionHandler(TestCase): | ||
"""Test the extended exception handler.""" | ||
|
||
def test_api_exception(self): | ||
"""Test the response includes 'standard' exceptions as an attribute.""" | ||
exception = ParseError() | ||
response = handler(exception, {}) | ||
self.assertEqual(response.status_code, 400) | ||
self.assertEqual(response.exc, exception) | ||
|
||
def test_unexpected_exception(self): | ||
"""Test that unexpected responses return None.""" | ||
response = handler(ValueError('unexpected'), {}) | ||
self.assertIsNone(response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,20 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Tests for simple history overrides in v1 API.""" | ||
|
||
from ..test_history import TestMiddleware | ||
from ..test_history import TestBaseMiddleware | ||
from .base import NamespaceMixin | ||
|
||
|
||
class TestMiddlewareInV1(TestMiddleware): | ||
class TestMiddleware(NamespaceMixin, TestBaseMiddleware): | ||
"""Test the Middleware using v1 API endpoints.""" | ||
|
||
__test__ = True # Run these tests | ||
namespace = 'v1' # Use the v1 API endpoints | ||
def api_data(self): | ||
"""Return JSON data for creating a new browser.""" | ||
return { | ||
'browsers': { | ||
'slug': 'firefox', | ||
'name': { | ||
'en': 'Firefox' | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,23 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Tests for simple history overrides in v2 API.""" | ||
|
||
from ..test_history import TestMiddleware | ||
from ..test_history import TestBaseMiddleware | ||
from .base import NamespaceMixin | ||
|
||
|
||
class TestMiddlewareInV2(TestMiddleware, NamespaceMixin): | ||
"""Test the Middleware using API endpoints.""" | ||
class TestMiddleware(NamespaceMixin, TestBaseMiddleware): | ||
"""Test the Middleware using v2 API endpoints.""" | ||
|
||
def api_data(self): | ||
"""Return JSON data for creating a new browser.""" | ||
return { | ||
'data': { | ||
'type': 'browsers', | ||
'attributes': { | ||
'slug': 'firefox', | ||
'name': { | ||
'en': 'Firefox' | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.