Add user add view Test Case

pull/530/head
ibuler 8 years ago
parent b55b516fc3
commit c37dea2079

@ -1,5 +1,5 @@
<div class="row border-bottom">
<nav class="navbar navbar-static-top" role="navigation" style="margin-bottom: 0">
<nav class="navbar navbar-static-top white-bg" role="navigation" style="margin-bottom: 0">
<div class="navbar-header">
<a class="navbar-minimalize minimalize-styl-2 btn btn-primary " href="#"><i class="fa fa-bars"></i> </a>
<form role="search" class="navbar-form-custom" method="get" action="">

@ -130,13 +130,13 @@ class User(AbstractUser):
# If user not set name, it's default equal username
if not self.name:
self.name = self.username
super(User, self).save(args, **kwargs)
super(User, self).save(*args, **kwargs)
# Set user default group 'All'
# Todo: It's have bug
group = UserGroup.initial()
if group not in self.groups.all():
self.groups.add(group)
super(User, self).save(args, **kwargs)
# super(User, self).save(*args, **kwargs)
class Meta:
db_table = 'user'

@ -10,6 +10,7 @@ from django.test.utils import setup_test_environment
from django.db import IntegrityError, transaction
from .models import User, UserGroup, Role, init_all_models
from django.contrib.auth.models import Permission
from django.conf import settings
def gen_username():
@ -24,6 +25,11 @@ def gen_name():
return forgery_py.name.full_name()
def get_role():
role = choice(Role.objects.all())
return role
class UserModelTest(TransactionTestCase):
def setUp(self):
init_all_models()
@ -123,18 +129,52 @@ class UserGroupModelTestCase(TransactionTestCase):
pass
class UserListViewTests(TestCase):
class UserListViewTests(TransactionTestCase):
def setUp(self):
init_all_models()
User.generate_fake()
def test_list_view_with_one_user(self):
def test_a_new_user_in_list(self):
username = gen_username()
user = User(username=username, email=gen_email(), role=get_role())
user.save()
response = self.client.get(reverse('users:user-list'))
self.assertContains(response, username)
def test_list_view_with_admin_user(self):
response = self.client.get(reverse('users:user-list'))
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Admin')
self.assertQuerysetEqual(response.context['user_list'], [repr(user) for user in User.objects.all()])
self.assertEqual(response.context['user_list'].count(), User.objects.all().count())
def test_pagination(self):
settings.CONFIG.DISPLAY_PER_PAGE = 10
User.generate_fake(count=20)
response = self.client.get(reverse('users:user-list'))
self.assertEqual(response.context['is_paginated'], True)
class UserAddTests(TestCase):
def setUp(self):
init_all_models()
def test_add_a_new_user(self):
username = gen_username()
data = {
'username': username,
'comment': '',
'name': gen_name(),
'email': gen_email(),
'groups': [UserGroup.objects.first().id, ],
'role': get_role().id,
'date_expired': '2086-08-06 19:12:22',
}
response = self.client.post(reverse('users:user-add'), data)
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], reverse('users:user-list'))
response = self.client.get(reverse('users:user-list'))
self.assertContains(response.status_code, 200)
self.assertContains(response, username)

Loading…
Cancel
Save