def test_trash_email_message(self, get_message_info_mock, trash_email_message_mock): """ Test the GmailManager on trashing an email message. """ message_id = '15af6279f554fd15' # Mock the responses of the API call. with open('lily/messaging/email/tests/data/trash_email_message_{0}.json'.format(message_id)) as infile: json_obj = json.load(infile) trash_email_message_mock.return_value = json_obj with open('lily/messaging/email/tests/data/get_message_info_{0}_trash.json'.format(message_id)) as infile: json_obj = json.load(infile) get_message_info_mock.return_value = json_obj email_account = EmailAccount.objects.first() email_message = EmailMessageFactory.create(account=email_account, message_id=message_id) labels = [settings.GMAIL_LABEL_UNREAD, settings.GMAIL_LABEL_TRASH, settings.GMAIL_LABEL_INBOX] for label in labels: EmailLabelFactory.create(account=email_account, label_id=label) manager = GmailManager(email_account) manager.trash_email_message(email_message) # Verify that the email message is trashed by looking at the labels, ie. INBOX is not presnt and TRASH is. 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_TRASH]))
def _create_object_stub(self, size=1, action=None, with_send_from=True, with_message=False, **kwargs): object_list = super(DraftEmailTests, self)._create_object_stub(size, force_to_list=True, **kwargs) for obj in object_list: obj['action'] = action or 'compose' # 'compose' is the default. account = EmailAccountFactory(owner=self.user_obj, tenant=self.user_obj.tenant) obj['send_from'] = None if with_send_from: obj['send_from'] = account.id if with_message: obj['message'] = EmailMessageFactory.create(account=account).id if size == 1: return object_list[0] return object_list
def test_private_account(self): """ Test if having an email account set to private returns no data for unauthorized users. """ email_account = EmailAccountFactory.create(tenant=self.tenant, privacy=EmailAccount.PRIVATE, owner=self.owner) # Share the email account with a user. config = { 'user': self.users[1], 'email_account': email_account, 'privacy': EmailAccount.PUBLIC, 'tenant': self.tenant, } email_account.sharedemailconfig_set.create(**config) email_messages = EmailMessageFactory.create_batch(account=email_account, size=10) for user in self.users: filtered_messages = [] for email_message in email_messages: filtered_message = get_filtered_message(email_message, email_account, user) if filtered_message: filtered_messages.append(filtered_message) if self._can_view_full_message(email_account, user): self.assertEqual(filtered_messages, email_messages) else: self.assertEqual(filtered_messages, [])
def test_public_account(self): """ Test if having an email account set to public returns all data for everyone. """ email_account = EmailAccountFactory.create(tenant=self.tenant, privacy=EmailAccount.PUBLIC, owner=self.owner) # Share the email account with a user. config = { 'user': self.users[1], 'email_account': email_account, 'privacy': EmailAccount.PUBLIC, 'tenant': self.tenant, } email_account.sharedemailconfig_set.create(**config) email_messages = EmailMessageFactory.create_batch(account=email_account, size=10) for user in self.users: filtered_messages = [] for email_message in email_messages: filtered_message = get_filtered_message(email_message, email_account, user) if filtered_message: filtered_messages.append(filtered_message) self.assertEqual(filtered_messages, email_messages)
def test_metadata_only(self): """ Test if having an email account set to metadata only returns filtered data for unauthorized users. """ email_account = EmailAccountFactory.create( tenant=self.tenant, privacy=EmailAccount.METADATA, owner=self.owner) # Share the email account with a user. config = { 'user': self.users[1], 'email_account': email_account, 'privacy': EmailAccount.PUBLIC, 'tenant': self.tenant, } email_account.sharedemailconfig_set.create(**config) email_messages = EmailMessageFactory.create_batch( account=email_account, size=10) stripped_messages = [] for email_message in email_messages: stripped_messages.append({ 'id': email_message.id, 'sender': email_message.sender, 'received_by': email_message.received_by.all(), 'received_by_cc': email_message.received_by_cc.all(), 'sent_date': email_message.sent_date, 'account': email_message.account, }) for user in self.users: filtered_messages = [] for email_message in email_messages: filtered_message = get_filtered_message( email_message, email_account, user) if filtered_message: filtered_messages.append(filtered_message) if self._can_view_full_message(email_account, user): self.assertEqual(filtered_messages, email_messages) else: # Comparing lists of dicts doesn't seem to work properly. # So loop through all filter and stripped messages and compare them. for i in range(len(filtered_messages)): self.assertItemsEqual(filtered_messages[i], stripped_messages[i])
def test_metadata_only(self): """ Test if having an email account set to metadata only returns filtered data for unauthorized users. """ email_account = EmailAccountFactory.create(tenant=self.tenant, privacy=EmailAccount.METADATA, owner=self.owner) # Share the email account with a user. config = { 'user': self.users[1], 'email_account': email_account, 'privacy': EmailAccount.PUBLIC, 'tenant': self.tenant, } email_account.sharedemailconfig_set.create(**config) email_messages = EmailMessageFactory.create_batch(account=email_account, size=10) stripped_messages = [] for email_message in email_messages: stripped_messages.append({ 'id': email_message.id, 'sender': email_message.sender, 'received_by': email_message.received_by.all(), 'received_by_cc': email_message.received_by_cc.all(), 'sent_date': email_message.sent_date, 'account': email_message.account, }) for user in self.users: filtered_messages = [] for email_message in email_messages: filtered_message = get_filtered_message(email_message, email_account, user) if filtered_message: filtered_messages.append(filtered_message) if self._can_view_full_message(email_account, user): self.assertEqual(filtered_messages, email_messages) else: # Comparing lists of dicts doesn't seem to work properly. # So loop through all filter and stripped messages and compare them. for i in range(len(filtered_messages)): self.assertItemsEqual(filtered_messages[i], stripped_messages[i])
def _create_object_stub(self, size=1, action=None, with_send_from=True, with_message=False, **kwargs): object_list = super(DraftEmailTests, self)._create_object_stub(size, force_to_list=True, **kwargs) for obj in object_list: obj['action'] = action or 'compose' # 'compose' is the default. account = EmailAccountFactory( owner=self.user_obj, tenant=self.user_obj.tenant ) obj['send_from'] = None if with_send_from: obj['send_from'] = account.id if with_message: obj['message'] = EmailMessageFactory.create(account=account).id if size == 1: return object_list[0] return object_list