You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jumpserver/apps/users/tests/test_models.py

110 lines
3.5 KiB

8 years ago
# ~*~ coding: utf-8 ~*~
8 years ago
from django.utils import timezone
8 years ago
from django.shortcuts import reverse
8 years ago
from django.test import TestCase, Client, TransactionTestCase
from django.db import IntegrityError
from users.models import User, UserGroup, Role, init_all_models
8 years ago
from django.contrib.auth.models import Permission
from .base import gen_name, gen_username, gen_email, get_role
8 years ago
class UserModelTest(TransactionTestCase):
def setUp(self):
init_all_models()
8 years ago
# 创建一个用户用于测试
role = get_role()
8 years ago
user = User(name='test', username='test', email='test@email.org', role=role)
user.save()
8 years ago
def test_initial(self):
self.assertEqual(User.objects.all().count(), 2)
self.assertEqual(Role.objects.all().count(), 3)
self.assertEqual(UserGroup.objects.all().count(), 1)
@property
def role(self):
return get_role()
8 years ago
# 创建一个姓名一致的用户, 应该创建成功
def test_user_name_duplicate(self):
8 years ago
user1 = User(name='test', username=gen_username(), password_raw=gen_username(),
8 years ago
email=gen_email(), role=self.role)
8 years ago
try:
user1.save()
user1.delete()
except IntegrityError:
self.assertTrue(0, 'Duplicate <name> not allowed.')
8 years ago
# 创建一个用户名一致的用户, 应该创建不成功
def test_user_username_duplicate(self):
user2 = User(username='test', email=gen_email(), role=self.role)
with self.assertRaises(IntegrityError):
8 years ago
user2.save()
8 years ago
# 创建一个Email一致的用户,应该创建不成功
def test_user_email_duplicate(self):
user3 = User(username=gen_username(), email='test@email.org', role=self.role)
with self.assertRaises(IntegrityError):
8 years ago
user3.save()
8 years ago
# 用户过期测试
8 years ago
def test_user_was_expired(self):
date = timezone.now() - timezone.timedelta(days=1)
user = User(name=gen_name(), username=gen_username(),
8 years ago
email=gen_email(), role=self.role, date_expired=date)
8 years ago
self.assertTrue(user.is_expired())
8 years ago
# 测试用户默认会输入All用户组
8 years ago
def test_user_with_default_group(self):
role = get_role()
8 years ago
user = User(username=gen_username(), email=gen_email(), role=role)
user.save()
self.assertEqual(user.groups.count(), 1)
8 years ago
self.assertEqual(user.groups.first().name, 'Default')
8 years ago
8 years ago
def test_user_password_authenticated(self):
password = gen_username() * 3
user = User(username=gen_username(), password_raw=password, role=self.role)
user.save()
self.assertTrue(user.check_password(password))
self.assertFalse(user.check_password(password*2))
8 years ago
8 years ago
def tearDown(self):
User.objects.all().delete()
UserGroup.objects.all().delete()
Role.objects.all().delete()
class RoleModelTestCase(TransactionTestCase):
def setUp(self):
Role.objects.all().delete()
Role.initial()
def test_role_initial(self):
self.assertEqual(Role.objects.all().count(), 3)
def test_create_new_role(self):
role = Role(name=gen_name(), comment=gen_name()*3)
role.save()
role.permissions = Permission.objects.all()
role.save()
self.assertEqual(Role.objects.count(), 4)
role = Role.objects.last()
self.assertEqual(role.permissions.all().count(), Permission.objects.all().count())
class UserGroupModelTestCase(TransactionTestCase):
8 years ago
pass
8 years ago