Skip to content

Commit

Permalink
[#3046] improve patching in test
Browse files Browse the repository at this point in the history
  • Loading branch information
Knut Hühne committed Jun 7, 2016
1 parent ff93ffb commit 42632dc
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions ckan/tests/controllers/test_package.py
Expand Up @@ -8,7 +8,7 @@
assert_true,
)

from mock import patch
from mock import patch, MagicMock
from routes import url_for

import ckan.model as model
Expand Down Expand Up @@ -58,22 +58,37 @@ def test_needs_organization_but_no_organizations_has_button(self,):
assert 'dataset-edit' not in response.forms
assert url_for(controller='organization', action='new') in response

@patch('ckan.logic.auth.create.organization_create',
return_value={'success': False})
@helpers.change_config('ckan.auth.create_unowned_dataset', 'false')
def test_needs_organization_but_no_organizations_no_button(self, *args):
@helpers.change_config('ckan.auth.user_create_organizations', 'false')
def test_needs_organization_but_no_organizations_no_button(self):
''' Scenario: The settings say every dataset needs an organization
but there are no organizations. If the user is not allowed to create an
organization they should be told to ask the admin but no link should be
presented'''
presented. Note: This cannot happen with the default ckan and requires
a plugin to overwrite the package_create behavior'''

# Make sure that the patched `package_create` is actually looked at
# by the authentication functions.
from ckan.authz import clear_auth_functions_cache
p = patch('ckan.logic.auth.create.package_create',
new=MagicMock(return_value={'success': True}))
p.start()
clear_auth_functions_cache()

app = self._get_test_app()
sysadmin = factories.Sysadmin()
user = factories.User()

env = {'REMOTE_USER': sysadmin['name'].encode('ascii')}
env = {'REMOTE_USER': user['name'].encode('ascii')}
response = app.get(
url=url_for(controller='package', action='new'),
extra_environ=env
)

# Stop patching `package_create` and make sure that the other
# tests are not influenced by clearing the cache again
p.stop()
clear_auth_functions_cache()

assert 'dataset-edit' not in response.forms
assert url_for(controller='organization', action='new') not in response
assert 'Ask a system administrator' in response
Expand Down

0 comments on commit 42632dc

Please sign in to comment.