Skip to content

Commit

Permalink
Fix traceback when adding new users in Admin. Fixes #642
Browse files Browse the repository at this point in the history
  • Loading branch information
atodorov committed Nov 30, 2018
1 parent 55209c2 commit af92bb8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
8 changes: 6 additions & 2 deletions tcms/kiwi_auth/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,18 @@ def get_readonly_fields(self, request, obj=None):
readonly_fields = ['username', 'is_staff', 'is_active',
'is_superuser', 'last_login', 'date_joined',
'groups', 'user_permissions']
if not _modifying_myself(request, obj.pk):
if obj and not _modifying_myself(request, obj.pk):
readonly_fields.extend(['first_name', 'last_name', 'email'])

return readonly_fields

def get_fieldsets(self, request, obj=None):
# super-user adding new account
if not obj and request.user.is_superuser:
return super().get_fieldsets(request, obj)

first_fieldset_fields = ('username',)
if _modifying_myself(request, obj.pk):
if obj and _modifying_myself(request, obj.pk):
first_fieldset_fields = first_fieldset_fields + ('password',)

remaining_fieldsets = (
Expand Down
23 changes: 23 additions & 0 deletions tcms/kiwi_auth/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from http import HTTPStatus

from django.conf import settings
from django.contrib.auth.models import User

from tcms.tests import LoggedInTestCase
from tcms.tests.factories import UserFactory
Expand Down Expand Up @@ -77,3 +78,25 @@ def test_admin_can_update_other_users(self):

self.tester.refresh_from_db()
self.assertEqual(self.tester.first_name, 'Changed by admin')

def test_admin_can_open_the_add_users_page(self):
# test for https://github.com/kiwitcms/Kiwi/issues/642
self.client.login( # nosec:B106:hardcoded_password_funcarg
username=self.admin.username,
password='admin-password')
response = self.client.get('/admin/auth/user/add/')

self.assertEqual(HTTPStatus.OK, response.status_code)

def test_admin_can_add_new_users(self):
self.client.login( # nosec:B106:hardcoded_password_funcarg
username=self.admin.username,
password='admin-password')
response = self.client.post('/admin/auth/user/add/', {
'username': 'added-by-admin',
'password1': 'xo-xo-xo',
'password2': 'xo-xo-xo',
}, follow=True)

self.assertEqual(HTTPStatus.OK, response.status_code)
self.assertTrue(User.objects.filter(username='added-by-admin').exists())

0 comments on commit af92bb8

Please sign in to comment.