Skip to content

Commit

Permalink
Merge pull request #3082 from k-nut/3027-test
Browse files Browse the repository at this point in the history
Allow Sysadmins to create users with the form (and test)
  • Loading branch information
wardi committed Jun 15, 2016
2 parents 60b951d + a75630d commit 55ae76e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
14 changes: 11 additions & 3 deletions ckan/controllers/user.py
Expand Up @@ -166,7 +166,8 @@ def new(self, data=None, errors=None, error_summary=None):
'''GET to display a form for registering a new user.
or POST the form data to actually do the user registration.
'''
context = {'model': model, 'session': model.Session,
context = {'model': model,
'session': model.Session,
'user': c.user,
'auth_user_obj': c.userobj,
'schema': self._new_form_to_db_schema(),
Expand All @@ -180,7 +181,7 @@ def new(self, data=None, errors=None, error_summary=None):
if context['save'] and not data:
return self._save_new(context)

if c.user and not data:
if c.user and not data and not authz.is_sysadmin(c.user):
# #1799 Don't offer the registration form if already logged in
return render('user/logout_first.html')

Expand Down Expand Up @@ -264,7 +265,14 @@ def _save_new(self, context):
h.flash_success(_('User "%s" is now registered but you are still '
'logged in as "%s" from before') %
(data_dict['name'], c.user))
return render('user/logout_first.html')
if authz.is_sysadmin(c.user):
# the sysadmin created a new user. We redirect him to the
# activity page for the newly created user
h.redirect_to(controller='user',
action='activity',
id=data_dict['name'])
else:
return render('user/logout_first.html')

def edit(self, id=None, data=None, errors=None, error_summary=None):
context = {'save': 'save' in request.params,
Expand Down
31 changes: 31 additions & 0 deletions ckan/tests/controllers/test_user.py
Expand Up @@ -59,6 +59,37 @@ def test_register_user_bad_password(self):
response = form.submit('save')
assert_true('The passwords you entered do not match' in response)

def test_create_user_as_sysadmin(self):
admin_pass = 'pass'
sysadmin = factories.Sysadmin(password=admin_pass)
app = self._get_test_app()

# Have to do an actual login as this test relies on repoze
# cookie handling.

# get the form
response = app.get('/user/login')
# ...it's the second one
login_form = response.forms[1]
# fill it in
login_form['login'] = sysadmin['name']
login_form['password'] = admin_pass
# submit it
login_form.submit('save')

response = app.get(
url=url_for(controller='user', action='register'),
)
assert "user-register-form" in response.forms
form = response.forms['user-register-form']
form['name'] = 'newestuser'
form['fullname'] = 'Newest User'
form['email'] = 'test@test.com'
form['password1'] = 'testpassword'
form['password2'] = 'testpassword'
response2 = form.submit('save')
assert '/user/activity' in response2.location


class TestLoginView(helpers.FunctionalTestBase):
def test_registered_user_login(self):
Expand Down

0 comments on commit 55ae76e

Please sign in to comment.