-
Notifications
You must be signed in to change notification settings - Fork 7
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 #76 from maykinmedia/feature/geo-headers
Feature/geo headers
- Loading branch information
Showing
10 changed files
with
673 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from rest_framework.exceptions import NotAcceptable | ||
from rest_framework.renderers import BrowsableAPIRenderer | ||
from vng_api_common.exceptions import PreconditionFailed | ||
from vng_api_common.geo import ( | ||
DEFAULT_CRS, | ||
HEADER_ACCEPT, | ||
HEADER_CONTENT, | ||
GeoMixin as _GeoMixin, | ||
extract_header, | ||
) | ||
|
||
|
||
class GeoMixin(_GeoMixin): | ||
def perform_crs_negotation(self, request): | ||
# don't cripple the browsable API... | ||
if isinstance(request.accepted_renderer, BrowsableAPIRenderer): | ||
return | ||
|
||
# methods with request bodies need to have the CRS specified | ||
if request.method.lower() in ("post", "put", "patch"): | ||
content_crs = extract_header(request, HEADER_CONTENT) | ||
if content_crs is None: | ||
raise PreconditionFailed(detail=f"'{HEADER_CONTENT}' header ontbreekt") | ||
if content_crs != DEFAULT_CRS: | ||
raise NotAcceptable(detail=f"CRS '{content_crs}' is niet ondersteund") | ||
|
||
if request.method.lower() == "delete": | ||
return | ||
|
||
# check optional header | ||
requested_crs = extract_header(request, HEADER_ACCEPT) | ||
if requested_crs and requested_crs != DEFAULT_CRS: | ||
raise NotAcceptable(detail=f"CRS '{requested_crs}' is niet ondersteund") |
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,104 @@ | ||
from django.urls import reverse | ||
|
||
from rest_framework import status | ||
from rest_framework.test import APITestCase | ||
|
||
from objects.core.tests.factores import ObjectFactory | ||
|
||
from .constants import GEO_READ_KWARGS, POLYGON_AMSTERDAM_CENTRUM | ||
|
||
OBJECT_TYPE = "https://example.com/objecttypes/v1/types/a6c109" | ||
|
||
|
||
class GeoHeaderTests(APITestCase): | ||
def assertResponseHasGeoHeaders(self, response): | ||
self.assertTrue("Content-Crs" in response) | ||
self.assertEqual(response["Content-Crs"], "EPSG:4326") | ||
|
||
def test_get_without_geo_headers(self): | ||
object = ObjectFactory.create() | ||
url = reverse("object-detail", args=[object.uuid]) | ||
|
||
response = self.client.get(url) | ||
|
||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertResponseHasGeoHeaders(response) | ||
|
||
def test_get_with_geo_headers(self): | ||
object = ObjectFactory.create() | ||
url = reverse("object-detail", args=[object.uuid]) | ||
|
||
response = self.client.get(url, **GEO_READ_KWARGS) | ||
|
||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertResponseHasGeoHeaders(response) | ||
|
||
def test_get_with_incorrect_get_headers(self): | ||
object = ObjectFactory.create() | ||
url = reverse("object-detail", args=[object.uuid]) | ||
|
||
response = self.client.get(url, HTTP_ACCEPT_CRS="EPSG:3857") | ||
|
||
self.assertEqual(response.status_code, status.HTTP_406_NOT_ACCEPTABLE) | ||
|
||
def test_create_without_geo_headers(self): | ||
data = { | ||
"type": OBJECT_TYPE, | ||
"record": { | ||
"typeVersion": 1, | ||
"data": {"diameter": 30}, | ||
"startDate": "2020-01-01", | ||
}, | ||
} | ||
url = reverse("object-list") | ||
|
||
response = self.client.post(url, data) | ||
|
||
self.assertEqual(response.status_code, status.HTTP_412_PRECONDITION_FAILED) | ||
|
||
def test_update_without_geo_headers(self): | ||
object = ObjectFactory.create() | ||
url = reverse("object-detail", args=[object.uuid]) | ||
data = { | ||
"type": OBJECT_TYPE, | ||
"record": { | ||
"typeVersion": 1, | ||
"data": {"diameter": 30}, | ||
"startDate": "2020-01-01", | ||
}, | ||
} | ||
|
||
for method in ("put", "patch"): | ||
with self.subTest(method=method): | ||
do_request = getattr(self.client, method) | ||
|
||
response = do_request(url, data) | ||
|
||
self.assertEqual( | ||
response.status_code, status.HTTP_412_PRECONDITION_FAILED | ||
) | ||
|
||
def test_delete_without_geo_headers(self): | ||
object = ObjectFactory.create() | ||
url = reverse("object-detail", args=[object.uuid]) | ||
|
||
response = self.client.delete(url) | ||
|
||
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) | ||
|
||
def test_search_without_geo_headers(self): | ||
url = reverse("object-search") | ||
|
||
response = self.client.post( | ||
url, | ||
{ | ||
"geometry": { | ||
"within": { | ||
"type": "Polygon", | ||
"coordinates": [POLYGON_AMSTERDAM_CENTRUM], | ||
} | ||
} | ||
}, | ||
) | ||
|
||
self.assertEqual(response.status_code, status.HTTP_412_PRECONDITION_FAILED) |
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
Oops, something went wrong.