示例#1
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)
示例#2
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[:]