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 _do(file_ext, parse): from pykern import pkio from pykern import pkunit from pykern import pkcollections from pykern.pkdebug import pkdp from pykern.pkunit import pkeq, pkfail, pkok from sirepo import srunit import re fc = srunit.flask_client() for suffix in '', ' (2)', ' (3)': for f in pkio.sorted_glob(pkunit.data_dir().join('*.' + file_ext)): json, stream = parse(f) sim_type = re.search(r'^([a-z]+)_', f.basename).group(1) fc.get('/{}'.format(sim_type)) is_dev = 'deviance' in f.basename if not is_dev: sim_name = pkcollections.json_load_any(json).models.simulation.name res = fc.sr_post_form( 'importFile', { 'file': (stream, f.basename), 'folder': '/importer_test', }, {'simulation_type': sim_type}, ) if is_dev: m = re.search(r'Error: (.+)', json) if m: expect = m.group(1) pkeq(expect, res.error) continue pkeq(sim_name + suffix, res.models.simulation.name)
def test_auth_login(): from pykern import pkcollections from pykern.pkdebug import pkdp from pykern.pkunit import pkeq from sirepo import srunit fc = srunit.flask_client({ 'SIREPO_FEATURE_CONFIG_API_MODULES': 'bluesky', 'SIREPO_BLUESKY_AUTH_SECRET': '3SExmbOzn1WeoCWeJxekaE6bMDUj034Pu5az1hLNnvENyvL1FAJ1q3eowwODoa3f', }) from sirepo import simulation_db from sirepo import bluesky from sirepo import cookie fc.get('/srw') data = fc.sr_post( 'listSimulations', {'simulationType': 'srw', 'search': {'simulationName': 'Bending Magnet Radiation'}}, ) fc.cookie_jar.clear() data = data[0].simulation req = pkcollections.Dict( simulationType='srw', simulationId=data.simulationId, ) bluesky.auth_hash(req) resp = fc.sr_post('blueskyAuth', req) pkeq('ok', resp['state']) pkeq(req.simulationId, simulation_db.parse_sid(resp['data'])) pkeq('srw', resp['schema']['simulationType'])
def _fc(github_deprecated=True): from pykern.pkdebug import pkdp from sirepo import srunit sim_type = 'myapp' cfg = { 'SIREPO_AUTH_METHODS': 'email:guest', 'SIREPO_AUTH_EMAIL_FROM_EMAIL': 'x', 'SIREPO_AUTH_EMAIL_FROM_NAME': 'x', 'SIREPO_AUTH_EMAIL_SMTP_PASSWORD': '******', 'SIREPO_AUTH_EMAIL_SMTP_SERVER': 'dev', 'SIREPO_AUTH_EMAIL_SMTP_USER': '******', 'SIREPO_AUTH_GITHUB_CALLBACK_URI': '/uri', 'SIREPO_AUTH_GITHUB_KEY': 'key', 'SIREPO_AUTH_GITHUB_SECRET': 'secret', 'SIREPO_FEATURE_CONFIG_SIM_TYPES': sim_type, } if github_deprecated: cfg['SIREPO_AUTH_DEPRECATED_METHODS'] = 'github' else: cfg['SIREPO_AUTH_METHODS'] += ':github' fc = srunit.flask_client(cfg=cfg) # set the sentinel fc.cookie_jar.clear() fc.sr_get_root(sim_type) return fc, sim_type
def _do(file_ext, parse): from pykern import pkio from pykern import pkunit from pykern import pkcollections from pykern.pkdebug import pkdp from pykern.pkunit import pkeq, pkfail, pkok from sirepo import srunit import re fc = srunit.flask_client(sim_types='srw:myapp') fc.sr_login_as_guest() for suffix in '', ' 2', ' 3': for f in pkio.sorted_glob(pkunit.data_dir().join('*.' + file_ext)): json, stream = parse(f) sim_type = re.search(r'^([a-z]+)_', f.basename).group(1) fc.sr_get_root(sim_type) is_dev = 'deviance' in f.basename if not is_dev: sim_name = pkcollections.json_load_any(json).models.simulation.name res = fc.sr_post_form( 'importFile', { 'file': (stream, f.basename), 'folder': '/importer_test', }, {'simulation_type': sim_type}, ) if is_dev: m = re.search(r'Error: (.+)', json) if m: expect = m.group(1) pkeq(expect, res.error) continue pkeq(sim_name + suffix, res.models.simulation.name)
def test_not_found(): from pykern.pkdebug import pkdp from pykern.pkunit import pkeq from sirepo import srunit fc = srunit.flask_client() for uri in ('/some random uri', '/srw/wrong-param', '/export-archive'): resp = fc.get(uri) pkeq(404, resp.status_code)
def test_1_serial_stomp(): from pykern.pkdebug import pkdp, pkdpretty from pykern.pkunit import pkfail, pkok from sirepo import srunit import copy fc = srunit.flask_client() sim_type = 'srw' fc.get('/{}'.format(sim_type)) data = fc.sr_post('listSimulations', {'simulationType': sim_type}) for youngs in data: if youngs['name'] == "Young's Double Slit Experiment": break else: pkfail("{}: Young's not found", pkdpretty(data)) data = fc.sr_get( 'simulationData', { 'simulation_type': sim_type, 'pretty': '0', 'simulation_id': youngs['simulationId'], }, ) prev_serial = data['models']['simulation']['simulationSerial'] prev_data = copy.deepcopy(data) pkok( prev_serial > _MIN_SERIAL, '{}: serial must be greater than {}', prev_serial, _MIN_SERIAL, ) data['models']['beamline'][4]['position'] = '61' curr_data = fc.sr_post('saveSimulationData', data) curr_serial = curr_data['models']['simulation']['simulationSerial'] pkok( prev_serial < curr_serial, '{}: serial not incremented, still < {}', prev_serial, curr_serial, ) prev_data['models']['beamline'][4]['position'] = '60.5' failure = fc.sr_post('saveSimulationData', prev_data) pkok( failure['error'] == 'invalidSerial', '{}: unexpected status, expected serial failure', failure, ) curr_data['models']['beamline'][4]['position'] = '60.5' curr_serial = curr_data['models']['simulation']['simulationSerial'] new_data = fc.sr_post('saveSimulationData', curr_data) new_serial = new_data['models']['simulation']['simulationSerial'] pkok( curr_serial < new_serial, '{}: serial not incremented, still < {}', new_serial, curr_serial, )
def test_basic(): from sirepo import srunit from pykern import pkunit fc = srunit.flask_client(sim_types='elegant:srw:myapp') resp = fc.get('/old') assert 'LandingPageController' in resp.get_data(), \ 'Top level document is the landing page' resp = fc.get('/robots.txt') pkunit.pkre('elegant.*myapp.*srw', resp.get_data())
def test_missing_cookies(): from pykern.pkunit import pkeq from sirepo import srunit import json fc = srunit.flask_client() sim_type = 'srw' resp = fc.post('/simulation-list', data=json.dumps({'simulationType': sim_type}), content_type='application/json') pkeq(401, resp.status_code)
def t(): from pykern.pkdebug import pkdp from pykern.pkunit import pkeq, pkexcept, pkre from sirepo import uri_router import re fc = srunit.flask_client() uri = uri_router.uri_for_api('homePage') pkre('http://[^/]+/about$', uri) uri = uri_router.uri_for_api('homePage', external=False) pkre('^/about$', uri) with pkexcept(KeyError): uri_router.uri_for_api('notAnApi') with pkexcept('missing parameter'): uri_router.uri_for_api('exportArchive', {'simulation_type': 'srw'})
def _fc(monkeypatch, user_name): from sirepo import srunit sim_type = 'myapp' fc = srunit.flask_client({ 'SIREPO_AUTH_METHODS': 'guest:github', 'SIREPO_FEATURE_CONFIG_SIM_TYPES': sim_type, 'SIREPO_AUTH_GITHUB_CALLBACK_URI': '/uri', 'SIREPO_AUTH_GITHUB_KEY': 'key', 'SIREPO_AUTH_GITHUB_SECRET': 'secret', }) from sirepo import github_srunit from sirepo.auth import github fc.cookie_jar.clear() oc = github_srunit.MockOAuthClient(monkeypatch, user_name=user_name) return fc, sim_type, oc
def test_srw(): from pykern import pkio from pykern.pkdebug import pkdpretty from sirepo import srunit import json fc = srunit.flask_client() resp = fc.get('/srw') assert '<!DOCTYPE html' in resp.get_data(), \ 'Top level document is html' data = fc.sr_post( 'listSimulations', { 'simulationType': 'srw', 'search': '' }, )
def test_srw(): from pykern import pkio from pykern.pkdebug import pkdpretty from pykern.pkunit import pkeq from sirepo import srunit import json fc = srunit.flask_client() resp = fc.get('/srw') assert '<!DOCTYPE html' in resp.get_data(), \ 'Top level document is html' data = fc.sr_post( 'listSimulations', {'simulationType': 'srw', 'search': ''}, ) pkeq(fc.get('/find-by-name/srw/default/UndulatorRadiation').status_code, 404) for sep in (' ', '%20', '+'): pkeq(fc.get('/find-by-name/srw/default/Undulator{}Radiation'.format(sep)).status_code, 200)
def test_processed_image(): from pykern.pkdebug import pkdp from pykern.pkunit import pkeq, pkfail from pykern import pkunit from sirepo import srunit fc = srunit.flask_client() sim_type = 'srw' fc.get('/{}'.format(sim_type)) sim_id = fc.sr_sim_data(sim_type, 'Sample from Image') resp = fc.sr_post( 'getApplicationData', { 'simulationId': sim_id, 'simulationType': sim_type, 'method': 'processedImage', 'baseImage': 'sample.tif', 'model': { 'cutoffBackgroundNoise': 0.5, 'backgroundColor': 0, 'rotateAngle': 0, 'rotateReshape': '0', 'cropArea': '1', 'areaXStart': 0, 'areaXEnd': 1280, 'areaYStart': 0, 'areaYEnd': 834, 'shiftX': 0, 'shiftY': 0, 'invert': '0', 'tileImage': '0', 'tileRows': 1, 'tileColumns': 1, 'outputImageFormat': 'tif', } }, { 'filename': 'foo.tif', }, raw_response=True, ) with open(str(pkunit.work_dir().join('x.tif')), 'wb') as f: f.write(resp.data)
def test_auth_login(): from pykern import pkcollections from pykern.pkdebug import pkdp from pykern.pkunit import pkeq, pkexcept from sirepo import srunit fc = srunit.flask_client( cfg={ 'SIREPO_AUTH_BLUESKY_SECRET': '3SExmbOzn1WeoCWeJxekaE6bMDUj034Pu5az1hLNnvENyvL1FAJ1q3eowwODoa3f', 'SIREPO_AUTH_METHODS': 'bluesky:guest', 'SIREPO_FEATURE_CONFIG_SIM_TYPES': 'srw:myapp', }) from sirepo import simulation_db from sirepo.auth import bluesky import werkzeug.exceptions sim_type = 'srw' uid = fc.sr_login_as_guest(sim_type) data = fc.sr_post( 'listSimulations', { 'simulationType': 'srw', 'search': { 'simulationName': 'Bending Magnet Radiatin' } }, ) fc.cookie_jar.clear() fc.sr_get('authState') data = data[0].simulation req = pkcollections.Dict( simulationType='srw', simulationId=data.simulationId, ) bluesky.auth_hash(req) resp = fc.sr_post('authBlueskyLogin', req) pkeq('ok', resp['state']) pkeq(req.simulationId, simulation_db.parse_sid(resp['data'])) pkeq('srw', resp['schema']['simulationType']) req.authHash = 'not match' resp = fc.sr_post('authBlueskyLogin', req, raw_response=True) pkeq(401, resp.status_code)
def t(): from pykern.pkdebug import pkdp from pykern.pkunit import pkeq, pkexcept, pkre, pkeq from sirepo import uri_router import re fc = srunit.flask_client() uri = uri_router.uri_for_api('homePage', params={'path_info': None}) pkre('http://[^/]+/en$', uri) uri = uri_router.uri_for_api( 'homePage', params={'path_info': 'terms.html'}, external=False, ) pkeq('/en/terms.html', uri) with pkexcept(KeyError): uri_router.uri_for_api('notAnApi') with pkexcept('missing parameter'): uri_router.uri_for_api('exportArchive', {'simulation_type': 'srw'})
def test_srw(): from pykern import pkio from pykern.pkdebug import pkdpretty from pykern.pkunit import pkeq, pkre from sirepo import srunit import json fc = srunit.flask_client(sim_types='elegant:srw:myapp') sim_type = 'srw' r = fc.sr_get_root(sim_type) pkre('<!DOCTYPE html', r.data) fc.sr_login_as_guest(sim_type) d = fc.sr_post('listSimulations', {'simulationType': sim_type}) pkeq( fc.get('/find-by-name/srw/default/UndulatorRadiation').status_code, 404) for sep in (' ', '%20', '+'): pkeq( fc.get('/find-by-name/srw/default/Undulator{}Radiation'.format( sep)).status_code, 200)
def test_create_zip(): from pykern import pkio from pykern import pkunit from pykern.pkdebug import pkdp, pkdpretty from pykern.pkunit import pkfail, pkok from sirepo import srunit import copy import zipfile fc = srunit.flask_client(sim_types='elegant:srw:warppba') fc.sr_login_as_guest('srw') imported = _import(fc) for sim_type, sim_name, expect in imported + [ ('elegant', 'bunchComp - fourDipoleCSR', ['WAKE-inputfile.knsl45.liwake.sdds', 'run.py', 'sirepo-data.json']), ('srw', 'Tabulated Undulator Example', ['magnetic_measurements.zip', 'run.py', 'sirepo-data.json']), ('warppba', 'Laser Pulse', ['run.py', 'sirepo-data.json']), ]: sim_id = fc.sr_sim_data( sim_type, sim_name)['models']['simulation']['simulationId'] resp = fc.sr_get( 'exportArchive', { 'simulation_type': sim_type, 'simulation_id': sim_id, 'filename': 'anything.zip', }, ) with pkio.save_chdir(pkunit.work_dir()): fn = sim_name + '.zip' with open(fn, 'wb') as f: f.write(resp.data) z = zipfile.ZipFile(fn) nl = sorted(z.namelist()) pkok( nl == expect, '{}: zip namelist incorrect, expect={}', nl, expect, )
def test_login_logout(): from pykern import pkconfig from pykern.pkunit import pkfail, pkok from sirepo import srunit import re fc = srunit.flask_client({ 'SIREPO_FEATURE_CONFIG_API_MODULES': 'oauth', 'SIREPO_OAUTH_GITHUB_KEY': 'n/a', 'SIREPO_OAUTH_GITHUB_SECRET': 'n/a', 'SIREPO_OAUTH_GITHUB_CALLBACK_URI': 'n/a', }) sim_type = 'srw' fc.get('/{}'.format(sim_type)) fc.sr_post('listSimulations', {'simulationType': sim_type}) text = fc.sr_get( 'oauthLogin', { 'simulation_type': sim_type, 'oauth_type': 'github', }, raw_response=True, ).data state = re.search(r'state=(.*?)"', text).group(1) #TODO(pjm): causes a forbidden error due to missing variables, need to mock-up an oauth test type text = fc.get('/oauth-authorized/github') text = fc.sr_get( 'oauthLogout', { 'simulation_type': sim_type, }, raw_response=True, ).data pkok( text.find('Redirecting') > 0, 'missing redirect', ) pkok( text.find('"/{}"'.format(sim_type)) > 0, 'missing redirect target', )
def _fc(guest_deprecated=False): from pykern.pkdebug import pkdp from pykern.pkcollections import PKDict from sirepo import srunit sim_type = 'myapp' cfg = PKDict( SIREPO_AUTH_DEPRECATED_METHODS='guest', SIREPO_AUTH_EMAIL_FROM_EMAIL='x', SIREPO_AUTH_EMAIL_FROM_NAME='x', SIREPO_AUTH_EMAIL_SMTP_PASSWORD='******', SIREPO_AUTH_EMAIL_SMTP_SERVER='dev', SIREPO_AUTH_EMAIL_SMTP_USER='******', SIREPO_AUTH_GUEST_EXPIRY_DAYS='1', SIREPO_AUTH_METHODS='email', SIREPO_FEATURE_CONFIG_SIM_TYPES=sim_type, ) fc = srunit.flask_client(cfg=cfg) # set the sentinel fc.cookie_jar.clear() fc.sr_get_root(sim_type) return fc, sim_type
def test_create_zip(): from pykern import pkio from pykern import pkunit from pykern.pkdebug import pkdp, pkdpretty from pykern.pkunit import pkfail, pkok from sirepo import srunit import copy import zipfile fc = srunit.flask_client() fc.get('/srw') imported = _import(fc) for sim_type, sim_name, expect in imported + [ ('elegant', 'bunchComp - fourDipoleCSR', ['WAKE-inputfile.knsl45.liwake.sdds', 'run.py', 'sirepo-data.json']), ('srw', 'Tabulated Undulator Example', ['magnetic_measurements.zip', 'run.py', 'sirepo-data.json']), ('warppba', 'Laser Pulse', ['run.py', 'sirepo-data.json']), ]: sim_id = fc.sr_sim_data(sim_type, sim_name)['models']['simulation']['simulationId'] resp = fc.sr_get( 'exportArchive', { 'simulation_type': sim_type, 'simulation_id': sim_id, 'filename': 'anything.zip', }, raw_response=True, ) with pkio.save_chdir(pkunit.work_dir()): fn = sim_name + '.zip' with open(fn, 'wb') as f: f.write(resp.data) z = zipfile.ZipFile(fn) nl = sorted(z.namelist()) pkok( nl == expect, '{}: zip namelist incorrect, expect={}', nl, expect, )
def test_generate_python(): from pykern import pkio from pykern.pkunit import pkeq from sirepo.template import srw from sirepo import srunit fc = srunit.flask_client() fc.get('/{}'.format(srw.SIM_TYPE)) for name in ('NSLS-II CHX beamline', 'Sample from Image', 'Boron Fiber (CRL with 3 lenses)', 'Tabulated Undulator Example', 'Gaussian X-ray beam through a Beamline containing Imperfect Mirrors', 'NSLS-II SRX beamline', 'NSLS-II ESM beamline', 'Mask example', 'NSLS-II SMI beamline'): sim = fc.sr_sim_data(srw.SIM_TYPE, name) resp = fc.sr_get( 'pythonSource', { 'simulation_id': sim['models']['simulation']['simulationId'], 'simulation_type': srw.SIM_TYPE, }, raw_response=True, ) filename = '{}.py'.format(name) with open(str(pkunit.work_dir().join(filename)), 'wb') as f: f.write(resp.data) expect = pkio.read_text(pkunit.data_dir().join(filename)) pkeq(expect, resp.data)
def _setup_client(): from sirepo import srunit fc = srunit.flask_client(sim_types='srw') fc.sr_login_as_guest('srw') return fc
def test_runner_myapp(): os.environ['SIREPO_FEATURE_CONFIG_RUNNER_DAEMON'] = '1' os.environ['PYTHONUNBUFFERED'] = '1' py3_env = _assert_py3() from pykern import pkio from pykern import pkunit from pykern.pkdebug import pkdc, pkdp from sirepo import srunit fc = srunit.flask_client(sim_types='myapp') fc.sr_login_as_guest() from sirepo import srdb pkdc(srdb.runner_socket_path()) pkio.unchecked_remove(srdb.runner_socket_path()) runner_env = dict(py3_env) runner_env['SIREPO_SRDB_ROOT'] = str(srdb.root()) runner = subprocess.Popen( ['pyenv', 'exec', 'sirepo', 'runner', 'start'], env=runner_env, ) try: for _ in range(30): if srdb.runner_socket_path().exists(): break time.sleep(0.1) else: pkunit.pkfail('runner daemon did not start up') fc.get('/myapp') data = fc.sr_post( 'listSimulations', {'simulationType': 'myapp', 'search': {'simulationName': 'heightWeightReport'}}, ) pkdc(data) data = data[0].simulation pkdc(data) data = fc.sr_get_json( 'simulationData', params=dict( pretty='1', simulation_id=data.simulationId, simulation_type='myapp', ), ) pkdc(data) run = fc.sr_post( 'runSimulation', dict( forceRun=False, models=data.models, report='heightWeightReport', simulationId=data.models.simulation.simulationId, simulationType=data.simulationType, ), ) pkdc(run) for _ in range(10): run = fc.sr_post( 'runStatus', run.nextRequest ) pkdc(run) if run.state == 'completed': break time.sleep(1) else: pkunit.pkfail('runStatus: failed to complete: {}', run) # Just double-check it actually worked assert u'plots' in run finally: runner.terminate() runner.wait()
def _setup_client(): from sirepo.template import srw from sirepo import srunit fc = srunit.flask_client() fc.get('/{}'.format(srw.SIM_TYPE)) return fc
def test_basic(): from sirepo import srunit fc = srunit.flask_client() resp = fc.get('/') assert 'LandingPageController' in resp.get_data(), \ 'Top level document is the landing page'
def test_get_data_file(): from sirepo import srunit from pykern import pkunit from pykern import pkio import sdds fc = srunit.flask_client() fc.get('/elegant') data = fc.sr_post( 'listSimulations', {'simulationType': 'elegant', 'search': {'simulationName': 'fourDipoleCSR'}}, ) data = data[0].simulation data = fc.sr_get( 'simulationData', params=dict( pretty='1', simulation_id=data.simulationId, simulation_type='elegant', ), ) run = fc.sr_post( 'runSimulation', dict( forceRun=False, models=data.models, report='bunchReport1', simulationId=data.models.simulation.simulationId, simulationType=data.simulationType, ), ) for _ in range(10): run = fc.sr_post( 'runStatus', run.nextRequest ) if run.state == 'completed': break time.sleep(1) else: pkunit.pkfail('runStatus: failed to complete: {}', run) resp = fc.sr_get( 'downloadDataFile', dict( simulation_type=data.simulationType, simulation_id=data.models.simulation.simulationId, model='bunchReport1', frame='-1', suffix='csv', ), raw_response=True, ) rows = csv.reader(StringIO.StringIO(resp.get_data())) assert len(list(rows)) == 5001 resp = fc.sr_get( 'downloadDataFile', dict( simulation_type=data.simulationType, simulation_id=data.models.simulation.simulationId, model='bunchReport1', frame='-1', ), raw_response=True, ) m = re.search(r'attachment; filename="([^"]+)"', resp.headers['Content-Disposition']) with pkunit.save_chdir_work(): path = pkio.py_path(m.group(1)) with open(str(path), 'w') as f: f.write(resp.get_data()) assert sdds.sddsdata.InitializeInput(0, str(path)) == 1, \ '{}: sdds failed to open'.format(path) # Verify we can read something assert 0 <= len(sdds.sddsdata.GetColumnNames(0)) sdds.sddsdata.Terminate(0)