Skip to content

Commit

Permalink
Merge pull request #341 from localcontexts/8689rhmp9-additional-gis-f…
Browse files Browse the repository at this point in the history
…e-testcases

Mock External API For Testcases
  • Loading branch information
birbjam authored Oct 3, 2024
2 parents 9e960f7 + f7d74b9 commit c886a39
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 5 deletions.
Empty file added api/templatetags/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions api/templatetags/custom_api_tags.py
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
8 changes: 4 additions & 4 deletions templates/communities/add-community-boundary.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends 'register-base.html' %} {% block title %} Add Community {% endblock %} {% load static %} {% block card %}
{% extends 'register-base.html' %} {% block title %} Add Community {% endblock %} {% load static %} {% block card %} {% load custom_api_tags %}
<style>
#define-community-top-text{
width: 94%;
Expand Down Expand Up @@ -184,7 +184,7 @@ <h2 class="mt-0">Search the Native Land Digital database</h2>
</div>

<script>
fetch('https://native-land.ca/wp-json/nativeland/v1/map-list').then(resp => resp.json()).then(response => {
fetch('{% external_api "https://native-land.ca/wp-json/nativeland/v1/map-list" %}').then(resp => resp.json()).then(response => {

document.querySelector('#region-container input.search').addEventListener("keyup", (event) => {
let html = ''
Expand Down Expand Up @@ -248,8 +248,8 @@ <h2 class="mt-0">Search the Native Land Digital database</h2>
// do post call to get boundary
const href = document.querySelector('#community-boundary-continue-btn').getAttribute('href')
const slug = document.querySelector('#selected-title').getAttribute('data-slug')

const url = `https://native-land.ca/wp-json/nativeland/v1/api/index.php?maps=territories&name=${slug}`
const url = `{% external_api "https://native-land.ca/wp-json/nativeland/v1/api/index.php?maps=territories&name=${slug}" %}`
const boundaryResponse = await fetch(url)
const boundaryData = await boundaryResponse.json()
document.querySelector("#community-boundary-continue-btn .fa-spinner").classList.remove("spinner-container");
Expand Down
14 changes: 14 additions & 0 deletions tests/external_api/urls.py
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)
]
23 changes: 23 additions & 0 deletions tests/external_api/views.py
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)
152 changes: 152 additions & 0 deletions tests/fixtures/native_land_boundary_response.json
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"
}
}
]
20 changes: 20 additions & 0 deletions tests/fixtures/native_land_map_list.json
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"
}
]
1 change: 1 addition & 0 deletions tests/functional/test_create_community_gis_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
class TestFeatures(UiFeatureHelper):
def setUp(self):
self.login()
os.environ['TEST_LIVE_SERVER_DOMAIN'] = self.live_server_url
self.alert_dialog = Alert(self.py.webdriver)
self.community_name = 'placeholder'
self.fake = faker.Faker()
Expand Down
5 changes: 4 additions & 1 deletion tests/functional/ui_feature_testcase_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import pytest

from django.contrib.auth.hashers import make_password
from django.urls import reverse
from django.contrib.auth.hashers import make_password
from django.test.utils import override_settings
from django.contrib.staticfiles.testing import StaticLiveServerTestCase

from factories.accounts_factories import UserFactory
from external_api.urls import UrlsWithMockedExternalApi


@override_settings(ROOT_URLCONF=UrlsWithMockedExternalApi)
@pytest.mark.usefixtures("py")
class UiFeatureHelper(StaticLiveServerTestCase):
def login(self,):
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/test_custom_api_tags.py
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)

0 comments on commit c886a39

Please sign in to comment.