feat(redis): add native JSON session storage for 4.1.0
Build & Publish Package / publish (push) Successful in 49s
Build & Publish Package / publish (push) Successful in 49s
- Add opt-in SESSION_REDIS_JSON storage for asyncio Redis backends. - Check JSON command support at startup and reject JSON mode on Trio. - Read legacy strings and migrate sessions on their next save. - Validate JSON objects before transactional replacement and expiry. - Preserve tagged serialization for datetime, tuple, and other types. - Cover command contracts, error handling, and session round-trips. - Document server requirements, permissions, and migration limits. - Normalize indentation and whitespace across code, tests, and examples. - Bump the package and lockfile to 4.1.0 and update the changelog. - Refresh the locked pycparser dependency to 3.12.
This commit is contained in:
@@ -2,59 +2,59 @@ import pytest
|
||||
from itsdangerous import Signer
|
||||
|
||||
def _cookie_value(response, name="session"):
|
||||
header = response.headers.get("Set-Cookie")
|
||||
assert header is not None, "no Set-Cookie header was sent"
|
||||
assert header.startswith(f"{name}=")
|
||||
return header[len(name) + 1:].split(";", 1)[0]
|
||||
header = response.headers.get("Set-Cookie")
|
||||
assert header is not None, "no Set-Cookie header was sent"
|
||||
assert header.startswith(f"{name}=")
|
||||
return header[len(name) + 1:].split(";", 1)[0]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_unsigned_cookie_is_plain_sid(app_factory):
|
||||
"""Regression: str sid used to blow up with AttributeError on .decode()."""
|
||||
app = app_factory(SESSION_USE_SIGNER=False)
|
||||
client = app.test_client()
|
||||
"""Regression: str sid used to blow up with AttributeError on .decode()."""
|
||||
app = app_factory(SESSION_USE_SIGNER=False)
|
||||
client = app.test_client()
|
||||
|
||||
response = await client.get("/")
|
||||
response = await client.get("/")
|
||||
|
||||
assert response.status_code == 200
|
||||
data = await response.get_json()
|
||||
assert data["hits"] == 1
|
||||
assert _cookie_value(response) == data["sid"]
|
||||
assert response.status_code == 200
|
||||
data = await response.get_json()
|
||||
assert data["hits"] == 1
|
||||
assert _cookie_value(response) == data["sid"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_signed_cookie_is_signed_sid(app_factory):
|
||||
app = app_factory(SESSION_USE_SIGNER=True)
|
||||
client = app.test_client()
|
||||
app = app_factory(SESSION_USE_SIGNER=True)
|
||||
client = app.test_client()
|
||||
|
||||
response = await client.get("/")
|
||||
response = await client.get("/")
|
||||
|
||||
assert response.status_code == 200
|
||||
data = await response.get_json()
|
||||
cookie = _cookie_value(response)
|
||||
assert cookie != data["sid"]
|
||||
assert response.status_code == 200
|
||||
data = await response.get_json()
|
||||
cookie = _cookie_value(response)
|
||||
assert cookie != data["sid"]
|
||||
|
||||
signer = Signer(app.secret_key, salt="quart-session",
|
||||
key_derivation="hmac")
|
||||
assert signer.unsign(cookie).decode() == data["sid"]
|
||||
signer = Signer(app.secret_key, salt="quart-session",
|
||||
key_derivation="hmac")
|
||||
assert signer.unsign(cookie).decode() == data["sid"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("use_signer", [False, True])
|
||||
async def test_session_survives_next_request(app_factory, use_signer):
|
||||
app = app_factory(SESSION_USE_SIGNER=use_signer)
|
||||
client = app.test_client()
|
||||
app = app_factory(SESSION_USE_SIGNER=use_signer)
|
||||
client = app.test_client()
|
||||
|
||||
first = await client.get("/")
|
||||
second = await client.get("/")
|
||||
first = await client.get("/")
|
||||
second = await client.get("/")
|
||||
|
||||
assert (await first.get_json())["hits"] == 1
|
||||
assert (await second.get_json())["hits"] == 2
|
||||
assert (await first.get_json())["hits"] == 1
|
||||
assert (await second.get_json())["hits"] == 2
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_signer_without_secret_key_sets_no_cookie(app_factory):
|
||||
app = app_factory(SESSION_USE_SIGNER=True)
|
||||
app.secret_key = None
|
||||
client = app.test_client()
|
||||
app = app_factory(SESSION_USE_SIGNER=True)
|
||||
app.secret_key = None
|
||||
client = app.test_client()
|
||||
|
||||
response = await client.get("/")
|
||||
response = await client.get("/")
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers.get("Set-Cookie") is None
|
||||
assert response.status_code == 200
|
||||
assert response.headers.get("Set-Cookie") is None
|
||||
|
||||
Reference in New Issue
Block a user