def test_class_cannot_be_loaded(self, class_path, class_name, path_exist, class_exist, expected_error): class_package = class_path class_package += ':' + class_name if class_name else '' actions.plugins.toolkit.config = {PARSER_CONFIG_PROP: class_package} # Recover exception actions.plugins.toolkit.ValidationError = self._plugins.toolkit.ValidationError # Configure the mock package = MagicMock() if class_name and not class_exist: delattr(package, class_name) actions.importlib.import_module = MagicMock(side_effect=ImportError(IMPORT_ERROR_MSG) if not path_exist else None, return_value=package if path_exist else None) if expected_error: with self.assertRaises(actions.plugins.toolkit.ValidationError) as cm: actions.package_acquired({}, {}) self.assertEqual(cm.exception.error_dict['message'], expected_error) else: # Exception is not risen self.assertEquals(None, actions.package_acquired({}, {})) # Checks self.assertEquals(0, actions.plugins.toolkit.get_action.call_count)
def test_class_cannot_be_loaded(self, class_path, class_name, path_exist, class_exist, expected_error): class_package = class_path class_package += ':' + class_name if class_name else '' actions.config = {PARSER_CONFIG_PROP: class_package} # Recover exception actions.plugins.toolkit.ValidationError = self._plugins.toolkit.ValidationError # Configure the mock package = MagicMock() if class_name and not class_exist: delattr(package, class_name) actions.importlib.import_module = MagicMock( side_effect=ImportError(IMPORT_ERROR_MSG) if not path_exist else None, return_value=package if path_exist else None) if expected_error: with self.assertRaises( actions.plugins.toolkit.ValidationError) as cm: actions.package_acquired({}, {}) self.assertEqual(cm.exception.error_dict['message'], expected_error) else: # Exception is not risen self.assertEquals(None, actions.package_acquired({}, {})) # Checks self.assertEquals(0, actions.plugins.toolkit.get_action.call_count)
def test_add_users(self, users_info, datasets_not_found, not_updatable_datasets, allowed_users=[]): parse_result = {'users_datasets': []} creator_user = {'name': 'ckan', 'id': '1234'} # Transform user_info for user in users_info: parse_result['users_datasets'].append({'user': user, 'datasets': users_info[user]}) parse_notification, package_show, package_update, user_show = self.configure_mocks(parse_result, datasets_not_found, not_updatable_datasets, allowed_users, creator_user) # Call the function context = {'user': '******', 'model': 'model', 'auth_obj': {'id': 1}} result = actions.package_acquired(context, users_info) # Calculate the list of warns warns = [] for user_datasets in parse_result['users_datasets']: for dataset_id in user_datasets['datasets']: if dataset_id in datasets_not_found: warns.append('Dataset %s was not found in this instance' % dataset_id) elif dataset_id in not_updatable_datasets: # warns.append('%s(%s): %s' % (dataset_id, 'allowed_users', ADD_USERS_ERROR)) warns.append('Unable to upload the dataset %s: It\'s a public dataset' % dataset_id) expected_result = {'warns': warns} if len(warns) > 0 else None # Check that the returned result is as expected self.assertEquals(expected_result, result) # Check that the initial functions (check_access and parse_notification) has been called properly parse_notification.assert_called_once_with(users_info) actions.plugins.toolkit.check_access.assert_called_once_with('package_acquired', context, users_info) for user_datasets in parse_result['users_datasets']: for dataset_id in user_datasets['datasets']: # The show function is always called context_show = context.copy() context_show['ignore_auth'] = True context_show['updating_via_cb'] = True package_show.assert_any_call(context_show, {'id': dataset_id}) # The update function is called only when the show function does not throw an exception and # when the user is not in the list of allowed users. if dataset_id not in datasets_not_found and allowed_users is not None and user_datasets['user'] not in allowed_users and dataset_id not in not_updatable_datasets: # Calculate the list of allowed_users expected_allowed_users = list(allowed_users) expected_allowed_users.append(user_datasets['user']) context_update = context.copy() context_update['ignore_auth'] = True context_update['user'] = creator_user['name'] package_update.assert_any_call(context_update, {'id': dataset_id, 'allowed_users': expected_allowed_users, 'private': True, 'creator_user_id': creator_user['id']})
def test_add_users(self, users_info, datasets_not_found, not_updatable_datasets, allowed_users=[]): parse_result = { 'users_datasets': [{ 'user': user, 'datasets': users_info[user] } for user in users_info] } creator_user = {'name': 'ckan', 'id': '1234'} parse_notification, package_show, package_update, user_show = self.configure_mocks( parse_result, datasets_not_found, not_updatable_datasets, allowed_users, creator_user) # Call the function context = { 'user': '******', 'model': 'model', 'auth_obj': { 'id': 1 }, 'method': 'grant' } result = actions.package_acquired(context, users_info) # Calculate the list of warns warns = [] for user_datasets in parse_result['users_datasets']: for dataset_id in user_datasets['datasets']: if dataset_id in datasets_not_found: warns.append('Dataset %s was not found in this instance' % dataset_id) elif dataset_id in not_updatable_datasets: # warns.append('%s(%s): %s' % (dataset_id, 'allowed_users', ADD_USERS_ERROR)) warns.append( 'Unable to upload the dataset %s: It\'s a public dataset' % dataset_id) expected_result = {'warns': warns} if len(warns) > 0 else None # Check that the returned result is as expected self.assertEquals(expected_result, result) # Check that the initial functions (check_access and parse_notification) has been called properly parse_notification.assert_called_once_with(users_info) actions.plugins.toolkit.check_access.assert_called_once_with( 'package_acquired', context, users_info) for user_datasets in parse_result['users_datasets']: for dataset_id in user_datasets['datasets']: # The show function is always called context_show = context.copy() context_show['ignore_auth'] = True context_show['updating_via_cb'] = True package_show.assert_any_call(context_show, {'id': dataset_id}) # The update function is called only when the show function does not throw an exception and # when the user is not in the list of allowed users. if dataset_id not in datasets_not_found and allowed_users is not None and user_datasets[ 'user'] not in allowed_users and dataset_id not in not_updatable_datasets: # Calculate the list of allowed_users expected_allowed_users = list(allowed_users) expected_allowed_users.append(user_datasets['user']) context_update = context.copy() context_update['ignore_auth'] = True context_update['user'] = creator_user['name'] package_update.assert_any_call( context_update, { 'id': dataset_id, 'allowed_users': expected_allowed_users, 'private': True, 'creator_user_id': creator_user['id'] })