def test_message_sent_with_contact_recipients(self): """ A list of strings or Contacts can be provided as the To/CC/BCC recipients """ mock_post = Mock() mock_post.status_code = 200 self.mock_post.return_value = mock_post message = Message(self.account, '', '', [Contact('*****@*****.**')]) message.send()
def test_is_read_status(self): """ Test that the correct value is returned after changing the is_read status """ mock_patch = Mock() mock_patch.status_code = 200 self.mock_patch.return_value = mock_patch message = Message(self.account, 'test body', 'test subject', [], is_read=False) message.is_read = True self.assertTrue(message.is_read)
def test_category_added(self): """ Test that Message.categories is updated in addition to the API call made """ mock_patch = Mock() mock_patch.status_code = 200 self.mock_patch.return_value = mock_patch message = Message(self.account, 'test body', 'test subject', [], categories=['A']) message.add_category('B') self.assertIn('A', message.categories) self.assertIn('B', message.categories)
def messages(self): """ Retrieves the messages in this Folder, returning a list of :class:`Messages <pyOutlook.core.message.Message>`.""" headers = self.headers r = requests.get('https://outlook.office.com/api/v2.0/me/MailFolders/' + self.id + '/messages', headers=headers) check_response(r) return Message._json_to_messages(self.account, r.json())
def send_email(self, body=None, subject=None, to=list, cc=None, bcc=None, send_as=None, attachments=None): """Sends an email in one method, a shortcut for creating an instance of :class:`Message <pyOutlook.core.message.Message>` . Args: body (str): The body of the email subject (str): The subject of the email to (list): A list of :class:`Contacts <pyOutlook.core.contact.Contact>` cc (list): A list of :class:`Contacts <pyOutlook.core.contact.Contact>` which will be added to the 'Carbon Copy' line bcc (list): A list of :class:`Contacts <pyOutlook.core.contact.Contact>` while be blindly added to the email send_as (Contact): A :class:`Contact <pyOutlook.core.contact.Contact>` whose email the OutlookAccount has access to attachments (list): A list of dictionaries with two parts [1] 'name' - a string which will become the file's name [2] 'bytes' - the bytes of the file. """ email = Message(self, body, subject, to, cc=cc, bcc=bcc, sender=send_as,) if attachments is not None: for attachment in attachments: email.attach(attachment.get('bytes'), attachment.get('name')) email.send()
def new_email(self, body='', subject='', to=list): """Creates a :class:`Message <pyOutlook.core.message.Message>` object. Keyword Args: body (str): The body of the email subject (str): The subject of the email to (List[Contact]): A list of recipients to email Returns: :class:`Message <pyOutlook.core.message.Message>` """ return Message(self, body, subject, to)
def new_email(self, body='', subject='', to=list, sender=None, cc=None, bcc=None): """Creates a :class:`Message <pyOutlook.core.message.Message>` object. Keyword Args: body (str): The body of the email subject (str): The subject of the email to (List[Contact]): A list of recipients to email Returns: :class:`Message <pyOutlook.core.message.Message>` """ return Message(self.access_token, body, subject, to, sender=sender, cc=cc, bcc=bcc)
def _get_messages_from_folder_name(self, folder_name): """ Retrieves all messages from a folder, specified by its name. This only works with "Well Known" folders, such as 'Inbox' or 'Drafts'. Args: folder_name (str): The name of the folder to retrieve Returns: List[:class:`Message <pyOutlook.core.message.Message>` ] """ r = requests.get('https://outlook.office.com/api/v2.0/me/MailFolders/' + folder_name + '/messages', headers=self._headers) check_response(r) return Message._json_to_messages(self, r.json())
def get_message(self, message_id): """Gets message matching provided id. the Outlook email matching the provided message_id. Args: message_id: A string for the intended message, provided by Outlook Returns: :class:`Message <pyOutlook.core.message.Message>` """ r = requests.get('https://outlook.office.com/api/v2.0/me/messages/' + message_id, headers=self._headers) check_response(r) return Message._json_to_message(self, r.json())
def test_recipients_missing_json(self): """ Test that a response with no ToRecipients does not cause Message deserialization to fail """ json_message = { "Id": "AAMkAGI2THVSAAA=", "CreatedDateTime": "2014-10-20T00:41:57Z", "LastModifiedDateTime": "2014-10-20T00:41:57Z", "ReceivedDateTime": "2014-10-20T00:41:57Z", "SentDateTime": "2014-10-20T00:41:53Z", "Subject": "Re: Meeting Notes", "Body": { "ContentType": "Text", "Content": "\n\nFrom: Alex D\nSent: Sunday, October 19, 2014 5:28 PM\nTo: Katie Jordan\nSubject: " "Meeting Notes\n\nPlease send me the meeting notes ASAP\n" }, "BodyPreview": "\nFrom: Alex D\nSent: Sunday, October 19, 2014 5:28 PM\nTo: Katie Jordan\n" "Subject: Meeting Notes\n\nPlease send me the meeting notes ASAP", "Sender": { "EmailAddress": { "Name": "Katie Jordan", "Address": "*****@*****.**" } }, "From": { "EmailAddress": { "Name": "Katie Jordan", "Address": "*****@*****.**" } }, "CcRecipients": [], "BccRecipients": [], "ReplyTo": [], "ConversationId": "AAQkAGI2yEto=", "IsRead": False, 'HasAttachments': True } Message._json_to_message(self.account, json_message)
def test_json_to_message_format(self): """ Test that JSON is turned into a Message correctly """ mock_response = Mock() mock_response.json.return_value = sample_message mock_response.status_code = 200 self.mock_get.return_value = mock_response account = OutlookAccount('token') message = Message._json_to_message(account, sample_message) self.assertEqual(message.subject, 'Re: Meeting Notes') sender = Contact('*****@*****.**', 'Katie Jordan') self.assertIsInstance(message.sender, Contact) self.assertEqual(message.sender.email, sender.email) self.assertEqual(message.sender.name, sender.name)
def get_messages(self, page=0): """Get first 10 messages in account, across all folders. Keyword Args: page (int): Integer representing the 'page' of results to fetch Returns: List[:class:`Message <pyOutlook.core.message.Message>`] """ endpoint = 'https://outlook.office.com/api/v2.0/me/messages' if page > 0: endpoint = endpoint + '/?%24skip=' + str(page) + '0' log.debug('Getting messages from endpoint: {} with Headers: {}'.format(endpoint, self._headers)) r = requests.get(endpoint, headers=self._headers) check_response(r) return Message._json_to_messages(self, r.json())
def test_attachments_added(self): """ Test that attachments are added to Message in the correct format """ message = Message(self.account, '', '', []) message.attach('abc', 'Test/Attachment.csv') message.attach(b'some bytes', 'attached.pdf') self.assertEqual(len(message._attachments), 2) file_bytes = [attachment._content for attachment in message._attachments] file_names = [attachment.name for attachment in message._attachments] # The files are base64'd for the API some_bytes = base64.b64encode(b'some bytes') abc = base64.b64encode(b'abc') self.assertIn(some_bytes.decode('UTF-8'), file_bytes) self.assertIn(abc.decode('UTF-8'), file_bytes) self.assertIn('TestAttachment.csv', file_names)