class UtilTests(unittest.TestCase): """Tests for utility methods.""" DOES_NOT_EXIST = unique() EXISTS = unique() VALUE = unique() def setUp(self) -> None: os.environ[self.EXISTS] = self.VALUE def test_exists(self): x = from_environ(self.EXISTS) self.assertEqual(self.VALUE, x) def test_does_not_exist(self): x = from_environ(self.DOES_NOT_EXIST) self.assertIsNone(x) def test_does_not_exist_with_default(self): default = unique() x = from_environ(self.DOES_NOT_EXIST, default=default) self.assertEqual(default, x) def test_valid_email_address(self): local, domain = parse_email_address('[email protected]') self.assertEqual('a', local) self.assertEqual('b.c', domain) def test_invalid_email_address(self): with self.assertRaises(InvalidEMailAddressError): parse_email_address('abc') def test_email_address_none(self): with self.assertRaises(InvalidEMailAddressError): # noinspection PyTypeChecker parse_email_address(None) def test_expand(self): local = 'a' domain = 'b.c' self.assertEqual( '[email protected]', expand_placeholders(f'1{PLACEHOLDER_ADDRESS}2', local, domain)) self.assertEqual( '3a4', expand_placeholders(f'3{PLACEHOLDER_LOCALPART}4', local, domain)) self.assertEqual( '5b.c6', expand_placeholders(f'5{PLACEHOLDER_DOMAIN}6', local, domain))
def _payload(local, domain): address = f'{local}@{domain}' uuid = unique() inner = { 'EmailAccountDescription': address, 'EmailAccountName': None, 'EmailAccountType': 'EmailTypeIMAP', 'EmailAddress': address, 'IncomingMailServerAuthentication': 'EmailAuthPassword', 'IncomingMailServerHostName': None, 'IncomingMailServerPortNumber': -1, 'IncomingMailServerUseSSL': None, 'IncomingMailServerUsername': None, 'OutgoingMailServerAuthentication': 'EmailAuthPassword', 'OutgoingMailServerHostName': None, 'OutgoingMailServerPortNumber': -1, 'OutgoingMailServerUseSSL': None, 'OutgoingMailServerUsername': None, 'OutgoingPasswordSameAsIncomingPassword': True, 'PayloadDescription': f'Email account configuration for {address}', 'PayloadDisplayName': domain, 'PayloadIdentifier': f'com.apple.mail.managed.{uuid}', 'PayloadType': 'com.apple.mail.managed', 'PayloadUUID': uuid, 'PayloadVersion': 1, 'SMIMEEnablePerMessageSwitch': False, 'SMIMEEnabled': False, 'SMIMEEncryptionEnabled': False, 'SMIMESigningEnabled': False, 'allowMailDrop': False, 'disableMailRecentsSyncing': False, } uuid = unique() outer = { 'PayloadContent': [inner], 'PayloadDisplayName': f'Mail account {domain}', 'PayloadIdentifier': branded_id(uuid), 'PayloadRemovalDisallowed': False, 'PayloadType': 'Configuration', 'PayloadUUID': uuid, 'PayloadVersion': 1, } return inner, outer
class LdapTests(TestCase): """Tests for LDAP access methods.""" EXISTS_LOCAL = 'a' EXISTS_DOMAIN = 'example.com' EXISTS_EMAIL = f'{EXISTS_LOCAL}@{EXISTS_DOMAIN}' EXISTS_CN = 'John Doe' EXISTS_UID = 'jd' UNIQUE = unique() ATTRIBUTES = {'attributes': {'x': [UNIQUE]}} @staticmethod def search_filter(email_address): return f'(mail={email_address})' def setUp(self) -> None: super().setUp() self.ldap = LdapAccess(hostname=LDAP_HOSTNAME, user=LDAP_BIND_USER, password=LDAP_BIND_PASSWORD, use_ssl=True) def test_attribute_exists(self): self.assertEqual(self.UNIQUE, self.ldap.get_attribute(self.ATTRIBUTES, 'x')) def test_attribute_missing(self): self.assertIsNone(self.ldap.get_attribute(self.ATTRIBUTES, 'y')) @unittest.skip # Avoid triggering fail2ban def test_bind_failed(self): self.ldap = LdapAccess(hostname=LDAP_HOSTNAME, user=LDAP_BIND_USER, password=self.UNIQUE) x: LookupResult = self.ldap.lookup( LDAP_SEARCH_BASE, self.search_filter(self.EXISTS_EMAIL)) self.assertEqual(STATUS_ERROR, x.status) def test_does_not_exist(self): x: LookupResult = self.ldap.lookup(LDAP_SEARCH_BASE, self.search_filter(self.UNIQUE)) self.assertEqual(STATUS_NO_MATCH, x.status) def test_exists(self): x: LookupResult = self.ldap.lookup(LDAP_SEARCH_BASE, self.search_filter( self.EXISTS_EMAIL), attr_cn='cn') self.assertEqual(STATUS_SUCCESS, x.status) self.assertEqual(self.EXISTS_CN, x.cn) self.assertEqual(self.EXISTS_UID, x.uid) def test_apple_generator_ldap(self): with app.app_context(): gen = AppleGenerator() gen.client_config(self.EXISTS_LOCAL, self.EXISTS_DOMAIN, '') def test_outlook_generator_ldap(self): with app.app_context(): gen = OutlookGenerator() gen.client_config(self.EXISTS_LOCAL, self.EXISTS_DOMAIN, '') def test_mozilla_generator_ldap_exists(self): with app.app_context(): server = Ldapserver.query.filter_by(id=LDAP_PORT).one() gen = MozillaGenerator() x = gen.ldap_lookup(self.EXISTS_EMAIL, server) self.assertEqual(STATUS_SUCCESS, x.status) def test_mozilla_generator_ldap_no_match(self): with app.app_context(): server = Ldapserver.query.filter_by(id=LDAP_PORT).one() gen = MozillaGenerator() with self.assertRaises(LdapNoMatch): gen.ldap_lookup(self.UNIQUE, server) def test_mozilla_generator_ldap_missing_server(self): with self.assertRaises(LdapLookupError): gen = MozillaGenerator() gen.ldap_lookup(self.UNIQUE, None)
def test_does_not_exist_with_default(self): default = unique() x = from_environ(self.DOES_NOT_EXIST, default=default) self.assertEqual(default, x)