Skip to content

Commit 8668bed

Browse files
panagiksasvetlov
authored andcommitted
Replace assert with proper check & add tests (#235)
1 parent ced49ad commit 8668bed

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

aiohttp_session/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ async def get_session(request):
114114

115115
def session_middleware(storage):
116116

117-
assert isinstance(storage, AbstractStorage), storage
117+
if not isinstance(storage, AbstractStorage):
118+
raise RuntimeError("Expected AbstractStorage got {}".format(storage))
118119

119120
@web.middleware
120121
async def factory(request, handler):

tests/test_abstract_storage.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import time
44

55
from aiohttp import web
6-
from aiohttp_session import (session_middleware,
7-
get_session, SimpleCookieStorage)
6+
from aiohttp_session import get_session, SimpleCookieStorage
7+
from aiohttp_session import setup as setup_middleware
88

99

1010
def make_cookie(client, data):
@@ -18,8 +18,8 @@ def make_cookie(client, data):
1818

1919

2020
def create_app(loop, handler):
21-
middleware = session_middleware(SimpleCookieStorage(max_age=10))
22-
app = web.Application(middlewares=[middleware], loop=loop)
21+
app = web.Application(loop=loop)
22+
setup_middleware(app, SimpleCookieStorage(max_age=10))
2323
app.router.add_route('GET', '/', handler)
2424
return app
2525

tests/test_get_session.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from aiohttp.test_utils import make_mocked_request
44

5-
from aiohttp_session import Session, get_session, SESSION_KEY
5+
from aiohttp_session import Session, get_session, SESSION_KEY, STORAGE_KEY
66

77

88
async def test_get_stored_session():
@@ -19,3 +19,16 @@ async def test_session_is_not_stored():
1919

2020
with pytest.raises(RuntimeError):
2121
await get_session(req)
22+
23+
24+
async def test_storage_returns_not_session_on_load_session():
25+
req = make_mocked_request('GET', '/')
26+
27+
class Storage():
28+
async def load_session(self, request):
29+
return None
30+
31+
req[STORAGE_KEY] = Storage()
32+
33+
with pytest.raises(RuntimeError):
34+
await get_session(req)

tests/test_session_middleware.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import pytest
2+
3+
from aiohttp_session import session_middleware
4+
5+
6+
async def test_session_middleware_bad_storage():
7+
with pytest.raises(RuntimeError):
8+
session_middleware(None)

0 commit comments

Comments
 (0)