Skip to content

Commit

Permalink
Initial setup_template_variables tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aarranz committed Sep 12, 2018
1 parent 0278eb2 commit 536ad2b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 21 deletions.
37 changes: 17 additions & 20 deletions ckanext/right_time_context/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,35 +121,32 @@ def setup_template_variables(self, context, data_dict):
resource.setdefault('auth_type', 'none')

url = resource['url']
if format_lower == NGSI_FORMAT and not check_query(resource):
if not self.proxy_is_enabled:
details = "</br></br>Enable resource_proxy</br></br></br>"
f_details = "Enable resource_proxy."
h.flash_error(f_details, allow_html=False)
view_enable = [False, details]
elif resource['auth_type'] != 'none' and not self.oauth2_is_enabled:
details = "</br></br>In order to see this resource properly, enable oauth2 extension</br></br></br>"
f_details = "In order to see this resource properly, enable oauth2 extension."
h.flash_error(f_details, allow_html=False)
view_enable = [False, details]
elif format_lower == NGSI_FORMAT and not check_query(resource):
details = "</br></br>This is not a ContextBroker query, please check <a href='https://forge.fiware.org/plugins/mediawiki/wiki/fiware/index.php/Publish/Subscribe_Broker_-_Orion_Context_Broker_-_User_and_Programmers_Guide'>Orion Context Broker documentation</a></br></br></br>"
f_details = "This is not a ContextBroker query, please check Orion Context Broker documentation."
h.flash_error(f_details, allow_html=False)
view_enable = [False, details]
elif not self.proxy_is_enabled:
details = "</br></br>Enable resource_proxy</br></br></br>"
f_details = "Enable resource_proxy."
elif resource['auth_type'] != 'none' and not p.toolkit.c.user:
details = "</br></br>In order to see this resource properly, you need to be logged in.</br></br></br>"
f_details = "In order to see this resource properly, you need to be logged in."
h.flash_error(f_details, allow_html=False)
view_enable = [False, details]
url = ''
else:
# All checks passed
url = self.get_proxified_ngsi_url(data_dict)

if resource['auth_type'] != 'none' and not p.toolkit.c.user:
details = "</br></br>In order to see this resource properly, you need to be logged in.</br></br></br>"
f_details = "In order to see this resource properly, you need to be logged in."
h.flash_error(f_details, allow_html=False)
view_enable = [False, details]

elif resource['auth_type'] != 'none' and not self.oauth2_is_enabled:
details = "</br></br>In order to see this resource properly, enable oauth2 extension</br></br></br>"
f_details = "In order to see this resource properly, enable oauth2 extension."
h.flash_error(f_details, allow_html=False)
view_enable = [False, details]

else:
data_dict['resource']['url'] = url
view_enable = [True, 'OK']
data_dict['resource']['url'] = url
view_enable = [True, 'OK']

return {
'resource_json': json.dumps(data_dict['resource']),
Expand Down
45 changes: 44 additions & 1 deletion ckanext/right_time_context/tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
# You should have received a copy of the GNU Affero General Public License
# along with ckanext-right_time_context. If not, see http://www.gnu.org/licenses/.

import json
import unittest

from mock import DEFAULT, patch
from mock import ANY, DEFAULT, patch
from parameterized import parameterized

from ckan.plugins.toolkit import ValidationError
Expand Down Expand Up @@ -88,3 +89,45 @@ def test_before_show(self, resource, deserialized):

instance.before_show(resource)
self.assertEquals(deserialized, resource)

@parameterized.expand([
{"fiware-ngsi", "https://context.example.org/v2/entities", "none", None},
{"fiware-ngsi", "https://context.example.org/v1/queryContext", "none", None},
{"fiware-ngsi", "https://context.example.org/v1/contextEntities", "none", None},
{"fiware-ngsi", "https://context.example.org/v1/contextEntities/Entity", "none", None},
{"fiware-ngsi", "https://context.example.org/v2/entities", "none", "noproxy"},
{"fiware-ngsi", "https://context.example.org/v2/entities", "oauth2", "nooauth2"},
{"fiware-ngsi", "https://context.example.org/other/path", "none", "noquery"},
{"fiware-ngsi", "https://context.example.org/v2/entities", "oauth2", "nologged"},
{"fiware-ngsi-registry", "https://context.example.org/", "none", None},
{"fiware-ngsi-registry", "https://context.example.org/", "none", "noproxy"},
{"fiware-ngsi-registry", "https://context.example.org/", "oauth2", "nooauth2"},
{"fiware-ngsi-registry", "https://context.example.org/", "oauth2", "nologged"},
])
@patch.multiple('ckanext.right_time_context.plugin', p=DEFAULT, h=DEFAULT)
def test_setup_template_variables(self, resource_format, url, auth_type, error, p, h):
instance = plugin.NgsiView()
data_dict = {
"resource": {
"auth_type": auth_type,
"format": resource_format,
"url": url,
}
}
instance.proxy_is_enabled = error != "noproxy"
instance.oauth2_is_enabled = error != "nooauth2"
p.toolkit.c.user = error != "nologged"

with patch.object(instance, "get_proxified_ngsi_url", return_value="proxied_url"):
result = instance.setup_template_variables(None, data_dict)

view_enable = json.loads(result['view_enable'])
self.assertEqual(result['resource_json'], json.dumps(data_dict['resource']))

if error is None:
self.assertEqual(view_enable, {True, 'OK'})
self.assertEqual(result['resource_url'], '"proxied_url"')
else:
h.flash_error.assert_called_with(ANY, allow_html=False)
self.assertFalse(view_enable[0])
self.assertNotEqual(view_enable[1], 'OK')

0 comments on commit 536ad2b

Please sign in to comment.