Beispiel #1
0
def test_login():
    from pykern import pkunit
    from pykern.pkdebug import pkdp
    from pykern.pkunit import pkeq, pkok, pkre, pkexcept
    from sirepo import auth
    import flask
    import sirepo.auth.guest
    import sirepo.cookie
    import sirepo.http_request

    r = auth.api_authState()
    pkre('LoggedIn": false.*Registration": false', r.data)
    delattr(flask.g, 'sirepo_cookie')
    auth.process_request()
    with pkunit.pkexcept('SRException.*routeName=login'):
        auth.logged_in_user()
    with pkexcept('SRException.*routeName=login'):
        auth.require_user()
    sirepo.cookie.set_sentinel()
    # copying examples for new user takes time
    with pkunit.pkexcept('SRException.*routeName=None'):
        r = auth.login(sirepo.auth.guest, sim_type='myapp')
    r = auth.api_authState()
    pkre('LoggedIn": true.*Registration": false', r.data)
    u = auth.logged_in_user()
    pkok(u, 'user should exist')
    # guests do not require completeRegistration
    auth.require_user()
Beispiel #2
0
def test_login():
    from pykern import pkunit, pkcompat
    from pykern.pkunit import pkeq, pkok, pkre, pkfail, pkexcept
    from sirepo import auth
    import flask
    import sirepo.auth.guest
    import sirepo.cookie
    import sirepo.http_request
    import sirepo.util

    r = auth.api_authState()
    pkre('LoggedIn": false.*Registration": false', pkcompat.from_bytes(r.data))
    delattr(flask.g, 'sirepo_cookie')
    auth.process_request()
    with pkunit.pkexcept('SRException.*routeName=login'):
        auth.logged_in_user()
    with pkexcept('SRException.*routeName=login'):
        auth.require_user()
    sirepo.cookie.set_sentinel()
    # copying examples for new user takes time
    try:
        r = auth.login(sirepo.auth.guest, sim_type='myapp')
        pkfail('expecting sirepo.util.Response')
    except sirepo.util.Response as e:
        r = e.sr_args.response
    pkre(r'LoggedIn":\s*true.*Registration":\s*false',
         pkcompat.from_bytes(r.data))
    u = auth.logged_in_user()
    pkok(u, 'user should exist')
    # guests do not require completeRegistration
    auth.require_user()
Beispiel #3
0
def check_api_call(func):
    expect = getattr(func, api_perm.ATTR)
    a = api_perm.APIPerm
    if expect in (a.REQUIRE_COOKIE_SENTINEL, a.REQUIRE_USER):
        if not cookie.has_sentinel():
            raise sirepo.util.SRException('missingCookies', None)
        if expect == a.REQUIRE_USER:
            auth.require_user()
    elif expect == a.ALLOW_VISITOR:
        pass
    elif expect in (a.ALLOW_COOKIELESS_SET_USER, a.ALLOW_COOKIELESS_REQUIRE_USER):
        cookie.set_sentinel()
        if expect == a.ALLOW_COOKIELESS_REQUIRE_USER:
            auth.require_user()
    elif expect == a.REQUIRE_AUTH_BASIC:
        auth.require_auth_basic()
    else:
        raise AssertionError('unhandled api_perm={}'.format(expect))
Beispiel #4
0
def test_login():
    from pykern import pkunit
    from pykern.pkdebug import pkdp
    from pykern.pkunit import pkeq, pkok, pkre
    from sirepo import auth
    import flask
    import sirepo.auth.guest
    import sirepo.cookie
    import sirepo.http_request

    r = auth.api_authState()
    pkre('LoggedIn": false.*Registration": false', r.data)
    delattr(flask.g, 'sirepo_cookie')
    auth.process_request()
    with pkunit.pkexcept('Unauthorized'):
        auth.logged_in_user()
    r = auth.require_user()
    pkeq(400, r.status_code, 'status should be BAD REQUEST')
    pkre('"routeName": "login"', r.data)
    sirepo.cookie.set_sentinel()
    # copying examples for new user takes time
    r = auth.login(sirepo.auth.guest)
    pkeq(None, r, 'user created')
    r = auth.api_authState()
    pkre('LoggedIn": true.*Registration": true', r.data)
    u = auth.logged_in_user()
    pkok(u, 'user should exist')
    r = auth.require_user()
    pkeq(400, r.status_code, 'status should be BAD REQUEST')
    pkre('"routeName": "completeRegistration"', r.data)
    flask.request = 'abcdef'

    def parse_json(*args, **kwargs):
        return pkcollections.Dict(simulationType='myapp',
                                  displayName='Joe Bob')

    setattr(sirepo.http_request, 'parse_json', parse_json)
    auth.api_authCompleteRegistration()
    r = auth.api_authState()
    pkre('Name": "Joe Bob".*In": true.*.*Registration": false', r.data)