Skip to content
Merged
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
17 changes: 16 additions & 1 deletion ckanext/unaids/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ckan.lib.helpers import build_nav_main as core_build_nav_main


from urllib.parse import quote, urlencode
from urllib.parse import quote, urlencode, urlsplit

log = logging.getLogger()
BULK_FILE_UPLOADER_DEFAULT_FIELDS = 'ckanext.bulk_file_uploader_default_fields'
Expand Down Expand Up @@ -218,6 +218,21 @@ def get_google_analytics_id():
return from_env


def get_support_url():
url = os.environ.get('CKAN_UNAIDS_SUPPORT_URL', None) \
or toolkit.config.get('ckanext.unaids.support_url', None)
if not url:
return None
url = url.strip()
try:
parts = urlsplit(url)
except ValueError:
return None
if parts.scheme == 'https' and parts.netloc:
return url
return None


def is_an_estimates_dataset(dataset_type_name):
return 'estimates' in dataset_type_name.lower()

Expand Down
2 changes: 2 additions & 0 deletions ckanext/unaids/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
get_language_code,
build_pages_nav_main,
get_google_analytics_id,
get_support_url,
is_an_estimates_dataset,
url_encode,
get_profile_editor_url,
Expand Down Expand Up @@ -152,6 +153,7 @@ def get_helpers(self):
"get_language_code": get_language_code,
"build_nav_main": build_pages_nav_main,
"get_google_analytics_id": get_google_analytics_id,
"get_support_url": get_support_url,
"is_an_estimates_dataset": is_an_estimates_dataset,
"url_encode": url_encode,
"get_profile_editor_url": get_profile_editor_url,
Expand Down
40 changes: 40 additions & 0 deletions ckanext/unaids/tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
import pytest
from ckan.tests import factories
from ckan.plugins import toolkit
from ckanext.unaids.helpers import get_support_url


class TestGetSupportUrl(object):
@pytest.mark.parametrize('value,expected', [
('https://support.example.org/new', 'https://support.example.org/new'),
(' https://support.example.org/new ', 'https://support.example.org/new'),
('HTTPS://support.example.org', 'HTTPS://support.example.org'),
('http://support.example.org', None),
('javascript:alert(1)', None),
('data:text/html,hi', None),
('//evil.example.org', None),
('/relative/path', None),
('https://', None),
('https:///no-host', None),
])
def test_only_absolute_https_is_exposed(self, monkeypatch, value, expected):
monkeypatch.setenv('CKAN_UNAIDS_SUPPORT_URL', value)
assert get_support_url() == expected

def test_none_when_unset(self, monkeypatch):
monkeypatch.delenv('CKAN_UNAIDS_SUPPORT_URL', raising=False)
monkeypatch.delitem(
toolkit.config, 'ckanext.unaids.support_url', raising=False)
assert get_support_url() is None

def test_falls_back_to_config(self, monkeypatch):
monkeypatch.delenv('CKAN_UNAIDS_SUPPORT_URL', raising=False)
monkeypatch.setitem(
toolkit.config, 'ckanext.unaids.support_url',
'https://config.example.org/new')
assert get_support_url() == 'https://config.example.org/new'

def test_env_takes_precedence_over_config(self, monkeypatch):
monkeypatch.setitem(
toolkit.config, 'ckanext.unaids.support_url',
'https://config.example.org/new')
monkeypatch.setenv(
'CKAN_UNAIDS_SUPPORT_URL', 'https://env.example.org/new')
assert get_support_url() == 'https://env.example.org/new'


@pytest.mark.ckan_config('ckan.plugins', 'activity ytp_request unaids scheming_datasets versions')
Expand Down
4 changes: 3 additions & 1 deletion ckanext/unaids/theme/templates/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
<li><a href={{h.url_for("/pages/terms-and-conditions")}}>{{ _('Terms and Conditions') }}</a></li>
<li><a href={{h.url_for("/pages/cookies")}}>{{ _('Cookie Policy') }}</a></li>
<li><a target="blank" href="https://github.com/fjelltopp/adx_develop">{{_('Github Source Code')}}</a></li>
<li><a target="blank" href="https://adr.unaids.org/bug")>{{_('Report a Bug')}}</a></li>
{% if h.get_support_url() %}
<li><a target="_blank" rel="noopener" href="{{ h.get_support_url() }}">{{_('Report a Bug')}}</a></li>
{% endif %}
{% endblock %}
</ul>
{% endblock %}
Expand Down