def test_custom_serializer(): was_used = [False, False] class CustomSerializer(object): def loads(self, data_string): was_used[0] = True return json.loads(data_string.decode('utf-8')) def dumps(self, data): was_used[1] = True return json.dumps(data).encode('utf-8') serializer = CustomSerializer() options = {'session.validate_key':'hoobermas', 'session.type':'cookie', 'data_serializer': serializer} app = TestApp(SessionMiddleware(simple_app, **options)) res = app.get('/') assert 'current value is: 1' in res res = app.get('/') cookie = SignedCookie('hoobermas') session_data = cookie.value_decode(app.cookies['beaker.session.id'])[0] session_data = b64decode(session_data) data = serializer.loads(session_data) assert data['value'] == 2 res = app.get('/') assert 'current value is: 3' in res assert all(was_used)
def test_custom_serializer(): was_used = [False, False] class CustomSerializer(object): def loads(self, data_string): was_used[0] = True return json.loads(data_string.decode("utf-8")) def dumps(self, data): was_used[1] = True return json.dumps(data).encode("utf-8") serializer = CustomSerializer() options = { "session.validate_key": "hoobermas", "session.type": "cookie", "data_serializer": serializer, } app = TestApp(SessionMiddleware(simple_app, **options)) res = app.get("/") assert "current value is: 1" in res res = app.get("/") cookie = SignedCookie("hoobermas") session_data = cookie.value_decode(app.cookies["beaker.session.id"])[0] session_data = b64decode(session_data) data = serializer.loads(session_data) assert data["value"] == 2 res = app.get("/") assert "current value is: 3" in res assert all(was_used)
def test_pickle_serializer(): options = {'session.validate_key':'hoobermas', 'session.type':'cookie', 'data_serializer': 'pickle'} app = TestApp(SessionMiddleware(simple_app, **options)) res = app.get('/') assert 'current value is: 1' in res res = app.get('/') cookie = SignedCookie('hoobermas') session_data = cookie.value_decode(app.cookies['beaker.session.id'])[0] session_data = b64decode(session_data) data = beaker.util.deserialize(session_data, 'pickle') assert data['value'] == 2 res = app.get('/') assert 'current value is: 3' in res
def update_session_from_cookie_header(header): """Update the flask session from the beaker file identified by the cookie header """ maps = _init_maps() try: cookie = SignedCookie(cfg.secret, input=header) if not cfg.key in cookie: return None identifier = cookie[cfg.key].value if not identifier: return None path = beaker.util.encoded_path( str(flask.current_app.sirepo_db_dir.join('beaker/container_file')), [identifier], extension='.cache', digest_filenames=False, ) with open(path, 'rb') as fh: values = pickle.load(fh) res = {} if 'session' in values and _ORIG_KEY in values['session']: for f in maps['key'].keys(): v = values['session'].get(f) if not v is None: if not isinstance(v, str): # pickle decodes certains strings as unicode in Python 2 v = v.encode('ascii') res[maps['key'][f]] = maps['value'].get(v, v) pkdlog('retrieved user from beaker cookie: res={}', res) return res except Exception as e: pkdlog('ignoring exception with beaker compat: error={}, header={}', e, header) return None
def test_json_serializer(): options = { "session.validate_key": "hoobermas", "session.type": "cookie", "data_serializer": "json", } app = TestApp(SessionMiddleware(simple_app, **options)) res = app.get("/") assert "current value is: 1" in res res = app.get("/") cookie = SignedCookie("hoobermas") session_data = cookie.value_decode(app.cookies["beaker.session.id"])[0] session_data = b64decode(session_data) data = beaker.util.deserialize(session_data, "json") assert data["value"] == 2 res = app.get("/") assert "current value is: 3" in res