def create_examples(): """Adds missing app examples to all users. """ from pykern import pkio from sirepo import feature_config from sirepo import server from sirepo import simulation_db from sirepo import cookie server.init() for d in pkio.sorted_glob(simulation_db.user_dir_name('*')): if _is_src_dir(d): continue; uid = simulation_db.uid_from_dir_name(d) cookie.init_mock(uid) for sim_type in feature_config.cfg.sim_types: simulation_db.verify_app_directory(sim_type) names = map( lambda x: x['name'], simulation_db.iterate_simulation_datafiles(sim_type, simulation_db.process_simulation_list, { 'simulation.isExample': True, })) for s in simulation_db.examples(sim_type): if s.models.simulation.name not in names: simulation_db.save_new_example(s)
def test_1(): import shutil from pykern import pkconfig, pkunit, pkio from pykern.pkunit import pkeq from pykern.pkdebug import pkdp from sirepo import srunit srunit.flask_client() from sirepo import cookie cookie.init_mock() cookie.init('x') with pkunit.pkexcept('Unauthorized'): cookie.get_user() with pkunit.pkexcept('Unauthorized'): cookie.get_user(checked=False) cookie.set_sentinel() cookie.set_user('abc') 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('abc', cookie.get_user()) cookie.clear_user() cookie.unchecked_remove('hi') pkeq(None, cookie.get_user(checked=False)) cookie.init('sirepo_dev={}'.format(r.args[1])) pkeq('hello', cookie.get_value('hi')) pkeq('abc', cookie.get_user())
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 init_mock(uid=None, sim_type=None): """A mock user for pkcli""" cookie.init_mock() import sirepo.auth.guest if uid: _login_user(sirepo.auth.guest, uid) else: login(sirepo.auth.guest, is_mock=True)
def init_mock(uid): """A mock user for pkcli""" cookie.init_mock() if uid: import sirepo.auth.guest _login_user(sirepo.auth.guest, uid)