Skip to content

Commit

Permalink
[#1226] Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelbabu committed Oct 14, 2013
1 parent 5023e0c commit 878772c
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 4 deletions.
190 changes: 187 additions & 3 deletions ckan/tests/functional/api/test_user.py
@@ -1,20 +1,26 @@
import paste
from pylons import config
from nose.tools import assert_equal

import ckan.logic as logic
import ckan.new_authz as new_authz
from ckan import model
from ckan.lib.create_test_data import CreateTestData
from ckan.tests import TestController as ControllerTestCase
from ckan.tests.pylons_controller import PylonsTestCase
from ckan.tests import url_for
import ckan.config.middleware
from ckan.common import json

class TestUserApi(ControllerTestCase):
@classmethod
def setup_class(cls):
CreateTestData.create()

@classmethod
def teardown_class(cls):
model.repo.rebuild_db()

def test_autocomplete(self):
response = self.app.get(
url=url_for(controller='api', action='user_autocomplete', ver=2),
Expand Down Expand Up @@ -51,8 +57,186 @@ def test_autocomplete_limit(self):
print response.json
assert_equal(len(response.json), 1)


class TestCreateUserApiDisabled(PylonsTestCase):
'''
Tests for the creating user when create_user_via_api is disabled.
'''

@classmethod
def setup_class(cls):
CreateTestData.create()
cls._original_config = config.copy()
new_authz.clear_auth_functions_cache()
wsgiapp = ckan.config.middleware.make_app(config['global_conf'],
**config)
cls.app = paste.fixture.TestApp(wsgiapp)
cls.sysadmin_user = model.User.get('testsysadmin')
PylonsTestCase.setup_class()

@classmethod
def teardown_class(cls):
config.clear()
config.update(cls._original_config)
new_authz.clear_auth_functions_cache()
PylonsTestCase.teardown_class()

model.repo.rebuild_db()

def test_user_create_api_disabled_sysadmin(self):
params = {
'name': 'testinganewusersysadmin',
'email': 'testinganewuser@ckan.org',
'password': 'random',
}
res = self.app.post('/api/3/action/user_create', json.dumps(params),
extra_environ={'Authorization': str(self.sysadmin_user.apikey)},
expect_errors=True)
res_dict = res.json
assert res_dict['success'] is False

def test_user_create_api_disabled_anon(self):
params = {
'name': 'testinganewuseranon',
'email': 'testinganewuser@ckan.org',
'password': 'random',
}
res = self.app.post('/api/3/action/user_create', json.dumps(params),
expect_errors=True)
res_dict = res.json
assert res_dict['success'] is False


class TestCreateUserApiEnabled(PylonsTestCase):
'''
Tests for the creating user when create_user_via_api is enabled.
'''

@classmethod
def setup_class(cls):
CreateTestData.create()
cls._original_config = config.copy()
config['ckan.auth.create_user_via_api'] = True
new_authz.clear_auth_functions_cache()
wsgiapp = ckan.config.middleware.make_app(config['global_conf'],
**config)
cls.app = paste.fixture.TestApp(wsgiapp)
PylonsTestCase.setup_class()
cls.sysadmin_user = model.User.get('testsysadmin')

@classmethod
def teardown_class(cls):
config.clear()
config.update(cls._original_config)
new_authz.clear_auth_functions_cache()
PylonsTestCase.teardown_class()

model.repo.rebuild_db()

def test_user_create_api_disabled_sysadmin(self):
params = {
'name': 'testinganewusersysadmin',
'email': 'testinganewuser@ckan.org',
'password': 'random',
}
res = self.app.post('/api/3/action/user_create', json.dumps(params),
extra_environ={'Authorization':
str(self.sysadmin_user.apikey)})
res_dict = res.json
assert res_dict['success'] is True

def test_user_create_api_disabled_anon(self):
params = {
'name': 'testinganewuseranon',
'email': 'testinganewuser@ckan.org',
'password': 'random',
}
res = self.app.post('/api/3/action/user_create', json.dumps(params),
expect_errors=True)
res_dict = res.json
assert res_dict['success'] is True


class TestCreateUserWebDisabled(PylonsTestCase):
'''
Tests for the creating user by create_user_via_web is disabled.
'''

@classmethod
def setup_class(cls):
CreateTestData.create()
cls._original_config = config.copy()
config['ckan.auth.create_user_via_web'] = False
new_authz.clear_auth_functions_cache()
wsgiapp = ckan.config.middleware.make_app(config['global_conf'],
**config)
cls.app = paste.fixture.TestApp(wsgiapp)
cls.sysadmin_user = model.User.get('testsysadmin')
PylonsTestCase.setup_class()

@classmethod
def teardown_class(cls):
config.clear()
config.update(cls._original_config)
new_authz.clear_auth_functions_cache()
PylonsTestCase.teardown_class()

model.repo.rebuild_db()

def test_user_create_api_disabled(self):
params = {
'name': 'testinganewuser',
'email': 'testinganewuser@ckan.org',
'password': 'random',
}
res = self.app.post('/api/3/action/user_create', json.dumps(params),
extra_environ={'Authorization': str(self.sysadmin_user.apikey)},
expect_errors=True)
res_dict = res.json
assert res_dict['success'] is False


class TestCreateUserWebEnabled(PylonsTestCase):
'''
Tests for the creating user by create_user_via_web is enabled.
'''

@classmethod
def setup_class(cls):
CreateTestData.create()
cls._original_config = config.copy()
config['ckan.auth.create_user_via_web'] = True
new_authz.clear_auth_functions_cache()
wsgiapp = ckan.config.middleware.make_app(config['global_conf'],
**config)
cls.app = paste.fixture.TestApp(wsgiapp)
cls.sysadmin_user = model.User.get('testsysadmin')
PylonsTestCase.setup_class()

@classmethod
def teardown_class(cls):
config.clear()
config.update(cls._original_config)
new_authz.clear_auth_functions_cache()
PylonsTestCase.teardown_class()

model.repo.rebuild_db()

def test_user_create_api_disabled(self):
params = {
'name': 'testinganewuser',
'email': 'testinganewuser@ckan.org',
'password': 'random',
}
res = self.app.post('/api/3/action/user_create', json.dumps(params),
extra_environ={'Authorization': str(self.sysadmin_user.apikey)},
expect_errors=True)
res_dict = res.json
assert res_dict['success'] is False


class TestUserActions(object):

@classmethod
def setup_class(cls):
CreateTestData.create()
Expand Down
3 changes: 2 additions & 1 deletion ckan/tests/logic/test_auth.py
Expand Up @@ -11,8 +11,9 @@
'user_create_organizations': False,
'user_delete_groups': False,
'user_delete_organizations': False,
'create_user_via_api': False,
'create_unowned_dataset': False,
'create_user_via_api': False,
'create_user_via_web': True,
}


Expand Down

0 comments on commit 878772c

Please sign in to comment.