From 35eb6f983365156947cbf4afa7ea5cd026fa29f1 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Fri, 12 Jun 2020 20:39:09 +0200 Subject: [PATCH] Tests for backend --- authens/tests/test_backend.py | 27 +++++++++++++++++++++++++-- tests/settings.py | 15 ++++++++------- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/authens/tests/test_backend.py b/authens/tests/test_backend.py index 6c77c6d..486db62 100644 --- a/authens/tests/test_backend.py +++ b/authens/tests/test_backend.py @@ -4,7 +4,7 @@ from django.contrib.auth import authenticate, get_user_model from django.test import TestCase from authens.backends import ENSCASBackend -from authens.models import CASAccount +from authens.models import CASAccount, OldCASAccount from authens.tests.cas_utils import FakeCASClient UserModel = get_user_model() @@ -94,8 +94,31 @@ class TestCASBackend(TestCase): # Log the new 'johndoe' in. new_user = authenticate(None, ticket="dummy ticket") 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_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.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) diff --git a/tests/settings.py b/tests/settings.py index 82c5d96..1bcad09 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -16,16 +16,17 @@ INSTALLED_APPS = [ AUTHENTICATION_BACKENDS = [ "django.contrib.auth.backends.ModelBackend", "authens.backends.ENSCASBackend", + "authens.backends.OldCASBackend", ] MIDDLEWARE = [ - 'django.middleware.security.SecurityMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware', + "django.middleware.security.SecurityMiddleware", + "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", ] TEMPLATES = [