def test_download_message(self, get_message_info_mock): """ Test the GmailManager on downloading a message and that it is stored in the database. """ message_id = '15a6008a4baa65f3' with open('lily/messaging/email/tests/data/get_message_info_{0}.json'.format(message_id)) as infile: json_obj = json.load(infile) get_message_info_mock.return_value = json_obj email_account = EmailAccount.objects.first() labels = [settings.GMAIL_LABEL_UNREAD, settings.GMAIL_LABEL_INBOX] for label in labels: EmailLabelFactory.create(account=email_account, label_id=label) manager = GmailManager(email_account) # Message isn't present in the db, so it will do a mocked API call. manager.download_message(message_id) # Verify that the email message is stored in the db. self.assertTrue(EmailMessage.objects.filter(account=email_account, message_id=message_id).exists()) # Verify that the email message has the correct labels. email_message = EmailMessage.objects.get(account=email_account, message_id=message_id) email_message_labels = set(email_message.labels.all().values_list('label_id', flat=True)) self.assertEqual(email_message_labels, set([settings.GMAIL_LABEL_UNREAD, settings.GMAIL_LABEL_INBOX]))
def test_download_message_exists(self, get_message_info_mock, get_short_message_info_mock): """ Test the GmailManager on updating an existing message. """ message_id = '15a6008a4baa65f3' email_account = EmailAccount.objects.first() manager = GmailManager(email_account) labels = [ settings.GMAIL_LABEL_UNREAD, settings.GMAIL_LABEL_IMPORTANT, settings.GMAIL_LABEL_PERSONAL, settings.GMAIL_LABEL_INBOX ] for label in labels: EmailLabelFactory.create(account=email_account, label_id=label) with open('lily/messaging/email/tests/data/get_message_info_{0}.json'. format(message_id)) as infile: json_obj = json.load(infile) get_message_info_mock.return_value = json_obj # Message isn't present in the db, so it will do a mocked API call. manager.download_message(message_id) # The message with labels is now present in the database so downloading it again will only update it's labels. with open( 'lily/messaging/email/tests/data/get_short_message_info_{0}_archived.json' .format(message_id)) as infile: json_obj = json.load(infile) get_short_message_info_mock.return_value = json_obj try: manager.download_message(message_id) except StopIteration: # Because the email message is already in the database it should not do a (mocked) API call again. self.fail('StopIteration should have been raised.') # Verify that the email message has the correct labels after the updated short message info with the Inbox # label removed. email_message = EmailMessage.objects.get(account=email_account, message_id=message_id) email_message_labels = set(email_message.labels.all().values_list( 'label_id', flat=True)) self.assertEqual( email_message_labels, set([ settings.GMAIL_LABEL_UNREAD, settings.GMAIL_LABEL_IMPORTANT, settings.GMAIL_LABEL_PERSONAL ]))
def test_download_message_utf16(self, get_message_info_mock): """ Test the GmailManager on downloading a utf-16 encoded message. """ message_id = '161f6052954d7758' with open('lily/messaging/email/tests/data/get_message_info_{0}.json'.format(message_id)) as infile: json_obj = json.load(infile) get_message_info_mock.return_value = json_obj email_account = EmailAccount.objects.first() labels = [settings.GMAIL_LABEL_UNREAD, settings.GMAIL_LABEL_INBOX] for label in labels: EmailLabelFactory.create(account=email_account, label_id=label) manager = GmailManager(email_account) # Message isn't present in the db, so it will do a mocked API call. manager.download_message(message_id) # Verify that the email message is stored in the db. self.assertTrue(EmailMessage.objects.filter(account=email_account, message_id=message_id).exists())