mirror of https://github.com/jumpserver/jumpserver
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.
110 lines
3.5 KiB
110 lines
3.5 KiB
# ~*~ coding: utf-8 ~*~
|
|
|
|
|
|
from django.utils import timezone
|
|
from django.shortcuts import reverse
|
|
from django.test import TestCase, Client, TransactionTestCase
|
|
from django.db import IntegrityError
|
|
from users.models import User, UserGroup, Role, init_all_models
|
|
from django.contrib.auth.models import Permission
|
|
|
|
from .base import gen_name, gen_username, gen_email, get_role
|
|
|
|
|
|
class UserModelTest(TransactionTestCase):
|
|
def setUp(self):
|
|
init_all_models()
|
|
|
|
# 创建一个用户用于测试
|
|
role = get_role()
|
|
user = User(name='test', username='test', email='test@email.org', role=role)
|
|
user.save()
|
|
|
|
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()
|
|
|
|
# 创建一个姓名一致的用户, 应该创建成功
|
|
def test_user_name_duplicate(self):
|
|
user1 = User(name='test', username=gen_username(), password_raw=gen_username(),
|
|
email=gen_email(), role=self.role)
|
|
try:
|
|
user1.save()
|
|
user1.delete()
|
|
except IntegrityError:
|
|
self.assertTrue(0, 'Duplicate <name> not allowed.')
|
|
|
|
# 创建一个用户名一致的用户, 应该创建不成功
|
|
def test_user_username_duplicate(self):
|
|
user2 = User(username='test', email=gen_email(), role=self.role)
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
user2.save()
|
|
|
|
# 创建一个Email一致的用户,应该创建不成功
|
|
def test_user_email_duplicate(self):
|
|
user3 = User(username=gen_username(), email='test@email.org', role=self.role)
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
user3.save()
|
|
|
|
# 用户过期测试
|
|
def test_user_was_expired(self):
|
|
date = timezone.now() - timezone.timedelta(days=1)
|
|
user = User(name=gen_name(), username=gen_username(),
|
|
email=gen_email(), role=self.role, date_expired=date)
|
|
|
|
self.assertTrue(user.is_expired())
|
|
|
|
# 测试用户默认会输入All用户组
|
|
def test_user_with_default_group(self):
|
|
role = get_role()
|
|
user = User(username=gen_username(), email=gen_email(), role=role)
|
|
user.save()
|
|
|
|
self.assertEqual(user.groups.count(), 1)
|
|
self.assertEqual(user.groups.first().name, 'Default')
|
|
|
|
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))
|
|
|
|
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):
|
|
pass
|
|
|
|
|
|
|