def test_set_get(): from pykern import pkunit from pykern.pkunit import pkeq from pykern.pkdebug import pkdp from pykern import pkcollections from sirepo import cookie class _Response(pkcollections.Dict): def set_cookie(self, *args, **kwargs): self.args = args self.kwargs = kwargs cookie.process_header('x') with pkunit.pkexcept('KeyError'): cookie.get_value('hi') with pkunit.pkexcept('AssertionError'): cookie.set_value('hi', 'hello') pkeq(None, cookie.unchecked_get_value('hi')) cookie.init_mock() cookie.set_value('hi', 'hello') r = _Response(status_code=200) cookie.save_to_cookie(r) pkeq('sirepo_dev', r.args[0]) pkeq(False, r.kwargs['secure']) pkeq('hello', cookie.get_value('hi')) cookie.unchecked_remove('hi') pkeq(None, cookie.unchecked_get_value('hi')) cookie.process_header('sirepo_dev={}'.format(r.args[1])) pkeq('hello', cookie.get_value('hi'))
def test_set_get(): from pykern import pkunit, pkcompat from pykern.pkunit import pkeq from pykern.pkdebug import pkdp from sirepo import cookie with cookie.process_header('x'): with pkunit.pkexcept('KeyError'): cookie.get_value('hi1') with pkunit.pkexcept('AssertionError'): cookie.set_value('hi2', 'hello') pkeq(None, cookie.unchecked_get_value('hi3'))
def test_cookie_outside_of_flask_request(): from pykern import pkcompat from pykern.pkunit import pkeq from sirepo import cookie from sirepo import srunit with srunit.auth_db_session(), \ cookie.set_cookie_outside_of_flask_request(): cookie.set_value('hi4', 'hello') r = _Response(status_code=200) cookie.save_to_cookie(r) pkeq('sirepo_dev', r.args[0]) pkeq(False, r.kwargs['secure']) pkeq('hello', cookie.get_value('hi4')) cookie.unchecked_remove('hi4') pkeq(None, cookie.unchecked_get_value('hi4')) # Nest cookie contexts with cookie.process_header( 'sirepo_dev={}'.format(pkcompat.from_bytes(r.args[1])), ): pkeq('hello', cookie.get_value('hi4'))
def process_request(unit_test=None): cookie.process_header(unit_test) _set_log_user()
def process_request(unit_test=None): with auth_db.session(), cookie.process_header(unit_test): # Logging happens after the return to Flask so the log user must persist # beyond the life of process_request _set_log_user() yield