Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion GeoHealthCheck/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,10 @@ def edit_resource(resource_identifier):
flash(gettext('Resource not found'), 'danger')
return redirect(request.referrer)

probes_avail = views.get_probes_avail(resource.resource_type, resource)
# Editing must remain possible when the monitored service is unavailable.
# Probe expansion can make live GetCapabilities requests.
probes_avail = views.get_probes_avail(resource.resource_type, resource,
expand_params=False)

suggestions = json.dumps(Recipient.get_suggestions('email',
g.user.username))
Expand Down
3 changes: 3 additions & 0 deletions GeoHealthCheck/probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ class Probe(Plugin):

def __init__(self):
Plugin.__init__(self)
# expand_params() modifies these definitions for a Resource. Keep
# choices from one Resource out of other requests and edit forms.
self.PARAM_DEFS = Plugin.copy(self.PARAM_DEFS)
self._resource = None
self._session = create_requests_retry_session()

Expand Down
4 changes: 2 additions & 2 deletions GeoHealthCheck/templates/includes/probe_edit_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
{% set probe_param_type = 'list' %}
{% set probe_param_val = probe_param_val|join(',') %}
{% endif %}
<!-- parameters: use select box when range defined, else regular input box -->
{% if probe_param_range %}
<!-- Keep a saved value editable when it is absent from static choices. -->
{% if probe_param_range and (not probe_param_val or probe_param_val in probe_param_range) %}
<select class="form-control" data-probe-param="{{ param }}" data-probe-param-type="{{ probe_param_type }}">
<!-- select box for range: select value if defined else use default value if defined -->
{% for probe_param_range_val in probe_param_range %}
Expand Down
5 changes: 3 additions & 2 deletions GeoHealthCheck/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,12 @@ def get_query_field_term(query):
return [field, term]


def get_probes_avail(resource_type=None, resource=None):
def get_probes_avail(resource_type=None, resource=None, expand_params=True):
"""
Get all available Probes with their attributes.
:param resource_type: optional resource type e.g. OGC:WMS
:param resource: optional Resource instance
:param expand_params: fetch resource metadata to populate parameter choices
:return:
"""

Expand All @@ -227,7 +228,7 @@ def get_probes_avail(resource_type=None, resource=None):
for probe_class in probe_classes:
probe = Factory.create_obj(probe_class)
if probe:
if resource:
if resource and expand_params:
try:
probe._resource = resource
probe.expand_params(resource)
Expand Down
7 changes: 5 additions & 2 deletions tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@
# Shorthand to run all test scripts in tests dir.
# TODO use nose or more intelligent test_*.py discovery
if __name__ == '__main__':
unittest.main(module='test_plugins', exit=False)
unittest.main(module='test_resources')
results = [
unittest.main(module=module, exit=False).result
for module in ('test_plugins', 'test_resources', 'test_edit_offline')
]
sys.exit(0 if all(result.wasSuccessful() for result in results) else 1)
70 changes: 70 additions & 0 deletions tests/test_edit_offline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""The resource editor must work while an OGC service is unavailable."""

import os
import unittest
from unittest.mock import patch

from app import APP
from models import DB, CheckVars, ProbeVars, Resource, User, load_data
from GeoHealthCheck.plugins.probe.wmts import WmtsGetTile, WmtsGetTileAll


TEST_DIR = os.path.dirname(os.path.abspath(__file__))


class OfflineResourceEditTest(unittest.TestCase):
def setUp(self):
with patch('models.util.geocode', return_value=(0.0, 0.0)):
load_data('%s/data/minimal.json' % TEST_DIR)
owner = User.query.filter_by(username='admin').one()
resource = Resource(owner, 'OGC:WMTS', 'Offline WMTS',
'https://example.invalid/wmts', [])
resource.active = False

probe = ProbeVars(resource,
'GeoHealthCheck.plugins.probe.wmts.WmtsGetTile',
{'layers': ['layer_a', 'layer_b'],
'tilematrixset': 'EPSG:4326',
'kvprest': 'KVP'})
CheckVars(probe,
'GeoHealthCheck.plugins.check.checks.HttpStatusNoError',
{})
DB.session.add(resource)
DB.session.commit()
self.resource_id = resource.identifier
self.client = APP.test_client()
response = self.client.post('/login',
data={'username': 'admin',
'password': 'admin'})
self.assertEqual(response.status_code, 302)

def tearDown(self):
DB.session.remove()
DB.drop_all()

def test_edit_page_does_not_expand_remote_probe_parameters(self):
with patch.object(WmtsGetTile, 'expand_params') as expand_tile, \
patch.object(WmtsGetTileAll, 'expand_params') as expand_all:
response = self.client.get(
'/resource/%d/edit' % self.resource_id)

self.assertEqual(response.status_code, 200)
expand_tile.assert_not_called()
expand_all.assert_not_called()
self.assertIn(b'value="layer_a,layer_b"', response.data)
# A saved value outside the static choices must not become the
# browser's first choice when the form is saved.
self.assertIn(b'value="EPSG:4326"', response.data)
self.assertIn(b'value="KVP"', response.data)

def test_probe_parameter_definitions_are_isolated(self):
first = WmtsGetTile()
second = WmtsGetTile()
first.PARAM_DEFS['layers']['range'] = ['another_resource']

self.assertIsNone(second.PARAM_DEFS['layers']['range'])
self.assertIsNone(WmtsGetTile.PARAM_DEFS['layers']['range'])


if __name__ == '__main__':
unittest.main()