def test_extract_user_id_configure_by_user_class(self): user = User() user.USERNAME_FIELD = 'email' user.email = 'test_email' assert BaseProcessor('entity-id').get_user_id(user) == 'test_email'
def test_identity_dict_creation(self): def random_method(self): return "test method result" User.random_method = random_method user = User() user.name = 'Test Name' user.email = '*****@*****.**' user.other_setting = 'Test Setting' user.setting_not_passed = 'Test Setting Not Passed' service_provider = ServiceProvider(entity_id='entity-id', _attribute_mapping=json.dumps({ 'name': 'fullName', 'email': 'emailAddress', 'other_setting': 'otherSetting', 'random_method': 'randomMethodTest' })) _ = IDP.load().config expected_result = { 'fullName': 'Test Name', 'emailAddress': '*****@*****.**', 'otherSetting': 'Test Setting', 'randomMethodTest': 'test method result' } assert BaseProcessor('entity_id').create_identity(user, service_provider.attribute_mapping) == expected_result
def test_identity_dict_creation(self): def random_method(self): return "test method result" User.random_method = random_method user = User() user.name = 'Test Name' user.email = '*****@*****.**' user.other_setting = 'Test Setting' user.setting_not_passed = 'Test Setting Not Passed' sp_config = { 'attribute_mapping': { 'name': 'fullName', 'email': 'emailAddress', 'other_setting': 'otherSetting', 'random_method': 'randomMethodTest' } } expected_result = { 'fullName': 'Test Name', 'emailAddress': '*****@*****.**', 'otherSetting': 'Test Setting', 'randomMethodTest': 'test method result' } assert BaseProcessor('entity_id').create_identity( user, sp_config) == expected_result
def test_extract_user_id_default_to_username(self): user = User() user.username = '******' service_provider = ServiceProvider(entity_id='entity-id') idp = IDP.load().config assert BaseProcessor('entity-id').get_user_id(user, NAMEID_FORMAT_UNSPECIFIED, service_provider, idp) == 'test_username'
def test_extract_user_id_from_sp_config(self): user = User() user.special_id = 'test_special_id' sp_config = {'nameid_field': 'special_id'} assert BaseProcessor('entity_id').get_user_id( user, sp_config) == 'test_special_id'
def test_extract_user_id_configure_by_settings(self, settings): """Should use `settings.SAML_IDP_DJANGO_USERNAME_FIELD` to determine the user id field""" settings.SAML_IDP_DJANGO_USERNAME_FIELD = 'first_name' user = User() user.first_name = 'test_first_name' assert BaseProcessor('entity-id').get_user_id(user) == 'test_first_name'
def test_extract_user_id_configure_on_service_provider(self): user = User() user.USERNAME_FIELD = 'email' user.email = 'test_email' service_provider = ServiceProvider(entity_id='entity-id', _nameid_field='email') idp = IDP.load().config assert BaseProcessor('entity-id').get_user_id(user, NAMEID_FORMAT_UNSPECIFIED, service_provider, idp) == 'test_email'
def test_extract_user_id_configure_by_settings(self, settings): """Should use `settings.SAML_IDP_DJANGO_USERNAME_FIELD` to determine the user id field""" settings.SAML_IDP_DJANGO_USERNAME_FIELD = 'first_name' user = User(first_name='test_first_name') service_provider = ServiceProvider(entity_id='entity-id') idp = IDP.load().config assert BaseProcessor('entity-id').get_user_id(user, NAMEID_FORMAT_UNSPECIFIED, service_provider, idp) == 'test_first_name'
def test_extract_user_id_from_sp_config_if_method(self): def random_method(self): return "test method result" User.random_method = random_method user = User() sp_config = {'nameid_field': 'random_method'} assert BaseProcessor('entity_id').get_user_id( user, sp_config) == 'test method result'
def test_extract_user_id_from_sp_config_if_method(self): def random_method(self): return "test method result" User.random_method = random_method user = User() service_provider = ServiceProvider(entity_id='entity-id', _nameid_field='random_method') idp = IDP.load().config assert BaseProcessor('entity-id').get_user_id(user, NAMEID_FORMAT_UNSPECIFIED, service_provider, idp) == 'test method result'
def get_processor(self, entity_id, sp_config): # pylint: disable=no-self-use """ Instantiate user-specified processor or default to an all-access base processor. Raises an exception if the configured processor class can not be found or initialized. """ processor_string = sp_config.get('processor', None) if processor_string: try: return import_string(processor_string)(entity_id) except Exception as e: # pylint: disable=invalid-name logger.error("Failed to instantiate processor: {} - {}".format( processor_string, e), exc_info=True) # pylint: disable=logging-format-interpolation raise return BaseProcessor(entity_id)
def test_enable_multifactor_returns_false_by_default(self): user = User() assert BaseProcessor('entity-id').enable_multifactor(user) is False
def test_has_access_returns_true_by_default(self): request = HttpRequest() request.method = "GET" assert BaseProcessor('entity-id').has_access(request) is True