def setUpTestData(cls): # Create a user, handled by UserBasedTest. super(GmailConnectorTests, cls).setUpTestData() # Create an email account for the user. EmailAccountFactory.create(owner=cls.user_obj, tenant=cls.user_obj.tenant)
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_create_action_object(self, action, with_send_from=True, with_message=False, should_succeed=True, with_primary_email_account=True): """ This helper function tests the create functionality for the passed action. """ if with_primary_email_account: self.user_obj.primary_email_account = EmailAccountFactory( owner=self.user_obj, tenant=self.user_obj.tenant) self.user_obj.save() set_current_user(self.user_obj) stub_dict = self._create_object_stub(action=action, with_send_from=with_send_from, with_message=with_message) request = self.user.post(self.get_url(self.list_url), stub_dict) if should_succeed: self.assertStatus(request, status.HTTP_201_CREATED, stub_dict) created_id = json.loads(request.content).get('id') self.assertIsNotNone(created_id) db_obj = self.model_cls.objects.get(pk=created_id) self._compare_objects(db_obj, json.loads(request.content)) else: self.assertStatus(request, status.HTTP_400_BAD_REQUEST, stub_dict)
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 setUpTestData(cls): # Create a user, handled by UserBasedTest. super(EmailAccountManagementTests, cls).setUpTestData() # Create an email account for the user. cls.email_accounts = EmailAccountFactory.create_batch(size=2, owner=cls.user_obj, tenant=cls.user_obj.tenant, is_authorized=False)
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 setUpTestData(cls): # Create a user, handled by UserBasedTest. super(EmailAccountManagementTests, cls).setUpTestData() # Create an email account for the user. cls.email_accounts = EmailAccountFactory.create_batch( size=2, owner=cls.user_obj, tenant=cls.user_obj.tenant, is_authorized=False)
def get_or_create_draft(self): """ Both _create_object and get_url require an email draft, while some tests make that only one of these functions are called. To simplify, we use this method to return a draft when there isn't one. """ if hasattr(self, 'email_draft'): return self.email_draft user = get_current_user() email_account = EmailAccountFactory.create(owner=user, tenant=user.tenant) self.email_draft = EmailDraftFactory(tenant=user.tenant, send_from=email_account) return self.email_draft
def get_or_create_draft(self): """ Both _create_object and get_url require an email draft, while some tests make that only one of these functions are called. To simplify, we use this method to return a draft when there isn't one. """ if hasattr(self, 'email_draft'): return self.email_draft user = get_current_user() email_account = EmailAccountFactory.create(owner=user, tenant=user.tenant) self.email_draft = EmailDraftFactory(tenant=user.tenant, send_from=email_account) return self.email_draft
def setUpTestData(cls): # Create a user, handled by UserBasedTest. super(EmailMessageTests, cls).setUpTestData() # Create an email account for the user. cls.email_account = EmailAccountFactory.create( owner=cls.user_obj, tenant=cls.user_obj.tenant) # Create a default recipient recipient = Recipient.objects.create(name='Firstname Lastname', email_address='*****@*****.**') # Create a default email message, initially without any labels. cls.email_message = EmailMessage.objects.create( account=cls.email_account, sent_date=datetime.now(), sender=recipient)
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, **kwargs): object_list = super(DraftEmailAttachmentTests, self)._create_object_stub(size, force_to_list=True, **kwargs) account = EmailAccountFactory(owner=self.user_obj, tenant=self.user_obj.tenant) email_draft = EmailDraftFactory.create(send_from=account) for obj in object_list: obj['email_draft'] = email_draft.id if 'somename' not in obj: obj['filename'] = 'theterminator.png' if size == 1: return object_list[0] return object_list
def setUpTestData(cls): super(EmailOutboxMessageTests, cls).setUpTestData() # Create an email account for the user. cls.email_account = EmailAccountFactory.create( owner=cls.user_obj, tenant=cls.user_obj.tenant ) cls.body_content = ( 'In hac habitasse platea dictumst. Class ' 'aptent taciti sociosqu ad litora torquent per conubia nostra' ', per inceptos himenaeos. Ut aliquet elit sed augue bibendum ' 'malesuada.' ) to = '*****@*****.**' kwargs = dict( subject='Mauris ex tortor, hendrerit non sem eu, varius purus.', send_from=cls.email_account, to=anyjson.dumps([to]), cc=anyjson.dumps(None), bcc=anyjson.dumps(None), body='<html><body><br/>{}</body></html>'.format(cls.body_content), headers={}, mapped_attachments=0, template_attachment_ids='', original_message_id='', tenant=cls.user_obj.tenant ) cls.email_outbox_message = EmailOutboxMessage.objects.create(**kwargs) # Some fields are different types in EmailDraft kwargs['to'] = [to] kwargs['cc'] = [] kwargs['bcc'] = [] kwargs['send_from'] = cls.email_account cls.email_draft = EmailDraft.objects.create(**kwargs)
def setupEmailMessage(cls): # Create an email account for the user. cls.email_account = EmailAccountFactory.create(owner=cls.user_obj, tenant=cls.user_obj.tenant) # Create a default recipient sender = Recipient.objects.create( name='Firstname Lastname', email_address='*****@*****.**', ) # Create a default email message, initially without any labels. cls.email_message = EmailMessage.objects.create( subject='Simple Subject', account=cls.email_account, sent_date=timezone.now(), sender=sender, ) received_by = Recipient.objects.create( name='Simple Name', email_address='*****@*****.**', ) cls.email_message.received_by.add(received_by)
def setUp(self): email_account = EmailAccountFactory.create(owner=self.user_obj, tenant=self.user_obj.tenant) self.email_draft = EmailDraftFactory(tenant=self.user_obj.tenant, send_from=email_account)
def setUpTestData(cls): # Create a user, handled by UserBasedTest. super(EmailTests, cls).setUpTestData() # Create an email account for the user. cls.email_account = EmailAccountFactory.create(owner=cls.user_obj, tenant=cls.user_obj.tenant)
def setUp(self): email_account = EmailAccountFactory.create(owner=self.user_obj, tenant=self.user_obj.tenant) self.email_draft = EmailDraftFactory(tenant=self.user_obj.tenant, send_from=email_account)