Skip to content

Commit 4763645

Browse files
committed
updates
1 parent 10fda0d commit 4763645

File tree

1 file changed

+65
-3
lines changed

1 file changed

+65
-3
lines changed

tests/testprofiles/tests.py

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,24 @@ def test_set_attribute(self):
8484
self.assertEqual(u.custom_attribute, 'new_value')
8585

8686

87-
class DefaultSaml2BackendTests(TestCase):
87+
class Saml2BackendTests(TestCase):
88+
""" UnitTests on backend classes
89+
"""
90+
backend_cls = Saml2Backend
8891

8992
def setUp(self):
90-
self.backend = Saml2Backend()
93+
self.backend = self.backend_cls()
9194
self.user = TestUser.objects.create(username='john')
92-
# self.test_user = TestUser.objects.create(username='john')
95+
96+
def test_is_authorized(self):
97+
self.assertTrue(self.backend.is_authorized({}, {}))
98+
99+
def test_clean_attributes(self):
100+
attributes = {'random': 'dummy', 'value': 123}
101+
self.assertEqual(self.backend.clean_attributes(attributes), attributes)
102+
103+
def test_clean_user_main_attribute(self):
104+
self.assertEqual(self.backend.clean_user_main_attribute('value'), 'value')
93105

94106
def test_update_user(self):
95107
attribute_mapping = {
@@ -260,6 +272,56 @@ def test_get_or_create_user_create(self):
260272
)
261273

262274

275+
class CustomizedBackend(Saml2Backend):
276+
""" Override the available methods with some customized implementation to test customization
277+
"""
278+
def is_authorized(self, attributes, attribute_mapping):
279+
''' Allow only staff users from the IDP '''
280+
return attributes.get('is_staff', (None, ))[0] == 'true'
281+
282+
def clean_attributes(self, attributes: dict):
283+
''' Keep only age attribute '''
284+
return {
285+
'age': attributes.get('age', ()),
286+
}
287+
288+
def clean_user_main_attribute(self, main_attribute):
289+
''' Replace all spaces an dashes by underscores '''
290+
return main_attribute.replace('-', '_').replace(' ', '_')
291+
292+
def get_or_create_user(self, user_lookup_key, user_lookup_value, create_unknown_user, **kwargs):
293+
return super().get_or_create_user(user_lookup_key, user_lookup_value, create_unknown_user, **kwargs)
294+
295+
296+
class CustomizedSaml2BackendTests(Saml2BackendTests):
297+
backend_cls = CustomizedBackend
298+
299+
def test_is_authorized(self):
300+
attribute_mapping = {
301+
'uid': ('username', ),
302+
'mail': ('email', ),
303+
'cn': ('first_name', ),
304+
'sn': ('last_name', ),
305+
}
306+
attributes = {
307+
'uid': ('john', ),
308+
'mail': ('[email protected]', ),
309+
'cn': ('John', ),
310+
'sn': ('Doe', ),
311+
}
312+
self.assertFalse(self.backend.is_authorized(attributes, attribute_mapping))
313+
attributes['is_staff'] = ('true', )
314+
self.assertTrue(self.backend.is_authorized(attributes, attribute_mapping))
315+
316+
def test_clean_attributes(self):
317+
attributes = {'random': 'dummy', 'value': 123, 'age': '28'}
318+
self.assertEqual(self.backend.clean_attributes(attributes), {'age': '28'})
319+
320+
def test_clean_user_main_attribute(self):
321+
self.assertEqual(self.backend.clean_user_main_attribute('va--l__ u -e'), 'va__l___u__e')
322+
323+
324+
263325
class LowerCaseSaml2Backend(Saml2Backend):
264326
def clean_attributes(self, attributes):
265327
return dict([k.lower(), v] for k, v in attributes.items())

0 commit comments

Comments
 (0)