forked from volker48/flask-mandrill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
113 lines (94 loc) · 3 KB
/
tests.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
import unittest
from mock import patch, MagicMock
from flask_mandrill import Mandrill
import json
# Set up test API key for emails
app = MagicMock()
app.config = {}
app.config['TESTING'] = True
app.config['MANDRILL_API_KEY'] = '12345'
app.config['MANDRILL_DEFAULT_FROM'] = 'from'
headers={'Content-Type': 'application/json'}
class FlaskMandrillTest(unittest.TestCase):
def setUp(self):
self.mail = Mandrill(app)
def test_get_api_key(self):
self.assertEqual(self.mail.api_key, app.config['MANDRILL_API_KEY'])
@patch('flask_mandrill.requests')
def test_base_email(self, mock_requests):
self.mail.send_email(
subject="subject",
text="text",
from_email='from',
to=[{'email': 'a@pop'}]
)
data = {
'async': False,
'ip_pool': '',
'key': app.config['MANDRILL_API_KEY'],
'message': {
'subject': 'subject',
'text': 'text',
'from_email': 'from',
'to': [{'email': 'a@pop'}]
}
}
mock_requests.post.assert_called_with(
self.mail.messages_endpoint,
data=json.dumps(data),
headers=headers
)
@patch('flask_mandrill.requests')
def test_specify_key(self, mock_requests):
mail = Mandrill()
mail.send_email(
key="ABCDEFG",
from_email='from'
)
data = {
'async': False,
'ip_pool': '',
'key': "ABCDEFG",
'message': {'from_email': 'from'}
}
mock_requests.post.assert_called_with(
self.mail.messages_endpoint,
data=json.dumps(data),
headers=headers
)
def test_fails_no_key(self):
mail = Mandrill()
self.assertRaises(ValueError, mail.send_email)
def test_fails_no_sender(self):
mail = Mandrill()
self.assertRaises(ValueError, mail.send_email, key='ABCDEFG')
@patch('flask_mandrill.requests')
def test_default_sender(self, mock_requests):
self.mail.send_email()
data = {
'async': False,
'ip_pool': '',
'key': app.config['MANDRILL_API_KEY'],
'message': {'from_email': 'from'}
}
mock_requests.post.assert_called_with(
self.mail.messages_endpoint,
data=json.dumps(data),
headers=headers
)
@patch('flask_mandrill.requests')
def test_sends_to_template(self, mock_requests):
self.mail.send_email(template_name="a_name")
data = {
'async': False,
'ip_pool': '',
'key': app.config['MANDRILL_API_KEY'],
'template_name': 'a_name',
'template_content': [],
'message': {'from_email': 'from'}
}
mock_requests.post.assert_called_with(
self.mail.templates_endpoint,
data=json.dumps(data),
headers=headers
)