| 1 |
import os |
| 2 |
import shutil |
| 3 |
from contextlib import contextmanager |
| 4 |
|
| 5 |
import flask |
| 6 |
from cachelib.file import FileSystemCache |
| 7 |
from flask_session.cachelib import CacheLibSession |
| 8 |
|
| 9 |
|
| 10 |
class TestCachelibSession: |
| 11 |
session_dir = "testing_session_storage" |
| 12 |
|
| 13 |
@contextmanager |
| 14 |
def setup_filesystem(self): |
| 15 |
try: |
| 16 |
yield |
| 17 |
finally: |
| 18 |
pass |
| 19 |
if self.session_dir and os.path.isdir(self.session_dir): |
| 20 |
shutil.rmtree(self.session_dir) |
| 21 |
|
| 22 |
def retrieve_stored_session(self, key, app): |
| 23 |
return app.session_interface.cache.get(key) |
| 24 |
|
| 25 |
def test_filesystem_default(self, app_utils): |
| 26 |
app = app_utils.create_app( |
| 27 |
{ |
| 28 |
"SESSION_TYPE": "cachelib", |
| 29 |
"SESSION_SERIALIZATION_FORMAT": "json", |
| 30 |
"SESSION_CACHELIB": FileSystemCache( |
| 31 |
threshold=500, cache_dir=self.session_dir |
| 32 |
), |
| 33 |
} |
| 34 |
) |
| 35 |
|
| 36 |
# Should be using CacheLib (FileSystem) |
| 37 |
with self.setup_filesystem(), app.test_request_context(): |
| 38 |
assert isinstance( |
| 39 |
flask.session, |
| 40 |
CacheLibSession, |
| 41 |
) |
| 42 |
app_utils.test_session(app) |
| 43 |
|
| 44 |
# Check if the session is stored in the filesystem |
| 45 |
cookie = app_utils.test_session_with_cookie(app) |
| 46 |
session_id = cookie.split(";")[0].split("=")[1] |
| 47 |
stored_session = self.retrieve_stored_session(f"session:{session_id}", app) |
| 48 |
assert stored_session.get("value") == "44" |