1from typing import TYPE_CHECKING
2
3from plain.runtime import settings
4
5from .core import SessionStore
6
7if TYPE_CHECKING:
8 from plain.testing import Client
9
10__all__ = ["get_client_session"]
11
12
13def get_client_session(client: Client) -> SessionStore:
14 """The session a test client's cookie points to.
15
16 A client that has no session yet gets a new one, and its cookie.
17 """
18 cookie = client.cookies.get(settings.SESSION_COOKIE_NAME)
19 if cookie:
20 return SessionStore(cookie.value)
21 session = SessionStore()
22 session.save()
23 assert session.session_key is not None
24 client.cookies[settings.SESSION_COOKIE_NAME] = session.session_key
25 return session