ckanext-privatedatasets/ckanext/privatedatasets/tests/test_controller_ui.py

165 lines
6.4 KiB
Python
Raw Normal View History

2014-09-02 17:52:55 +02:00
# -*- coding: utf-8 -*-
# Copyright (c) 2014 CoNWeT Lab., Universidad Politécnica de Madrid
# This file is part of CKAN Private Dataset Extension.
# CKAN Private Dataset Extension is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# CKAN Private Dataset Extension is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with CKAN Private Dataset Extension. If not, see <http://www.gnu.org/licenses/>.
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-08-28 15:19:34 +02:00
self.instanceUI = controller.AcquiredDatasetsControllerUI()
2014-07-08 12:01:54 +02:00
# Load the mocks
self._plugins = controller.plugins
controller.plugins = MagicMock()
2014-07-08 12:01:54 +02:00
self._model = controller.model
controller.model = MagicMock()
2014-07-08 12:01:54 +02:00
self._db = controller.db
controller.db = MagicMock()
2014-07-08 12:01:54 +02:00
# Set exceptions
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
controller.plugins = self._plugins
controller.model = self._model
controller.db = self._db
2014-07-08 12:01:54 +02:00
@parameterized.expand([
(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)
controller.plugins.toolkit.get_action = MagicMock(return_value=user_show)
2014-07-08 12:01:54 +02:00
# Call the function
2014-08-28 15:19:34 +02:00
self.instanceUI.user_acquired_datasets()
2014-07-08 12:01:54 +02:00
# Assertations
expected_context = {
'model': controller.model,
'session': controller.model.Session,
'user': controller.plugins.toolkit.c.user
2014-07-08 12:01:54 +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},),
({1: controller.plugins.toolkit.NotAuthorized, 2: controller.plugins.toolkit.ObjectNotFound},),
({}, [1]),
({}, [3, 2]),
({1: controller.plugins.toolkit.NotAuthorized}, [2]),
({1: controller.plugins.toolkit.NotAuthorized, 2: controller.plugins.toolkit.ObjectNotFound}, [1, 3]),
2014-07-08 12:47:57 +02:00
])
def test_no_error_loading_users(self, package_errors={}, deleted_packages=[]):
2014-07-08 12:01:54 +02:00
pkgs_ids = [0, 1, 2, 3]
user = 'example_user_test'
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']
pkg['state'] = 'deleted' if data_dict['id'] in deleted_packages else 'active'
2014-07-08 12:47:57 +02:00
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
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
pkg.user_name = user
2014-07-08 12:01:54 +02:00
query_res.append(pkg)
controller.db.AllowedUser.get = MagicMock(return_value=query_res)
2014-07-08 12:01:54 +02:00
# Call the function
2014-08-28 15:19:34 +02:00
returned = self.instanceUI.user_acquired_datasets()
2014-07-08 12:01:54 +02:00
# Check that the database has been initialized properly
controller.db.init_db.assert_called_once_with(controller.model)
2014-07-08 12:01:54 +02:00
# User_show called correctly
expected_context = {
'model': controller.model,
'session': controller.model.Session,
'user': controller.plugins.toolkit.c.user
2014-07-08 12:01:54 +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
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()
2014-08-28 15:19:34 +02:00
expected_user_dict['acquired_datasets'] = []
2014-07-08 12:01:54 +02:00
for i in pkgs_ids:
if i not in package_errors and i not in deleted_packages:
2014-07-08 12:47:57 +02:00
pkg = default_package.copy()
pkg['pkg_id'] = i
pkg['state'] = 'deleted' if i in deleted_packages else 'active'
2014-08-28 15:19:34 +02:00
expected_user_dict['acquired_datasets'].append(pkg)
2014-07-08 12:47:57 +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
self.assertEquals(controller.plugins.toolkit.render.return_value, returned)
2014-08-28 15:19:34 +02:00
controller.plugins.toolkit.render.assert_called_once_with('user/dashboard_acquired.html')