Search code examples
python-2.7unit-testingflaskflask-wtformscsrf-protection

Cannot disable WTF CSRF protection during testing


I cannot work out how to disable the CSRF protection while I am conducting my unittesting.

My test_login() function fails the unittest. When I debug I can see that in the Form the csrf_enabled value is set to True

The documentation states that app.config['WTF_CSRF_ENABLED'] = False should be sufficient however that does not appear to be working

Can anyone advise how I can disable CSRF protection?

Thanks

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

Solution

  • I've worked it out.

    I just had a typo in my variable name app & appy

    def create_app(self):
        app = Flask(__name__)
        appy.config['TESTING'] = True
        appy.config['WTF_CSRF_ENABLED'] = False
        return appy