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)
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)
def test_sanitize_old_emails(self): sanitize_txt = '<redacted>' # Create two test emails msg1 = IncomingMessageFactory.create() msg2 = IncomingMessageFactory.create() self.assertNotEqual(msg1.subject, sanitize_txt) self.assertNotEqual(msg1.body_html, sanitize_txt) self.assertNotEqual(msg1.body_text, sanitize_txt) self.assertNotEqual(msg2.subject, sanitize_txt) self.assertNotEqual(msg2.body_html, sanitize_txt) self.assertNotEqual(msg2.body_text, sanitize_txt) # Messages are not old enough so nothing should happen sanitize_old_emails.delay() msg1.refresh_from_db() msg2.refresh_from_db() self.assertNotEqual(msg1.subject, sanitize_txt) self.assertNotEqual(msg1.body_html, sanitize_txt) self.assertNotEqual(msg1.body_text, sanitize_txt) self.assertNotEqual(msg2.subject, sanitize_txt) self.assertNotEqual(msg2.body_html, sanitize_txt) self.assertNotEqual(msg2.body_text, sanitize_txt) # Make msg1 old enough so it should get sanitized msg1.created_at -= datetime.timedelta(days=7) msg1.save() sanitize_old_emails.delay() msg1.refresh_from_db() msg2.refresh_from_db() self.assertEqual(msg1.subject, sanitize_txt) self.assertEqual(msg1.body_html, sanitize_txt) self.assertEqual(msg1.body_text, sanitize_txt) self.assertNotEqual(msg2.subject, sanitize_txt) self.assertNotEqual(msg2.body_html, sanitize_txt) self.assertNotEqual(msg2.body_text, sanitize_txt)
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, {})
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[:]