-
Notifications
You must be signed in to change notification settings - Fork 0
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 #341 from localcontexts/8689rhmp9-additional-gis-f…
…e-testcases Mock External API For Testcases
- Loading branch information
Showing
10 changed files
with
288 additions
and
5 deletions.
There are no files selected for viewing
Empty file.
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 @@ | ||
import os | ||
from urllib.parse import urlparse | ||
from urllib.parse import urljoin | ||
|
||
from django import template | ||
|
||
|
||
register = template.Library() | ||
|
||
|
||
@register.simple_tag | ||
def external_api(external_url: str) -> str: | ||
""" | ||
Tag converts an external_url into an internal_url | ||
so the response can be mocked with a fixture. | ||
Args: | ||
external_url: Url to an external API resource. | ||
Returns: | ||
When TEST_LIVE_SERVER_DOMAIN is available, the url is transformed so | ||
the external domain is swapped with the TEST_LIVE_SERVER_DOMAIN. | ||
""" | ||
test_live_server_domain = os.environ.get('TEST_LIVE_SERVER_DOMAIN') | ||
if not test_live_server_domain: | ||
return external_url | ||
url_components = urlparse(external_url) | ||
|
||
try: | ||
# return the internal_url formed by the domain and url_components.path | ||
return urljoin(test_live_server_domain, url_components.path) | ||
except Exception: | ||
return external_url |
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,14 @@ | ||
from localcontexts.urls import urlpatterns | ||
from django.urls import path | ||
|
||
from . views import ( | ||
native_land_map_list, | ||
native_land_boundary_response | ||
) | ||
|
||
|
||
class UrlsWithMockedExternalApi: | ||
urlpatterns = urlpatterns + [ | ||
path('wp-json/nativeland/v1/map-list/', native_land_map_list), | ||
path('wp-json/nativeland/v1/api/index.php', native_land_boundary_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import json | ||
|
||
from django.http import JsonResponse | ||
|
||
|
||
def native_land_map_list(request): | ||
""" | ||
This view is called when populating the dropdown | ||
of Native Land territories | ||
""" | ||
json_data = open('tests/fixtures/native_land_map_list.json') | ||
data = json.load(json_data) | ||
return JsonResponse(data, safe=False) | ||
|
||
|
||
def native_land_boundary_response(request): | ||
""" | ||
This view is called when getting specific boundary data | ||
for a single Native Land territory | ||
""" | ||
json_data = open('tests/fixtures/native_land_boundary_response.json') | ||
data = json.load(json_data) | ||
return JsonResponse(data, safe=False) |
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,152 @@ | ||
[ | ||
{ | ||
"id": "d7c0c7e80a4f5618f41853d63a81c1b7", | ||
"type": "Feature", | ||
"properties": { | ||
"Name": "Panamakas", | ||
"ID": 32506, | ||
"Slug": "panamakas", | ||
"description": "https://native-land.ca/maps/territories/panamakas/", | ||
"color": "#FE6826" | ||
}, | ||
"geometry": { | ||
"coordinates": [ | ||
[ | ||
[ | ||
-86.08771202284044, | ||
13.582979800801652 | ||
], | ||
[ | ||
-85.98792298182137, | ||
13.6058017660135 | ||
], | ||
[ | ||
-85.85291427926839, | ||
13.702770533636055 | ||
], | ||
[ | ||
-85.77660501260364, | ||
13.74838907062059 | ||
], | ||
[ | ||
-85.665076084405, | ||
13.850998251941007 | ||
], | ||
[ | ||
-85.6005067049246, | ||
13.902285878470721 | ||
], | ||
[ | ||
-85.40679856647384, | ||
13.964955323125139 | ||
], | ||
[ | ||
-85.25418003314911, | ||
14.044691778389478 | ||
], | ||
[ | ||
-85.17200082289698, | ||
14.124400484351256 | ||
], | ||
[ | ||
-85.0722117818779, | ||
14.204081296445551 | ||
], | ||
[ | ||
-84.96655279726666, | ||
14.272356824780402 | ||
], | ||
[ | ||
-84.83741403830108, | ||
14.340611662938457 | ||
], | ||
[ | ||
-84.71708019472135, | ||
14.349142058421137 | ||
], | ||
[ | ||
-84.59674635113683, | ||
14.357672129027975 | ||
], | ||
[ | ||
-84.51456714088471, | ||
14.260979004431931 | ||
], | ||
[ | ||
-84.47347753575863, | ||
14.124400484351256 | ||
], | ||
[ | ||
-84.46760759217126, | ||
14.004827010370363 | ||
], | ||
[ | ||
-84.50869719729732, | ||
13.850998251941007 | ||
], | ||
[ | ||
-84.55565674601078, | ||
13.771195008964412 | ||
], | ||
[ | ||
-84.68479550497635, | ||
13.697067593148148 | ||
], | ||
[ | ||
-84.81980420753413, | ||
13.600096480816973 | ||
], | ||
[ | ||
-85.0780817254653, | ||
13.51449937543768 | ||
], | ||
[ | ||
-85.33635924340126, | ||
13.383192932862997 | ||
], | ||
[ | ||
-85.5682220151796, | ||
13.308943841325785 | ||
], | ||
[ | ||
-85.74725529465715, | ||
13.223247127704198 | ||
], | ||
[ | ||
-85.89987382797705, | ||
13.108934499398941 | ||
], | ||
[ | ||
-86.03488253053482, | ||
13.04603866930087 | ||
], | ||
[ | ||
-86.11706174078695, | ||
13.057475273637039 | ||
], | ||
[ | ||
-86.18750106385951, | ||
13.171812127937073 | ||
], | ||
[ | ||
-86.24033055616515, | ||
13.291808375225287 | ||
], | ||
[ | ||
-86.24033055616515, | ||
13.394615090294153 | ||
], | ||
[ | ||
-86.22272072539819, | ||
13.5030856470853 | ||
], | ||
[ | ||
-86.08771202284044, | ||
13.582979800801652 | ||
] | ||
] | ||
], | ||
"type": "Polygon" | ||
} | ||
} | ||
] |
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,20 @@ | ||
[ | ||
{ | ||
"post_title": "Prinzus", | ||
"post_id": 27229, | ||
"category": "Territories", | ||
"slug": "prinzus" | ||
}, | ||
{ | ||
"post_title": "Bawikas", | ||
"post_id": 27226, | ||
"category": "Territories", | ||
"slug": "bawikas" | ||
}, | ||
{ | ||
"post_title": "Panamakas", | ||
"post_id": 27222, | ||
"category": "Territories", | ||
"slug": "panamakas" | ||
} | ||
] |
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,37 @@ | ||
import os | ||
from unittest.mock import patch | ||
|
||
from django.test import TransactionTestCase | ||
from api.templatetags.custom_api_tags import external_api | ||
|
||
|
||
class TestCustomApiTags(TransactionTestCase): | ||
def test_external_api_when_test_live_server_does_not_exist(self): | ||
external_url = 'scheme://netloc/path;parameters?query#fragment' | ||
|
||
with patch('os.environ.get') as mock_get: | ||
mock_get.return_value = None | ||
output_url = external_api(external_url) | ||
|
||
self.assertEqual(external_url, output_url) | ||
|
||
def test_external_api_when_test_live_server_exists(self): | ||
external_url = 'scheme://netloc/path;parameters?query#fragment' | ||
os.environ['TEST_LIVE_SERVER_DOMAIN'] = 'https://example.com' | ||
expected_url = 'https://example.com/path;parameters' | ||
|
||
output_url = external_api(external_url) | ||
self.assertEqual(expected_url, output_url) | ||
|
||
def test_external_api_when_test_live_server_exists_and_there_is_an_error(self): | ||
""" | ||
When an error occurs, the external_url is used. And an error will occur b/c | ||
the test_live_server_domain is not a string value. | ||
""" | ||
external_url = 'scheme://netloc/path;parameters?query#fragment' | ||
|
||
with patch('os.environ.get') as mock_get: | ||
mock_get.return_value = -1 | ||
output_url = external_api(external_url) | ||
|
||
self.assertEqual(external_url, output_url) |