Tests for backend

This commit is contained in:
Ludovic Stephan 2020-06-12 20:39:09 +02:00
parent 6a84639afe
commit 35eb6f9833
2 changed files with 33 additions and 9 deletions

View file

@ -4,7 +4,7 @@ from django.contrib.auth import authenticate, get_user_model
from django.test import TestCase from django.test import TestCase
from authens.backends import ENSCASBackend from authens.backends import ENSCASBackend
from authens.models import CASAccount from authens.models import CASAccount, OldCASAccount
from authens.tests.cas_utils import FakeCASClient from authens.tests.cas_utils import FakeCASClient
UserModel = get_user_model() UserModel = get_user_model()
@ -94,8 +94,31 @@ class TestCASBackend(TestCase):
# Log the new 'johndoe' in. # Log the new 'johndoe' in.
new_user = authenticate(None, ticket="dummy ticket") new_user = authenticate(None, ticket="dummy ticket")
new_clipper = new_user.cas_account new_clipper = new_user.cas_account
# Check that it gets a fresh user and a fresh clipper account.
# Check that it gets a fresh user and a fresh clipper account
self.assertNotEqual(old_user, new_user) self.assertNotEqual(old_user, new_user)
self.assertNotEqual(old_clipper, new_clipper) self.assertNotEqual(old_clipper, new_clipper)
# Check that the created CAS account matches the old one
self.assertEqual(new_clipper.cas_login, fake_cas_client.cas_login) self.assertEqual(new_clipper.cas_login, fake_cas_client.cas_login)
self.assertEqual(new_clipper.entrance_year, fake_cas_client.entrance_year) self.assertEqual(new_clipper.entrance_year, fake_cas_client.entrance_year)
# Check deprecation of the old CAS account
old_user.refresh_from_db()
self.assertFalse(hasattr(old_user, "cas_account"))
self.assertTrue(hasattr(old_user, "old_cas_account"))
old_cas = old_user.old_cas_account
self.assertEqual(old_cas.cas_login, old_clipper.cas_login)
self.assertEqual(old_cas.entrance_year, old_clipper.entrance_year)
class TestOldCASBackend(TestCase):
def test_simple_auth(self):
user = UserModel.objects.create_user(username="johndoe31", password="password")
wrong_user = UserModel.objects.create_user("johndoe", "password")
OldCASAccount.objects.create(user=user, cas_login="johndoe", entrance_year=2019)
auth_user = authenticate(
None, cas_login="johndoe", entrance_year=2019, password="password"
)
self.assertEqual(auth_user, user)

View file

@ -16,16 +16,17 @@ INSTALLED_APPS = [
AUTHENTICATION_BACKENDS = [ AUTHENTICATION_BACKENDS = [
"django.contrib.auth.backends.ModelBackend", "django.contrib.auth.backends.ModelBackend",
"authens.backends.ENSCASBackend", "authens.backends.ENSCASBackend",
"authens.backends.OldCASBackend",
] ]
MIDDLEWARE = [ MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware', "django.middleware.security.SecurityMiddleware",
'django.contrib.sessions.middleware.SessionMiddleware', "django.contrib.sessions.middleware.SessionMiddleware",
'django.middleware.common.CommonMiddleware', "django.middleware.common.CommonMiddleware",
'django.middleware.csrf.CsrfViewMiddleware', "django.middleware.csrf.CsrfViewMiddleware",
'django.contrib.auth.middleware.AuthenticationMiddleware', "django.contrib.auth.middleware.AuthenticationMiddleware",
'django.contrib.messages.middleware.MessageMiddleware', "django.contrib.messages.middleware.MessageMiddleware",
'django.middleware.clickjacking.XFrameOptionsMiddleware', "django.middleware.clickjacking.XFrameOptionsMiddleware",
] ]
TEMPLATES = [ TEMPLATES = [