def test_add_field(self): field = TextField('foo', field_uri='bar') Item.add_field(field, insert_after='subject') try: self.assertEqual(Item.get_field_by_fieldname('foo'), field) finally: Item.remove_field(field)
def test_add_field(self): field = TextField("foo", field_uri="bar") Item.add_field(field, insert_after="subject") try: self.assertEqual(Item.get_field_by_fieldname("foo"), field) finally: Item.remove_field(field)
def test_parse_incident_from_item_with_attachments(): """ Given: - Message item with attachment that contains email attachments When: - Parsing incident from item Verify: - Parsing runs successfully """ content = b'ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901;' \ b' d=microsoft.com; cv=none;b=ES/YXpFlV19rlN1iV+ORg5RzID8GPSQL' \ b'nUT26MNdeTzcQSwK679doIz5Avpv8Ps2H/aBkBamwRNOCJBkl7iCHyy+04yRj3ghikw3u/ufIFHi0sQ7QG95mO1PVPLibv9A==' message = Message( datetime_created=EWSDate(year=2021, month=1, day=25), to_recipients=[], attachments=[ ItemAttachment( item=Item(mime_content=content, headers=[]), attachment_id=AttachmentId(), last_modified_time=EWSDate(year=2021, month=1, day=25), ), ], ) incident = parse_incident_from_item(message) assert incident['attachment']
def test_parse_incident_from_item(): """ Given: - Message item with attachment that contains non UTF-8 encoded char When: - Parsing incident from item Verify: - Parsing runs successfully - Incidnet attachment is not empty """ message = Message( datetime_created=EWSDate(year=2021, month=1, day=25), to_recipients=[], attachments=[ ItemAttachment( item=Item(mime_content=b'\xc400'), attachment_id=AttachmentId(), last_modified_time=EWSDate(year=2021, month=1, day=25), ), ], ) incident = parse_incident_from_item(message) assert incident['attachment']
def test_attachment_failure(self): att1 = FileAttachment(name='my_file_1.txt', content='Hello from unicode æøå'.encode('utf-8')) att1.attachment_id = 'XXX' with self.assertRaises(ValueError): att1.attach() # Cannot have an attachment ID att1.attachment_id = None with self.assertRaises(ValueError): att1.attach() # Must have a parent item att1.parent_item = Item() with self.assertRaises(ValueError): att1.attach() # Parent item must have an account att1.parent_item = None with self.assertRaises(ValueError): att1.detach() # Must have an attachment ID att1.attachment_id = 'XXX' with self.assertRaises(ValueError): att1.detach() # Must have a parent item att1.parent_item = Item() with self.assertRaises(ValueError): att1.detach() # Parent item must have an account att1.parent_item = None att1.attachment_id = None
def test_attachment_failure(self): att1 = FileAttachment(name="my_file_1.txt", content="Hello from unicode æøå".encode("utf-8")) att1.attachment_id = "XXX" with self.assertRaises(ValueError) as e: att1.attach() self.assertEqual(e.exception.args[0], "This attachment has already been created") att1.attachment_id = None with self.assertRaises(ValueError) as e: att1.attach() self.assertEqual(e.exception.args[0], "Parent item None must have an account") att1.parent_item = Item() with self.assertRaises(ValueError) as e: att1.attach() self.assertEqual(e.exception.args[0], "Parent item Item(attachments=[]) must have an account") att1.parent_item = None with self.assertRaises(ValueError) as e: att1.detach() self.assertEqual(e.exception.args[0], "This attachment has not been created") att1.attachment_id = "XXX" with self.assertRaises(ValueError) as e: att1.detach() self.assertEqual(e.exception.args[0], "Parent item None must have an account") att1.parent_item = Item() with self.assertRaises(ValueError) as e: att1.detach() self.assertEqual(e.exception.args[0], "Parent item Item(attachments=[]) must have an account") att1.parent_item = "XXX" with self.assertRaises(TypeError) as e: att1.clean() self.assertEqual( e.exception.args[0], "'parent_item' 'XXX' must be of type <class 'exchangelib.items.item.Item'>" ) with self.assertRaises(ValueError) as e: Message(attachments=[att1]) self.assertIn("must point to this item", e.exception.args[0]) att1.parent_item = None att1.attachment_id = None
def test_invalid_field(self): test_field = Item.get_field_by_fieldname(fieldname='text_body') self.assertIsInstance(test_field, TextField) self.assertEqual(test_field.name, 'text_body') with self.assertRaises(InvalidField): Item.get_field_by_fieldname(fieldname='xxx') Item.validate_field(field=test_field, version=Version(build=EXCHANGE_2013)) with self.assertRaises(InvalidFieldForVersion) as e: Item.validate_field(field=test_field, version=Version(build=EXCHANGE_2010)) self.assertEqual( e.exception.args[0], "Field 'text_body' is not supported on server version Build=14.0.0.0, API=Exchange2010, Fullname=Microsoft " "Exchange Server 2010 (supported from: 15.0.0.0, deprecated from: None)" )