dennismonsewicz
dennismonsewicz

Reputation: 25542

Flask-Testing: issues with session management with unittests

Here is my route:

@blueprint.before_request
def load_session_from_cookie():
    if request.endpoint != 'client.me':
        try:
            cookie = request.cookies.get(settings.COOKIE_NAME, None)

            # if cookie does not exist, redirect to login url
            if not cookie:
                session.pop('accountId', None)
                return redirect(settings.LOGIN_URL)

            account = check_sso_cookie(cookie)

            if 'accountId' in session:
                return
            elif 'accountId' in account:
                session['accountId'] = account.get('accountId')
                return
            else:
                session.pop('accountId', None)
                return redirect(settings.LOGIN_URL)
        except BadSignature:
            session.pop('accountId', None)
            return redirect(settings.LOGIN_URL)


@blueprint.route('/')
def home():
    session.permanent = True
    return render_template('index.html')

Here is my test:

from flask import Flask
from flask.ext.testing import TestCase
from api.client import blueprint


class TestInitViews(TestCase):

    render_templates = False

    def create_app(self):
        app = Flask(__name__)
        app.config['TESTING'] = True
        app.config['SECRET_KEY'] = 'sekrit!'

        app.register_blueprint(blueprint)

        return app

    def setUp(self):
        self.app = self.create_app()
        self.cookie = '{ "account_id": 100 }'

    def test_root_route(self):
        resp = self.client.get("/")
        self.assert_template_used('index.html')

    def test_root_route_404(self):
        res = self.client.get('/foo')
        self.assertEqual(res.status_code, 404)

The problem is that the test test_root_route fails because a redirect happens because the session doesn't exist. I can't find any good resource online that shows how to incorporate session management with Flask-Tests... anyone have a good way of doing this?

Upvotes: 0

Views: 629

Answers (1)

tbicr
tbicr

Reputation: 26050

You can make login request before:

def create_app(self):
    ...
    app.config['TESTING'] = True  # should off csrf
    ...

def test_root_route(self):
    self.client.post(settings.LOGIN_URL, data={'login': 'l', 'password': 'p'})
    resp = self.client.get('/')
    self.assert_template_used('index.html')

For some difficult cases login route can be mocked.

Or manually set cookie:

def test_root_route(self):
    resp = self.client.get('/', headers={'Cookie': 'accountId=test'})
    self.assert_template_used('index.html')

Or set session with session transaction (http://flask.pocoo.org/docs/testing/#accessing-and-modifying-sessions):

def test_root_route(self):
    with self.client.session_transaction() as session:
        session['accountId'] = 'test'
    resp = self.client.get('/')
    self.assert_template_used('index.html')

Upvotes: 1

Related Questions