2017-02-10 22:58:01 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
This file demonstrates writing tests using the unittest module. These will pass
|
|
|
|
when you run "manage.py test".
|
|
|
|
|
|
|
|
Replace this with more appropriate tests for your application.
|
|
|
|
"""
|
2017-02-11 15:07:45 +01:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2017-02-10 23:47:45 +01:00
|
|
|
from django.db.utils import IntegrityError
|
2017-02-11 15:07:45 +01:00
|
|
|
from django.test import Client, TestCase
|
2017-02-09 21:04:32 +01:00
|
|
|
|
2017-02-10 22:58:01 +01:00
|
|
|
from gestion.models import Profile, User
|
|
|
|
|
2017-02-11 00:21:24 +01:00
|
|
|
|
2017-02-10 23:47:45 +01:00
|
|
|
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.
|
|
|
|
"""
|
2017-02-12 14:18:51 +01:00
|
|
|
User.objects.create(username=username,
|
|
|
|
first_name=username.title(),
|
|
|
|
last_name=username.title())
|
2017-02-10 23:47:45 +01:00
|
|
|
p, = Profile.objects.filter(user__username=username)
|
|
|
|
return p
|
|
|
|
|
2017-02-10 22:58:01 +01:00
|
|
|
|
|
|
|
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())
|
2017-02-10 23:47:45 +01:00
|
|
|
|
|
|
|
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')
|
2017-02-12 14:18:51 +01:00
|
|
|
Account.objects.create(profile=p1, trigramme='BAR')
|
|
|
|
CofProfile.objects.create(profile=p2)
|
|
|
|
Account.objects.create(profile=p2, trigramme='BAZ')
|
2017-02-10 23:47:45 +01:00
|
|
|
self.assertRaises(IntegrityError,
|
|
|
|
CofProfile.objects.create, profile=p2)
|
2017-02-11 15:07:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestProfile(TestCase):
|
|
|
|
@patch('kfet.signals.messages')
|
|
|
|
def test_profile(self, mock_messages):
|
2017-02-12 14:18:51 +01:00
|
|
|
User.objects.create_superuser('root', 'foo@bar.com', 'root')
|
2017-02-11 15:07:45 +01:00
|
|
|
c = Client()
|
|
|
|
c.login(username='root', password='root')
|
2017-02-11 15:47:31 +01:00
|
|
|
response = c.get('/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2017-02-12 15:19:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
)
|