aiohttp_session.get_session

Here are the examples of the python api aiohttp_session.get_session taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

40 Examples 7

Example 1

Project: ooi3
License: View license
Source File: frontend.py
Function: form
    @aiohttp_jinja2.template('form.html')
    @asyncio.coroutine
    def form(self, request):
        """展示登录表单。

        :param request: aiohttp.web.Request
        :return: dict
        """
        session = yield from get_session(request)
        if 'mode' in session:
            mode = session['mode']
        else:
            session['mode'] = 1
            mode = 1

        return {'mode': mode}

Example 2

Project: ooi3
License: View license
Source File: frontend.py
    @asyncio.coroutine
    def kcv(self, request):
        """适配KanColleViewer或者74EO中进行游戏的页面,提供一个iframe,在iframe中载入游戏FLASH。该页面会检查会话中是否有api_token、
        api_starttime和world_ip三个参数,缺少其中任意一个都不能进行游戏,跳转回登录页面。

        :param request: aiohttp.web.Request
        :return: aiohttp.web.Response or aiohttp.web.HTTPFound
        """
        session = yield from get_session(request)
        token = session.get('api_token', None)
        starttime = session.get('api_starttime', None)
        world_ip = session.get('world_ip', None)
        if token and starttime and world_ip:
            return aiohttp_jinja2.render_template('kcv.html', request, context={})
        else:
            self.clear_session(session)
            return aiohttp.web.HTTPFound('/')

Example 3

Project: ooi3
License: View license
Source File: frontend.py
    @asyncio.coroutine
    def connector(self, request):
        """适配登录器直连模式结果页面,提供osapi.dmm.com的链接。

        :param request: aiohttp.web.Request
        :return: aiohttp.web.Response or aiohttp.web.HTTPFound
        """
        session = yield from get_session(request)
        osapi_url = session.get('osapi_url', None)
        if osapi_url:
            context = {'osapi_url': osapi_url}
            return aiohttp_jinja2.render_template('connector.html', request, context)
        else:
            self.clear_session(session)
            return aiohttp.web.HTTPFound('/')

Example 4

Project: ooi3
License: View license
Source File: frontend.py
    @asyncio.coroutine
    def logout(self, request):
        """ 注销已登录的用户。
        清除所有的session,返回首页。

        :return: aiohttp.web.HTTPFound
        """
        session = yield from get_session(request)
        self.clear_session(session)
        return aiohttp.web.HTTPFound('/')

Example 5

Project: aiohttp-session
License: View license
Source File: main.py
@asyncio.coroutine
def handler(request):
    session = yield from get_session(request)
    last_visit = session['last_visit'] if 'last_visit' in session else None
    session['last_visit'] = time.time()
    text = 'Last visited: {}'.format(last_visit)
    return web.Response(body=text.encode('utf-8'))

Example 6

@asyncio.coroutine
def test_max_age_also_returns_expires(test_client):

    @asyncio.coroutine
    def handler(request):
        time.monotonic.return_value = 0.0
        session = yield from get_session(request)
        session['c'] = 3
        return web.Response(body=b'OK')

    with mock.patch('time.monotonic') as m_monotonic:
        m_monotonic.return_value = 0.0

        client = yield from test_client(create_app, handler)
        make_cookie(client, {'a': 1, 'b': 2})
        resp = yield from client.get('/')
        assert resp.status == 200
        assert 'expires=Thu, 01-Jan-1970 00:00:10 GMT' in \
               resp.headers['SET-COOKIE']

Example 7

Project: aiohttp-session
License: View license
Source File: test_cookie_storage.py
@asyncio.coroutine
def test_create_new_sesssion(test_client):

    @asyncio.coroutine
    def handler(request):
        session = yield from get_session(request)
        assert isinstance(session, Session)
        assert session.new
        assert not session._changed
        assert {} == session
        return web.Response(body=b'OK')

    client = yield from test_client(create_app, handler)
    resp = yield from client.get('/')
    assert resp.status == 200

Example 8

Project: aiohttp-session
License: View license
Source File: test_cookie_storage.py
@asyncio.coroutine
def test_load_existing_sesssion(test_client):

    @asyncio.coroutine
    def handler(request):
        session = yield from get_session(request)
        assert isinstance(session, Session)
        assert not session.new
        assert not session._changed
        assert session.created is not None
        assert {'a': 1, 'b': 2} == session
        return web.Response(body=b'OK')

    client = yield from test_client(create_app, handler)
    make_cookie(client, {'a': 1, 'b': 2})
    resp = yield from client.get('/')
    assert resp.status == 200

Example 9

Project: aiohttp-session
License: View license
Source File: test_cookie_storage.py
@asyncio.coroutine
def test_clear_cookie_on_sesssion_invalidation(test_client):

    @asyncio.coroutine
    def handler(request):
        session = yield from get_session(request)
        session.invalidate()
        return web.Response(body=b'OK')

    client = yield from test_client(create_app, handler)
    make_cookie(client, {'a': 1, 'b': 2})
    resp = yield from client.get('/')
    assert resp.status == 200
    assert ('Set-Cookie: AIOHTTP_SESSION="{}"; '
            'domain=127.0.0.1; httponly; Path=/'.upper()) == \
        resp.cookies['AIOHTTP_SESSION'].output().upper()

Example 10

@asyncio.coroutine
def test_create_new_sesssion_broken_by_format(test_client, fernet, key):

    @asyncio.coroutine
    def handler(request):
        session = yield from get_session(request)
        assert isinstance(session, Session)
        assert session.new
        assert not session._changed
        assert {} == session
        return web.Response(body=b'OK')

    new_fernet = Fernet(Fernet.generate_key())
    client = yield from test_client(create_app, handler, key)
    make_cookie(client, new_fernet, {'a': 1, 'b': 12})
    resp = yield from client.get('/')
    assert resp.status == 200

Example 11

@asyncio.coroutine
def test_load_existing_sesssion(test_client, fernet, key):

    @asyncio.coroutine
    def handler(request):
        session = yield from get_session(request)
        assert isinstance(session, Session)
        assert not session.new
        assert not session._changed
        assert {'a': 1, 'b': 12} == session
        return web.Response(body=b'OK')

    client = yield from test_client(create_app, handler, key)
    make_cookie(client, fernet, {'a': 1, 'b': 12})
    resp = yield from client.get('/')
    assert resp.status == 200

Example 12

@asyncio.coroutine
def test_clear_cookie_on_sesssion_invalidation(test_client, fernet, key):

    @asyncio.coroutine
    def handler(request):
        session = yield from get_session(request)
        session.invalidate()
        return web.Response(body=b'OK')

    client = yield from test_client(create_app, handler, key)
    make_cookie(client, fernet, {'a': 1, 'b': 2})
    resp = yield from client.get('/')
    assert resp.status == 200

    morsel = resp.cookies['AIOHTTP_SESSION']
    assert '' == morsel.value
    assert not morsel['httponly']
    assert morsel['path'] == '/'

Example 13

Project: aiohttp-session
License: View license
Source File: test_get_session.py
@asyncio.coroutine
def test_get_stored_session():
    req = make_mocked_request('GET', '/')
    session = Session('identity', data=None, new=False)
    req[SESSION_KEY] = session

    ret = yield from get_session(req)
    assert session is ret

Example 14

Project: aiohttp-session
License: View license
Source File: test_get_session.py
@asyncio.coroutine
def test_session_is_not_stored():
    req = make_mocked_request('GET', '/')

    with pytest.raises(RuntimeError):
        yield from get_session(req)

Example 15

Project: aiohttp-session
License: View license
Source File: test_nacl_storage.py
@asyncio.coroutine
def test_create_new_sesssion(test_client, secretbox, key):

    @asyncio.coroutine
    def handler(request):
        session = yield from get_session(request)
        assert isinstance(session, Session)
        assert session.new
        assert not session._changed
        assert {} == session
        return web.Response(body=b'OK')

    client = yield from test_client(create_app, handler, key)
    resp = yield from client.get('/')
    assert resp.status == 200

Example 16

Project: aiohttp-session
License: View license
Source File: test_nacl_storage.py
@asyncio.coroutine
def test_load_existing_sesssion(test_client, secretbox, key):

    @asyncio.coroutine
    def handler(request):
        session = yield from get_session(request)
        assert isinstance(session, Session)
        assert not session.new
        assert not session._changed
        assert {'a': 1, 'b': 12} == session
        return web.Response(body=b'OK')

    client = yield from test_client(create_app, handler, key)
    make_cookie(client, secretbox, {'a': 1, 'b': 12})
    resp = yield from client.get('/')
    assert resp.status == 200

Example 17

Project: aiohttp-session
License: View license
Source File: test_nacl_storage.py
@asyncio.coroutine
def test_clear_cookie_on_session_invalidation(test_client, secretbox, key):

    @asyncio.coroutine
    def handler(request):
        session = yield from get_session(request)
        session.invalidate()
        return web.Response(body=b'OK')

    client = yield from test_client(create_app, handler, key)
    make_cookie(client, secretbox, {'a': 1, 'b': 2})
    resp = yield from client.get('/')
    assert resp.status == 200

    morsel = resp.cookies['AIOHTTP_SESSION']
    assert '' == morsel.value
    assert not morsel['httponly']
    assert morsel['path'] == '/'

Example 18

Project: aiohttp-session
License: View license
Source File: test_redis_storage.py
@asyncio.coroutine
def test_create_new_sesssion(test_client, redis):

    @asyncio.coroutine
    def handler(request):
        session = yield from get_session(request)
        assert isinstance(session, Session)
        assert session.new
        assert not session._changed
        assert {} == session
        return web.Response(body=b'OK')

    client = yield from test_client(create_app, handler, redis)
    resp = yield from client.get('/')
    assert resp.status == 200

Example 19

Project: aiohttp-session
License: View license
Source File: test_redis_storage.py
@asyncio.coroutine
def test_load_existing_sesssion(test_client, redis):

    @asyncio.coroutine
    def handler(request):
        session = yield from get_session(request)
        assert isinstance(session, Session)
        assert not session.new
        assert not session._changed
        assert {'a': 1, 'b': 12} == session
        return web.Response(body=b'OK')

    client = yield from test_client(create_app, handler, redis)
    yield from make_cookie(client, redis, {'a': 1, 'b': 12})
    resp = yield from client.get('/')
    assert resp.status == 200

Example 20

@asyncio.coroutine
def test_clear_cookie_on_sesssion_invalidation(test_client, redis):

    @asyncio.coroutine
    def handler(request):
        session = yield from get_session(request)
        session.invalidate()
        return web.Response(body=b'OK')

    client = yield from test_client(create_app, handler, redis)
    yield from make_cookie(client, redis, {'a': 1, 'b': 2})
    resp = yield from client.get('/')
    assert resp.status == 200

    value = yield from load_cookie(client, redis)
    assert {} == value
    morsel = resp.cookies['AIOHTTP_SESSION']
    assert morsel['httponly']
    assert morsel['path'] == '/'

Example 21

Project: aiohttp-session
License: View license
Source File: test_redis_storage.py
@asyncio.coroutine
def test_create_new_sesssion_if_key_doesnt_exists_in_redis(test_client, redis):

    @asyncio.coroutine
    def handler(request):
        session = yield from get_session(request)
        assert session.new
        return web.Response(body=b'OK')

    client = yield from test_client(create_app, handler, redis)
    client.session.cookie_jar.update_cookies(
        {'AIOHTTP_SESSION': 'invalid_key'})
    resp = yield from client.get('/')
    assert resp.status == 200

Example 22

Project: ooi3
License: View license
Source File: api.py
    @asyncio.coroutine
    def world_image(self, request):
        """ 显示正确的镇守府图片。
        舰娘游戏中客户端FLASH请求的镇守府图片是根据FLASH本身的URL生成的,需要根据用户所在的镇守府IP为其显示正确的图片。

        :param request: aiohttp.web.Request
        :return: aiohttp.web.HTTPFound or aiohttp.web.HTTPBadRequest
        """
        size = request.match_info['size']
        session = yield from get_session(request)
        world_ip = session['world_ip']
        if world_ip:
            ip_sections = map(int, world_ip.split('.'))
            image_name = '_'.join([format(x, '03') for x in ip_sections]) + '_' + size
            if image_name in self.worlds:
                body = self.worlds[image_name]
            else:
                url = 'http://203.104.209.102/kcs/resources/image/world/' + image_name + '.png'
                coro = aiohttp.get(url, connector=self.connector)
                try:
                    response = yield from asyncio.wait_for(coro, timeout=5)
                except asyncio.TimeoutError:
                    return aiohttp.web.HTTPBadRequest()
                body = yield from response.read()
                self.worlds[image_name] = body
            return aiohttp.web.Response(body=body, headers={'Content-Type': 'image/png', 'Cache-Control': 'no-cache'})
        else:
            return aiohttp.web.HTTPBadRequest()

Example 23

Project: ooi3
License: View license
Source File: api.py
    @asyncio.coroutine
    def api(self, request):
        """ 转发客户端和游戏服务器之间的API通信。

        :param request: aiohttp.web.Request
        :return: aiohttp.web.Response or aiohttp.web.HTTPBadRequest
        """
        action = request.match_info['action']
        session = yield from get_session(request)
        world_ip = session['world_ip']
        if world_ip:
            if action == 'api_start2' and self.api_start2 is not None:
                return aiohttp.web.Response(body=self.api_start2,
                                            headers=aiohttp.MultiDict({'Content-Type': 'text/plain'}))
            else:
                referrer = request.headers.get('REFERER')
                referrer = referrer.replace(request.host, world_ip)
                referrer = referrer.replace('https://', 'http://')
                url = 'http://' + world_ip + '/kcsapi/' + action
                headers = aiohttp.MultiDict({
                    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko',
                    'Origin': 'http://' + world_ip + '/',
                    'Referer': referrer,
                    'X-Requested-With': 'ShockwaveFlash/18.0.0.232'
                })
                data = yield from request.post()
                coro = aiohttp.post(url, data=data, headers=headers, connector=self.connector)
                try:
                    response = yield from asyncio.wait_for(coro, timeout=5)
                except asyncio.TimeoutError:
                    return aiohttp.web.HTTPBadRequest()
                body = yield from response.read()
                if action == 'api_start2' and len(body) > 100000:
                    self.api_start2 = body
                return aiohttp.web.Response(body=body, headers=aiohttp.MultiDict({'Content-Type': 'text/plain'}))
        else:
            return aiohttp.web.HTTPBadRequest()

Example 24

Project: ooi3
License: View license
Source File: frontend.py
    @asyncio.coroutine
    def login(self, request):
        """接受登录表单提交的数据,登录后跳转或登录失败后展示错误信息。

        :param request: aiohttp.web.Request
        :return: aiohttp.web.HTTPFound or aiohttp.web.Response
        """
        post = yield from request.post()
        session = yield from get_session(request)

        login_id = post.get('login_id', None)
        password = post.get('password', None)
        mode = int(post.get('mode', 1))

        session['mode'] = mode

        if login_id and password:
            kancolle = KancolleAuth(login_id, password)
            if mode in (1, 2, 3):
                try:
                    yield from kancolle.get_flash()
                    session['api_token'] = kancolle.api_token
                    session['api_starttime'] = kancolle.api_starttime
                    session['world_ip'] = kancolle.world_ip
                    if mode == 2:
                        return aiohttp.web.HTTPFound('/kcv')
                    elif mode == 3:
                        return aiohttp.web.HTTPFound('/poi')
                    else:
                        return aiohttp.web.HTTPFound('/kancolle')

                except OOIAuthException as e:
                    context = {'errmsg': e.message, 'mode': mode}
                    return aiohttp_jinja2.render_template('form.html', request, context)
            elif mode == 4:
                try:
                    osapi_url = yield from kancolle.get_osapi()
                    session['osapi_url'] = osapi_url
                    return aiohttp.web.HTTPFound('/connector')
                except OOIAuthException as e:
                    context = {'errmsg': e.message, 'mode': mode}
                    return aiohttp_jinja2.render_template('form.html', request, context)
            else:
                raise aiohttp.web.HTTPBadRequest()
        else:
            context = {'errmsg': '请输入完整的登录ID和密码', 'mode': mode}
            return aiohttp_jinja2.render_template('form.html', request, context)

Example 25

Project: ooi3
License: View license
Source File: frontend.py
    @asyncio.coroutine
    def normal(self, request):
        """适配浏览器中进行游戏的页面,该页面会检查会话中是否有api_token、api_starttime和world_ip三个参数,缺少其中任意一个都不能进行
        游戏,跳转回登录页面。

        :param request: aiohttp.web.Request
        :return: aiohttp.web.Response or aiohttp.web.HTTPFound
        """
        session = yield from get_session(request)
        token = session.get('api_token', None)
        starttime = session.get('api_starttime', None)
        world_ip = session.get('world_ip', None)
        if token and starttime and world_ip:
            context = {'scheme': request.scheme,
                       'host': request.host,
                       'token': token,
                       'starttime': starttime}
            return aiohttp_jinja2.render_template('normal.html', request, context)
        else:
            self.clear_session(session)
            return aiohttp.web.HTTPFound('/')

Example 26

Project: ooi3
License: View license
Source File: frontend.py
    @asyncio.coroutine
    def flash(self, request):
        """适配KanColleViewer或者74EO中进行游戏的页面,展示,该页面会检查会话中是否有api_token、api_starttime和world_ip三个参数,
        缺少其中任意一个都不能进行游戏,跳转回登录页面。

        :param request: aiohttp.web.Request
        :return: aiohttp.web.Response or aiohttp.web.HTTPFound
        """
        session = yield from get_session(request)
        token = session.get('api_token', None)
        starttime = session.get('api_starttime', None)
        world_ip = session.get('world_ip', None)
        if token and starttime and world_ip:
            context = {'scheme': request.scheme,
                       'host': request.host,
                       'token': token,
                       'starttime': starttime}
            return aiohttp_jinja2.render_template('flash.html', request, context)
        else:
            self.clear_session(session)
            return aiohttp.web.HTTPFound('/')

Example 27

Project: ooi3
License: View license
Source File: frontend.py
    @asyncio.coroutine
    def poi(self, request):
        """适配poi中进行游戏的页面,显示FLASH。该页面会检查会话中是否有api_token、api_starttime和world_ip三个参数,缺少其中任意一个
        都不能进行游戏,跳转回登录页面。

        :param request: aiohttp.web.Request
        :return: aiohttp.web.Response or aiohttp.web.HTTPFound
        """
        session = yield from get_session(request)
        token = session.get('api_token', None)
        starttime = session.get('api_starttime', None)
        world_ip = session.get('world_ip', None)
        if token and starttime and world_ip:
            context = {'scheme': request.scheme,
                       'host': request.host,
                       'token': token,
                       'starttime': starttime}
            return aiohttp_jinja2.render_template('poi.html', request, context)
        else:
            self.clear_session(session)
            return aiohttp.web.HTTPFound('/')

Example 28

Project: aiohttp-security
License: View license
Source File: session_identity.py
Function: identify
    @asyncio.coroutine
    def identify(self, request):
        session = yield from get_session(request)
        return session.get(self._session_key)

Example 29

Project: aiohttp-security
License: View license
Source File: session_identity.py
Function: remember
    @asyncio.coroutine
    def remember(self, request, response, identity, **kwargs):
        session = yield from get_session(request)
        session[self._session_key] = identity

Example 30

Project: aiohttp-security
License: View license
Source File: session_identity.py
Function: forget
    @asyncio.coroutine
    def forget(self, request, response):
        session = yield from get_session(request)
        session.pop(self._session_key, None)

Example 31

Project: aiohttp-security
License: View license
Source File: test_session_identity.py
Function: test_remember
@asyncio.coroutine
def test_remember(make_app, test_client):

    @asyncio.coroutine
    def handler(request):
        response = web.Response()
        yield from remember(request, response, 'Andrew')
        return response

    @asyncio.coroutine
    def check(request):
        session = yield from get_session(request)
        assert session['AIOHTTP_SECURITY'] == 'Andrew'
        return web.HTTPOk()

    app = make_app()
    app.router.add_route('GET', '/', handler)
    app.router.add_route('GET', '/check', check)
    client = yield from test_client(app)
    resp = yield from client.get('/')
    assert 200 == resp.status
    yield from resp.release()

    resp = yield from client.get('/check')
    assert 200 == resp.status
    yield from resp.release()

Example 32

Project: aiohttp-security
License: View license
Source File: test_session_identity.py
Function: test_forget
@asyncio.coroutine
def test_forget(make_app, test_client):

    @asyncio.coroutine
    def index(request):
        session = yield from get_session(request)
        return web.HTTPOk(text=session.get('AIOHTTP_SECURITY', ''))

    @asyncio.coroutine
    def login(request):
        response = web.HTTPFound(location='/')
        yield from remember(request, response, 'Andrew')
        return response

    @asyncio.coroutine
    def logout(request):
        response = web.HTTPFound('/')
        yield from forget(request, response)
        return response

    app = make_app()
    app.router.add_route('GET', '/', index)
    app.router.add_route('POST', '/login', login)
    app.router.add_route('POST', '/logout', logout)

    client = yield from test_client(app)

    resp = yield from client.post('/login')
    assert 200 == resp.status
    assert resp.url.endswith('/')
    txt = yield from resp.text()
    assert 'Andrew' == txt
    yield from resp.release()

    resp = yield from client.post('/logout')
    assert 200 == resp.status
    assert resp.url.endswith('/')
    txt = yield from resp.text()
    assert '' == txt
    yield from resp.release()

Example 33

@asyncio.coroutine
def test_change_sesssion(test_client):

    @asyncio.coroutine
    def handler(request):
        session = yield from get_session(request)
        session['c'] = 3
        return web.Response(body=b'OK')

    client = yield from test_client(create_app, handler)
    make_cookie(client, {'a': 1, 'b': 2})
    resp = yield from client.get('/')
    assert resp.status == 200

    morsel = resp.cookies['AIOHTTP_SESSION']
    cookie_data = json.loads(morsel.value)
    assert 'session' in cookie_data
    assert 'a' in cookie_data['session']
    assert 'b' in cookie_data['session']
    assert 'c' in cookie_data['session']
    assert 'created' in cookie_data
    assert cookie_data['session']['a'] == 1
    assert cookie_data['session']['b'] == 2
    assert cookie_data['session']['c'] == 3
    assert morsel['httponly']
    assert '/' == morsel['path']

Example 34

@asyncio.coroutine
def test_change_sesssion(test_client, fernet, key):

    @asyncio.coroutine
    def handler(request):
        session = yield from get_session(request)
        session['c'] = 3
        return web.Response(body=b'OK')

    client = yield from test_client(create_app, handler, key)
    make_cookie(client, fernet, {'a': 1, 'b': 2})
    resp = yield from client.get('/')
    assert resp.status == 200

    morsel = resp.cookies['AIOHTTP_SESSION']
    cookie_data = decrypt(fernet, morsel.value)
    assert 'session' in cookie_data
    assert 'a' in cookie_data['session']
    assert 'b' in cookie_data['session']
    assert 'c' in cookie_data['session']
    assert 'created' in cookie_data
    assert cookie_data['session']['a'] == 1
    assert cookie_data['session']['b'] == 2
    assert cookie_data['session']['c'] == 3
    assert morsel['httponly']
    assert '/' == morsel['path']

Example 35

Project: aiohttp-session
License: View license
Source File: test_http_exception.py
Function: test_exceptions
@asyncio.coroutine
def test_exceptions(test_client):

    @asyncio.coroutine
    def save(request):
        session = yield from get_session(request)
        session['message'] = 'works'
        raise web.HTTPFound('/show')

    @asyncio.coroutine
    def show(request):
        session = yield from get_session(request)
        message = session.get('message')
        return web.Response(text=str(message))

    client = yield from test_client(create_app,
                                    ('/save', save),
                                    ('/show', show))

    resp = yield from client.get('/save')
    assert resp.status == 200
    assert resp.url[-5:] == '/show'
    text = yield from resp.text()
    assert text == 'works'

Example 36

Project: aiohttp-session
License: View license
Source File: test_nacl_storage.py
@asyncio.coroutine
def test_change_session(test_client, secretbox, key):

    @asyncio.coroutine
    def handler(request):
        session = yield from get_session(request)
        session['c'] = 3
        return web.Response(body=b'OK')

    client = yield from test_client(create_app, handler, key)
    make_cookie(client, secretbox, {'a': 1, 'b': 2})
    resp = yield from client.get('/')
    assert resp.status == 200

    morsel = resp.cookies['AIOHTTP_SESSION']
    cookie_data = decrypt(secretbox, morsel.value)
    assert 'session' in cookie_data
    assert 'a' in cookie_data['session']
    assert 'b' in cookie_data['session']
    assert 'c' in cookie_data['session']
    assert 'created' in cookie_data
    assert cookie_data['session']['a'] == 1
    assert cookie_data['session']['b'] == 2
    assert cookie_data['session']['c'] == 3
    assert morsel['httponly']
    assert '/' == morsel['path']

Example 37

@asyncio.coroutine
def test_change_sesssion(test_client, redis):

    @asyncio.coroutine
    def handler(request):
        session = yield from get_session(request)
        session['c'] = 3
        return web.Response(body=b'OK')

    client = yield from test_client(create_app, handler, redis)
    yield from make_cookie(client, redis, {'a': 1, 'b': 2})
    resp = yield from client.get('/')
    assert resp.status == 200

    value = yield from load_cookie(client, redis)
    assert 'session' in value
    assert 'a' in value['session']
    assert 'b' in value['session']
    assert 'c' in value['session']
    assert 'created' in value
    assert value['session']['a'] == 1
    assert value['session']['b'] == 2
    assert value['session']['c'] == 3
    morsel = resp.cookies['AIOHTTP_SESSION']
    assert morsel['httponly']
    assert '/' == morsel['path']

Example 38

Project: aiohttp-session
License: View license
Source File: test_redis_storage.py
@asyncio.coroutine
def test_create_cookie_in_handler(test_client, redis):

    @asyncio.coroutine
    def handler(request):
        session = yield from get_session(request)
        session['a'] = 1
        session['b'] = 2
        return web.Response(body=b'OK', headers={'HOST': 'example.com'})

    client = yield from test_client(create_app, handler, redis)
    resp = yield from client.get('/')
    assert resp.status == 200

    value = yield from load_cookie(client, redis)
    assert 'session' in value
    assert 'a' in value['session']
    assert 'b' in value['session']
    assert 'created' in value
    assert value['session']['a'] == 1
    assert value['session']['b'] == 2
    morsel = resp.cookies['AIOHTTP_SESSION']
    assert morsel['httponly']
    assert morsel['path'] == '/'
    with (yield from redis) as conn:
        exists = yield from conn.exists('AIOHTTP_SESSION_' +
                                        morsel.value)
        assert exists

Example 39

Project: aiohttp-session
License: View license
Source File: test_redis_storage.py
@asyncio.coroutine
def test_set_ttl_on_session_saving(test_client, redis):

    @asyncio.coroutine
    def handler(request):
        session = yield from get_session(request)
        session['a'] = 1
        return web.Response(body=b'OK')

    client = yield from test_client(create_app, handler, redis, max_age=10)
    resp = yield from client.get('/')
    assert resp.status == 200

    key = resp.cookies['AIOHTTP_SESSION'].value

    with (yield from redis) as conn:
        ttl = yield from conn.ttl('AIOHTTP_SESSION_'+key)

    assert ttl > 9
    assert ttl <= 10

Example 40

Project: aiohttp-session
License: View license
Source File: test_redis_storage.py
@asyncio.coroutine
def test_set_ttl_manually_set(test_client, redis):

    @asyncio.coroutine
    def handler(request):
        session = yield from get_session(request)
        session.max_age = 10
        session['a'] = 1
        return web.Response(body=b'OK')

    client = yield from test_client(create_app, handler, redis)
    resp = yield from client.get('/')
    assert resp.status == 200

    key = resp.cookies['AIOHTTP_SESSION'].value

    with (yield from redis) as conn:
        ttl = yield from conn.ttl('AIOHTTP_SESSION_'+key)

    assert ttl > 9
    assert ttl <= 10