2016-08-22 11:53:01 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
|
2016-08-18 14:29:35 +00:00
|
|
|
|
2016-08-22 11:53:01 +00:00
|
|
|
from django.utils import timezone
|
2016-08-22 16:39:07 +00:00
|
|
|
from django.shortcuts import reverse
|
2016-08-24 09:14:21 +00:00
|
|
|
from django.test import TestCase, TransactionTestCase
|
2016-08-23 14:19:39 +00:00
|
|
|
from django.db import IntegrityError
|
2016-08-24 09:14:21 +00:00
|
|
|
from users.models import User, UserGroup, init_all_models
|
2016-08-22 16:39:07 +00:00
|
|
|
from django.contrib.auth.models import Permission
|
2016-08-18 14:29:35 +00:00
|
|
|
|
2016-08-23 14:19:39 +00:00
|
|
|
from .base import gen_name, gen_username, gen_email, get_role
|
2016-08-23 11:36:15 +00:00
|
|
|
|
|
|
|
|
2016-08-22 11:53:01 +00:00
|
|
|
class UserModelTest(TransactionTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
init_all_models()
|
|
|
|
|
2016-08-22 16:39:07 +00:00
|
|
|
# 创建一个用户用于测试
|
2016-08-23 14:19:39 +00:00
|
|
|
role = get_role()
|
2016-08-22 11:53:01 +00:00
|
|
|
user = User(name='test', username='test', email='test@email.org', role=role)
|
|
|
|
user.save()
|
|
|
|
|
2016-08-22 16:39:07 +00:00
|
|
|
def test_initial(self):
|
|
|
|
self.assertEqual(User.objects.all().count(), 2)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def role(self):
|
2016-08-23 14:19:39 +00:00
|
|
|
return get_role()
|
2016-08-22 16:39:07 +00:00
|
|
|
|
|
|
|
# 创建一个姓名一致的用户, 应该创建成功
|
|
|
|
def test_user_name_duplicate(self):
|
2016-08-22 11:53:01 +00:00
|
|
|
user1 = User(name='test', username=gen_username(), password_raw=gen_username(),
|
2016-08-24 09:14:21 +00:00
|
|
|
email=gen_email())
|
2016-08-22 11:53:01 +00:00
|
|
|
try:
|
|
|
|
user1.save()
|
|
|
|
user1.delete()
|
|
|
|
except IntegrityError:
|
|
|
|
self.assertTrue(0, 'Duplicate <name> not allowed.')
|
|
|
|
|
2016-08-22 16:39:07 +00:00
|
|
|
# 创建一个用户名一致的用户, 应该创建不成功
|
|
|
|
def test_user_username_duplicate(self):
|
|
|
|
user2 = User(username='test', email=gen_email(), role=self.role)
|
2016-08-23 14:19:39 +00:00
|
|
|
|
|
|
|
with self.assertRaises(IntegrityError):
|
2016-08-22 11:53:01 +00:00
|
|
|
user2.save()
|
|
|
|
|
2016-08-22 16:39:07 +00:00
|
|
|
# 创建一个Email一致的用户,应该创建不成功
|
|
|
|
def test_user_email_duplicate(self):
|
|
|
|
user3 = User(username=gen_username(), email='test@email.org', role=self.role)
|
2016-08-23 14:19:39 +00:00
|
|
|
|
|
|
|
with self.assertRaises(IntegrityError):
|
2016-08-22 11:53:01 +00:00
|
|
|
user3.save()
|
|
|
|
|
2016-08-22 16:39:07 +00:00
|
|
|
# 用户过期测试
|
2016-08-22 11:53:01 +00:00
|
|
|
def test_user_was_expired(self):
|
|
|
|
date = timezone.now() - timezone.timedelta(days=1)
|
|
|
|
user = User(name=gen_name(), username=gen_username(),
|
2016-08-22 16:39:07 +00:00
|
|
|
email=gen_email(), role=self.role, date_expired=date)
|
2016-08-22 11:53:01 +00:00
|
|
|
|
2016-08-24 09:14:21 +00:00
|
|
|
self.assertTrue(user.is_expired)
|
2016-08-22 11:53:01 +00:00
|
|
|
|
2016-08-22 16:39:07 +00:00
|
|
|
# 测试用户默认会输入All用户组
|
2016-08-22 11:53:01 +00:00
|
|
|
def test_user_with_default_group(self):
|
2016-08-23 14:19:39 +00:00
|
|
|
role = get_role()
|
2016-08-22 11:53:01 +00:00
|
|
|
user = User(username=gen_username(), email=gen_email(), role=role)
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
self.assertEqual(user.groups.count(), 1)
|
2016-08-22 16:39:07 +00:00
|
|
|
self.assertEqual(user.groups.first().name, 'Default')
|
2016-08-22 11:53:01 +00:00
|
|
|
|
2016-08-22 16:39:07 +00:00
|
|
|
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))
|
2016-08-22 11:53:01 +00:00
|
|
|
|
2016-08-22 16:39:07 +00:00
|
|
|
def tearDown(self):
|
|
|
|
User.objects.all().delete()
|
|
|
|
UserGroup.objects.all().delete()
|
|
|
|
|
|
|
|
|
|
|
|
class UserGroupModelTestCase(TransactionTestCase):
|
2016-08-19 16:18:44 +00:00
|
|
|
pass
|
2016-08-22 16:39:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|