def test_empty_bindings(self): """Test empty bindings.""" empty_bindings = {} settings = Settings() settings.use_json_bindings(empty_bindings) # fields should contain default self.assertIsNone(settings.field_bindings)
def test_save_two_records(self): """Test two correct records.""" temp_file, temp_path = tempfile.mkstemp() settings = Settings() bindings = { '1': [1, 'sAMAccountName'], '2': [4, 'mail'], } settings.use_json_bindings(bindings) entries = ({ 'attributes': { 'mail': '[email protected]', 'sAMAccountName': 'admin' } }, { 'attributes': { 'mail': '[email protected]', 'key_error': 'adminok' } }, { 'attributes': { 'mail': '[email protected]', 'sAMAccountName': 'adminka' } }) total = get_ldap_users.save_records_to_csv(entries, settings.field_bindings, temp_path) self.assertEqual(total, 3) with open(temp_path, encoding='utf-8') as output_file: data = output_file.read() os.close(temp_file) os.remove(temp_path) self.assertEqual(data, 'admin;[email protected]\nadminka;[email protected]\n')
def test_invalid_input(self): """Test parser with incorrect settings.""" wrong_bindings = { 'login': ['sAMAccountName'], 'description': [4, 'department'], 'email': [1, 'mail'], 'phone': [2, 'extensionAttribute7'], 'aaa': [] } settings = Settings() settings.use_json_bindings(wrong_bindings) self.assertIsNone(settings.field_bindings)
def test_missing_elements(self): """Test bindings with missed elements.""" missing_elements = { 'login': [0, 'sAMAccountName'], 'description': [10, 'department'], 'email': [20, 'mail'], 'phone': [1, 'extensionAttribute7'] } settings = Settings() settings.use_json_bindings(missing_elements) self.assertEqual(len(settings.field_bindings), 4) self.assertEqual(list(settings.field_bindings.items())[0][0], 'login') self.assertEqual(list(settings.field_bindings.items())[1][0], 'phone') self.assertEqual( list(settings.field_bindings.items())[2][0], 'description') self.assertEqual(list(settings.field_bindings.items())[3][0], 'email')
def test_order(self): """Test parser with correct settings.""" correct_bindings = { 'login': [3, 'sAMAccountName'], 'description': [4, 'department'], 'email': [1, 'mail'], 'phone': [2, 'extensionAttribute7'] } settings = Settings() settings.use_json_bindings(correct_bindings) self.assertEqual(len(settings.field_bindings), 4) self.assertEqual(list(settings.field_bindings.items())[0][0], 'email') self.assertEqual(list(settings.field_bindings.items())[1][0], 'phone') self.assertEqual(list(settings.field_bindings.items())[2][0], 'login') self.assertEqual( list(settings.field_bindings.items())[3][0], 'description')
def parse_settings_file(parsed_args): """ Parse JSON file with settings """ logger.debug("Passed JSON file with settings: %s", parsed_args.settings_file.name) settings = Settings() try: json_settings = json.load(parsed_args.settings_file) except ValueError as exp: logger.error("Failed to parse %s: %s", parsed_args.settings_file.name, exp) return None try: settings.from_json(json_settings) except KeyError as exp: logger.warning("Can't find %s in %s.", exp, parsed_args.settings_file.name) return None if parsed_args.domain_user is not None: settings.ldap_username = parsed_args.domain_user logger.debug("Using ldap_username from command line parameters.") if parsed_args.domain_password is not None: settings.ldap_password = parsed_args.domain_password logger.debug("Using ldap_password from command line parameters.") return settings
def test_save_special_symbols(self): """Test emoji characters.""" temp_file, temp_path = tempfile.mkstemp() settings = Settings() bindings = { '1': [1, 'sAMAccountName'], '2': [4, 'mail'], } settings.use_json_bindings(bindings) entries = ({ 'attributes': { 'mail': '[email protected]', 'sAMAccountName': 'ad;"min♌💃 ' } }, ) total = get_ldap_users.save_records_to_csv(entries, settings.field_bindings, temp_path) self.assertEqual(total, 1) with open(temp_path, encoding='utf-8') as output_file: data = output_file.read() os.close(temp_file) os.remove(temp_path) self.assertEqual(data, '"ad;""min♌💃 ";[email protected]\n')
def test_print_default_settings(self): """Test default settings generator.""" default_settings = Settings() temp_file, temp_path = tempfile.mkstemp() with patch('sys.argv', ["1.py", "gen-defaults", temp_path]): get_ldap_users.main() with open(temp_path) as settings_file: args = namedtuple('Args', "domain_user domain_password settings_file") parsed_args = args(None, 'pass word', settings_file) self.assertFalse(parsed_args.settings_file.closed) settings = get_ldap_users.parse_settings_file(parsed_args) self.assertIsNotNone(settings) self.assertNotEqual(default_settings.__dict__, settings.__dict__) self.assertEqual(settings.ldap_password, 'pass word') settings.ldap_password = '******' self.assertEqual(default_settings.__dict__, settings.__dict__) os.close(temp_file) os.remove(temp_path)
def test_none_bindings(self): """Test None.""" settings = Settings() settings.use_json_bindings(None) # fields should contain default self.assertIsNone(settings.field_bindings)
def test_one_element(self): """Test bindings with missed elements.""" one_element = {'login': [1000, 'sAMAccountName']} settings = Settings() settings.use_json_bindings(one_element) self.assertEqual(len(settings.field_bindings), 1)
def print_sample_json(args): """Print sample JSON file""" settings = Settings() print(settings.to_json(), file=args.output_file)