Skip to content

Tests for build notifications #3654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)