Exemplo n.º 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()
Exemplo n.º 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()
Exemplo n.º 3
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)
Exemplo n.º 4
0
def _dispatch(path):
    """Called by Flask and routes the base_uri with parameters

    Args:
        path (str): what to route

    Returns:
        Flask.response
    """
    auth.process_request()
    try:
        if path is None:
            return call_api(_empty_route.func, {})
        # werkzeug doesn't convert '+' to ' '
        parts = re.sub(r'\+', ' ', path).split('/')
        try:
            route = _uri_to_route[parts[0]]
            parts.pop(0)
        except KeyError:
            route = _default_route
        kwargs = pkcollections.Dict()
        for p in route.params:
            if not parts:
                if not p.is_optional:
                    raise NotFound('{}: uri missing parameter ({})', path, p.name)
                break
            if p.is_path_info:
                kwargs[p.name] = '/'.join(parts)
                parts = None
                break
            kwargs[p.name] = parts.pop(0)
        if parts:
            raise NotFound('{}: unknown parameters in uri ({})', parts, path)
        return call_api(route.func, kwargs)
    except NotFound as e:
        util.raise_not_found(e.log_fmt, *e.args, **e.kwargs)
    except Exception as e:
        pkdlog('{}: error: {}', path, pkdexc())
        raise