Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flask mutable session (with flask-session RedisSessionInterface) #2337

Closed
decentral1se opened this issue May 26, 2017 · 4 comments
Closed

flask mutable session (with flask-session RedisSessionInterface) #2337

decentral1se opened this issue May 26, 2017 · 4 comments

Comments

@decentral1se
Copy link

I am using the RedisSessionInterface of flask-session to implement my server side sessions. I have a test case that relies on mutating the session and I have read that this is possible from the Flask documentation.

Here's my test code (using pytest):

def test_code_already_used(app, client):
    uri = url_for("login.username")
    response = client.get(uri)  # stores the initial state in the session
    assert response.status_code == 302

    query = urlparse(response.location).query
    payload = dict(parse_qsl(query))
    state = payload['state']  # get key for session state

    with app.test_client() as context:
        transaction = context.session_transaction()
        with transaction as mutable_session:
            mutable_session[state] = {'exchanged': True}  # mutates the session

    # test code later fails becase the modification is not propagated

My application relies on seeing exchanged=True but this change is not there.

Any ideas!?

@davidism davidism reopened this May 27, 2017
@davidism
Copy link
Member

I can't reproduce your issue using the built-in session interface. If it is an issue with Flask-Session, you'll need to report it there.

from flask import Flask, session

app = Flask(__name__)
app.secret_key = 'test'

@app.route('/')
def index():
    return session.get('test', 'none')

with app.test_client() as c:
    with c.session_transaction() as s:
        s['test'] = 'test'

    r = c.get('/')
    print(r.data)  # b'test'

@decentral1se
Copy link
Author

OK, thanks for taking a look @davidism!

@decentral1se
Copy link
Author

decentral1se commented May 29, 2017

I should note, the documentation says:

This works independently of the session backend used

Which, in this case, is not true (part of the reason I reported here). Should this be amended?

@davidism
Copy link
Member

It does work independently. If it doesn't, it's either a problem with how you're using it or a problem with the backend.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants