Skip to content

Commit

Permalink
Tests for build notifications (#3654)
Browse files Browse the repository at this point in the history
* Test for sending build notifications

* Fix email subject
  • Loading branch information
humitos authored and agjohnson committed Feb 22, 2018
1 parent 1b58bed commit e9d0d2f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions readthedocs/projects/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
48 changes: 48 additions & 0 deletions readthedocs/rtd_tests/tests/test_build_notifications.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit e9d0d2f

Please sign in to comment.