forked from DGNum/gestioCOF
- Permissions of 'gestion' app are correctly added to the staff groups of associations. - Add tests to ensure staff groups of COF and BDS are correctly setup. - Shortcut functions are added to retrieve COF and BDS association.
183 lines
6.3 KiB
Python
183 lines
6.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
from unittest.mock import patch
|
|
|
|
from django.contrib.auth import get_user_model
|
|
from django.contrib.auth.models import Permission
|
|
from django.db.utils import IntegrityError
|
|
from django.test import Client, TestCase
|
|
|
|
from bds.models import get_bds_assoc
|
|
from cof.models import get_cof_assoc
|
|
|
|
from .models import Profile
|
|
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
def create_profile(username):
|
|
"""
|
|
In order to create a profile, I need to create a User first.
|
|
This is annoying in most situations, as I just want a profile.
|
|
"""
|
|
User.objects.create(username=username,
|
|
first_name=username.title(),
|
|
last_name=username.title())
|
|
p, = Profile.objects.filter(user__username=username)
|
|
return p
|
|
|
|
|
|
class SimpleTest(TestCase):
|
|
def test_delete_user(self):
|
|
u = User(username='foo', first_name='foo', last_name='bar')
|
|
|
|
# to each user there's a cofprofile associated
|
|
u.save()
|
|
self.assertTrue(Profile.objects.filter(user__username='foo').exists())
|
|
|
|
# there's no point in having a cofprofile without a user associated.
|
|
u.delete()
|
|
self.assertFalse(Profile.objects.filter(user__username='foo').exists())
|
|
|
|
# there's no point in having a user without a cofprofile associated.
|
|
u.save()
|
|
Profile.objects.get(user__username='foo').delete()
|
|
self.assertFalse(User.objects.filter(username='foo').exists())
|
|
|
|
def test_create_profiles(self):
|
|
from kfet.models import Account
|
|
from cof.models import CofProfile
|
|
|
|
# test that two accounts can be linked to the same profile.
|
|
p = create_profile('foo')
|
|
pkfet = Account.objects.create(profile=p, trigramme='PIG')
|
|
pcof = CofProfile.objects.create(profile=p)
|
|
self.assertEqual(pkfet.profile, pcof.profile)
|
|
|
|
# each profile is linked to at most one account of the same type.
|
|
p1 = create_profile('bar')
|
|
p2 = create_profile('baz')
|
|
Account.objects.create(profile=p1, trigramme='BAR')
|
|
CofProfile.objects.create(profile=p2)
|
|
Account.objects.create(profile=p2, trigramme='BAZ')
|
|
self.assertRaises(IntegrityError,
|
|
CofProfile.objects.create, profile=p2)
|
|
|
|
|
|
class TestProfile(TestCase):
|
|
@patch('kfet.signals.messages')
|
|
def test_profile(self, mock_messages):
|
|
# Test that the root user can log in
|
|
User.objects.create_superuser('root', 'foo@bar.com', 'root')
|
|
c = Client()
|
|
c.login(username='root', password='root')
|
|
response = c.get('/')
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
# Test the profile edition
|
|
post_data = {
|
|
"first_name": "foo",
|
|
"last_name": "bar",
|
|
"phone": "0123456789",
|
|
"departement": "baz"
|
|
}
|
|
c.post("/profile", post_data)
|
|
user = User.objects.get(username="root")
|
|
self.assertEqual(user.first_name, post_data['first_name'])
|
|
self.assertEqual(user.last_name, post_data["last_name"])
|
|
self.assertEqual(user.profile.phone, post_data["phone"])
|
|
self.assertEqual(user.profile.departement, post_data["departement"])
|
|
|
|
|
|
class AuthTest(TestCase):
|
|
def test_login(self):
|
|
client = Client()
|
|
# Setup a regular Django user
|
|
user = User.objects.create(username="foo")
|
|
user.set_password("bar")
|
|
user.save()
|
|
# Test the django auth
|
|
resp = client.post(
|
|
"/outsider/login",
|
|
{"username": "foo", "password": "bar"},
|
|
follow=True
|
|
)
|
|
self.assertEqual(resp.status_code, 200)
|
|
resp = client.get("/logout")
|
|
self.assertEqual(resp.status_code, 200)
|
|
# Give the user a clipper login
|
|
user.profile.login_clipper = "foo"
|
|
user.profile.save()
|
|
# Assert that it cannot use the regular auth
|
|
resp = client.post(
|
|
"/outsider/login",
|
|
{"username": "foo", "password": "bar"}
|
|
)
|
|
self.assertIn("error_type", resp.context)
|
|
self.assertEqual(resp.context["error_type"], "use_clipper_login")
|
|
# Test the CAS redirect
|
|
resp = client.get("/cas/login")
|
|
self.assertEqual(resp.status_code, 302)
|
|
self.assertEqual(
|
|
resp.url.split('?')[0],
|
|
"https://cas.eleves.ens.fr/login"
|
|
)
|
|
|
|
|
|
class AssociationTests(TestCase):
|
|
|
|
def setUp(self):
|
|
self.gestion_p = Permission.objects.filter(
|
|
content_type__app_label='gestion',
|
|
)
|
|
|
|
def assertAllAppPerms(self, group, app_label):
|
|
group_p = group.permissions.all()
|
|
app_p = Permission.objects.filter(
|
|
content_type__app_label=app_label,
|
|
)
|
|
for perm in app_p:
|
|
self.assertIn(perm, group_p)
|
|
|
|
def test_cof_assoc(self):
|
|
cof_assoc = get_cof_assoc()
|
|
|
|
# Check cof buro group has 'cof.buro' permission.
|
|
buro_perm = Permission.objects.get(
|
|
content_type__app_label='cof',
|
|
codename='buro',
|
|
)
|
|
self.assertIn(buro_perm, cof_assoc.staff_group.permissions.all())
|
|
|
|
# Check cof buro group has all permissions of 'gestion' and 'cof' apps.
|
|
self.assertAllAppPerms(cof_assoc.staff_group, 'cof')
|
|
self.assertAllAppPerms(cof_assoc.staff_group, 'gestion')
|
|
|
|
# Check cof members group has 'cof.member' permission.
|
|
member_perm = Permission.objects.get(
|
|
content_type__app_label='cof',
|
|
codename='member',
|
|
)
|
|
self.assertIn(member_perm, cof_assoc.members_group.permissions.all())
|
|
|
|
def test_bds_assoc(self):
|
|
bds_assoc = get_bds_assoc()
|
|
|
|
# Check cof buro group has 'bds.buro' permission.
|
|
buro_perm = Permission.objects.get(
|
|
content_type__app_label='bds',
|
|
codename='buro',
|
|
)
|
|
self.assertIn(buro_perm, bds_assoc.staff_group.permissions.all())
|
|
|
|
# Check bds buro group has all permissions of 'gestion' and 'bds' apps.
|
|
self.assertAllAppPerms(bds_assoc.staff_group, 'bds')
|
|
self.assertAllAppPerms(bds_assoc.staff_group, 'gestion')
|
|
|
|
# Check cof members group has 'bds.member' permission.
|
|
member_perm = Permission.objects.get(
|
|
content_type__app_label='bds',
|
|
codename='member',
|
|
)
|
|
self.assertIn(member_perm, bds_assoc.members_group.permissions.all())
|