mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-11 09:28:27 -04:00
upgrade flask_login to 0.5.0
flask_login sets a bunch of variables in the session object. We only use one of them, `user_id`. We set that to the user id from the database, and refer to it all over the place. However, in flask_login 0.5.0 they prefix this with an underscore to prevent people accidentally overwriting it etc. So when a user logs in we need to make sure that we set user_id manually so we can still use it. flask_login sets a bunch of variables on the `flask.session` object. However, this session object isn't the one that gets passed in to the request context by flask - that one can only be modified outside of requests from within the session_transaction context manager (see [1]). So, flask_login populates the normal session and then we need to copy all of those values across. We didn't need to do this previously because we already set the `user_id` value on line 20 of tests/__init__.py, but now that flask_login is looking for `_user_id` instead we need to do this properly. [1] https://flask.palletsprojects.com/en/1.1.x/testing/#accessing-and-modifying-sessions
This commit is contained in:
@@ -4,6 +4,7 @@ from unittest.mock import patch
|
||||
from urllib.parse import parse_qs, urlparse
|
||||
|
||||
import pytest
|
||||
from flask import session as flask_session
|
||||
from flask import url_for
|
||||
from flask.testing import FlaskClient
|
||||
from flask_login import login_user
|
||||
@@ -27,6 +28,9 @@ class TestClient(FlaskClient):
|
||||
|
||||
with patch('app.events_api_client.create_event'):
|
||||
login_user(model_user)
|
||||
with self.session_transaction() as test_session:
|
||||
for key, value in flask_session.items():
|
||||
test_session[key] = value
|
||||
|
||||
def logout(self, user):
|
||||
self.get(url_for("main.sign_out"))
|
||||
|
||||
Reference in New Issue
Block a user