gpt4 book ai didi

python - 带 Unittest 的 Flask-WTF/WTForms 验证失败,但没有 Unittest 也能工作

转载 作者:太空狗 更新时间:2023-10-29 17:53:06 27 4
gpt4 key购买 nike

当我正常运行应用程序并在浏览器中登录时,它可以正常工作。但是使用 Unittest 它不会让我登录....,它会再次返回登录页面。
“print rv.data”都只是打印登录页面的内容,但它应该打印索引页面的内容,即 login_required
如果有帮助,我正在使用 SQLAlchemy 作为 ORM。任何人都知道什么可能是问题?如果需要更多代码,我会提供。我试图在这里搜索类似的问题,但没有找到。

#!flask/bin/python
import os
import unittest

from config import basedir
from app import app, db
from app.models import User,Exam,Attempt,Subject

class TestCase(unittest.TestCase):
def setUp(self):
app.config['TESTING'] = True
app.config['CSRF_ENABLED'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'test.db')
self.app = app.test_client()
db.create_all()

def tearDown(self):
db.session.remove()
db.drop_all()

def login(self, username, password):
return self.app.post('/login', data=dict(
username=username,
password=password
), follow_redirects=True)

def test_users(self):
u = User(username = 'gaucan', password = 'gau')
db.session.add(u)
db.session.commit()

rv = self.login('gaucan','gau')
print rv.data
rv = self.app.get('/',follow_redirects=True)
print rv.data

if __name__ == '__main__':
unittest.main()

我添加了查看登录功能给你看...

@app.route('/login', methods = ['GET', 'POST'])
def login():
if g.user is not None and g.user.is_authenticated():
return redirect(url_for('index'))

form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username = form.username.data).first()
if user is not None and form.password.data == user.password:
login_user(user)
flash("Logged in successfully.")
return redirect(request.args.get('next') or url_for('index'))

flash('Invalid password')

return render_template('login.html', title='Sign In', form=form)

这里是 form.validate_on_submit 上面一行的回溯


>/home/gaucan/webdev/projekt2/app/views.py(131)login()
-> 如果 form.validate_on_submit():
(PDB)BT
/home/gaucan/webdev/projekt2/shit.py(41)<模块>()
-> 单元测试.main()
/usr/lib64/python2.7/unittest/main.py(95)__init__()
-> self.runTests()
/usr/lib64/python2.7/unittest/main.py(232)runTests()
-> self.result = testRunner.run(self.test)
/usr/lib64/python2.7/unittest/runner.py(151)run()
-> 测试(结果)
/usr/lib64/python2.7/unittest/suite.py(70)__call__()
-> 返回 self.run(*args, **kwds)
/usr/lib64/python2.7/unittest/suite.py(108)run()
-> 测试(结果)
/usr/lib64/python2.7/unittest/suite.py(70)__call__()
-> 返回 self.run(*args, **kwds)
/usr/lib64/python2.7/unittest/suite.py(108)run()
-> 测试(结果)
/usr/lib64/python2.7/unittest/case.py(433)__call__()
-> 返回 self.run(*args, **kwds)
/usr/lib64/python2.7/unittest/case.py(369)run()
-> 测试方法()
/home/gaucan/webdev/projekt2/shit.py(35)test_users()
-> rv = self.login('gaucan','gau')
/home/gaucan/webdev/projekt2/shit.py(28)login()
-> ), follow_redirects=True)
/home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/werkzeug/test.py(771)post()
-> 返回 self.open(*args, **kw)
/home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/flask/testing.py(108)open()
-> follow_redirects=follow_redirects)
/home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/werkzeug/test.py(735)open()
-> response = self.run_wsgi_app(environ, buffered=buffered)
/home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/werkzeug/test.py(658)run_wsgi_app()
-> rv = run_wsgi_app(self.application, environ, buffered=buffered)
/home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/werkzeug/test.py(854)run_wsgi_app()
-> app_iter = app(environ, start_response)
/home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/flask/app.py(1836)__call__()
-> 返回 self.wsgi_app(environ, start_response)
/home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/flask/app.py(1817)wsgi_app()
-> 响应 = self.full_dispatch_request()
/home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/flask/app.py(1475)full_dispatch_request()
-> rv = self.dispatch_request()
/home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/flask/app.py(1461)dispatch_request()
-> 返回 self.view_functions[rule.endpoint](**req.view_args)
>/home/gaucan/webdev/projekt2/app/views.py(131)login()
-> 如果 form.validate_on_submit():
(PDB)

最佳答案

您应该设置 WTF_CSRF_ENABLED 而不是 CSRF_ENABLED - 现在,Flask-WTForms 正在尝试验证您的 CSRF token ,但您没有提供:

class TestCase(unittest.TestCase):
def setUp(self):
app.config['TESTING'] = True
# Wrong key:
# app.config['CSRF_ENABLED'] = False
# Right key:
app.config['WTF_CSRF_ENABLED'] = False

关于python - 带 Unittest 的 Flask-WTF/WTForms 验证失败,但没有 Unittest 也能工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21577481/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com