|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Notifications sent after build is completed.""" |
| 3 | + |
| 4 | +from __future__ import ( |
| 5 | + absolute_import, division, print_function, unicode_literals) |
| 6 | + |
| 7 | +import django_dynamic_fixture as fixture |
| 8 | +from django.core import mail |
| 9 | +from django.test import TestCase |
| 10 | +from mock import patch |
| 11 | + |
| 12 | +from readthedocs.builds.models import Build, Version |
| 13 | +from readthedocs.projects.models import Project, EmailHook, WebHook |
| 14 | +from readthedocs.projects.tasks import send_notifications |
| 15 | + |
| 16 | + |
| 17 | +class BuildNotificationsTests(TestCase): |
| 18 | + def setUp(self): |
| 19 | + self.project = fixture.get(Project) |
| 20 | + self.version = fixture.get(Version, project=self.project) |
| 21 | + self.build = fixture.get(Build, version=self.version) |
| 22 | + |
| 23 | + def test_send_notification_none(self): |
| 24 | + send_notifications(self.version.pk, self.build.pk) |
| 25 | + self.assertEqual(len(mail.outbox), 0) |
| 26 | + |
| 27 | + def test_send_webhook_notification(self): |
| 28 | + fixture.get(WebHook, project=self.project) |
| 29 | + with patch('readthedocs.projects.tasks.requests.post') as mock: |
| 30 | + mock.return_value = None |
| 31 | + send_notifications(self.version.pk, self.build.pk) |
| 32 | + mock.assert_called_once() |
| 33 | + |
| 34 | + self.assertEqual(len(mail.outbox), 0) |
| 35 | + |
| 36 | + def test_send_email_notification(self): |
| 37 | + fixture.get(EmailHook, project=self.project) |
| 38 | + send_notifications(self.version.pk, self.build.pk) |
| 39 | + self.assertEqual(len(mail.outbox), 1) |
| 40 | + |
| 41 | + def test_send_email_and_webhook__notification(self): |
| 42 | + fixture.get(EmailHook, project=self.project) |
| 43 | + fixture.get(WebHook, project=self.project) |
| 44 | + with patch('readthedocs.projects.tasks.requests.post') as mock: |
| 45 | + mock.return_value = None |
| 46 | + send_notifications(self.version.pk, self.build.pk) |
| 47 | + mock.assert_called_once() |
| 48 | + self.assertEqual(len(mail.outbox), 1) |
0 commit comments