【发布时间】:2019-04-22 23:25:52
【问题描述】:
我的应用中有以下测试:
@pytest.mark.usefixtures('database')
def test_reset_email(client):
assert current_app.config['TESTING']
with mail.record_messages() as outbox:
response = client.post('/reset', data=dict(email=tconst.ADMIN_EMAIL),
follow_redirects=True)
msg = outbox[-1]
assert const.RESET_PASSWORD_REQUEST_FLASH in str(response.data)
assert msg.subject == const.RESET_EMAIL_SUBJECT
assert 'Reset Password' in msg.html
assert 'Reset Password' in msg.body
pattern = ('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*'
+ '\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+') # noqa W605
url = re.findall(pattern, msg.body)[0]
path = urlparse(url).path
response = client.post(path, data=dict(password='newpass'), follow_redirects=True)
log_in(client, tconst.ADMIN_EMAIL, 'newpass')
assert 'Logout' in str(response.data)
根据文档,如果app.config['TESTING'] 是True,电子邮件将被禁止。测试套件通过了,但实际上发送了电子邮件。
@user_blueprint.route('/reset', methods=['GET', 'POST'])
def reset():
"""Sends a tokenized email link to the user. Fails silently if email doesn't exist."""
form = ResetPasswordForm()
if form.validate_on_submit():
email = form.email.data
user = User.select_by_email(email=email).first()
if user:
timed_serializer = URLSafeTimedSerializer(current_app.config['SECRET_KEY'])
token = timed_serializer.dumps(email, salt='recovery-token')
url = url_for('user.reset_with_token', token=token, _external=True)
body = render_template('email/recover.txt', url=url)
html = render_template('email/recover.html', url=url)
msg = Message(body=body, html=html, recipients=[email],
subject=const.RESET_EMAIL_SUBJECT)
mail.send(msg)
flash(const.RESET_PASSWORD_REQUEST_FLASH, 'success')
return redirect(url_for('user.login'))
return render_template('user/reset.html', form=form)
我还尝试将应用程序的配置的MAIL_SUPPRESS_SEND 设置为True(以及False,这使assert 失败)。
我做错了什么?
【问题讨论】:
标签: testing flask flask-mail