ckanext-privatedatasets/ckanext/privatedatasets/tests/test_helpers.py

83 lines
2.5 KiB
Python

import unittest
import ckanext.privatedatasets.helpers as helpers
from mock import MagicMock
from nose_parameterized import parameterized
class HelpersTest(unittest.TestCase):
def setUp(self):
# Create mocks
self._model = helpers.model
helpers.model = MagicMock()
self._tk = helpers.tk
helpers.tk = MagicMock()
self._db = helpers.db
helpers.db = MagicMock()
def tearDown(self):
helpers.model = self._model
helpers.tk = self._tk
helpers.db = self._db
@parameterized.expand([
(False, False, 'user', False),
(True, False, 'user', True),
(False, True, 'user', False),
(True, True, 'user', True),
(False, False, None, False),
(True, False, None, False),
(False, True, None, False),
(True, True, None, False),
])
def test_is_adquired(self, db_auth, table_initialized, user, adquired):
# Configure test
helpers.tk.c.user = user
pkg_dict = {'id': 'package_id'}
helpers.db.package_allowed_users_table = None if not table_initialized else MagicMock()
db_response = []
if db_auth is True:
out = helpers.db.AllowedUser()
out.package_id = 'package_id'
out.user_name = user
db_response.append(out)
helpers.db.AllowedUser.get = MagicMock(return_value=db_response)
# Check the function returns the expected result
self.assertEquals(adquired, helpers.is_adquired(pkg_dict))
if not table_initialized:
helpers.db.init_db.assert_called_once_with(helpers.model)
else:
self.assertEquals(0, helpers.db.init_db.call_count)
@parameterized.expand([
(1, 1, True),
(1, 2, False),
(1, None, False)
])
def test_is_owner(self, creator_user_id, user_id, owner):
# Configure test
user = MagicMock()
user.id = user_id
helpers.tk.c.userobj = user
pkg_dict = {'creator_user_id': creator_user_id}
# Check that the functions return the expected result
self.assertEquals(owner, helpers.is_owner(pkg_dict))
@parameterized.expand([
([], ''),
(['a'], 'a'),
(['a', 'b'], 'a,b'),
(['a', 'b', 'c', 'd'], 'a,b,c,d'),
])
def test_get_allowed_users_str(self, allowed_users, expected_result):
self.assertEquals(expected_result, helpers.get_allowed_users_str(allowed_users))