Skip to content

Commit 7e60142

Browse files
committed
Tests
2 parents 6cc9c22 + 2855743 commit 7e60142

File tree

2 files changed

+65
-15
lines changed

2 files changed

+65
-15
lines changed

djangosaml2/backends.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
logger = logging.getLogger('djangosaml2')
2828

2929

30-
def get_model(model_path):
30+
def get_model(model_path: str):
3131
from django.apps import apps
3232
try:
3333
return apps.get_model(model_path)
@@ -52,15 +52,19 @@ def get_django_user_lookup_attribute(userModel) -> str:
5252
return getattr(userModel, 'USERNAME_FIELD', 'username')
5353

5454

55-
def set_attribute(obj, attr, new_value) -> bool:
55+
def set_attribute(obj: Any, attr: str, new_value: Any) -> bool:
5656
""" Set an attribute of an object to a specific value, if it wasn't that already.
5757
Return True if the attribute was changed and False otherwise.
5858
"""
5959

60-
old_value = getattr(obj, attr)
61-
if new_value != old_value:
60+
if not hasattr(obj, attr):
6261
setattr(obj, attr, new_value)
6362
return True
63+
else:
64+
old_value = getattr(obj, attr)
65+
if new_value != old_value:
66+
setattr(obj, attr, new_value)
67+
return True
6468

6569
return False
6670

tests/testprofiles/tests.py

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,69 @@
1919
from django.contrib.auth import get_user_model
2020
from django.contrib.auth.models import User as DjangoUserModel
2121
from django.test import TestCase, override_settings
22-
from .models import TestUser
23-
from djangosaml2.backends import (Saml2Backend,
24-
get_django_user_lookup_attribute,
22+
from django.core.exceptions import ImproperlyConfigured
23+
from djangosaml2.backends import (Saml2Backend, get_model, get_saml_user_model, set_attribute,
24+
get_django_user_lookup_attribute, get_django_user_lookup_attribute,
2525
get_saml_user_model)
2626

27+
from .models import TestUser
28+
2729
User = get_user_model()
2830

29-
if sys.version_info < (3, 4):
30-
# Monkey-patch TestCase to add the assertLogs method introduced in
31-
# Python 3.4
32-
from unittest2.case import _AssertLogsContext
3331

34-
class LoggerTestCase(TestCase):
35-
def assertLogs(self, logger=None, level=None):
36-
return _AssertLogsContext(self, logger, level)
32+
class BackendUtilsTests(TestCase):
33+
def test_get_model_ok(self):
34+
user_model = get_model('testprofiles.TestUser')
35+
self.assertEqual(user_model, TestUser)
36+
37+
def test_get_model_nonexisting(self):
38+
nonexisting_model = 'testprofiles.NonExisting'
3739

38-
TestCase = LoggerTestCase
40+
with self.assertRaisesMessage(ImproperlyConfigured, f"SAML_USER_MODEL refers to model '{nonexisting_model}' that has not been installed"):
41+
get_model(nonexisting_model)
42+
43+
def test_get_model_invalid_specifier(self):
44+
nonexisting_model = 'random_package.specifier.testprofiles.NonExisting'
45+
46+
with self.assertRaisesMessage(ImproperlyConfigured, "SAML_USER_MODEL must be of the form 'app_label.model_name'"):
47+
get_model(nonexisting_model)
48+
49+
def test_get_saml_user_model_specified(self):
50+
with override_settings(AUTH_USER_MODEL='auth.User'):
51+
with override_settings(SAML_USER_MODEL='testprofiles.TestUser'):
52+
self.assertEqual(get_saml_user_model(), TestUser)
53+
54+
def test_get_saml_user_model_default(self):
55+
with override_settings(AUTH_USER_MODEL='auth.User'):
56+
self.assertEqual(get_saml_user_model(), DjangoUserModel)
57+
58+
def test_get_django_user_lookup_attribute_specified(self):
59+
with override_settings(SAML_USER_MODEL='testprofiles.TestUser'):
60+
with override_settings(SAML_DJANGO_USER_MAIN_ATTRIBUTE='age'):
61+
self.assertEqual(get_django_user_lookup_attribute(TestUser), 'age')
62+
63+
def test_get_django_user_lookup_attribute_default(self):
64+
with override_settings(SAML_USER_MODEL='testprofiles.TestUser'):
65+
self.assertEqual(get_django_user_lookup_attribute(TestUser), 'username')
66+
67+
def test_set_attribute(self):
68+
u = TestUser()
69+
self.assertFalse(hasattr(u, 'custom_attribute'))
70+
71+
# Set attribute initially
72+
changed = set_attribute(u, 'custom_attribute', 'value')
73+
self.assertTrue(changed)
74+
self.assertEqual(u.custom_attribute, 'value')
75+
76+
# 'Update' to the same value again
77+
changed_same = set_attribute(u, 'custom_attribute', 'value')
78+
self.assertFalse(changed_same)
79+
self.assertEqual(u.custom_attribute, 'value')
80+
81+
# Update to a different value
82+
changed_different = set_attribute(u, 'custom_attribute', 'new_value')
83+
self.assertTrue(changed_different)
84+
self.assertEqual(u.custom_attribute, 'new_value')
3985

4086

4187
class Saml2BackendTests(TestCase):

0 commit comments

Comments
 (0)