|
| 1 | +import asyncio |
| 2 | +import json |
| 3 | +import uuid |
| 4 | +import aiomcache |
| 5 | +import time |
| 6 | +import pytest |
| 7 | + |
| 8 | +from aiohttp import web |
| 9 | +from aiohttp_session import Session, session_middleware, get_session |
| 10 | +from aiohttp_session.memcached_storage import MemcachedStorage |
| 11 | + |
| 12 | + |
| 13 | +@pytest.yield_fixture |
| 14 | +def memcached(loop): |
| 15 | + conn = aiomcache.Client("127.0.0.1", 11211, loop=loop) |
| 16 | + yield conn |
| 17 | + conn.close() |
| 18 | + |
| 19 | + |
| 20 | +def create_app(loop, handler, memcached, max_age=None, |
| 21 | + key_factory=lambda: uuid.uuid4().hex): |
| 22 | + middleware = session_middleware( |
| 23 | + MemcachedStorage(memcached, max_age=max_age, key_factory=key_factory)) |
| 24 | + app = web.Application(middlewares=[middleware], loop=loop) |
| 25 | + app.router.add_route('GET', '/', handler) |
| 26 | + return app |
| 27 | + |
| 28 | + |
| 29 | +@asyncio.coroutine |
| 30 | +def make_cookie(client, memcached, data): |
| 31 | + session_data = { |
| 32 | + 'session': data, |
| 33 | + 'created': int(time.time()) |
| 34 | + } |
| 35 | + value = json.dumps(session_data) |
| 36 | + key = uuid.uuid4().hex |
| 37 | + storage_key = ('AIOHTTP_SESSION_' + key).encode('utf-8') |
| 38 | + yield from memcached.set(storage_key, bytes(value, 'utf-8')) |
| 39 | + client.session.cookie_jar.update_cookies({'AIOHTTP_SESSION': key}) |
| 40 | + |
| 41 | + |
| 42 | +@asyncio.coroutine |
| 43 | +def make_cookie_with_bad_value(client, memcached): |
| 44 | + key = uuid.uuid4().hex |
| 45 | + storage_key = ('AIOHTTP_SESSION_' + key).encode('utf-8') |
| 46 | + yield from memcached.set(storage_key, b'') |
| 47 | + client.session.cookie_jar.update_cookies({'AIOHTTP_SESSION': key}) |
| 48 | + |
| 49 | + |
| 50 | +@asyncio.coroutine |
| 51 | +def load_cookie(client, memcached): |
| 52 | + cookies = client.session.cookie_jar.filter_cookies(client.make_url('/')) |
| 53 | + key = cookies['AIOHTTP_SESSION'] |
| 54 | + storage_key = ('AIOHTTP_SESSION_' + key.value).encode('utf-8') |
| 55 | + encoded = yield from memcached.get(storage_key) |
| 56 | + s = encoded.decode('utf-8') |
| 57 | + value = json.loads(s) |
| 58 | + return value |
| 59 | + |
| 60 | + |
| 61 | +@asyncio.coroutine |
| 62 | +def test_create_new_sesssion(test_client, memcached): |
| 63 | + |
| 64 | + @asyncio.coroutine |
| 65 | + def handler(request): |
| 66 | + session = yield from get_session(request) |
| 67 | + assert isinstance(session, Session) |
| 68 | + assert session.new |
| 69 | + assert not session._changed |
| 70 | + assert {} == session |
| 71 | + return web.Response(body=b'OK') |
| 72 | + |
| 73 | + client = yield from test_client(create_app, handler, memcached) |
| 74 | + resp = yield from client.get('/') |
| 75 | + assert resp.status == 200 |
| 76 | + |
| 77 | + |
| 78 | +@asyncio.coroutine |
| 79 | +def test_load_existing_sesssion(test_client, memcached): |
| 80 | + |
| 81 | + @asyncio.coroutine |
| 82 | + def handler(request): |
| 83 | + session = yield from get_session(request) |
| 84 | + assert isinstance(session, Session) |
| 85 | + assert not session.new |
| 86 | + assert not session._changed |
| 87 | + assert {'a': 1, 'b': 12} == session |
| 88 | + return web.Response(body=b'OK') |
| 89 | + |
| 90 | + client = yield from test_client(create_app, handler, memcached) |
| 91 | + yield from make_cookie(client, memcached, {'a': 1, 'b': 12}) |
| 92 | + resp = yield from client.get('/') |
| 93 | + assert resp.status == 200 |
| 94 | + |
| 95 | + |
| 96 | +@asyncio.coroutine |
| 97 | +def test_load_bad_sesssion(test_client, memcached): |
| 98 | + @asyncio.coroutine |
| 99 | + def handler(request): |
| 100 | + session = yield from get_session(request) |
| 101 | + assert isinstance(session, Session) |
| 102 | + assert not session.new |
| 103 | + assert not session._changed |
| 104 | + assert {} == session |
| 105 | + return web.Response(body=b'OK') |
| 106 | + |
| 107 | + client = yield from test_client(create_app, handler, memcached) |
| 108 | + yield from make_cookie_with_bad_value(client, memcached) |
| 109 | + resp = yield from client.get('/') |
| 110 | + assert resp.status == 200 |
| 111 | + |
| 112 | + |
| 113 | +@asyncio.coroutine |
| 114 | +def test_change_sesssion(test_client, memcached): |
| 115 | + |
| 116 | + @asyncio.coroutine |
| 117 | + def handler(request): |
| 118 | + session = yield from get_session(request) |
| 119 | + session['c'] = 3 |
| 120 | + return web.Response(body=b'OK') |
| 121 | + |
| 122 | + client = yield from test_client(create_app, handler, memcached) |
| 123 | + yield from make_cookie(client, memcached, {'a': 1, 'b': 2}) |
| 124 | + resp = yield from client.get('/') |
| 125 | + assert resp.status == 200 |
| 126 | + |
| 127 | + value = yield from load_cookie(client, memcached) |
| 128 | + assert 'session' in value |
| 129 | + assert 'a' in value['session'] |
| 130 | + assert 'b' in value['session'] |
| 131 | + assert 'c' in value['session'] |
| 132 | + assert 'created' in value |
| 133 | + assert value['session']['a'] == 1 |
| 134 | + assert value['session']['b'] == 2 |
| 135 | + assert value['session']['c'] == 3 |
| 136 | + morsel = resp.cookies['AIOHTTP_SESSION'] |
| 137 | + assert morsel['httponly'] |
| 138 | + assert '/' == morsel['path'] |
| 139 | + |
| 140 | + |
| 141 | +@asyncio.coroutine |
| 142 | +def test_clear_cookie_on_sesssion_invalidation(test_client, memcached): |
| 143 | + |
| 144 | + @asyncio.coroutine |
| 145 | + def handler(request): |
| 146 | + session = yield from get_session(request) |
| 147 | + session.invalidate() |
| 148 | + return web.Response(body=b'OK') |
| 149 | + |
| 150 | + client = yield from test_client(create_app, handler, memcached) |
| 151 | + yield from make_cookie(client, memcached, {'a': 1, 'b': 2}) |
| 152 | + resp = yield from client.get('/') |
| 153 | + assert resp.status == 200 |
| 154 | + |
| 155 | + value = yield from load_cookie(client, memcached) |
| 156 | + assert {} == value |
| 157 | + morsel = resp.cookies['AIOHTTP_SESSION'] |
| 158 | + assert morsel['path'] == '/' |
| 159 | + assert morsel['expires'] == "Thu, 01 Jan 1970 00:00:00 GMT" |
| 160 | + assert morsel['max-age'] == "0" |
| 161 | + |
| 162 | + |
| 163 | +@asyncio.coroutine |
| 164 | +def test_create_cookie_in_handler(test_client, memcached): |
| 165 | + |
| 166 | + @asyncio.coroutine |
| 167 | + def handler(request): |
| 168 | + session = yield from get_session(request) |
| 169 | + session['a'] = 1 |
| 170 | + session['b'] = 2 |
| 171 | + return web.Response(body=b'OK', headers={'HOST': 'example.com'}) |
| 172 | + |
| 173 | + client = yield from test_client(create_app, handler, memcached) |
| 174 | + resp = yield from client.get('/') |
| 175 | + assert resp.status == 200 |
| 176 | + |
| 177 | + value = yield from load_cookie(client, memcached) |
| 178 | + assert 'session' in value |
| 179 | + assert 'a' in value['session'] |
| 180 | + assert 'b' in value['session'] |
| 181 | + assert 'created' in value |
| 182 | + assert value['session']['a'] == 1 |
| 183 | + assert value['session']['b'] == 2 |
| 184 | + morsel = resp.cookies['AIOHTTP_SESSION'] |
| 185 | + assert morsel['httponly'] |
| 186 | + assert morsel['path'] == '/' |
| 187 | + storage_key = ('AIOHTTP_SESSION_' + morsel.value).encode('utf-8') |
| 188 | + exists = yield from memcached.get(storage_key) |
| 189 | + assert exists |
| 190 | + |
| 191 | + |
| 192 | +@asyncio.coroutine |
| 193 | +def test_create_new_sesssion_if_key_doesnt_exists_in_memcached( |
| 194 | + test_client, memcached): |
| 195 | + |
| 196 | + @asyncio.coroutine |
| 197 | + def handler(request): |
| 198 | + session = yield from get_session(request) |
| 199 | + assert session.new |
| 200 | + return web.Response(body=b'OK') |
| 201 | + |
| 202 | + client = yield from test_client(create_app, handler, memcached) |
| 203 | + client.session.cookie_jar.update_cookies( |
| 204 | + {'AIOHTTP_SESSION': 'invalid_key'}) |
| 205 | + resp = yield from client.get('/') |
| 206 | + assert resp.status == 200 |
| 207 | + |
| 208 | + |
| 209 | +@asyncio.coroutine |
| 210 | +def test_create_storate_with_custom_key_factory(test_client, memcached): |
| 211 | + |
| 212 | + @asyncio.coroutine |
| 213 | + def handler(request): |
| 214 | + session = yield from get_session(request) |
| 215 | + session['key'] = 'value' |
| 216 | + assert session.new |
| 217 | + return web.Response(body=b'OK') |
| 218 | + |
| 219 | + def key_factory(): |
| 220 | + return 'test-key' |
| 221 | + |
| 222 | + client = yield from test_client(create_app, handler, memcached, 8, |
| 223 | + key_factory) |
| 224 | + resp = yield from client.get('/') |
| 225 | + assert resp.status == 200 |
| 226 | + |
| 227 | + assert resp.cookies['AIOHTTP_SESSION'].value == 'test-key' |
| 228 | + |
| 229 | + value = yield from load_cookie(client, memcached) |
| 230 | + assert 'key' in value['session'] |
| 231 | + assert value['session']['key'] == 'value' |
0 commit comments