Example #1
0
def unlink(yes):
    """
    Unlink a linked Valohai project.
    """
    dir = get_project_directory()
    project = get_project()
    if not project:
        click.echo('{dir} or its parents do not seem linked to a project.'.format(dir=dir))
        return 1
    if not yes:
        click.confirm(
            'Unlink {dir} from {name}?'.format(
                dir=click.style(project.directory, bold=True),
                name=click.style(project.name, bold=True),
            ),
            abort=True,
        )
    links = settings.get('links', {})
    links.pop(dir)
    settings['links'] = links
    settings.save()
    success('Unlinked {dir} from {name}.'.format(
        dir=click.style(dir, bold=True),
        name=click.style(project.name, bold=True)
    ))
Example #2
0
def logout(yes):
    """Remove local authentication token."""
    user = settings.get('user')
    token = settings.get('token')
    if not (user or token):
        click.echo('You\'re not logged in.')
        return

    if not yes:
        user = settings['user']
        message = ('You are logged in as {username}.\n'
                   'Are you sure you wish to remove the authentication token?'
                   ).format(username=user['username'])
        click.confirm(message, abort=True)
    settings.update(host=None, user=None, token=None)
    settings.save()
    click.secho('Logged out.', fg='green', bold=True)
Example #3
0
def test_settings(temp_settings):
    assert not settings.get('foo')
    settings['foo'] = 'bar'
    settings.update(baz='quux')
    settings.save()
    assert os.path.isfile(settings.get_filename())
    settings._data = None  # Pretend we don't have data
    assert settings['foo'] == 'bar'
    assert settings['baz'] == 'quux'
Example #4
0
def login(username, password, host, yes):
    """Log in into Valohai."""
    if settings.get('user') and settings.get('token') and not yes:
        user = settings['user']
        message = ('You are already logged in as {username}.\n'
                   'Are you sure you wish to acquire a new token?').format(
                       username=user['username'])
        click.confirm(message, abort=True)
    with APISession(host) as sess:
        token_data = sess.post('/api/v0/get-token/',
                               data={
                                   'username': username,
                                   'password': password
                               }).json()
        token = token_data['token']
    with APISession(host, token) as sess:
        user_data = sess.get('/api/v0/users/me/').json()
    settings.update(host=host, user=user_data, token=token)
    settings.save()
    click.secho('Logged in. Hi!', fg='green', bold=True)
Example #5
0
def set_project_link(dir, project, inform=False):
    links = settings.get('links', {})
    links[dir] = project
    settings['links'] = links
    assert get_project(dir).id == project['id']
    settings.save()
    if inform:
        success('Linked {dir} to {name}.'.format(dir=click.style(dir,
                                                                 bold=True),
                                                 name=click.style(
                                                     project['name'],
                                                     bold=True)))
Example #6
0
def test_auth(runner):
    # Log in...
    with requests_mock.mock() as m:
        m.post('https://app.valohai.com/api/v0/get-token/', json={'token': 'X' * 40})
        m.get('https://app.valohai.com/api/v0/users/me/', json={'id': 1, 'username': '******'})
        result = runner.invoke(login, [
            '-u', 'john.smith',
            '-p', '123456',
        ])
        assert 'Logged in' in result.output
        assert settings['token'] == 'X' * 40

    # Attempting to re-login requires a confirmation, so this aborts.
    result = runner.invoke(login, ['-u', 'john.smith', '-p', '123456'])
    assert 'Aborted!' in result.output

    # Log out...

    result = runner.invoke(logout, input='y')
    assert not settings.get('token')

    # And again.

    assert 'not logged in' in runner.invoke(logout).output
Example #7
0
def get_project(dir=None, require=False):
    """
    Get the Valohai project object for a directory context.

    The object is augmented with the `dir` key.

    :param dir: Directory (defaults to cwd)
    :param require: Raise an exception if no project is found
    :return: Project object, or None.
    :rtype: valohai_cli.models.project.Project|None
    """
    links = settings.get('links') or {}
    if not links:
        if require:
            raise NoProject('No projects are configured')
        return None
    orig_dir = dir or get_project_directory()
    for dir in walk_directory_parents(orig_dir):
        project_obj = links.get(dir)
        if project_obj:
            return Project(data=project_obj, directory=dir)
    if require:
        raise NoProject('No project is linked to %s' % orig_dir)
    return None
Example #8
0
def get_host_and_token():
    host = settings.get('host')
    token = settings.get('token')
    if not (host and token):
        raise NotLoggedIn('You\'re not logged in; try `vh login` first.')
    return (host, token)