Skip to content

Commit

Permalink
Add tests for auth backend
Browse files Browse the repository at this point in the history
  • Loading branch information
frcroth committed Oct 15, 2023
1 parent 7a4d48c commit bbdb5f3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion myhpi/tests/core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
49 changes: 49 additions & 0 deletions myhpi/tests/core/test_auth.py
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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": "[email protected]",
"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, "[email protected]")
self.assertTrue(user.groups.filter(name="Student").exists())

def test_update_user(self):
claims = {
"email": "[email protected]",
"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")

0 comments on commit bbdb5f3

Please sign in to comment.