File: test_get_session.py

package info (click to toggle)
python-aiohttp-session 2.12.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 496 kB
  • sloc: python: 2,534; makefile: 197
file content (91 lines) | stat: -rw-r--r-- 2,425 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import pytest
from aiohttp import web
from aiohttp.test_utils import make_mocked_request

from aiohttp_session import (
    SESSION_KEY,
    STORAGE_KEY,
    AbstractStorage,
    Session,
    get_session,
    new_session,
)


async def test_get_stored_session() -> None:
    req = make_mocked_request("GET", "/")
    session = Session("identity", data=None, new=False)
    req[SESSION_KEY] = session

    ret = await get_session(req)
    assert session is ret


async def test_session_is_not_stored() -> None:
    req = make_mocked_request("GET", "/")

    with pytest.raises(RuntimeError):
        await get_session(req)


async def test_storage_returns_not_session_on_load_session() -> None:
    req = make_mocked_request("GET", "/")

    class Storage:
        async def load_session(self, request: web.Request) -> None:
            return None

    req[STORAGE_KEY] = Storage()

    with pytest.raises(RuntimeError):
        await get_session(req)


async def test_get_new_session() -> None:
    req = make_mocked_request("GET", "/")
    session = Session("identity", data=None, new=False)

    class Storage(AbstractStorage):
        async def load_session(self, request: web.Request) -> Session:  # type: ignore[empty-body]
            """Dummy"""

        async def save_session(
            self, request: web.Request, response: web.StreamResponse, session: Session
        ) -> None:
            """Dummy"""

    req[SESSION_KEY] = session
    req[STORAGE_KEY] = Storage()

    ret = await new_session(req)
    assert ret is not session


async def test_get_new_session_no_storage() -> None:
    req = make_mocked_request("GET", "/")
    session = Session("identity", data=None, new=False)
    req[SESSION_KEY] = session

    with pytest.raises(RuntimeError):
        await new_session(req)


async def test_get_new_session_bad_return() -> None:
    req = make_mocked_request("GET", "/")

    class Storage(AbstractStorage):
        async def new_session(self) -> Session:
            return ""  # type: ignore[return-value]

        async def load_session(self, request: web.Request) -> Session:  # type: ignore[empty-body]
            """Dummy"""

        async def save_session(
            self, request: web.Request, response: web.StreamResponse, session: Session
        ) -> None:
            """Dummy"""

    req[STORAGE_KEY] = Storage()

    with pytest.raises(RuntimeError):
        await new_session(req)