예제 #1
0
def test_adobe_users_config(tmp_config_files, modify_root_config, cli_args):
    (root_config_file, _, _) = tmp_config_files
    args = cli_args({'config_filename': root_config_file})

    # test default
    config_loader = ConfigLoader(args)
    options = config_loader.load_invocation_options()
    assert 'adobe_users' in options
    assert options['adobe_users'] == ['all']

    # test default invocation
    modify_root_config(['invocation_defaults', 'adobe_users'], "mapped")
    config_loader = ConfigLoader(args)
    options = config_loader.load_invocation_options()
    assert 'adobe_users' in options
    assert options['adobe_users'] == ['mapped']

    # test command line param
    modify_root_config(['invocation_defaults', 'adobe_users'], "all")
    args = cli_args({
        'config_filename': root_config_file,
        'adobe_users': ['mapped']
    })
    config_loader = ConfigLoader(args)
    options = config_loader.load_invocation_options()
    assert 'adobe_users' in options
    assert options['adobe_users'] == ['mapped']
예제 #2
0
def load_ldap_config_options(args):
    from user_sync.connector.directory import DirectoryConnector
    from user_sync.connector.directory_ldap import LDAPDirectoryConnector

    config_loader = ConfigLoader(args)
    dc_mod_name = config_loader.get_directory_connector_module_name()
    dc_mod = __import__(dc_mod_name, fromlist=[''])
    dc = DirectoryConnector(dc_mod)
    dc_config_options = config_loader.get_directory_connector_options(dc.name)
    caller_config = DictConfig('%s configuration' % dc.name, dc_config_options)
    return LDAPDirectoryConnector.get_options(caller_config)
예제 #3
0
def test_directory_users_config(tmp_config_files, modify_root_config,
                                cli_args):

    # test that if connectors is not present or misspelled, an assertion exception is thrown
    directory_users = {'not_connectors': {'ldap': 'connector-ldap.yml'}}
    root_config_file = modify_root_config(['directory_users'], directory_users)
    args = cli_args({'config_filename': root_config_file})
    config_loader = ConfigLoader(args)
    connector_name = 'ldap'
    with pytest.raises(AssertionException):
        config_loader.get_directory_connector_options(connector_name)
예제 #4
0
def test_max_adobe_percentage(modify_root_config, caplog):
    root_config_file = modify_root_config(['limits', 'max_adobe_only_users'], "50%")
    config = ConfigFileLoader.load_root_config(root_config_file)
    assert ('limits' in config and 'max_adobe_only_users' in config['limits'] and
            config['limits']['max_adobe_only_users'] == "50%")

    args = app.process_args(['-c', root_config_file])
    options = ConfigLoader(args).get_rule_options()
    assert 'max_adobe_only_users' in options and options['max_adobe_only_users'] == '50%'

    modify_root_config(['limits', 'max_adobe_only_users'], "error%")
    with pytest.raises(AssertionException):
        ConfigLoader(args).get_rule_options()
예제 #5
0
def test_extension_load(tmp_config_files, modify_root_config, cli_args,
                        tmp_extension_config, monkeypatch):
    """Test that extension config is loaded when config option is specified"""
    with monkeypatch.context() as m:
        m.setattr(flags, 'get_flag', lambda *a: True)
        (root_config_file, _, _) = tmp_config_files

        args = cli_args({'config_filename': root_config_file})
        options = ConfigLoader(args).get_rule_options()
        assert 'after_mapping_hook' in options and options[
            'after_mapping_hook'] is None

        modify_root_config(['directory_users', 'extension'],
                           tmp_extension_config)
        options = ConfigLoader(args).get_rule_options()
        assert 'after_mapping_hook' in options and options[
            'after_mapping_hook'] is not None
예제 #6
0
def test_extension_flag(tmp_config_files, modify_root_config, cli_args,
                        tmp_extension_config, monkeypatch):
    """Test that extension flag will prevent after-map hook from running"""
    with monkeypatch.context() as m:
        m.setattr(flags, 'get_flag', lambda *a: False)

        (root_config_file, _, _) = tmp_config_files

        args = cli_args({'config_filename': root_config_file})
        modify_root_config(['directory_users', 'extension'],
                           tmp_extension_config)
        options = ConfigLoader(args).get_rule_options()
        assert 'after_mapping_hook' in options and options[
            'after_mapping_hook'] is None
예제 #7
0
def test_additional_groups_config(modify_root_config, caplog):
    addl_groups = [
        {"source": r"ACL-(.+)", "target": r"ACL-Grp-(\1)"},
        {"source": r"(.+)-ACL", "target": r"ACL-Grp-(\1)"},
    ]
    root_config_file = modify_root_config(['directory_users', 'additional_groups'], addl_groups)
    config = ConfigFileLoader.load_root_config(root_config_file)
    assert ('additional_groups' in config['directory_users'] and
            len(config['directory_users']['additional_groups']) == 2)

    args = app.process_args(['-c', root_config_file])
    options = ConfigLoader(args).get_rule_options()
    assert addl_groups[0]['source'] in options['additional_groups'][0]['source'].pattern
    assert addl_groups[1]['source'] in options['additional_groups'][1]['source'].pattern
예제 #8
0
def test_shell_exec_flag(tmp_config_files, modify_root_config, cli_args,
                         monkeypatch):
    """Test that shell exec flag will raise an error if command is specified to get connector config"""
    from user_sync.connector.directory import DirectoryConnector

    with monkeypatch.context() as m:
        m.setattr(flags, 'get_flag', lambda *a: False)
        (root_config_file, _, _) = tmp_config_files

        args = cli_args({'config_filename': root_config_file})
        modify_root_config(['directory_users', 'connectors', 'ldap'],
                           "$(some command)")
        config_loader = ConfigLoader(args)

        directory_connector_module_name = config_loader.get_directory_connector_module_name(
        )
        if directory_connector_module_name is not None:
            directory_connector_module = __import__(
                directory_connector_module_name, fromlist=[''])
            directory_connector = DirectoryConnector(
                directory_connector_module)
            with pytest.raises(AssertionException):
                config_loader.get_directory_connector_options(
                    directory_connector.name)
예제 #9
0
def test_adobe_users_config(tmp_config_files, modify_root_config):
    (root_config_file, _, _) = tmp_config_files
    args = app.process_args(['-c', root_config_file])

    # test default
    config_loader = ConfigLoader(args)
    options = config_loader.load_invocation_options()
    assert 'adobe_users' in options
    assert options['adobe_users'] == ['all']

    # test default invocation
    modify_root_config(['invocation_defaults', 'adobe_users'], "mapped")
    config_loader = ConfigLoader(args)
    options = config_loader.load_invocation_options()
    assert 'adobe_users' in options
    assert options['adobe_users'] == ['mapped']

    # test command line param
    modify_root_config(['invocation_defaults', 'adobe_users'], "all")
    args = app.process_args(['-c', root_config_file, '--adobe-users', 'mapped'])
    config_loader = ConfigLoader(args)
    options = config_loader.load_invocation_options()
    assert 'adobe_users' in options
    assert options['adobe_users'] == ['mapped']
예제 #10
0
 def setUp(self, mock_yaml, mock_isfile):
     mock_isfile.return_value = True
     self.conf_load = ConfigLoader({'options': 'testOpt'})
예제 #11
0
class ConfigLoaderTest(unittest.TestCase):
    @mock.patch('os.path.isfile')
    @mock.patch('user_sync.config.ConfigLoader.load_from_yaml')
    def setUp(self, mock_yaml, mock_isfile):
        mock_isfile.return_value = True
        self.conf_load = ConfigLoader({'options': 'testOpt'})

    @mock.patch('user_sync.config.DictConfig.get_value')
    @mock.patch('user_sync.config.ConfigLoader.get_dict_from_sources')
    @mock.patch('user_sync.config.ConfigLoader.create_dashboard_options')
    def test_get_dashboard_options_creates_dashboard(self, mock_create_dash, mock_dict, mock_value):
        ret_value = 'create dash'
        mock_create_dash.return_value = ret_value
        self.assertEquals(self.conf_load.get_dashboard_options_for_owning(), ret_value,
                          'Returned with created dashboard')
        self.assertEquals(mock_create_dash.call_count, 1, 'Get dashboard options calls create dashboard options')

    @mock.patch('user_sync.config.ConfigLoader.get_directory_connector_configs')
    @mock.patch('user_sync.config.ConfigLoader.get_dict_from_sources')
    def test_get_directory_connector_options(self, mock_dict, mock_connector_conf):
        self.conf_load.get_directory_connector_options('dummy_connector')
        self.assertEquals(mock_dict.call_count, 3, 'connector options, source filters and credentials are loaded')

    @mock.patch('user_sync.config.DictConfig.get_dict_config')
    @mock.patch('user_sync.config.ConfigLoader.create_dashboard_options')
    @mock.patch('glob.glob1')
    @mock.patch('user_sync.config.ConfigLoader.parse_string')
    def test_get_dashboard_options_for_trustees(self, mock_parse, mock_glob, mock_create_dash, mock_get_dict):
        mock_create_dash.return_value = {'create_dash'}
        mock_glob.return_value = {''}
        mock_parse.return_value = {'organization_name': 'testOrgName'}

        self.assertEquals(self.conf_load.get_dashboard_options_for_trustees(), {'testOrgName': set(['create_dash'])},
                          'We return with trustee option in the expected format')
        self.assertEquals(mock_create_dash.call_count, 1, 'create dashboard options was called')

    def test_get_dict_from_sources_dict(self):
        self.assertEquals(self.conf_load.get_dict_from_sources([{'test1': 'test2'}, {'test1': 'test3'}], ''),
                          {'test1': 'test3'}, 'the two dictionaries are combined')

    @mock.patch('os.path.isfile')
    def test_get_dict_from_sources_str_not_found(self, mock_isfile):
        # AssertionException when file is not found
        mock_isfile.return_value = False
        self.assertRaises(AssertionException, lambda: self.conf_load.get_dict_from_sources(['test'], ''))

    @mock.patch('os.path.isfile')
    def test_get_dict_from_sources_str_found(self, mock_isfile):
        # IOError when file is found, but not loaded by load_from_yaml
        mock_isfile.return_value = True
        self.assertRaises(IOError, lambda: self.conf_load.get_dict_from_sources(['test'], ''))

    @mock.patch('user_sync.config.ConfigLoader.get_dict_from_sources')
    def test_create_dashboard_options(self, mock_dict):
        mock_dict.side_effect = [{'enterprise': {'org_id': 'test1'}}, 'test2']
        self.assertEquals(self.conf_load.create_dashboard_options('', ''), {'enterprise': 'test2'},
                          'enterprise section is processed')

    @mock.patch('user_sync.config.DictConfig.get_string')
    @mock.patch('user_sync.config.DictConfig.get_dict_config')
    @mock.patch('user_sync.identity_type.parse_identity_type')
    def test_get_rule_options(self, mock_id_type,mock_get_dict,mock_get_string):
        mock_id_type.return_value = 'new_acc'
        mock_get_dict.return_value = tests.helper.MockGetString()
        self.assertEquals(self.conf_load.get_rule_options(), {'username_filter_regex': None,
                                                              'update_user_info': True,
                                                              'manage_groups': True,
                                                              'new_account_type': 'new_acc',
                                                              'directory_group_filter': None,
                                                              'default_country_code': 'test',
                                                              'remove_user_key_list': None,
                                                              'remove_list_output_path': None,
                                                              'remove_nonexistent_users': False},
                          'rule options are returned')

    def test_parse_string(self):
        self.assertEquals(self.conf_load.parse_string('{1}{2}{3}', 'abcde'),
                          {'1': 'abc', '3': 'e', '2': 'd'}, 'test parsing 1')
        self.assertEquals(self.conf_load.parse_string('{1}', 'abcde'),{'1': 'abcde'}, 'test parsing 2')

    @mock.patch('user_sync.config.ConfigLoader.get_directory_connector_configs')
    def test_check_unused_config_keys_unused(self,mock_connector_conf):
        self.conf_load.options = {'directory_source_filters': {'filter':'test1'}}
        self.conf_load.directory_source_filters_accessed = set({'another_filter':'test2'})
        # Assertion Exception is raised for unused keys
        self.assertRaises(AssertionException,lambda : self.conf_load.check_unused_config_keys())

    @mock.patch('user_sync.config.ConfigLoader.get_directory_connector_configs')
    def test_check_unused_config_keys_used(self,mock_connector_conf):
        self.conf_load.options = {'directory_source_filters': {'filter':'test1'}}
        self.conf_load.directory_source_filters_accessed = set({'filter':'test2'})

        self.assertEquals(self.conf_load.check_unused_config_keys(),None,'no unused keys')