From 6c6e3a191cb0cfab4086c9d42cfcde5b7acfbbe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Arranz?= Date: Tue, 2 Oct 2018 17:34:33 +0200 Subject: [PATCH] Fix get_absolute_static_url calling get_current_scheme without passing the request --- src/wirecloud/commons/tests/utils.py | 28 +++++++++++++++++++++++++++- src/wirecloud/commons/utils/http.py | 2 +- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/wirecloud/commons/tests/utils.py b/src/wirecloud/commons/tests/utils.py index 4c0b913d42..83631bd9be 100644 --- a/src/wirecloud/commons/tests/utils.py +++ b/src/wirecloud/commons/tests/utils.py @@ -31,11 +31,12 @@ from wirecloud.commons.exceptions import ErrorResponse from wirecloud.commons.utils.html import clean_html, filter_changelog -from wirecloud.commons.utils.http import build_downloadfile_response, build_sendfile_response, get_current_domain, get_current_scheme, get_content_type, normalize_boolean_param, produces, validate_url_param +from wirecloud.commons.utils.http import build_downloadfile_response, build_sendfile_response, get_absolute_static_url, get_current_domain, get_current_scheme, get_content_type, normalize_boolean_param, produces, validate_url_param from wirecloud.commons.utils.log import SkipUnreadablePosts from wirecloud.commons.utils.mimeparser import best_match, InvalidMimeType, parse_mime_type from wirecloud.commons.utils.version import Version from wirecloud.commons.utils.wgt import WgtFile +from wirecloud.platform.core.plugins import get_version_hash # Avoid nose to repeat these tests (they are run through wirecloud/commons/tests/__init__.py) @@ -565,6 +566,31 @@ def test_get_content_type_invalid_mime_type(self): request.META['CONTENT_TYPE'] = 'application/json/ji' self.assertEqual(get_content_type(request), ('', {})) + @override_settings(STATIC_URL="/static/") + @patch("wirecloud.commons.utils.http.get_current_scheme") + @patch("wirecloud.commons.utils.http.get_current_domain") + def test_get_absolute_static_url_unversioned(self, get_current_domain, get_current_scheme): + request = self._prepare_request_mock() + get_current_scheme.return_value = "http" + get_current_domain.return_value = "dashboards.example.org" + + self.assertEqual(get_absolute_static_url("/path", request=request), 'http://dashboards.example.org/path') + + get_current_scheme.assert_called_once_with(request) + get_current_domain.assert_called_once_with(request) + + @override_settings(STATIC_URL="/static/") + @patch("wirecloud.commons.utils.http.get_current_scheme") + @patch("wirecloud.commons.utils.http.get_current_domain") + def test_get_absolute_static_url_versioned(self, get_current_domain, get_current_scheme): + request = self._prepare_request_mock() + get_current_scheme.return_value = "http" + get_current_domain.return_value = "dashboards.example.org" + self.assertEqual(get_absolute_static_url("/path", request=request, versioned=True), 'http://dashboards.example.org/path?v=' + get_version_hash()) + + get_current_scheme.assert_called_once_with(request) + get_current_domain.assert_called_once_with(request) + @override_settings(FORCE_PROTO=None) def test_get_current_scheme_http(self): request = self._prepare_request_mock() diff --git a/src/wirecloud/commons/utils/http.py b/src/wirecloud/commons/utils/http.py index f411508c0c..c1c9ed8213 100644 --- a/src/wirecloud/commons/utils/http.py +++ b/src/wirecloud/commons/utils/http.py @@ -343,7 +343,7 @@ def get_absolute_reverse_url(viewname, request=None, **kwargs): def get_absolute_static_url(url, request=None, versioned=False): from django.conf import settings - scheme = get_current_scheme() + scheme = get_current_scheme(request) base = urljoin(scheme + '://' + get_current_domain(request), settings.STATIC_URL) if versioned: