File: test_email_notifications.py

package info (click to toggle)
python-django-extensions 4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,812 kB
  • sloc: python: 18,601; javascript: 7,354; makefile: 108; xml: 17
file content (81 lines) | stat: -rw-r--r-- 3,200 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
from django.core import mail
from django.core.management import call_command
from django.test import TestCase
from django.test.utils import override_settings
from io import StringIO

from unittest.mock import patch


class EmailNotificationCommandTests(TestCase):
    """Tests for EmailNotificationCommand class."""

    @override_settings(ADMINS=[])
    @patch("sys.stdout", new_callable=StringIO)
    def test_should_print_that_no_email_recipients_available(self, m_stdout):
        with self.assertRaises(Exception):
            call_command(
                "test_email_notification_command", "--email-exception", verbosity=2
            )

        self.assertIn("No email recipients available", m_stdout.getvalue())
        self.assertListEqual(mail.outbox, [])

    @override_settings(
        ADMINS=["foo@bar.com", "bar@foo.com"], DEFAULT_FROM_EMAIL="webmaster@foo.bar"
    )
    def test_should_send_email_with_command_name_and_full_traceback_if_command_fail(
        self,
    ):
        expected_lines = """Reporting execution of command: 'test_email_notification_command'
Traceback:
    raise Exception()"""

        with self.assertRaises(Exception):
            call_command(
                "test_email_notification_command", "--email-exception", verbosity=2
            )

        self.assertIsNot(mail.outbox, [])
        self.assertEqual(
            mail.outbox[0].subject, "Django extensions email notification."
        )
        self.assertEqual(mail.outbox[0].from_email, "webmaster@foo.bar")
        self.assertListEqual(mail.outbox[0].to, ["foo@bar.com", "bar@foo.com"])
        for expected_line in expected_lines.splitlines():
            self.assertIn(expected_line, mail.outbox[0].body)

    @patch("sys.stdout", new_callable=StringIO)
    def test_should_not_notify_if_notification_level_is_not_set(self, m_stdout):
        call_command("runscript", "sample_script", "--email-notifications", verbosity=2)

        self.assertIn("Exiting, not in 'notify always' mode", m_stdout.getvalue())
        self.assertListEqual(mail.outbox, [])

    @override_settings(
        ADMINS=["foo@bar.com"],
        DEFAULT_FROM_EMAIL="webmaster@foo.bar",
        EMAIL_NOTIFICATIONS={
            "tests.testapp.scripts.sample_script": {
                "subject": "my_script subject",
                "body": "my_script body",
                "from_email": "from_email@example.com",
                "recipients": ("recipient0@example.com",),
                "no_admins": False,
                "no_traceback": False,
                "notification_level": 1,
                "fail_silently": False,
            }
        },
    )
    def test_should_notify_if_notification_level_is_greater_than_0(self):
        call_command("runscript", "sample_script", "--email-notifications", verbosity=2)

        self.assertIsNot(mail.outbox, [])
        self.assertEqual(mail.outbox[0].subject, "my_script subject")
        self.assertEqual(mail.outbox[0].body, "my_script body")
        self.assertEqual(mail.outbox[0].from_email, "from_email@example.com")
        self.assertListEqual(
            mail.outbox[0].to, ["recipient0@example.com", "foo@bar.com"]
        )