我正在试图找出为什么我的Flask测试无法正常工作。我正在测试一个视图函数'/ register',当我在localhost上运行该站点时,它成功重定向到仪表板。
我对此行为的测试失败,而是反馈200响应。
Traceback (most recent call last):
File "/Users/casey/python/storm/project/tests/test_views.py", line 30, in test_register_user
self.assertEqual(resp.status_code, 302)
AssertionError: 200 != 302
我的测试和查看代码如下:
# tests/helpers.py
from unittest import TestCase
from views import app
class PhotogTestCase(TestCase):
def setUp(self):
self.client = app.test_client()
self.client.testing = True
app.config['WTF_CSRF_ENABLED'] = False
def tearDown(self):
pass
# tests/test_views.py
from helpers import PhotogTestCase
class TestRegister(PhotogTestCase):
"""Test our registration view."""
def test_register_user(self):
# Ensure page loads with correct text
resp = self.client.get('/register')
assert 'Register for an Account' in resp.data
# Ensure that valid fields result in success.
resp = self.client.post('/register', {
'email': 'c@gmail.com',
'password': 'woot1LoveCookies!',
'password_again': 'woot1LoveCookies!'
}, follow_redirects=True)
self.assertEqual(resp.status_code, 302)
# views.py
import uuid
from forms import RegistrationForm
from flask import Flask, redirect, render_template, \
request, url_for, flash, current_app, abort
from flask.ext.stormpath import StormpathManager, login_required, \
groups_required, user, User
from stormpath.error import Error as StormpathError
from flask.ext.login import login_user
@app.route('/register', methods=['GET', 'POST'])
def register():
"""
Register a new user with Stormpath.
"""
form = RegistrationForm()
# If we received a POST request with valid information, we'll continue
# processing.
if form.validate_on_submit():
data = {}
# Attempt to create the user's account on Stormpath.
try:
# email and password
data['email'] = request.form['email']
data['password'] = request.form['password']
# given_name and surname are required fields
data['given_name'] = 'Anonymous'
data['surname'] = 'Anonymous'
# create a tenant ID
tenant_id = str(uuid.uuid4())
data['custom_data'] = {
'tenant_id': tenant_id,
'site_admin': True
}
# Create the user account on Stormpath. If this fails, an
# exception will be raised.
account = User.create(**data)
# create a new stormpath group
directory = stormpath_manager.application.default_account_store_mapping.account_store
tenant_group = directory.groups.create({
'name': tenant_id,
'description': data['email']
})
# assign new user to the newly created group
account.add_group(tenant_group)
account.add_group('site_admin')
# If we're able to successfully create the user's account,
# we'll log the user in (creating a secure session using
# Flask-Login), then redirect the user to the
# STORMPATH_REDIRECT_URL setting.
login_user(account, remember=True)
# redirect to dashboard
redirect_url = app.config['STORMPATH_REDIRECT_URL']
return redirect(redirect_url)
except StormpathError as err:
flash(err.message.get('message'))
return render_template(
'account/register.html',
form=form,
)
答案 0 :(得分:2)
在发出请求时,您设置follow_redirects=True
,因此您自然会看到最终页面而不是中间重定向。改为设置follow_redirects=False
。