-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathtest_utils.py
139 lines (117 loc) · 5.69 KB
/
test_utils.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from django.core.files.base import ContentFile
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.test.utils import override_settings
from django_mail_admin.models import OutgoingEmail, STATUS, PRIORITY, EmailTemplate, Attachment, create_attachments, \
send_mail
from django_mail_admin.utils import (parse_emails,
parse_priority, split_emails)
from django_mail_admin.validators import validate_email_with_name, validate_comma_separated_emails
from django_mail_admin.mail import send
@override_settings(EMAIL_BACKEND='django.core.mail.backends.locmem.EmailBackend')
class UtilsTest(TestCase):
def test_mail_status(self):
"""
Check that send_mail assigns the right status field to Email instances
"""
send_mail('subject', 'message', '[email protected]', ['[email protected]'],
priority=PRIORITY.medium)
email = OutgoingEmail.objects.latest('id')
self.assertEqual(email.status, STATUS.queued)
# Emails sent with "now" priority is sent right away
send_mail('subject', 'message', '[email protected]', ['[email protected]'],
priority=PRIORITY.now)
email = OutgoingEmail.objects.latest('id')
self.assertEqual(email.status, STATUS.sent)
def test_email_validator(self):
# These should validate
validate_email_with_name('[email protected]')
validate_email_with_name('Alice Bob <[email protected]>')
OutgoingEmail.objects.create(to=['[email protected]'], from_email='Alice <[email protected]>',
subject='Test', message='Message', status=STATUS.sent)
# Should also support international domains
validate_email_with_name('Alice Bob <[email protected]>')
# These should raise ValidationError
self.assertRaises(ValidationError, validate_email_with_name, 'invalid')
self.assertRaises(ValidationError, validate_email_with_name, 'Al <ab>')
def test_comma_separated_email_list_validator(self):
# These should validate
validate_comma_separated_emails(['[email protected]'])
validate_comma_separated_emails(
)
validate_comma_separated_emails(['Alice Bob <[email protected]>'])
# Should also support international domains
validate_comma_separated_emails(['[email protected]'])
# These should raise ValidationError
self.assertRaises(ValidationError, validate_comma_separated_emails,
['[email protected]', 'invalid_mail', '[email protected]'])
def test_split_emails(self):
"""
Check that split emails correctly divide email lists for multiprocessing
"""
for i in range(225):
OutgoingEmail.objects.create(from_email='[email protected]', to=['[email protected]'])
expected_size = [57, 56, 56, 56]
email_list = split_emails(OutgoingEmail.objects.all(), 4)
self.assertEqual(expected_size, [len(emails) for emails in email_list])
def test_create_attachments(self):
attachments = create_attachments({
'attachment_file1.txt': ContentFile('content'),
'attachment_file2.txt': ContentFile('content'),
})
self.assertEqual(len(attachments), 2)
self.assertIsInstance(attachments[0], Attachment)
self.assertTrue(attachments[0].pk)
self.assertEqual(attachments[0].file.read(), b'content')
self.assertTrue(attachments[0].name.startswith('attachment_file'))
self.assertEquals(attachments[0].mimetype, '')
def test_create_attachments_with_mimetype(self):
attachments = create_attachments({
'attachment_file1.txt': {
'file': ContentFile('content'),
'mimetype': 'text/plain'
},
'attachment_file2.jpg': {
'file': ContentFile('content'),
'mimetype': 'text/plain'
}
})
self.assertEqual(len(attachments), 2)
self.assertIsInstance(attachments[0], Attachment)
self.assertTrue(attachments[0].pk)
self.assertEquals(attachments[0].file.read(), b'content')
self.assertTrue(attachments[0].name.startswith('attachment_file'))
self.assertEquals(attachments[0].mimetype, 'text/plain')
def test_create_attachments_open_file(self):
attachments = create_attachments({
'attachment_file.py': __file__,
})
self.assertEqual(len(attachments), 1)
self.assertIsInstance(attachments[0], Attachment)
self.assertTrue(attachments[0].pk)
self.assertTrue(attachments[0].file.read())
self.assertEquals(attachments[0].name, 'attachment_file.py')
self.assertEquals(attachments[0].mimetype, u'')
def test_parse_priority(self):
self.assertEqual(parse_priority('now'), PRIORITY.now)
self.assertEqual(parse_priority('high'), PRIORITY.high)
self.assertEqual(parse_priority('medium'), PRIORITY.medium)
self.assertEqual(parse_priority('low'), PRIORITY.low)
def test_parse_emails(self):
# Converts a single email to list of email
self.assertEqual(
parse_emails('[email protected]'),
)
# None is converted into an empty list
self.assertEqual(parse_emails(None), [])
# Raises ValidationError if email is invalid
self.assertRaises(
ValidationError,
parse_emails, 'invalid_email'
)
self.assertRaises(
ValidationError,
parse_emails, ['invalid_email', '[email protected]']
)