forked from kiwitcms/Kiwi
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes kiwitcms#1603 - Adds test for tcms.core.utils.maito
- Loading branch information
Showing
1 changed file
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|