2017-08-16 17:45:59 +02:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.contrib.auth.models import Permission
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
from django.test import TestCase
|
|
|
|
|
2017-10-26 11:38:11 +02:00
|
|
|
from cof.models import CofProfile
|
2017-08-16 17:45:59 +02:00
|
|
|
|
|
|
|
from ..models import Account
|
|
|
|
from .testcases import TestCaseMixin
|
|
|
|
from .utils import (
|
|
|
|
create_user, create_team, create_root, get_perms, user_add_perms,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
|
|
|
|
|
|
class UserHelpersTests(TestCaseMixin, TestCase):
|
|
|
|
|
|
|
|
def test_create_user(self):
|
|
|
|
"""create_user creates a basic user and its account."""
|
|
|
|
u = create_user()
|
|
|
|
a = u.profile.account_kfet
|
|
|
|
|
|
|
|
self.assertInstanceExpected(u, {
|
|
|
|
'get_full_name': 'first last',
|
|
|
|
'username': 'user',
|
|
|
|
})
|
|
|
|
self.assertFalse(u.user_permissions.exists())
|
|
|
|
|
|
|
|
self.assertEqual('000', a.trigramme)
|
|
|
|
|
|
|
|
def test_create_team(self):
|
|
|
|
u = create_team()
|
|
|
|
a = u.profile.account_kfet
|
|
|
|
|
|
|
|
self.assertInstanceExpected(u, {
|
|
|
|
'get_full_name': 'team member',
|
|
|
|
'username': 'team',
|
|
|
|
})
|
|
|
|
self.assertTrue(u.has_perm('kfet.is_team'))
|
|
|
|
|
|
|
|
self.assertEqual('100', a.trigramme)
|
|
|
|
|
|
|
|
def test_create_root(self):
|
|
|
|
u = create_root()
|
|
|
|
a = u.profile.account_kfet
|
|
|
|
|
|
|
|
self.assertInstanceExpected(u, {
|
|
|
|
'get_full_name': 'super user',
|
|
|
|
'username': 'root',
|
|
|
|
'is_superuser': True,
|
|
|
|
'is_staff': True,
|
|
|
|
})
|
|
|
|
|
|
|
|
self.assertEqual('200', a.trigramme)
|
|
|
|
|
|
|
|
|
|
|
|
class PermHelpersTest(TestCaseMixin, TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
cts = ContentType.objects.get_for_models(Account, CofProfile)
|
|
|
|
self.perm1 = Permission.objects.create(
|
|
|
|
content_type=cts[Account],
|
|
|
|
codename='test_perm',
|
|
|
|
name='Perm for test',
|
|
|
|
)
|
|
|
|
self.perm2 = Permission.objects.create(
|
|
|
|
content_type=cts[CofProfile],
|
|
|
|
codename='another_test_perm',
|
|
|
|
name='Another one',
|
|
|
|
)
|
|
|
|
self.perm_team = Permission.objects.get(
|
|
|
|
content_type__app_label='kfet',
|
|
|
|
codename='is_team',
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_get_perms(self):
|
2017-10-26 11:38:11 +02:00
|
|
|
perms = get_perms('kfet.test_perm', 'cof.another_test_perm')
|
2017-08-16 17:45:59 +02:00
|
|
|
self.assertDictEqual(perms, {
|
|
|
|
'kfet.test_perm': self.perm1,
|
2017-10-26 11:38:11 +02:00
|
|
|
'cof.another_test_perm': self.perm2,
|
2017-08-16 17:45:59 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
def test_user_add_perms(self):
|
|
|
|
user = User.objects.create_user(username='user', password='user')
|
|
|
|
user.user_permissions.add(self.perm1)
|
|
|
|
|
2017-10-26 11:38:11 +02:00
|
|
|
user_add_perms(user, ['kfet.is_team', 'cof.another_test_perm'])
|
2017-08-16 17:45:59 +02:00
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
user.user_permissions.all(),
|
|
|
|
map(repr, [self.perm1, self.perm2, self.perm_team]),
|
|
|
|
ordered=False,
|
|
|
|
)
|