Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask unit test: send cookies after modifying the session

I'm writing some unit tests for my flask application and I need to simulate a request from a logged in user (I'm using flask login).

I learned here that to do this I need to modify the session and add the user id and a _fresh parameter:

with app.test_client() as c:
    with c.session_transaction() as sess:
        sess['user_id'] = 'myuserid'
        sess['_fresh'] = True
    resp = c.get('/someurl')

My problem is that I need to send some other cookies together with the request. Something like

headers = Headers({'Cookie':'MYCOOKIE=cookie_value;'})
with app.test_client() as c:
    with c.session_transaction() as sess:
        sess['user_id'] = 'myuserid'
        sess['_fresh'] = True
    resp = c.get('/someurl', headers=headers)

but when I perform this request the session "disappears" together with the variables I set.

I think (and someone else on IRC has the same idea) it's because my explicit definition of the cookie header overwrites the one containing the session cookie.

My question is: is there a way to set my cookie without removing the session one?

If not, is there a way to extract the session cookie after I modify the session so that I can add it manually to the list of cookies in the headers object?

like image 634
Giovanni Di Milia Avatar asked Apr 29 '13 15:04

Giovanni Di Milia


People also ask

How to use session in flask?

To use session you must set the secret key first. The session object of the flask package is used to set and get session data. The session object works like a dictionary but it can also keep track modifications. When we use sessions the data is stored in the browser as a cookie. The cookie used to store session data is known session cookie.

What is Python Flask unit testing?

The code those tests cover is likely where the bug is hiding. Python Flask is a framework that makes it easy to create web apps with Python. This guide will use a Flask app as an example and walk you through creating unit tests for it. Even if you don’t use Flask, the unit-testing concepts illustrated are generally applicable.

What is secret_key in flask and how to change it?

The SECRET_KEY is used to encrypt and decrypt session data, and if your secret key changes every time when you start the server, you can’t use the previous SECRET_KEY to decrypt session data. We can set it to a fixed value in Flask configuration file config.py like below.

Where should I store my unit tests in flask?

Based on the flexibility that using unit test runners gives you, you could probably store your unit test files in any location in your Flask application. However, I find it best to store the unit tests (in this case located in the ‘tests’ directory) at the same level as the files that you are going to be testing:


1 Answers

the solution was much easier than I thought.

The test client object has a method set_cookie, so the code should simply be:

with app.test_client() as c:
    with c.session_transaction() as sess:
        sess['user_id'] = 'myuserid'
        sess['_fresh'] = True
    c.set_cookie('localhost', 'MYCOOKIE', 'cookie_value')
    resp = c.get('/someurl')
like image 119
Giovanni Di Milia Avatar answered Oct 08 '22 06:10

Giovanni Di Milia