2014-07-08 12:30:28 +02:00
|
|
|
import ckanext.privatedatasets.controllers.ui_controller as controller
|
2014-07-08 12:01:54 +02:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from mock import MagicMock, ANY
|
|
|
|
from nose_parameterized import parameterized
|
|
|
|
|
|
|
|
|
|
|
|
class UIControllerTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
|
|
|
# Get the instance
|
2014-07-08 12:30:28 +02:00
|
|
|
self.instanceUI = controller.AdquiredDatasetsControllerUI()
|
2014-07-08 12:01:54 +02:00
|
|
|
|
|
|
|
# Load the mocks
|
2014-07-08 12:30:28 +02:00
|
|
|
self._plugins = controller.plugins
|
|
|
|
controller.plugins = MagicMock()
|
2014-07-08 12:01:54 +02:00
|
|
|
|
2014-07-08 12:30:28 +02:00
|
|
|
self._model = controller.model
|
|
|
|
controller.model = MagicMock()
|
2014-07-08 12:01:54 +02:00
|
|
|
|
2014-07-15 10:32:19 +02:00
|
|
|
self._db = controller.db
|
|
|
|
controller.db = MagicMock()
|
|
|
|
|
2014-07-08 12:01:54 +02:00
|
|
|
# Set exceptions
|
2014-07-08 12:30:28 +02:00
|
|
|
controller.plugins.toolkit.ObjectNotFound = self._plugins.toolkit.ObjectNotFound
|
|
|
|
controller.plugins.toolkit.NotAuthorized = self._plugins.toolkit.NotAuthorized
|
2014-07-08 12:01:54 +02:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
# Unmock
|
2014-07-08 12:30:28 +02:00
|
|
|
controller.plugins = self._plugins
|
|
|
|
controller.model = self._model
|
2014-07-15 10:32:19 +02:00
|
|
|
controller.db = self._db
|
2014-07-08 12:01:54 +02:00
|
|
|
|
|
|
|
@parameterized.expand([
|
2014-07-08 12:30:28 +02:00
|
|
|
(controller.plugins.toolkit.ObjectNotFound, 404),
|
|
|
|
(controller.plugins.toolkit.NotAuthorized, 401)
|
2014-07-08 12:01:54 +02:00
|
|
|
])
|
|
|
|
def test_exceptions_loading_users(self, exception, expected_status):
|
|
|
|
|
|
|
|
# Configure the mock
|
|
|
|
user_show = MagicMock(side_effect=exception)
|
2014-07-08 12:30:28 +02:00
|
|
|
controller.plugins.toolkit.get_action = MagicMock(return_value=user_show)
|
2014-07-08 12:01:54 +02:00
|
|
|
|
|
|
|
# Call the function
|
|
|
|
self.instanceUI.user_adquired_datasets()
|
|
|
|
|
|
|
|
# Assertations
|
|
|
|
expected_context = {
|
2014-07-08 12:30:28 +02:00
|
|
|
'model': controller.model,
|
|
|
|
'session': controller.model.Session,
|
|
|
|
'user': controller.plugins.toolkit.c.user
|
2014-07-08 12:01:54 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 12:30:28 +02:00
|
|
|
user_show.assert_called_once_with(expected_context, {'user_obj': controller.plugins.toolkit.c.userobj})
|
|
|
|
controller.plugins.toolkit.abort.assert_called_once_with(expected_status, ANY)
|
2014-07-08 12:01:54 +02:00
|
|
|
|
2014-07-08 12:47:57 +02:00
|
|
|
@parameterized.expand([
|
|
|
|
({},),
|
|
|
|
({2: controller.plugins.toolkit.ObjectNotFound},),
|
|
|
|
({1: controller.plugins.toolkit.NotAuthorized},)
|
|
|
|
])
|
|
|
|
def test_no_error_loading_users(self, package_errors={}):
|
2014-07-08 12:01:54 +02:00
|
|
|
|
|
|
|
pkgs_ids = [0, 1, 2, 3]
|
|
|
|
user = 'example_user_test'
|
2014-07-08 12:30:28 +02:00
|
|
|
controller.plugins.toolkit.c.user = user
|
2014-07-08 12:01:54 +02:00
|
|
|
|
|
|
|
# get_action mock
|
2014-07-08 12:47:57 +02:00
|
|
|
default_package = {'pkg_id': 0, 'test': 'ok', 'res': 'ta'}
|
|
|
|
|
|
|
|
def _package_show(context, data_dict):
|
|
|
|
if data_dict['id'] in package_errors:
|
|
|
|
raise package_errors[data_dict['id']]('ERROR')
|
|
|
|
else:
|
|
|
|
pkg = default_package.copy()
|
|
|
|
pkg['pkg_id'] = data_dict['id']
|
|
|
|
return pkg
|
|
|
|
|
2014-07-08 12:01:54 +02:00
|
|
|
user_dict = {'user_name': 'test', 'another_val': 'example value'}
|
2014-07-08 12:47:57 +02:00
|
|
|
package_show = MagicMock(side_effect=_package_show)
|
2014-07-08 12:01:54 +02:00
|
|
|
user_show = MagicMock(return_value=user_dict.copy())
|
|
|
|
|
|
|
|
def _get_action(action):
|
|
|
|
if action == 'package_show':
|
|
|
|
return package_show
|
|
|
|
elif action == 'user_show':
|
|
|
|
return user_show
|
|
|
|
|
2014-07-08 12:30:28 +02:00
|
|
|
controller.plugins.toolkit.get_action = MagicMock(side_effect=_get_action)
|
2014-07-08 12:01:54 +02:00
|
|
|
|
|
|
|
# query mock
|
|
|
|
query_res = []
|
|
|
|
for i in pkgs_ids:
|
|
|
|
pkg = MagicMock()
|
|
|
|
pkg.package_id = i
|
2014-07-15 10:32:19 +02:00
|
|
|
pkg.user_name = user
|
2014-07-08 12:01:54 +02:00
|
|
|
query_res.append(pkg)
|
|
|
|
|
2014-07-15 10:32:19 +02:00
|
|
|
controller.db.AllowedUser.get = MagicMock(return_value=query_res)
|
2014-07-08 12:01:54 +02:00
|
|
|
|
|
|
|
# Call the function
|
|
|
|
returned = self.instanceUI.user_adquired_datasets()
|
|
|
|
|
|
|
|
# User_show called correctly
|
|
|
|
expected_context = {
|
2014-07-08 12:30:28 +02:00
|
|
|
'model': controller.model,
|
|
|
|
'session': controller.model.Session,
|
|
|
|
'user': controller.plugins.toolkit.c.user
|
2014-07-08 12:01:54 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 12:30:28 +02:00
|
|
|
user_show.assert_called_once_with(expected_context, {'user_obj': controller.plugins.toolkit.c.userobj})
|
2014-07-08 12:01:54 +02:00
|
|
|
|
|
|
|
# Query called correctry
|
2014-07-15 10:32:19 +02:00
|
|
|
controller.db.AllowedUser.get.assert_called_once_with(user_name=user)
|
2014-07-08 12:01:54 +02:00
|
|
|
|
|
|
|
# Assert that the package_show has been called properly
|
|
|
|
self.assertEquals(len(pkgs_ids), package_show.call_count)
|
|
|
|
for i in pkgs_ids:
|
|
|
|
package_show.assert_any_call(expected_context, {'id': i})
|
|
|
|
|
2014-07-08 12:47:57 +02:00
|
|
|
# Check that the template receives the correct datasets
|
2014-07-08 12:01:54 +02:00
|
|
|
expected_user_dict = user_dict.copy()
|
|
|
|
expected_user_dict['adquired_datasets'] = []
|
|
|
|
for i in pkgs_ids:
|
2014-07-08 12:47:57 +02:00
|
|
|
if i not in package_errors:
|
|
|
|
pkg = default_package.copy()
|
|
|
|
pkg['pkg_id'] = i
|
|
|
|
expected_user_dict['adquired_datasets'].append(pkg)
|
|
|
|
|
2014-07-08 12:30:28 +02:00
|
|
|
self.assertEquals(expected_user_dict, controller.plugins.toolkit.c.user_dict)
|
2014-07-08 12:01:54 +02:00
|
|
|
|
|
|
|
# Check that the render method has been called and that its result has been returned
|
2014-07-08 12:30:28 +02:00
|
|
|
self.assertEquals(controller.plugins.toolkit.render.return_value, returned)
|
|
|
|
controller.plugins.toolkit.render.assert_called_once_with('user/dashboard_adquired.html')
|