def test_ping_online(capsys):
    run_commander('ping')

    captured = capsys.readouterr().out
    assert 'Online' in captured
    assert 'Version' in captured
    assert get_host()['url'] in captured
    assert get_host()['username'] in captured
def test_ping_offline(get_version_mock, capsys):
    get_version_mock.side_effect = McmdError('')
    run_commander('ping')

    captured = capsys.readouterr().out
    assert 'Offline' in captured
    assert 'Version' not in captured
    assert get_host()['url'] in captured
    assert get_host()['username'] in captured
def _user_can_login(username, password):
    session = molgenis.client.Session(urljoin(get_host()['url'], '/api/'))
    try:
        session.login(username, password)
    except molgenis.client.MolgenisRequestError:
        return False
    else:
        return True
def test_login_token_invalid_login_page(set_token_mock, host_mock, session, package):
    test_host = get_host()
    test_host['token'] = 'nonexistingtoken'
    host_mock.return_value = test_host

    # the import endpoint returns an html login page when the token is invalid
    run_commander('import testAutoId_unpackaged --in {}'.format(package))

    # a new token should've been set
    assert set_token_mock.called
    assert entity_type_exists(session, '{}_testAutoId'.format(package))
def _get_permissions(session, resource_type, principal_type, id_):
    # noinspection PyProtectedMember
    headers = session._get_token_header()
    headers.update({"Content-Type": "application/json"})
    response = requests.get(urljoin(
        get_host()['url'],
        'menu/admin/permissionmanager/{}/{}/{}'.format(resource_type,
                                                       principal_type, id_)),
                            headers=headers)
    permissions = json.loads(response.content)['permissions']
    return permissions
def test_login_token_invalid(set_token_mock, host_mock, session):
    # run a command with an invalid token
    test_host = get_host()
    test_host['token'] = 'nonexistingtoken'
    host_mock.return_value = test_host

    run_commander('set app title login4')

    # a new token should've been set
    assert set_token_mock.called
    settings = session.get('sys_set_app')[0]
    assert settings['title'] == 'login4'
def entity_is_row_level_secured(admin_session, entity_id):
    """Workaround: there is no endpoint to check whether an entity is row level secured."""
    assert not entity_is_empty(
        admin_session, entity_id), "Need an entity with data to check RLS"

    name = random_name()
    run_commander('add user {}'.format(name))
    run_commander('give {} read {}'.format(name, entity_id))

    user_session = molgenis.client.Session(urljoin(get_host()['url'], '/api/'))
    user_session.login(name, name)
    return entity_is_empty(user_session, entity_id)
def test_login_no_password(set_token_mock, host_mock, enter_password, session):
    test_host = get_host()
    password = test_host['password']
    enter_password.return_value = password

    # remove the password from the host and run a command that requires a login
    test_host.pop('password')
    host_mock.return_value = test_host

    run_commander('set app title login2')

    # after the login a new token should've been set
    assert set_token_mock.called
    settings = session.get('sys_set_app')[0]
    assert settings['title'] == 'login2'
def test_login_token(set_token_mock, host_mock, session):
    test_host = get_host()
    host_mock.return_value = test_host

    # login and capture the token
    run_commander('set app title something')
    token = set_token_mock.call_args[0][0]
    set_token_mock.reset_mock()

    # use the token on a subsequent command
    test_host['token'] = token
    host_mock.return_value = test_host

    run_commander('set app title login3')

    # because a token was present, no new token should've been set
    assert not set_token_mock.called
    settings = session.get('sys_set_app')[0]
    assert settings['title'] == 'login3'