def test_session_equality(values): session = Session(values) assert session == session assert session == Session(values) assert session == values values["x"] = True assert session != Session(values) assert session != values
def test_session_modified(): session = Session() assert session.modified is False session["foo"] = "lorem ipsum" assert session.modified is True session = Session({"foo": "lorem ipsum"}) assert session.modified is False # any set item marks the session as modified, # it doesn't matter if the end values are the same session["foo"] = "lorem ipsum" assert session.modified is True
async def test_session_middleware_handling_of_invalid_encrypted_signature(): app = FakeApplication() app.middlewares.append( SessionMiddleware( "LOREM_IPSUM", encryptor=FernetEncryptor(Fernet.generate_key()) ) ) @app.router.get("/") def home(request: Request): session = request.session assert isinstance(session, Session) assert len(session) == 0 assert "user_id" not in session return text("Hello, World") await app.start() # arrange invalid session cookie impostor_middleware = SessionMiddleware( "LOREM_IPSUM", encryptor=FernetEncryptor(Fernet.generate_key()) ) forged_cookie = impostor_middleware.write_session(Session({"user_id": "hahaha"})) await app( get_example_scope( "GET", "/", [ [b"cookie", b"session=" + forged_cookie.encode()], ], ), MockReceive(), MockSend(), ) response = app.response assert response.status == 200
def test_session_base_methods(): session = Session() assert "foo" not in session session["foo"] = "lorem ipsum" assert "foo" in session assert session["foo"] == "lorem ipsum" del session["foo"] assert "foo" not in session session.set("foo", "lorem ipsum") assert session.get("foo") == "lorem ipsum" assert session.get("ufo", ...) is ... session.update({"a": 1, "b": 2, "c": 3}) assert session["a"] == 1 assert session["b"] == 2 assert session["c"] == 3
async def test_session_middleware_handling_of_invalid_signature( app, mock_receive, mock_send): app.middlewares.append(SessionMiddleware("LOREM_IPSUM")) @app.router.get("/") def home(request: Request): session = request.session assert isinstance(session, Session) assert len(session) == 0 assert "user_id" not in session return text("Hello, World") await app.start() # arrange invalid session cookie impostor_middleware = SessionMiddleware("DOLOR_SIT_AMET") forged_cookie = impostor_middleware.write_session( Session({"user_id": "hahaha"})) await app( get_example_scope( "GET", "/", [ [b"cookie", b"session=" + forged_cookie.encode()], ], ), mock_receive(), mock_send, ) response = app.response assert response.status == 200
def test_session_to_dict(): value = {"a": 1, "b": 2, "c": 3, "d": 4} session = Session(value) assert session.to_dict() == value assert session.to_dict() is not value
def test_session_clear(): session = Session({"a": 1, "b": 2, "c": 3, "d": 4}) session.clear() assert session.modified assert len(session) == 0
def test_session_length(values, expected_len): session = Session(values) assert len(session) == expected_len
def test_session_key_error(): session = Session() with pytest.raises(KeyError): session["foo"]
def test_session_inequality(): session = Session() assert (session == []) is False assert (session == "") is False
session["foo"] = "lorem ipsum" assert session.modified is True def test_session_key_error(): session = Session() with pytest.raises(KeyError): session["foo"] @pytest.mark.parametrize( "value,session", [ ["{}", Session()], ['{"lorem":"ipsum"}', Session({"lorem": "ipsum"})], ['{"lorem":"ipsum ✨"}', Session({"lorem": "ipsum ✨"})], ], ) def test_session_json_serializer(value, session): serializer = JSONSerializer() assert serializer.write(session) == value assert serializer.read(value) == session @pytest.mark.asyncio async def test_session_middleware_basics(): app = FakeApplication()