-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_email.py
171 lines (136 loc) · 6.56 KB
/
test_email.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from tests.helpers import *
from CTFd.utils import get_config, set_config
from CTFd.utils.email import sendmail, verify_email_address, check_email_format
from freezegun import freeze_time
from mock import patch, Mock
from email.mime.text import MIMEText
import requests
def test_check_email_format():
"""Test that the check_email_format() works properly"""
assert check_email_format('[email protected]') is True
assert check_email_format('[email protected]') is True
assert check_email_format('[email protected]') is True
assert check_email_format('[email protected]') is True
assert check_email_format('user.period1234@b') is False
assert check_email_format('no.ampersand') is False
assert check_email_format('user@') is False
assert check_email_format('@ctfd.io') is False
assert check_email_format('user.io@ctfd') is False
assert check_email_format('user\\@ctfd') is False
try:
assert check_email_format(invalid_email) is False
except AssertionError:
print(invalid_email, 'did not pass validation')
@patch('smtplib.SMTP')
def test_sendmail_with_smtp(mock_smtp):
"""Does sendmail work properly with simple SMTP mail servers"""
app = create_ctfd()
with app.app_context():
set_config('mail_server', 'localhost')
set_config('mail_port', 25)
set_config('mail_useauth', True)
set_config('mail_username', 'username')
set_config('mail_password', 'password')
from_addr = get_config('mailfrom_addr') or app.config.get('MAILFROM_ADDR')
to_addr = '[email protected]'
msg = 'this is a test'
sendmail(to_addr, msg)
ctf_name = get_config('ctf_name')
email_msg = MIMEText(msg)
email_msg['Subject'] = "Message from {0}".format(ctf_name)
email_msg['From'] = from_addr
email_msg['To'] = to_addr
mock_smtp.return_value.sendmail.assert_called_once_with(from_addr, [to_addr], email_msg.as_string())
destroy_ctfd(app)
@patch.object(requests, 'post')
def test_sendmail_with_mailgun_from_config_file(fake_post_request):
"""Does sendmail work properly with Mailgun using file configuration"""
app = create_ctfd()
with app.app_context():
app.config['MAILGUN_API_KEY'] = 'key-1234567890-file-config'
app.config['MAILGUN_BASE_URL'] = 'https://api.mailgun.net/v3/file.faked.com'
from_addr = get_config('mailfrom_addr') or app.config.get('MAILFROM_ADDR')
to_addr = '[email protected]'
msg = 'this is a test'
sendmail(to_addr, msg)
ctf_name = get_config('ctf_name')
email_msg = MIMEText(msg)
email_msg['Subject'] = "Message from {0}".format(ctf_name)
email_msg['From'] = from_addr
email_msg['To'] = to_addr
fake_response = Mock()
fake_post_request.return_value = fake_response
fake_response.status_code = 200
status, message = sendmail(to_addr, msg)
args, kwargs = fake_post_request.call_args
assert args[0] == 'https://api.mailgun.net/v3/file.faked.com/messages'
assert kwargs['auth'] == ('api', u'key-1234567890-file-config')
assert kwargs['timeout'] == 1.0
assert kwargs['data'] == {'to': ['[email protected]'], 'text': 'this is a test',
'from': 'CTFd Admin <[email protected]>', 'subject': 'Message from CTFd'}
assert fake_response.status_code == 200
assert status is True
assert message == "Email sent"
destroy_ctfd(app)
@patch.object(requests, 'post')
def test_sendmail_with_mailgun_from_db_config(fake_post_request):
"""Does sendmail work properly with Mailgun using database configuration"""
app = create_ctfd()
with app.app_context():
app.config['MAILGUN_API_KEY'] = 'key-1234567890-file-config'
app.config['MAILGUN_BASE_URL'] = 'https://api.mailgun.net/v3/file.faked.com'
# db values should take precedence over file values
set_config('mailgun_api_key', 'key-1234567890-db-config')
set_config('mailgun_base_url', 'https://api.mailgun.net/v3/db.faked.com')
from_addr = get_config('mailfrom_addr') or app.config.get('MAILFROM_ADDR')
to_addr = '[email protected]'
msg = 'this is a test'
sendmail(to_addr, msg)
ctf_name = get_config('ctf_name')
email_msg = MIMEText(msg)
email_msg['Subject'] = "Message from {0}".format(ctf_name)
email_msg['From'] = from_addr
email_msg['To'] = to_addr
fake_response = Mock()
fake_post_request.return_value = fake_response
fake_response.status_code = 200
status, message = sendmail(to_addr, msg)
args, kwargs = fake_post_request.call_args
assert args[0] == 'https://api.mailgun.net/v3/db.faked.com/messages'
assert kwargs['auth'] == ('api', u'key-1234567890-db-config')
assert kwargs['timeout'] == 1.0
assert kwargs['data'] == {'to': ['[email protected]'], 'text': 'this is a test',
'from': 'CTFd Admin <[email protected]>', 'subject': 'Message from CTFd'}
assert fake_response.status_code == 200
assert status is True
assert message == "Email sent"
destroy_ctfd(app)
@patch('smtplib.SMTP')
@freeze_time("2012-01-14 03:21:34")
def test_verify_email(mock_smtp):
"""Does verify_email send emails"""
app = create_ctfd()
with app.app_context():
set_config('mail_server', 'localhost')
set_config('mail_port', 25)
set_config('mail_useauth', True)
set_config('mail_username', 'username')
set_config('mail_password', 'password')
set_config('verify_emails', True)
from_addr = get_config('mailfrom_addr') or app.config.get('MAILFROM_ADDR')
to_addr = '[email protected]'
verify_email_address(to_addr)
# This is currently not actually validated
msg = ("Please click the following link to confirm"
" your email address for CTFd:"
" http://localhost/confirm/InVzZXJAdXNlci5jb20i.TxD0vg.28dY_Gzqb1TH9nrcE_H7W8YFM-U")
ctf_name = get_config('ctf_name')
email_msg = MIMEText(msg)
email_msg['Subject'] = "Message from {0}".format(ctf_name)
email_msg['From'] = from_addr
email_msg['To'] = to_addr
# Need to freeze time to predict the value of the itsdangerous token.
# For now just assert that sendmail was called.
mock_smtp.return_value.sendmail.assert_called_with(from_addr, [to_addr], email_msg.as_string())
destroy_ctfd(app)