Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add checking for session expired #297

Merged
merged 2 commits into from
Jun 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions tests/test_memcached_storage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import uuid
import time
import asyncio

from aiohttp import web
from aiohttp_session import Session, session_middleware, get_session
Expand Down Expand Up @@ -227,3 +228,28 @@ async def logout(request):
client.session.cookie_jar.update_cookies({'AIOHTTP_SESSION': evil_cookie})
resp = await client.get('/')
assert resp.cookies['AIOHTTP_SESSION'].value != evil_cookie


async def test_load_session_dont_load_expired_session(aiohttp_client,
memcached):
async def handler(request):
session = await get_session(request)
exp_param = request.rel_url.query.get('exp', None)
if exp_param is None:
session['a'] = 1
session['b'] = 2
else:
assert {} == session

return web.Response(body=b'OK')

client = await aiohttp_client(
create_app(handler, memcached, 2)
)
resp = await client.get('/')
assert resp.status == 200

await asyncio.sleep(5)

resp = await client.get('/?exp=yes')
assert resp.status == 200
30 changes: 28 additions & 2 deletions tests/test_nacl_storage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import time
import asyncio

import pytest
import nacl.secret
Expand Down Expand Up @@ -30,8 +31,8 @@ def make_cookie(client, secretbox, data):
client.session.cookie_jar.update_cookies({'AIOHTTP_SESSION': data})


def create_app(handler, key):
middleware = session_middleware(NaClCookieStorage(key))
def create_app(handler, key, max_age=None):
middleware = session_middleware(NaClCookieStorage(key, max_age=max_age))
app = web.Application(middlewares=[middleware])
app.router.add_route('GET', '/', handler)
return app
Expand Down Expand Up @@ -153,3 +154,28 @@ async def logout(request):
client.session.cookie_jar.update_cookies({'AIOHTTP_SESSION': evil_cookie})
resp = await client.get('/')
assert resp.cookies['AIOHTTP_SESSION'].value != evil_cookie


async def test_load_session_dont_load_expired_session(aiohttp_client,
key):
async def handler(request):
session = await get_session(request)
exp_param = request.rel_url.query.get('exp', None)
if exp_param is None:
session['a'] = 1
session['b'] = 2
else:
assert {} == session

return web.Response(body=b'OK')

client = await aiohttp_client(
create_app(handler, key, 2)
)
resp = await client.get('/')
assert resp.status == 200

await asyncio.sleep(5)

resp = await client.get('/?exp=yes')
assert resp.status == 200
26 changes: 26 additions & 0 deletions tests/test_redis_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
import uuid
import time
import asyncio

from aiohttp import web
from aiohttp_session import Session, session_middleware, get_session
Expand Down Expand Up @@ -310,3 +311,28 @@ def __init__(self, *args, **kwargs):
mocker.patch('aiohttp_session.redis_storage.StrictVersion', Dummy)
with pytest.raises(RuntimeError):
create_app(handler=handler, redis=None)


async def test_load_session_dont_load_expired_session(aiohttp_client,
redis):
async def handler(request):
session = await get_session(request)
exp_param = request.rel_url.query.get('exp', None)
if exp_param is None:
session['a'] = 1
session['b'] = 2
else:
assert {} == session

return web.Response(body=b'OK')

client = await aiohttp_client(
create_app(handler, redis, 2)
)
resp = await client.get('/')
assert resp.status == 200

await asyncio.sleep(5)

resp = await client.get('/?exp=yes')
assert resp.status == 200