def setUp(self): self.api_prefix = 'v1' self.organization = OrganizationFactory() self.admin = UserFactory(groups=['administrators'], organization=self.organization) self.manager = UserFactory(groups=['managers'], organization=self.organization) self.user = UserFactory(groups=['users'], organization=self.organization) self.client = APIClient() self.client.login(identifier=self.admin.identifier, password='******')
def test_should_optout(self): user = UserFactory() def mk_message(i): message = MessageFactory(author=user) mail = MailFactory(recipient='*****@*****.**', message=message) MailStatusFactory(status=MailStatus.QUEUED, raw_msg='queued', mail=mail, status_code='5.1.1') MailStatusFactory(status=MailStatus.SENDING, raw_msg='sent', mail=mail, status_code='5.1.1') return MailStatusFactory(status=MailStatus.BOUNCED, raw_msg='Hardbounced', mail=mail, status_code='5.1.1') b1 = mk_message(1) self.assertFalse(b1.should_optout()) b2 = mk_message(2) self.assertFalse(b2.should_optout()) b3 = mk_message(3) self.assertTrue(b3.should_optout())
def setUp(self): self.smtp_application = SmtpApplicationFactory() self.domain = SendingDomainFactory( name='example.com', organization=self.smtp_application.author.organization) self.user = UserFactory(identifier='*****@*****.**', last_login=timezone.now())
def setUp(self): self.admin = UserFactory( is_admin=True, is_active=True, is_superuser=True) self.admin.organization.can_attach_files = False self.admin.organization.can_external_optout = False self.admin.organization.save() self.organization = self.admin.organization
def test_form_validation(self): user = UserFactory() message = MessageFactory(author=user) mail = MailFactory(message=message) f = AbuseNotificationForm({ 'mail': mail.identifier, 'comments': ''}) self.assertFalse(f.is_valid())
def setUp(self): self.api_prefix = 'v1' self.user = UserFactory(groups=['managers']) self.client = APIClient() self.client.login(identifier=self.user.identifier, password='******') self.html = '<html>{}</html>'.format( settings.OPTOUTS['UNSUBSCRIBE_PLACEHOLDER'])
def test_abuse_url(self): user = UserFactory() message = MessageFactory(author=user) mail = MailFactory(message=message) self.assertEqual( mail.abuse_url, 'http://test.munch.example.com/abuse/report/{}/'.format( mail.identifier))
def test_mail_resolution(self): user = UserFactory() message = MessageFactory(author=user) mail = MailFactory(message=message) f = AbuseNotificationForm({ 'mail': mail.identifier, 'comments': 'test'}) f.is_valid() f.save()
def setUp(self): settings.DOMAINS['DKIM_KEY_ID'] = 'test' self.user_1 = UserFactory() self.user_2 = UserFactory() self.resolver = mock_resolver(mock_queries) with fake_time('2099-10-10 06:00:00'): with patch('dns.resolver.query', self.resolver): SendingDomainFactory(organization=self.user_1.organization, name=self.ok_domain) SendingDomainFactory(organization=self.user_1.organization, name=self.bad_domain) SendingDomainFactory(organization=self.user_1.organization, name=self.inexistant_domain) SendingDomainFactory(organization=self.user_2.organization, name='example.com') SendingDomainFactory(organization=self.user_2.organization, name='gentle.example.com')
def test_cannot_delete_not_owned_category(self): user = UserFactory(groups=['administrators']) category = CategoryFactory(author=user) self.assertEqual(Category.objects.count(), 1) resp = self.client.delete('/{}/categories/{}/'.format( self.api_prefix, category.pk)) self.assertEqual(resp.status_code, 403) self.assertEqual(Category.objects.count(), 1)
def test_abuse_makes_optout(self): """ An abuse should create an optout for the given mail """ user = UserFactory() message = MessageFactory(author=user) mail = MailFactory(message=message) response = self.client.post( 'http://test.munch.example.com/abuse/report/{}/'.format( mail.identifier), {'mail': mail.identifier, 'comments': 'test'}) self.assertEqual(response.status_code, 302) optout = OptOut.objects.get(identifier=mail.identifier) self.assertEqual(optout.origin, OptOut.BY_ABUSE)
def setUp(self): self.api_prefix = 'v1' self.user = UserFactory(groups=['managers']) self.client = APIClient() self.client.login(identifier=self.user.identifier, password='******') self.html = '<html>{}</html>'.format( settings.OPTOUTS['UNSUBSCRIBE_PLACEHOLDER']) self.message = MessageFactory(author=self.user) self.message_url = self.client.get('/{}/messages/{}/'.format( self.api_prefix, self.message.id)).data['url']
def setUp(self): self.user = UserFactory() self.message = MessageFactory( author=self.user, track_clicks=True, track_open=False, msg_links={'AAAAABBBBB': 'http://example.com'}, html='<body><a href="http://example.com">Hi</a>{}</body>'.format( settings.OPTOUTS['UNSUBSCRIBE_PLACEHOLDER'])) with patch('munch.apps.spamcheck.SpamChecker.check', side_effect=get_spam_result_mock): self.message.save()
def test_mailmerge(self): user = UserFactory() message = MessageFactory(author=user, html='<h1>Hi {{ first_name }} %s</h1>' % (settings.OPTOUTS['UNSUBSCRIBE_PLACEHOLDER'])) mail = MailFactory(message=message, properties={'first_name': 'John'}) with patch('munch.apps.spamcheck.SpamChecker.check', side_effect=get_spam_result_mock): message.save() content = message.to_mail(mail) self.assertNotIn('{{ first_name }}', content.alternatives[0][0]) self.assertIn('John', content.alternatives[0][0])
def test_valid_unsubscribe(self): user = UserFactory() message = MessageFactory(author=user) mail = MailFactory(message=message) recipient = 'unsubscribe-{}@test.munch.example.com'.format( mail.identifier) unsubscribe = UNSUBSCRIBE_EMAIL.replace('||TO||', recipient) unsubscribe = unsubscribe.replace('||RETURNPATH||', recipient) message = email.message_from_string(unsubscribe) envelope = Envelope() envelope.parse_msg(message) envelope.recipients = [recipient] handler = UnsubscribeHandler(envelope, message) self.assertIsNotNone(handler.apply())
def test_delete_not_owned(self): user = UserFactory(groups=['managers']) message = MessageFactory(author=user) mail = MailFactory(message=message) self.client.login(identifier=user.identifier, password='******') response = self.client.get('/{}/recipients/{}/'.format( self.api_prefix, mail.id)) self.client.login(identifier=self.user.identifier, password='******') response = self.client.delete('/{}/recipients/'.format( self.api_prefix), [response.data['url']], format='json') self.assertEqual(response.status_code, 204) self.assertEqual(Mail.objects.count(), 1)
def test_valid_arf(self): user = UserFactory() message = MessageFactory(author=user) mail = MailFactory(message=message) recipient = 'return-{}@test.munch.example.com'.format( mail.identifier) arf = ARF_REPORT.replace('||TO||', '*****@*****.**') arf = arf.replace('||RETURNPATH||', recipient) message = email.message_from_string(arf) envelope = Envelope() envelope.parse_msg(message) envelope.recipients = [recipient] handler = ARFHandler(envelope, message) self.assertIsNotNone(handler.apply())
def test_several_bounce_imply_optout(self): """ Make an optout after a certain of ammount of bounce is recvd""" user = UserFactory() def mk_message(i): with self.settings(SKIP_SPAM_CHECK=True): message = MessageFactory(author=user) message.status = Message.MSG_OK message.save() message.status = Message.SENDING message.save() mail = MailFactory(message=message, recipient='*****@*****.**') # Simulate it has been passed to infrastructure MailStatusFactory(mail=mail, status=MailStatus.QUEUED, raw_msg='sent by local MTA', creation_date=datetime(2014, 8, 4, tzinfo=utc)) MailStatusFactory(mail=mail, status=MailStatus.SENDING, raw_msg='sent by local MTA', creation_date=datetime(2014, 8, 4, tzinfo=utc)) handle_dsn( 'To: {}'.format(mail.envelope_from), { 'Final-Recipient': '*****@*****.**', 'Diagnostic-Code': self.hardbounce_message, 'Status': '5.1.1', 'Arrival-Date': 'Fri, 5 Aug 2014 23:35:50 +0700 (WIT)' }) optout = OptOut.objects.filter(address='*****@*****.**') mk_message(1) self.assertFalse(optout.exists()) mk_message(2) self.assertFalse(optout.exists()) mk_message(3) self.assertTrue(optout.exists())
def test_get_organization(self): """ Check that all the category app define a get_organization() method """ user = UserFactory() message = MessageFactory(author=user) mail = MailFactory(message=message) optout = OptOutFactory(author=user, category=message.category, identifier=mail.identifier, address=mail.recipient) klasses = [ user.organization, message, mail.statuses.first(), mail, optout ] for obj in klasses: self.assertEqual(obj.get_organization().pk, user.organization.pk)
def setUp(self): self.user = UserFactory() self.message = MessageFactory(status='message_ok', author=self.user) mail = MailFactory(message=self.message) MailStatusFactory(mail=mail) MailStatusFactory(mail=mail, status='sending') MailStatusFactory(mail=mail, status='delivered') mail = MailFactory(message=self.message) MailStatusFactory(mail=mail) MailStatusFactory(mail=mail, status='sending') MailStatusFactory(mail=mail, status='bounced') mail = MailFactory(message=self.message) MailStatusFactory(mail=mail) MailStatusFactory(mail=mail, status='sending') MailStatusFactory(mail=mail, status='dropped', raw_msg='greylisted') MailStatusFactory(mail=mail, status='dropped', raw_msg='greylisted')
def test_admin_add_non_owned_organization(self): user = UserFactory(groups=['administrators']) self.client.login(identifier=user.identifier, password='******') parent_url = self.client.get('/{}/me/'.format( self.api_prefix)).data.get('organization') data = { 'name': 'another-example.com', 'alt_organizations': [parent_url] } self.client.login(identifier=self.admin.identifier, password='******') response = self.client.post('/{}/domains/'.format(self.api_prefix), data, format='json') self.assertEqual(400, response.status_code) self.assertEqual(len(data['alt_organizations']), 1) self.assertEqual(SendingDomain.objects.count(), 0)
def setUp(self): cache.clear() # To start testing DSN status, we have first to simulate that the mail # has been passed to infrastructure. self.user = UserFactory() self.message = MessageFactory(author=self.user) self.mail_1 = MailFactory(message=self.message) self.mail_2 = MailFactory(message=self.message) details = ((self.mail_1, MailStatus.QUEUED, datetime(2014, 8, 5, 22, tzinfo=utc) + timedelta(minutes=5)), (self.mail_1, MailStatus.SENDING, datetime(2014, 8, 6, 22, tzinfo=utc)), (self.mail_2, MailStatus.QUEUED, datetime(2014, 8, 5, 22, tzinfo=utc) + timedelta(minutes=5)), (self.mail_2, MailStatus.SENDING, datetime(2014, 8, 6, 22, tzinfo=utc))) for mail, status, date in details: MailStatusFactory(mail=mail, status=status, creation_date=date, raw_msg=status) self.hardbounce_message = ( "smtp; 550 5.1.1 <*****@*****.**>: Recipient address" "rejected: User unknown in virtual mailbox table")
def test_to_mail_html_does_not_include_view_in_browser(self): user = UserFactory() message = MessageFactory(author=user, html='test UNSUBSCRIBE_URL') mail = MailFactory(message=message) self.assertNotIn('/archive/', mail.as_message().body)
def setUp(self): self.user = UserFactory() self.message = MessageFactory(author=self.user) settings.CAMPAIGNS['SKIP_SPAM_CHECK'] = True
def setUp(self): self.user = UserFactory() self.message = MessageFactory(author=self.user) self.mail = MailFactory(message=self.message) MailStatusFactory(mail=self.mail, status=MailStatus.QUEUED) MailStatusFactory(mail=self.mail, status=MailStatus.SENDING)
def setUp(self): self.user = UserFactory() self.message = MessageFactory(author=self.user)
def setUp(self): self.client = Client() self.user = UserFactory()
def setUp(self): self.user = UserFactory()
def setUp(self): self.user = UserFactory() # Remove subscription email mail.outbox = [] self.client = APIClient() self.client.login(identifier=self.user.identifier, password='******')
def setUp(self): self.user = UserFactory() self.user.organization.contact_email = 'foo@bar' self.user.organization.settings.save() self.message = MessageFactory(author=self.user) django_mail.outbox = []