Skip to content

Commit e9d0d2f

Browse files
humitosagjohnson
authored andcommitted
Tests for build notifications (#3654)
* Test for sending build notifications * Fix email subject
1 parent 1b58bed commit e9d0d2f

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

readthedocs/projects/tasks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -929,9 +929,9 @@ def email_notification(version, build, email):
929929
}
930930

931931
if build.commit:
932-
title = _('Failed: {project.name} ({commit})').format(commit=build.commit[:8], **context)
932+
title = _('Failed: {project[name]} ({commit})').format(commit=build.commit[:8], **context)
933933
else:
934-
title = _('Failed: {project.name} ({version.verbose_name})').format(**context)
934+
title = _('Failed: {project[name]} ({version[verbose_name]})').format(**context)
935935

936936
send_email(
937937
email,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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

Comments
 (0)