Skip to content

Commit

Permalink
Fixes kiwitcms#1603 - Adds test for tcms.core.utils.maito
Browse files Browse the repository at this point in the history
  • Loading branch information
pandafy committed Nov 16, 2020
1 parent f07a84b commit 6fc1487
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions tcms/core/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import unittest

from django.conf import settings
from django.core.mail import send_mail
from django.template.loader import render_to_string
from unittest.mock import patch

from tcms.core.utils.mailto import mailto


class TestMailTo(unittest.TestCase):
@classmethod
def setUp(self):
self.subject = 'Test Subject'
self.body = 'Body text'
self.sender = '[email protected]'
self.recipients = None

@property
def test_kwargs(self):
return {
'target': send_mail,
'args': (settings.EMAIL_SUBJECT_PREFIX + self.subject,
self.body,
self.sender,
self.recipients),
'kwargs': {
'fail_silently': False
}
}

@patch('threading.Thread')
def test_string_recipient(self, mock):
self.recipients = ['[email protected]']
mailto(template_name=None,
subject=self.subject,
recipients='[email protected]',
context=self.body)
mock.assert_called_once_with(**self.test_kwargs)

@patch('threading.Thread')
def test_duplicate_recipients(self, mock):
self.recipients = ['[email protected]']
mailto(template_name=None,
subject=self.subject,
recipients=self.recipients * 4,
context=self.body)
mock.assert_called_once_with(**self.test_kwargs)

@patch('threading.Thread')
def test_cc_email(self, mock):
self.recipients = ['[email protected]', '[email protected]']
mailto(template_name=None,
subject=self.subject,
recipients='[email protected]',
context=self.body,
cc=['[email protected]'])
mock.assert_called_once_with(**self.test_kwargs)


@patch('django.conf.settings.DEBUG', True)
@patch('django.conf.settings.ADMINS', [('Admin', '[email protected]')])
@patch('threading.Thread')
def test_admin_email_on_debug(self, mock):
self.recipients = ['[email protected]', '[email protected]']
mailto(template_name=None,
subject=self.subject,
recipients='[email protected]',
context=self.body)
mock.assert_called_once_with(**self.test_kwargs)

@patch('threading.Thread')
def test_template(self, mock):
template_name = 'email/user_registered/notify_admins.txt'
context = {
'username': 'username',
'user_url': 'https://example.com/username/',
}
self.body = render_to_string(template_name, context)
self.recipients = ['[email protected]']
mailto(template_name=template_name,
subject=self.subject,
recipients=self.recipients,
context=context)
mock.assert_called_once_with(**self.test_kwargs)

0 comments on commit 6fc1487

Please sign in to comment.