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 _op(): from sirepo import cookie pkeq(uid, cookie.get_user(checked=False)) for expect, key in cases: if expect is None: pkeq(False, cookie.has_key(key)) else: pkeq(expect, cookie.get_value(key))
def _update_database(user_data, oauth_type): with _db_serial_lock: user = User.query.filter_by(oauth_id=user_data['id'], oauth_type=oauth_type).first() session_uid = cookie.get_user(checked=False) if user: if session_uid and session_uid != user.uid: simulation_db.move_user_simulations(user.uid) user.user_name = user_data['login'] user.display_name = user_data['name'] cookie.set_user(user.uid) else: if not session_uid: # ensures the user session (uid) is ready if new user logs in from logged-out session pkdlog('creating new session for user: {}', user_data['id']) simulation_db.simulation_dir('') user = User(cookie.get_user(), user_data['login'], user_data['name'], oauth_type, user_data['id']) _db.session.add(user) _db.session.commit() return user
def job_id(data): """A Job is a simulation and report name. A jid is words and dashes. Args: data (dict): extract sid and report Returns: str: unique name """ return '{}-{}-{}'.format( cookie.get_user(), data.simulationId, data.report, )
def _user_dir(): """User for the session Returns: str: unique id for user from flask session """ uid = cookie.get_user(checked=False) if not uid: uid = _user_dir_create() d = user_dir_name(uid) if d.check(): return d # flask session might have been deleted (in dev) so "logout" and "login" uid = _user_dir_create() return user_dir_name(uid)
def move_user_simulations(to_uid): """Moves all non-example simulations for the current session into the target user's dir. """ from_uid = cookie.get_user() with _global_lock: for path in glob.glob( str(user_dir_name(from_uid).join('*', '*', SIMULATION_DATA_FILE)), ): data = read_json(path) sim = data['models']['simulation'] if 'isExample' in sim and sim['isExample']: continue dir_path = os.path.dirname(path) new_dir_path = dir_path.replace(from_uid, to_uid) pkdlog('{} -> {}', dir_path, new_dir_path) pkio.mkdir_parent(new_dir_path) os.rename(dir_path, new_dir_path)