0
我無法弄清楚如何在進行單元測試時禁用CSRF保護。
無法在測試期間禁用WTF CSRF保護
我的test_login()
函數失敗unittest。當我調試,我可以看到,在表格中csrf_enabled
值設置爲True
的文檔指出app.config['WTF_CSRF_ENABLED'] = False
應但是足以不似乎工作
任何人都可以建議我如何可以禁用CSRF保護?
感謝
from flask.ext.testing import TestCase
from flask import Flask
from Shares import db, app as appy
from Shares.models import User
import manage
class test(TestCase):
def create_app(self):
app = Flask(__name__)
app.config['TESTING'] = True
app.config['WTF_CSRF_ENABLED'] = False
return appy
SQLALCHEMY_DATABASE_URI = "sqlite://"
TESTING = True
def setUp(self):
manage.initdb()
print self.login('lucas', 'test').data
def tearDown(self):
db.session.remove()
db.drop_all()
def login(self, username, password):
return self.client.post('/login', data=dict(
username=username,
password=password
), follow_redirects=True)
def logout(self):
return self.client.get('/logout', follow_redirects=True)
def test_adduser(self):
lucas=User(username="lucas", email="[email protected]", password="test")
user2 = User(username="lucas", email="[email protected]")
db.session.add(lucas)
db.session.commit()
assert lucas in db.session
assert user2 not in db.session
def test_login(self):
lucas=User(username="lucas", email="[email protected]", password="test")
db.session.add(lucas)
db.session.commit()
rv = self.login('lucas', 'test')
assert 'Welcome' in rv.data