Ejemplo n.º 1
0
    def test_parse_recognized_complex_html(self):
        RepositoryFactory.create(email_slug='test', login='******', name='Hello-World')
        resp = self.client.generic('POST',
            reverse('fb_emails:sendgrid-webhook-parse'),
            open(os.path.join(os.path.dirname(__file__), 'data', 'sendgrid-parse-from-sentry.txt'), 'rb').read(),
            content_type='multipart/form-data; boundary=xYzZY',
        )
        self.assertEqual(resp.status_code, 200)

        msg = IncomingMessage.objects.get()
        self.assertEqual(msg.attachment_set.count(), 0)
        self.assertEqual(msg.issue.issue_number, 1347)
        self.assertEqual(msg.status, IncomingMessage.Status.Processed)
        self.assertIsNotNone(msg.processed_at)
        self.assertTrue('Sent by {} ({})'.format(msg.from_name, msg.from_email) in msg.issue.body)

        test_text = """
\---------- Forwarded message ----------
From: **Sentry** <[[email protected]](mailto:[email protected])>
Date: Sat, Feb 18, 2017 at 8:54 PM
Subject: [FundersClub Production] error: ReferenceError: clearOverlappingSelection is not defined
To: [[email protected]](mailto:[email protected])
__
New alert from FundersClub Production.
[View on Sentry](https://sentry.io/)
#  [![Sentry](https://a0wx592cvgzripj.global.ssl.fastly.net/_static/e445fa38a26eeda40957d45e83ffe5b9/sentry/images/email/sentry_logo_full.png)](https://sentry.io)
---
##  New alert from [FundersClub Production](https://sentry.io/)
ID: b162081f6c7a4d29aa36050a275751f1
"""
        body = '\n'.join(
            l.strip() for l in msg.issue.body.splitlines() if l.strip()
        )
        self.assertTrue(test_text in body)
Ejemplo n.º 2
0
    def setUp(self):
        self.user1 = get_user_model().objects.create_user(username='******')
        self.repo1 = RepositoryFactory.create()
        self.repo1.admins.add(self.user1)
        self.repo1.emailmap_set.create(
            email='*****@*****.**',
            login='******',
        )

        self.user2 = get_user_model().objects.create_user(username='******')
        self.repo2 = RepositoryFactory.create()
        self.repo2.admins.add(self.user2)

        self.user3 = get_user_model().objects.create_user(username='******')
Ejemplo n.º 3
0
    def test_issue_create_error(self):
        # Hack to replace the issue creation API with one that fails
        self.requests_mock._adapter._matchers = [
            m for m in self.requests_mock._adapter._matchers
            if m._url != 'https://api.github.com/repos/firebot-test/Hello-World/issues'
        ]

        self.requests_mock.register_uri(
            'POST',
            'https://api.github.com/repos/firebot-test/Hello-World/issues',
            text='{}',
            status_code=404,
        )

        # Repo we'll be testing against (IncomingMessageFactory defaults to fake@)
        repo = RepositoryFactory.create(email_slug='fake', login='******', name='Hello-World')

        # Ensure we have an admin on this rpeo
        repo.admins.add(UserFactory.create())

        # Create and process message
        msg = IncomingMessageFactory.create(from_email='*****@*****.**')
        process_incoming_message.delay(msg.id)

        self.assertTrue(msg.from_email in mail.outbox[0].body)
        self.assertTrue(repo.email in mail.outbox[0].body)
Ejemplo n.º 4
0
    def test_parse_recognized_attachments(self):
        RepositoryFactory.create(email_slug='test', login='******', name='Hello-World')
        resp = self.client.generic('POST',
            reverse('fb_emails:sendgrid-webhook-parse'),
            open(os.path.join(os.path.dirname(__file__), 'data', 'sendgrid-parse-with-attachment.txt'), 'rb').read(),
            content_type='multipart/form-data; boundary=xYzZY',
        )
        self.assertEqual(resp.status_code, 200)

        msg = IncomingMessage.objects.get()
        self.assertEqual(msg.attachment_set.count(), 2)
        self.assertEqual(msg.issue.issue_number, 1347)
        self.assertEqual(msg.status, IncomingMessage.Status.Processed)
        self.assertIsNotNone(msg.processed_at)

        for attachment in msg.attachment_set.all():
            self.assertTrue(attachment.file.url in msg.issue.body)
Ejemplo n.º 5
0
    def test_text(self):
        repo = RepositoryFactory.create(email_slug='test', login='******', name='Hello-World')

        # Test an issue from an unrecognized email address
        msg = IncomingMessageFactory.create()
        issue = repo.create_issue_from_incoming_msg(msg)
        self.assertTrue(msg.body_text in issue.body)
        self.assertTrue('Sent by {} ({})'.format(msg.from_name, msg.from_email) in issue.body)

        # Add email to map and try again
        msg = IncomingMessageFactory.create()
        repo.emailmap_set.create(email=msg.from_email, login='******')
        issue = repo.create_issue_from_incoming_msg(msg)
        self.assertTrue(msg.body_text in issue.body)
        self.assertTrue('Sent by @lenny ({})'.format(msg.from_email) in issue.body)
Ejemplo n.º 6
0
    def test_sanitize_old_issues(self):
        repo = RepositoryFactory.create(email_slug='test',
                                        login='******',
                                        name='Hello-World')
        sanitize_txt = '<redacted>'

        msg1 = IncomingMessageFactory.create()
        issue1 = repo.create_issue_from_incoming_msg(msg1)

        msg2 = IncomingMessageFactory.create()
        issue2 = repo.create_issue_from_incoming_msg(msg2)

        self.assertNotEqual(issue1.body, sanitize_txt)
        self.assertNotEqual(issue1.gh_data, {})

        self.assertNotEqual(issue2.body, sanitize_txt)
        self.assertNotEqual(issue2.gh_data, {})

        # Run task, nothing interesting should happen
        sanitize_old_issues.delay()
        issue1.refresh_from_db()
        issue2.refresh_from_db()

        self.assertNotEqual(issue1.body, sanitize_txt)
        self.assertNotEqual(issue1.gh_data, {})

        self.assertNotEqual(issue2.body, sanitize_txt)
        self.assertNotEqual(issue2.gh_data, {})

        # Make issue1 old enough
        issue1.created_at -= datetime.timedelta(days=7)
        issue1.save()

        sanitize_old_issues.delay()
        issue1.refresh_from_db()
        issue2.refresh_from_db()

        self.assertEqual(issue1.body, sanitize_txt)
        self.assertEqual(issue1.gh_data, {})

        self.assertNotEqual(issue2.body, sanitize_txt)
        self.assertNotEqual(issue2.gh_data, {})
Ejemplo n.º 7
0
    def test_association_email(self):
        # Repo we'll be testing against (IncomingMessageFactory defaults to fake@)
        repo = RepositoryFactory.create(email_slug='fake', login='******', name='Hello-World')

        # This repo shouldn't affect the association because the slug is different
        repo2 = RepositoryFactory.create()
        repo2.emailmap_set.create(email='*****@*****.**', login='******')

        # First time we send from this address should trigger an email
        msg = IncomingMessageFactory.create(from_email='*****@*****.**')
        process_incoming_message.delay(msg.id)

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, 'Re: ' + msg.subject)
        self.assertTrue('Congratulations! You just created your first issue using fire.' in mail.outbox[0].body)

        expected_url = settings.BASE_URL + '/associate-email/{}/'.format(msg.uuid)
        self.assertTrue(expected_url in mail.outbox[0].body)
        del mail.outbox[:]

        # Send another message, we shouldn't receive an email this time unless it's from a new address
        msg = IncomingMessageFactory.create(from_email='*****@*****.**')
        process_incoming_message.delay(msg.id)

        self.assertEqual(len(mail.outbox), 0)

        msg = IncomingMessageFactory.create(from_email='*****@*****.**')
        process_incoming_message.delay(msg.id)

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, 'Re: ' + msg.subject)
        self.assertTrue('Congratulations! You just created your first issue using fire.' in mail.outbox[0].body)

        expected_url = settings.BASE_URL + '/associate-email/{}/'.format(msg.uuid)
        self.assertTrue(expected_url in mail.outbox[0].body)
        del mail.outbox[:]

        # Send a message from an address that is mapped on this repo
        map_entry = repo.emailmap_set.create(email='*****@*****.**', login='******')

        msg = IncomingMessageFactory.create(from_email='*****@*****.**')
        process_incoming_message.delay(msg.id)

        self.assertEqual(len(mail.outbox), 0)

        # Delete the map entry, still no emails should go because we already got an email from this user
        map_entry.delete()

        msg = IncomingMessageFactory.create(from_email='*****@*****.**')
        process_incoming_message.delay(msg.id)

        self.assertEqual(len(mail.outbox), 0)

        # Send an email to the other repo, this should trigger an email
        repo2_email = '{}@{}'.format(repo2.email_slug, settings.EMAIL_DOMAIN)
        msg = IncomingMessageFactory.create(
            from_email='*****@*****.**',
            to_email=repo2_email,
        )
        process_incoming_message.delay(msg.id)

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, 'Re: ' + msg.subject)
        self.assertTrue('Congratulations! You just created your first issue using fire.' in mail.outbox[0].body)

        expected_url = settings.BASE_URL + '/associate-email/{}/'.format(msg.uuid)
        self.assertTrue(expected_url in mail.outbox[0].body)
        del mail.outbox[:]

        # Deleting all emails from this address and sending again should trigger the email
        IncomingMessage.objects.filter(from_email='*****@*****.**').delete()

        msg = IncomingMessageFactory.create(from_email='*****@*****.**')
        process_incoming_message.delay(msg.id)

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, 'Re: ' + msg.subject)
        self.assertTrue('Congratulations! You just created your first issue using fire.' in mail.outbox[0].body)

        expected_url = settings.BASE_URL + '/associate-email/{}/'.format(msg.uuid)
        self.assertTrue(expected_url in mail.outbox[0].body)
        del mail.outbox[:]