Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-WTF / WTForms with Unittest fails validation, but works without Unittest

Tags:

python

flask

When I run app normally and do login in browser, it works. But with Unittest it won't log me in .... , it returns login page again.
Both "print rv.data" just prints content of login page, but it should print content of index page, which is login_required
if it helps, I'm using SQLAlchemy as ORM. Anyone knows what can be problem? If any more code is needed, I will provide. I ve tried to search for similar question here , but didnt find.

#!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()

I added view login function for you to see...

@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)

here is backtrace from line above form.validate_on_submit

> /home/gaucan/webdev/projekt2/app/views.py(131)login()
-> if form.validate_on_submit():
(Pdb) bt
  /home/gaucan/webdev/projekt2/shit.py(41)<module>()
-> unittest.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()
-> test(result)
  /usr/lib64/python2.7/unittest/suite.py(70)__call__()
-> return self.run(*args, **kwds)
  /usr/lib64/python2.7/unittest/suite.py(108)run()
-> test(result)
  /usr/lib64/python2.7/unittest/suite.py(70)__call__()
-> return self.run(*args, **kwds)
  /usr/lib64/python2.7/unittest/suite.py(108)run()
-> test(result)
  /usr/lib64/python2.7/unittest/case.py(433)__call__()
-> return self.run(*args, **kwds)
  /usr/lib64/python2.7/unittest/case.py(369)run()
-> testMethod()
  /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()
-> return 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__()
-> return self.wsgi_app(environ, start_response)
  /home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/flask/app.py(1817)wsgi_app()
-> response = 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()
-> return self.view_functions[rule.endpoint](**req.view_args)
> /home/gaucan/webdev/projekt2/app/views.py(131)login()
-> if form.validate_on_submit():
(Pdb) 
like image 507
user3263155 Avatar asked Feb 05 '14 12:02

user3263155


1 Answers

You should set WTF_CSRF_ENABLED not CSRF_ENABLED - right now, Flask-WTForms is trying to validate your CSRF token, but you are not providing one:

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
like image 196
Sean Vieira Avatar answered Sep 23 '22 11:09

Sean Vieira