From bbdb5f3c3cfed672b16ea6b41dcca2c5c7068e46 Mon Sep 17 00:00:00 2001 From: frcroth Date: Sun, 15 Oct 2023 13:49:13 +0200 Subject: [PATCH] Add tests for auth backend --- myhpi/tests/core/setup.py | 2 +- myhpi/tests/core/test_auth.py | 49 +++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 myhpi/tests/core/test_auth.py diff --git a/myhpi/tests/core/setup.py b/myhpi/tests/core/setup.py index eb0eb575..3cccb8ee 100644 --- a/myhpi/tests/core/setup.py +++ b/myhpi/tests/core/setup.py @@ -41,7 +41,7 @@ def create_users(): def create_groups(users): superuser, student, student_representative = users - students = Group.objects.create(name="Students") + students = Group.objects.create(name="Student") fsr = Group.objects.create(name="Student Representative Group") students.user_set.add(superuser) diff --git a/myhpi/tests/core/test_auth.py b/myhpi/tests/core/test_auth.py new file mode 100644 index 00000000..d81e7b4b --- /dev/null +++ b/myhpi/tests/core/test_auth.py @@ -0,0 +1,49 @@ +from myhpi.core.auth import MyHPIOIDCAB +from myhpi.tests.core.utils import MyHPIPageTestCase + + +class AuthTests(MyHPIPageTestCase): + def setUp(self): + super().setUp() + self.auth_backend = MyHPIOIDCAB() + + def test_create_user(self): + claims = { + "email": "ali.gator@example.org", + "given_name": "Ali", + "family_name": "Gator", + "sub": "ali.gator", + } + user = self.auth_backend.create_user(claims) + self.assertEqual(user.username, "ali.gator") + self.assertFalse(user.groups.filter(name="Student").exists()) + + matching_users = self.auth_backend.filter_users_by_claims(claims) + self.assertEqual(len(matching_users), 1) + + def test_create_student(self): + claims = { + "email": "grace.hopper@student.uni-potsdam.example.com", + "given_name": "Grace", + "family_name": "Hopper", + "sub": "grace.hopper", + } + user = self.auth_backend.create_user(claims) + self.assertEqual(user.username, "grace.hopper") + self.assertEqual(user.email, "grace.hopper@student.example.com") + self.assertTrue(user.groups.filter(name="Student").exists()) + + def test_update_user(self): + claims = { + "email": "jw.goethe@weimar.de", + "given_name": "Johann Wolfgang", + "family_name": "Goethe", + "sub": "jw.goethe", + } + user = self.auth_backend.create_user(claims) + self.assertEqual(user.username, "jw.goethe") + self.assertEqual(user.last_name, "Goethe") + claims["family_name"] = "von Goethe" + user = self.auth_backend.update_user(user, claims) + self.assertEqual(user.first_name, "Johann Wolfgang") + self.assertEqual(user.last_name, "von Goethe")