forked from DGNum/gestioCOF
88 lines
2.7 KiB
Python
88 lines
2.7 KiB
Python
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
|
|
|
|
from gestioncof.models import CofProfile
|
|
|
|
from ..models import Account
|
|
from .testcases import TestCaseMixin
|
|
from .utils import create_root, create_team, create_user, 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):
|
|
perms = get_perms("kfet.test_perm", "gestioncof.another_test_perm")
|
|
self.assertDictEqual(
|
|
perms,
|
|
{"kfet.test_perm": self.perm1, "gestioncof.another_test_perm": self.perm2},
|
|
)
|
|
|
|
def test_user_add_perms(self):
|
|
user = User.objects.create_user(username="user", password="user")
|
|
user.user_permissions.add(self.perm1)
|
|
|
|
user_add_perms(user, ["kfet.is_team", "gestioncof.another_test_perm"])
|
|
|
|
self.assertQuerysetEqual(
|
|
user.user_permissions.all(),
|
|
map(repr, [self.perm1, self.perm2, self.perm_team]),
|
|
ordered=False,
|
|
)
|