Changeset View
Standalone View
tests/unit/test-011-wallace_resources.py
Show First 20 Lines • Show All 87 Lines • ▼ Show 20 Lines | |||||
END:VCALENDAR | END:VCALENDAR | ||||
""" | """ | ||||
conf = pykolab.getConf() | conf = pykolab.getConf() | ||||
if not hasattr(conf, 'defaults'): | if not hasattr(conf, 'defaults'): | ||||
conf.finalize_conf() | conf.finalize_conf() | ||||
class TestWallaceResources(unittest.TestCase): | class TestWallaceResources(unittest.TestCase): | ||||
def setUp(self): | def setUp(self): | ||||
# monkey-patch the pykolab.auth module to check API calls | # monkey-patch the pykolab.auth module to check API calls | ||||
# without actually connecting to LDAP | # without actually connecting to LDAP | ||||
self.patch(pykolab.auth.Auth, "connect", self._mock_nop) | self.patch(pykolab.auth.Auth, "connect", self._mock_nop) | ||||
self.patch(pykolab.auth.Auth, "disconnect", self._mock_nop) | self.patch(pykolab.auth.Auth, "disconnect", self._mock_nop) | ||||
self.patch(pykolab.auth.Auth, "find_resource", self._mock_find_resource) | self.patch(pykolab.auth.Auth, "find_resource", self._mock_find_resource) | ||||
Lint: PEP8 E501: line too long (80 > 79 characters) | |||||
self.patch(pykolab.auth.Auth, "get_entry_attributes", self._mock_get_entry_attributes) | self.patch(pykolab.auth.Auth, "get_entry_attributes", self._mock_get_entry_attributes) | ||||
Lint: PEP8 E501 line too long (94 > 79 characters) Lint: PEP8 E501: line too long (94 > 79 characters) | |||||
self.patch(pykolab.auth.Auth, "search_entry_by_attribute", self._mock_search_entry_by_attribute) | self.patch(pykolab.auth.Auth, "search_entry_by_attribute", self._mock_search_entry_by_attribute) | ||||
Lint: PEP8 E501 line too long (104 > 79 characters) Lint: PEP8 E501: line too long (104 > 79 characters) | |||||
# intercept calls to smtplib.SMTP.sendmail() | # intercept calls to smtplib.SMTP.sendmail() | ||||
import smtplib | import smtplib | ||||
self.patch(smtplib.SMTP, "__init__", self._mock_smtp_init) | self.patch(smtplib.SMTP, "__init__", self._mock_smtp_init) | ||||
self.patch(smtplib.SMTP, "quit", self._mock_nop) | self.patch(smtplib.SMTP, "quit", self._mock_nop) | ||||
self.patch(smtplib.SMTP, "sendmail", self._mock_smtp_sendmail) | self.patch(smtplib.SMTP, "sendmail", self._mock_smtp_sendmail) | ||||
self.smtplog = []; | self.smtplog = [] | ||||
def _mock_nop(self, domain=None): | def _mock_nop(self, domain=None): | ||||
pass | pass | ||||
def _mock_find_resource(self, address): | def _mock_find_resource(self, address): | ||||
if not 'resource' in address: | if 'resource' not in address: | ||||
return []; | return [] | ||||
(prefix, domain) = address.split('@') | (prefix, domain) = address.split('@') | ||||
entry_dn = "cn=" + prefix + ",ou=Resources,dc=" + ",dc=".join(domain.split('.')) | entry_dn = "cn=" + prefix + ",ou=Resources,dc=" + ",dc=".join(domain.split('.')) | ||||
Lint: PEP8 E501 line too long (88 > 79 characters) Lint: PEP8 E501: line too long (88 > 79 characters) | |||||
return [ entry_dn ]; | return [entry_dn] | ||||
def _mock_get_entry_attributes(self, domain, entry, attributes): | def _mock_get_entry_attributes(self, domain, entry, attributes): | ||||
(_, uid) = entry.split(',')[0].split('=') | (_, uid) = entry.split(',')[0].split('=') | ||||
return { 'cn': uid, 'mail': uid + "@example.org", '_attrib': attributes } | return {'cn': uid, 'mail': uid + "@example.org", '_attrib': attributes} | ||||
def _mock_search_entry_by_attribute(self, attr, value, **kw): | def _mock_search_entry_by_attribute(self, attr, value, **kw): | ||||
results = [] | results = [] | ||||
if value == "cn=Room 101,ou=Resources,dc=example,dc=org": | if value == "cn=Room 101,ou=Resources,dc=example,dc=org": | ||||
results.append(('cn=Rooms,ou=Resources,dc=example,dc=org', { attr: value, 'owner': 'uid=doe,ou=People,dc=example,dc=org' })) | results.append(('cn=Rooms,ou=Resources,dc=example,dc=org', {attr: value, 'owner': 'uid=doe,ou=People,dc=example,dc=org'})) | ||||
Lint: PEP8 E501 line too long (134 > 79 characters) Lint: PEP8 E501: line too long (134 > 79 characters) | |||||
return results | return results | ||||
def _mock_smtp_init(self, host=None, port=None, local_hostname=None, timeout=0): | def _mock_smtp_init(self, host=None, port=None, local_hostname=None, timeout=0): | ||||
Lint: PEP8 E501 line too long (84 > 79 characters) Lint: PEP8 E501: line too long (84 > 79 characters) | |||||
pass | pass | ||||
def _mock_smtp_sendmail(self, from_addr, to_addr, message, mail_options=None, rcpt_options=None): | def _mock_smtp_sendmail(self, from_addr, to_addr, message, mail_options=None, rcpt_options=None): | ||||
Lint: PEP8 E501 line too long (101 > 79 characters) Lint: PEP8 E501: line too long (101 > 79 characters) | |||||
self.smtplog.append((from_addr, to_addr, message)) | self.smtplog.append((from_addr, to_addr, message)) | ||||
def _get_ics_part(self, message): | def _get_ics_part(self, message): | ||||
ics_part = None | ics_part = None | ||||
for part in message.walk(): | for part in message.walk(): | ||||
if part.get_content_type() == 'text/calendar': | if part.get_content_type() == 'text/calendar': | ||||
ics_part = part | ics_part = part | ||||
return ics_part | return ics_part | ||||
def _get_ical(self, ics): | def _get_ical(self, ics): | ||||
if hasattr(Calendar, 'from_ical'): | if hasattr(Calendar, 'from_ical'): | ||||
cal = Calendar.from_ical(ics) | cal = Calendar.from_ical(ics) | ||||
elif hasattr(Calendar, 'from_string'): | elif hasattr(Calendar, 'from_string'): | ||||
cal = Calendar.from_string(ics) | cal = Calendar.from_string(ics) | ||||
for e in cal.walk(): | for e in cal.walk(): | ||||
if e.name == "VEVENT": | if e.name == "VEVENT": | ||||
return e | return e | ||||
return None | return None | ||||
def test_002_resource_record_from_email_address(self): | def test_002_resource_record_from_email_address(self): | ||||
res = module_resources.resource_record_from_email_address("doe@example.org") | res = module_resources.resource_record_from_email_address("doe@example.org") | ||||
Lint: PEP8 E501 line too long (84 > 79 characters) Lint: PEP8 E501: line too long (84 > 79 characters) | |||||
self.assertEqual(len(res), 0); | self.assertEqual(len(res), 0) | ||||
def test_003_resource_records_from_itip_events(self): | def test_003_resource_records_from_itip_events(self): | ||||
message = message_from_string(itip_multipart) | message = message_from_string(itip_multipart) | ||||
itips = itip.events_from_message(message) | itips = itip.events_from_message(message) | ||||
res = module_resources.resource_records_from_itip_events(itips) | res = module_resources.resource_records_from_itip_events(itips) | ||||
self.assertEqual(len(res), 2, "Return resources: %r" % (res)); | self.assertEqual(len(res), 2, "Return resources: %r" % (res)) | ||||
res = module_resources.resource_records_from_itip_events(itips, message['To']) | res = module_resources.resource_records_from_itip_events(itips, message['To']) | ||||
Lint: PEP8 E501 line too long (86 > 79 characters) Lint: PEP8 E501: line too long (86 > 79 characters) | |||||
self.assertEqual(len(res), 1, "Return target resource: %r" % (res)); | self.assertEqual(len(res), 1, "Return target resource: %r" % (res)) | ||||
self.assertEqual("cn=resource-collection-car,ou=Resources,dc=example,dc=org", res[0]); | self.assertEqual("cn=resource-collection-car,ou=Resources,dc=example,dc=org", res[0]) | ||||
Lint: PEP8 E501 line too long (93 > 79 characters) Lint: PEP8 E501: line too long (93 > 79 characters) | |||||
def test_004_get_resource_owner(self): | def test_004_get_resource_owner(self): | ||||
owner1 = module_resources.get_resource_owner({ 'owner': "uid=foo,ou=People,cd=example,dc=org" }) | owner1 = module_resources.get_resource_owner({'owner': "uid=foo,ou=People,cd=example,dc=org"}) | ||||
Lint: PEP8 E501 line too long (102 > 79 characters) Lint: PEP8 E501: line too long (102 > 79 characters) | |||||
self.assertIsInstance(owner1, dict) | self.assertIsInstance(owner1, dict) | ||||
self.assertEqual("foo@example.org", owner1['mail']) | self.assertEqual("foo@example.org", owner1['mail']) | ||||
self.assertIn("telephoneNumber", owner1['_attrib']) | self.assertIn("telephoneNumber", owner1['_attrib']) | ||||
owner2 = module_resources.get_resource_owner({ 'owner': ["uid=john,ou=People,cd=example,dc=org", "uid=jane,ou=People,cd=example,dc=org"] }) | owner2 = module_resources.get_resource_owner({'owner': ["uid=john,ou=People,cd=example,dc=org", "uid=jane,ou=People,cd=example,dc=org"]}) | ||||
Lint: PEP8 E501 line too long (145 > 79 characters) Lint: PEP8 E501: line too long (145 > 79 characters) | |||||
self.assertIsInstance(owner2, dict) | self.assertIsInstance(owner2, dict) | ||||
self.assertEqual("john@example.org", owner2['mail']) | self.assertEqual("john@example.org", owner2['mail']) | ||||
owner3 = module_resources.get_resource_owner({ 'dn': "cn=cars,ou=Resources,cd=example,dc=org" }) | owner3 = module_resources.get_resource_owner({'dn': "cn=cars,ou=Resources,cd=example,dc=org"}) | ||||
Lint: PEP8 E501 line too long (102 > 79 characters) Lint: PEP8 E501: line too long (102 > 79 characters) | |||||
self.assertEqual(owner3, None) | self.assertEqual(owner3, None) | ||||
owner4 = module_resources.get_resource_owner({ 'dn': "cn=Room 101,ou=Resources,dc=example,dc=org" }) | owner4 = module_resources.get_resource_owner({'dn': "cn=Room 101,ou=Resources,dc=example,dc=org"}) | ||||
Lint: PEP8 E501 line too long (106 > 79 characters) Lint: PEP8 E501: line too long (106 > 79 characters) | |||||
self.assertEqual("doe@example.org", owner4['mail']) | self.assertEqual("doe@example.org", owner4['mail']) | ||||
def test_005_send_response_accept(self): | def test_005_send_response_accept(self): | ||||
itip_event = itip.events_from_message(message_from_string(itip_non_multipart)) | itip_event = itip.events_from_message(message_from_string(itip_non_multipart)) | ||||
Lint: PEP8 E501 line too long (86 > 79 characters) Lint: PEP8 E501: line too long (86 > 79 characters) | |||||
module_resources.send_response("resource-collection-car@example.org", itip_event) | module_resources.send_response("resource-collection-car@example.org", itip_event) | ||||
Lint: PEP8 E501 line too long (89 > 79 characters) Lint: PEP8 E501: line too long (89 > 79 characters) | |||||
self.assertEqual(len(self.smtplog), 1); | self.assertEqual(len(self.smtplog), 1) | ||||
self.assertEqual("resource-collection-car@example.org", self.smtplog[0][0]) | self.assertEqual("resource-collection-car@example.org", self.smtplog[0][0]) | ||||
Lint: PEP8 E501 line too long (83 > 79 characters) Lint: PEP8 E501: line too long (83 > 79 characters) | |||||
self.assertEqual("doe@example.org", self.smtplog[0][1]) | self.assertEqual("doe@example.org", self.smtplog[0][1]) | ||||
response = message_from_string(self.smtplog[0][2]) | response = message_from_string(self.smtplog[0][2]) | ||||
self.assertIn("ACCEPTED".lower(), response['subject'].lower(), "Participant status in message subject: %r" % (response['subject'])) | self.assertIn("ACCEPTED".lower(), response['subject'].lower(), "Participant status in message subject: %r" % (response['subject'])) | ||||
Lint: PEP8 E501 line too long (139 > 79 characters) Lint: PEP8 E501: line too long (139 > 79 characters) | |||||
self.assertTrue(response.is_multipart()) | self.assertTrue(response.is_multipart()) | ||||
# find ics part of the response | # find ics part of the response | ||||
ics_part = self._get_ics_part(response) | ics_part = self._get_ics_part(response) | ||||
self.assertIsInstance(ics_part, message.Message) | self.assertIsInstance(ics_part, message.Message) | ||||
self.assertEqual(ics_part.get_param('method'), "REPLY") | self.assertEqual(ics_part.get_param('method'), "REPLY") | ||||
def test_006_send_response_delegate(self): | def test_006_send_response_delegate(self): | ||||
# delegate resource-collection-car@example.org => resource-car-audi-a4@example.org | # delegate resource-collection-car@example.org => resource-car-audi-a4@example.org | ||||
Lint: PEP8 E501 line too long (90 > 79 characters) Lint: PEP8 E501: line too long (90 > 79 characters) | |||||
itip_event = itip.events_from_message(message_from_string(itip_non_multipart))[0] | itip_event = itip.events_from_message(message_from_string(itip_non_multipart))[0] | ||||
Lint: PEP8 E501 line too long (89 > 79 characters) Lint: PEP8 E501: line too long (89 > 79 characters) | |||||
itip_event['xml'].delegate('resource-collection-car@example.org', 'resource-car-audi-a4@example.org') | itip_event['xml'].delegate('resource-collection-car@example.org', 'resource-car-audi-a4@example.org') | ||||
Lint: PEP8 E501 line too long (109 > 79 characters) Lint: PEP8 E501: line too long (109 > 79 characters) | |||||
itip_event['xml'].set_attendee_participant_status(itip_event['xml'].get_attendee('resource-car-audi-a4@example.org'), "ACCEPTED") | itip_event['xml'].set_attendee_participant_status(itip_event['xml'].get_attendee('resource-car-audi-a4@example.org'), "ACCEPTED") | ||||
Lint: PEP8 E501 line too long (137 > 79 characters) Lint: PEP8 E501: line too long (137 > 79 characters) | |||||
module_resources.send_response("resource-collection-car@example.org", itip_event) | module_resources.send_response("resource-collection-car@example.org", itip_event) | ||||
Lint: PEP8 E501 line too long (89 > 79 characters) Lint: PEP8 E501: line too long (89 > 79 characters) | |||||
self.assertEqual(len(self.smtplog), 2); | self.assertEqual(len(self.smtplog), 2) | ||||
self.assertEqual("resource-collection-car@example.org", self.smtplog[0][0]) | self.assertEqual("resource-collection-car@example.org", self.smtplog[0][0]) | ||||
Lint: PEP8 E501 line too long (83 > 79 characters) Lint: PEP8 E501: line too long (83 > 79 characters) | |||||
self.assertEqual("resource-car-audi-a4@example.org", self.smtplog[1][0]) | self.assertEqual("resource-car-audi-a4@example.org", self.smtplog[1][0]) | ||||
Lint: PEP8 E501 line too long (80 > 79 characters) Lint: PEP8 E501: line too long (80 > 79 characters) | |||||
# delegated resource responds ACCEPTED | # delegated resource responds ACCEPTED | ||||
response1 = message_from_string(self.smtplog[0][2]) | response1 = message_from_string(self.smtplog[0][2]) | ||||
ical1 = self._get_ical(self._get_ics_part(response1).get_payload(decode=True)) | ical1 = self._get_ical(self._get_ics_part(response1).get_payload(decode=True)) | ||||
Lint: PEP8 E501 line too long (86 > 79 characters) Lint: PEP8 E501: line too long (86 > 79 characters) | |||||
self.assertIn("DELEGATED".lower(), response1['subject'].lower(), "Participant status in message subject: %r" % (response1['subject'])) | self.assertIn("DELEGATED".lower(), response1['subject'].lower(), "Participant status in message subject: %r" % (response1['subject'])) | ||||
Lint: PEP8 E501 line too long (142 > 79 characters) Lint: PEP8 E501: line too long (142 > 79 characters) | |||||
self.assertEqual(ical1['attendee'][1].__str__(), "MAILTO:resource-car-audi-a4@example.org") | self.assertEqual(ical1['attendee'][1].__str__(), "MAILTO:resource-car-audi-a4@example.org") | ||||
Lint: PEP8 E501 line too long (99 > 79 characters) Lint: PEP8 E501: line too long (99 > 79 characters) | |||||
# resource collection responds DELEGATED | # resource collection responds DELEGATED | ||||
response2 = message_from_string(self.smtplog[1][2]) | response2 = message_from_string(self.smtplog[1][2]) | ||||
ical2 = self._get_ical(self._get_ics_part(response2).get_payload(decode=True)) | ical2 = self._get_ical(self._get_ics_part(response2).get_payload(decode=True)) | ||||
Lint: PEP8 E501 line too long (86 > 79 characters) Lint: PEP8 E501: line too long (86 > 79 characters) | |||||
self.assertIn("ACCEPTED".lower(), response2['subject'].lower(), "Delegation message subject: %r" % (response2['subject'])) | self.assertIn("ACCEPTED".lower(), response2['subject'].lower(), "Delegation message subject: %r" % (response2['subject'])) | ||||
Lint: PEP8 E501 line too long (130 > 79 characters) Lint: PEP8 E501: line too long (130 > 79 characters) | |||||
self.assertEqual(ical2['attendee'].__str__(), "MAILTO:resource-car-audi-a4@example.org") | self.assertEqual(ical2['attendee'].__str__(), "MAILTO:resource-car-audi-a4@example.org") | ||||
Lint: PEP8 E501 line too long (96 > 79 characters) Lint: PEP8 E501: line too long (96 > 79 characters) | |||||
self.assertEqual(ical2['attendee'].params['PARTSTAT'], u"ACCEPTED") | self.assertEqual(ical2['attendee'].params['PARTSTAT'], u"ACCEPTED") | ||||
line too long (80 > 79 characters)