add tests for osusers.models.UserManager

This commit is contained in:
Jan Dittberner 2014-06-01 14:43:42 +02:00
parent d82146987e
commit df9800b827

View file

@ -4,6 +4,8 @@ from django.test.utils import override_settings
from mock import patch, MagicMock
from passlib.hash import sha512_crypt
from osusers.models import (
CANNOT_USE_PRIMARY_GROUP_AS_ADDITIONAL,
AdditionalGroup,
@ -157,5 +159,100 @@ class TaskResultManagerTest(TestCase):
self.assertEqual(tr.task_name, TEST_TASK_NAME)
@override_settings(
OSUSER_MINUID=10000, OSUSER_MINGID=10000, OSUSER_USERNAME_PREFIX='test',
OSUSER_HOME_BASEPATH='/home', OSUSER_DEFAULT_SHELL='/bin/fooshell'
)
class UserManagerTest(TestCaseWithCeleryTasks):
def _create_group(self):
return Group.objects.create(gid=10000, groupname='foo')
def test_get_next_uid_first(self):
self.assertEqual(User.objects.get_next_uid(), 10000)
def test_get_next_uid_second(self):
User.objects.create(
uid=10010, username='foo', group=self._create_group(),
homedir='/home/foo', shell='/bin/fooshell')
self.assertEqual(User.objects.get_next_uid(), 10011)
def test_get_next_username_first(self):
self.assertEqual(User.objects.get_next_username(), 'test01')
def test_get_next_username_second(self):
User.objects.create(
uid=10000, username='test01', group=self._create_group(),
homedir='/home/foo', shell='/bin/fooshell')
self.assertEqual(User.objects.get_next_username(), 'test02')
def test_get_next_username_gaps(self):
group = self._create_group()
User.objects.create(
uid=10000, username='test01', group=group,
homedir='/home/foo', shell='/bin/fooshell')
User.objects.create(
uid=10002, username='test03', group=group,
homedir='/home/foo', shell='/bin/fooshell')
self.assertEqual(User.objects.get_next_username(), 'test02')
def test_create_user_first(self):
user = User.objects.create_user()
self.assertIsInstance(user, User)
self.assertEqual(user.uid, 10000)
self.assertEqual(user.group.gid, 10000)
self.assertEqual(user.group.groupname, 'test01')
self.assertEqual(user.username, 'test01')
self.assertEqual(user.homedir, '/home/test01')
self.assertEqual(user.shell, '/bin/fooshell')
self.assertIsNotNone(user.shadow)
def test_create_user_second(self):
User.objects.create_user()
user = User.objects.create_user()
self.assertIsInstance(user, User)
self.assertEqual(user.uid, 10001)
self.assertEqual(user.group.gid, 10001)
self.assertEqual(user.group.groupname, 'test02')
self.assertEqual(user.username, 'test02')
self.assertEqual(user.homedir, '/home/test02')
self.assertEqual(user.shell, '/bin/fooshell')
self.assertIsNotNone(user.shadow)
self.assertEqual(len(User.objects.all()), 2)
def test_create_user_known_password(self):
user = User.objects.create_user(password='foobar')
self.assertIsInstance(user, User)
self.assertEqual(user.uid, 10000)
self.assertEqual(user.group.gid, 10000)
self.assertEqual(user.group.groupname, 'test01')
self.assertEqual(user.username, 'test01')
self.assertEqual(user.homedir, '/home/test01')
self.assertEqual(user.shell, '/bin/fooshell')
self.assertIsNotNone(user.shadow)
self.assertTrue(sha512_crypt.verify('foobar', user.shadow.passwd))
def test_create_user_predefined_username(self):
user = User.objects.create_user(username='tester')
self.assertIsInstance(user, User)
self.assertEqual(user.uid, 10000)
self.assertEqual(user.group.gid, 10000)
self.assertEqual(user.group.groupname, 'tester')
self.assertEqual(user.username, 'tester')
self.assertEqual(user.homedir, '/home/tester')
self.assertEqual(user.shell, '/bin/fooshell')
self.assertIsNotNone(user.shadow)
def test_create_user_commit(self):
user = User.objects.create_user(commit=True)
self.assertIsInstance(user, User)
self.assertEqual(user.uid, 10000)
self.assertEqual(user.group.gid, 10000)
self.assertEqual(user.group.groupname, 'test01')
self.assertEqual(user.username, 'test01')
self.assertEqual(user.homedir, '/home/test01')
self.assertEqual(user.shell, '/bin/fooshell')
self.assertIsNotNone(user.shadow)
class UserTest(TestCaseWithCeleryTasks):
pass