Skip to content

Commit d01a9d4

Browse files
committed
test updates
1 parent 04388f8 commit d01a9d4

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

djangosaml2/backends.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def get_model(model_path: str):
3838

3939

4040
def get_saml_user_model():
41-
''' Returns the user model specified in the settings, or the default one from this Django installation '''
41+
""" Returns the user model specified in the settings, or the default one from this Django installation """
4242
if hasattr(settings, 'SAML_USER_MODEL'):
4343
return get_model(settings.SAML_USER_MODEL)
4444
return auth.get_user_model()
@@ -75,7 +75,7 @@ def is_authorized(self, attributes, attribute_mapping) -> bool:
7575
return True
7676

7777
def clean_attributes(self, attributes: dict) -> dict:
78-
"""Hook to clean attributes from the SAML response. """
78+
""" Hook to clean attributes from the SAML response. """
7979
return attributes
8080

8181
def clean_user_main_attribute(self, main_attribute):

tests/testprofiles/tests.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,14 @@ class CustomizedBackend(Saml2Backend):
301301
"""
302302
def is_authorized(self, attributes, attribute_mapping):
303303
''' Allow only staff users from the IDP '''
304-
return attributes.get('is_staff', (None, ))[0] == 'true'
304+
return attributes.get('is_staff', (None, ))[0] == True
305305

306-
def clean_attributes(self, attributes: dict):
306+
def clean_attributes(self, attributes: dict) -> dict:
307307
''' Keep only age attribute '''
308308
return {
309-
'age': attributes.get('age', ()),
309+
'age': attributes.get('age', (None, )),
310+
'is_staff': attributes.get('is_staff', (None, )),
311+
'uid': attributes.get('uid', (None, )),
310312
}
311313

312314
def clean_user_main_attribute(self, main_attribute):
@@ -334,16 +336,48 @@ def test_is_authorized(self):
334336
'sn': ('Doe', ),
335337
}
336338
self.assertFalse(self.backend.is_authorized(attributes, attribute_mapping))
337-
attributes['is_staff'] = ('true', )
339+
attributes['is_staff'] = (True, )
338340
self.assertTrue(self.backend.is_authorized(attributes, attribute_mapping))
339341

340342
def test_clean_attributes(self):
341343
attributes = {'random': 'dummy', 'value': 123, 'age': '28'}
342-
self.assertEqual(self.backend.clean_attributes(attributes), {'age': '28'})
344+
self.assertEqual(self.backend.clean_attributes(attributes), {'age': '28', 'is_staff': (None,), 'uid': (None,)})
343345

344346
def test_clean_user_main_attribute(self):
345347
self.assertEqual(self.backend.clean_user_main_attribute('va--l__ u -e'), 'va__l___u__e')
346348

349+
def test_authenticate(self):
350+
attribute_mapping = {
351+
'uid': ('username', ),
352+
'mail': ('email', ),
353+
'cn': ('first_name', ),
354+
'sn': ('last_name', ),
355+
'age': ('age', ),
356+
'is_staff': ('is_staff', ),
357+
}
358+
attributes = {
359+
'uid': ('john', ),
360+
'mail': ('[email protected]', ),
361+
'cn': ('John', ),
362+
'sn': ('Doe', ),
363+
'age': ('28', ),
364+
'is_staff': (True, ),
365+
}
366+
367+
self.assertEqual(self.user.age, '')
368+
self.assertEqual(self.user.is_staff, False)
369+
370+
user = self.backend.authenticate(
371+
None,
372+
session_info={'ava': attributes},
373+
attribute_mapping=attribute_mapping,
374+
)
375+
376+
self.assertEqual(user, self.user)
377+
378+
self.user.refresh_from_db()
379+
self.assertEqual(self.user.age, '28')
380+
self.assertEqual(self.user.is_staff, True)
347381

348382

349383
class LowerCaseSaml2Backend(Saml2Backend):

0 commit comments

Comments
 (0)