implement test for User.set_password, add Shadow.set_password

This commit is contained in:
Jan Dittberner 2014-06-01 14:51:33 +02:00
parent df9800b827
commit 83562ba2bf
2 changed files with 26 additions and 3 deletions

View file

@ -221,6 +221,7 @@ class User(TimeStampedModel, models.Model):
return '{0} ({1})'.format(self.username, self.uid) return '{0} ({1})'.format(self.username, self.uid)
def set_password(self, password): def set_password(self, password):
self.shadow.set_password(password)
UserTaskResult.objects.create_usertaskresult( UserTaskResult.objects.create_usertaskresult(
self, self,
create_ldap_user.delay( create_ldap_user.delay(
@ -289,12 +290,12 @@ class ShadowManager(models.Manager):
def create_shadow(self, user, password): def create_shadow(self, user, password):
changedays = (timezone.now().date() - date(1970, 1, 1)).days changedays = (timezone.now().date() - date(1970, 1, 1)).days
pwhash = sha512_crypt.encrypt(password)
shadow = self.create( shadow = self.create(
user=user, changedays=changedays, user=user, changedays=changedays,
minage=0, maxage=None, gracedays=7, minage=0, maxage=None, gracedays=7,
inactdays=30, expiredays=None, passwd=pwhash inactdays=30, expiredays=None
) )
shadow.set_password(password)
shadow.save() shadow.save()
return shadow return shadow
@ -342,6 +343,9 @@ class Shadow(TimeStampedModel, models.Model):
def __str__(self): def __str__(self):
return 'for user {0}'.format(self.user) return 'for user {0}'.format(self.user)
def set_password(self, password):
self.passwd = sha512_crypt.encrypt(password)
@python_2_unicode_compatible @python_2_unicode_compatible
class AdditionalGroup(TimeStampedModel, models.Model): class AdditionalGroup(TimeStampedModel, models.Model):

View file

@ -14,6 +14,7 @@ from osusers.models import (
GroupTaskResult, GroupTaskResult,
Shadow, Shadow,
User, User,
UserTaskResult,
) )
@ -254,5 +255,23 @@ class UserManagerTest(TestCaseWithCeleryTasks):
self.assertIsNotNone(user.shadow) self.assertIsNotNone(user.shadow)
@override_settings(
OSUSER_MINUID=10000, OSUSER_MINGID=10000, OSUSER_USERNAME_PREFIX='test',
OSUSER_HOME_BASEPATH='/home', OSUSER_DEFAULT_SHELL='/bin/fooshell'
)
class UserTest(TestCaseWithCeleryTasks): class UserTest(TestCaseWithCeleryTasks):
pass
def test___str__(self):
user = User.objects.create_user()
self.assertEqual(str(user), 'test01 (10000)')
def test_set_password(self):
user = User.objects.create_user()
self.assertFalse(sha512_crypt.verify('test', user.shadow.passwd))
UserTaskResult.objects.all().delete()
user.set_password('test')
self.assertTrue(sha512_crypt.verify('test', user.shadow.passwd))
taskres = UserTaskResult.objects.all()
self.assertEqual(len(taskres), 1)
self.assertEqual(taskres[0].user, user)
self.assertEqual(taskres[0].task_name, 'create_ldap_user')