From e9d0d2ff5b70e1c0b58be512a6cf10e7c11930b3 Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Thu, 22 Feb 2018 16:06:44 -0500 Subject: [PATCH] Tests for build notifications (#3654) * Test for sending build notifications * Fix email subject --- readthedocs/projects/tasks.py | 4 +- .../tests/test_build_notifications.py | 48 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 readthedocs/rtd_tests/tests/test_build_notifications.py diff --git a/readthedocs/projects/tasks.py b/readthedocs/projects/tasks.py index d6165e70cf7..6a5342f3b2c 100644 --- a/readthedocs/projects/tasks.py +++ b/readthedocs/projects/tasks.py @@ -929,9 +929,9 @@ def email_notification(version, build, email): } if build.commit: - title = _('Failed: {project.name} ({commit})').format(commit=build.commit[:8], **context) + title = _('Failed: {project[name]} ({commit})').format(commit=build.commit[:8], **context) else: - title = _('Failed: {project.name} ({version.verbose_name})').format(**context) + title = _('Failed: {project[name]} ({version[verbose_name]})').format(**context) send_email( email, diff --git a/readthedocs/rtd_tests/tests/test_build_notifications.py b/readthedocs/rtd_tests/tests/test_build_notifications.py new file mode 100644 index 00000000000..126badb0497 --- /dev/null +++ b/readthedocs/rtd_tests/tests/test_build_notifications.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +"""Notifications sent after build is completed.""" + +from __future__ import ( + absolute_import, division, print_function, unicode_literals) + +import django_dynamic_fixture as fixture +from django.core import mail +from django.test import TestCase +from mock import patch + +from readthedocs.builds.models import Build, Version +from readthedocs.projects.models import Project, EmailHook, WebHook +from readthedocs.projects.tasks import send_notifications + + +class BuildNotificationsTests(TestCase): + def setUp(self): + self.project = fixture.get(Project) + self.version = fixture.get(Version, project=self.project) + self.build = fixture.get(Build, version=self.version) + + def test_send_notification_none(self): + send_notifications(self.version.pk, self.build.pk) + self.assertEqual(len(mail.outbox), 0) + + def test_send_webhook_notification(self): + fixture.get(WebHook, project=self.project) + with patch('readthedocs.projects.tasks.requests.post') as mock: + mock.return_value = None + send_notifications(self.version.pk, self.build.pk) + mock.assert_called_once() + + self.assertEqual(len(mail.outbox), 0) + + def test_send_email_notification(self): + fixture.get(EmailHook, project=self.project) + send_notifications(self.version.pk, self.build.pk) + self.assertEqual(len(mail.outbox), 1) + + def test_send_email_and_webhook__notification(self): + fixture.get(EmailHook, project=self.project) + fixture.get(WebHook, project=self.project) + with patch('readthedocs.projects.tasks.requests.post') as mock: + mock.return_value = None + send_notifications(self.version.pk, self.build.pk) + mock.assert_called_once() + self.assertEqual(len(mail.outbox), 1)