refactor UserManager.create_user

- remove duplicate create_ldap_group task
- create Shadow in User.set_password if necessary
- add test UserManagerTest.test_create_user_tasks for group task
This commit is contained in:
Jan Dittberner 2014-06-01 15:03:15 +02:00
parent 83562ba2bf
commit ceeffb6d1c
2 changed files with 13 additions and 7 deletions

View file

@ -185,15 +185,9 @@ class UserManager(models.Manager):
password = generate_password()
homedir = os.path.join(settings.OSUSER_HOME_BASEPATH, username)
group = Group.objects.create(groupname=username, gid=gid)
GroupTaskResult.objects.create_grouptaskresult(
group,
create_ldap_group.delay(group.groupname, group.gid, group.descr),
'create_ldap_group'
)
user = self.create(username=username, group=group, uid=uid,
homedir=homedir,
shell=settings.OSUSER_DEFAULT_SHELL)
Shadow.objects.create_shadow(user=user, password=password)
user.set_password(password)
if commit:
user.save()
@ -221,7 +215,12 @@ class User(TimeStampedModel, models.Model):
return '{0} ({1})'.format(self.username, self.uid)
def set_password(self, password):
self.shadow.set_password(password)
if hasattr(self, 'shadow'):
self.shadow.set_password(password)
else:
self.shadow = Shadow.objects.create_shadow(
user=self, password=password
)
UserTaskResult.objects.create_usertaskresult(
self,
create_ldap_user.delay(

View file

@ -207,6 +207,13 @@ class UserManagerTest(TestCaseWithCeleryTasks):
self.assertEqual(user.shell, '/bin/fooshell')
self.assertIsNotNone(user.shadow)
def test_create_user_tasks(self):
user = User.objects.create_user()
gtaskres = GroupTaskResult.objects.all()
self.assertEqual(len(gtaskres), 1)
self.assertEqual(gtaskres[0].task_name, 'create_ldap_group')
self.assertEqual(gtaskres[0].group, user.group)
def test_create_user_second(self):
User.objects.create_user()
user = User.objects.create_user()