Example #1
0
    def test_get_current_user_raises_exception(self, mock_requests_get,
                                               mock_request_get_api_key):
        with fake_creds('.jovian-invalid-key', 'credentials.json'):
            # setUp
            creds = {
                "WEBAPP_URL": "https://staging.jovian.ml/",
                "GUEST_KEY": "b6538d4dfde04fcf993463a828a9cec6",
                "API_URL": "https://api-staging.jovian.ai",
                "API_KEY": "fake_invalid_api_key",
                "ORG_ID": "staging"
            }
            write_creds(creds)

            with self.assertRaises(Exception) as context:
                get_current_user()

            mock_requests_get.assert_called_with(
                'https://api-staging.jovian.ai/user/profile',
                headers={
                    "Authorization": "Bearer fake_invalid_api_key",
                    "x-jovian-source": "library",
                    "x-jovian-library-version": __version__,
                    "x-jovian-guest": "b6538d4dfde04fcf993463a828a9cec6",
                    "x-jovian-org": "staging"
                },
                params=None)

            assert context.exception.args[
                0] == 'Failed to fetch current user profile. (HTTP 401) Signature verification failed'
Example #2
0
def test_get_current_user_raises_exception(
    mock_requests_get, mock_request_get_api_key
):
    with fake_creds():
        # setUp
        creds = {
            "WEBAPP_URL": "https://staging.jovian.ml/",
            "GUEST_KEY": "b6538d4dfde04fcf993463a828a9cec6",
            "API_URL": "https://api-staging.jovian.ai",
            "API_KEY": "fake_invalid_api_key",
            "ORG_ID": "staging",
        }
        write_creds(creds)

        with pytest.raises(Exception) as context:
            get_current_user()

        mock_requests_get.assert_called_with(
            "https://api-staging.jovian.ai/user/profile",
            headers={
                "Authorization": "Bearer fake_invalid_api_key",
                "x-jovian-source": "library",
                "x-jovian-library-version": __version__,
                "x-jovian-guest": "b6538d4dfde04fcf993463a828a9cec6",
                "x-jovian-org": "staging",
            },
            params=None,
        )

        assert (
            str(context.value)
            == "Failed to fetch current user profile. (HTTP 401) Signature verification failed"
        )
Example #3
0
def _parse_project(project, filename, new_project):
    """Perform the required checks and get the final project name"""
    current_slug = get_cached_slug()

    # Check for existing project in-memory or in .jovianrc
    if not new_project and project is None:
        # From in-memory variable
        if current_slug is not None:
            project = current_slug
        # From .jovianrc file
        else:
            project = get_notebook_slug(filename)

    # Skip if project is not provided & can't be read
    if project is None:
        return None, None

    # Get project metadata for UUID & username/title
    if is_uuid(project):
        project_title = None
        metadata = api.get_gist(project)
    elif '/' in project:
        project_title = project.split('/')[1]
        username = api.get_current_user()['username']
        metadata = api.get_gist(project)
    # Attach username to the title
    else:
        project_title = project
        username = api.get_current_user()['username']
        metadata = api.get_gist(username + '/' + project)

    # Skip if metadata could not be found
    if not metadata:
        log('Creating a new project "' + username + '/' + project_title + '"')
        return project_title, None

    # Extract information from metadata
    username = metadata['owner']['username']
    project_title = metadata['title']
    project_id = metadata['slug']

    # Check if the current user can commit to this project
    permissions = api.get_gist_access(project_id)
    if not permissions['write']:
        return project_title, None

    # Log whether this is an update or creation
    if project_id is None:
        log('Creating a new notebook on ' + read_webapp_url())
    else:
        log('Updating notebook "' + username + "/" + project_title + '" on ' +
            read_webapp_url())

    return project_title, project_id
Example #4
0
    def test_get_current_user(self, mock_requests_get):
        with fake_creds('.jovian', 'credentials.json'):
            get_current_user()

            mock_requests_get.assert_called_with(
                'https://api-staging.jovian.ai/user/profile',
                headers={
                    "Authorization": "Bearer fake_api_key",
                    "x-jovian-source": "library",
                    "x-jovian-library-version": __version__,
                    "x-jovian-guest": "b6538d4dfde04fcf993463a828a9cec6",
                    "x-jovian-org": "staging"
                },
                params=None)
Example #5
0
def perform_colab_commit(project, privacy):
    # file_id, project, privacy api key
    file_id = get_colab_file_id()
    if file_id is None:
        log("Colab File Id is not provided", error=True)

    # /gist/colab-commit data = {file_id, project}, return status
    if '/' not in project:
        project = get_current_user()['username'] + '/' + project

    data = {'project': project, 'file_id': file_id, 'visibility': privacy}

    if privacy == 'auto':
        data['public'] = True
    elif privacy == 'secret' or privacy == 'private':
        data['public'] = False

    auth_headers = _h()

    log("Uploading colab notebook to Jovian...")
    res = post(url=_u('/gist/colab-commit'),
               data=data,
               headers=auth_headers)

    if res.status_code == 200:
        return res.json()['data']
    raise ApiError('Colab commit failed: ' + pretty(res))
Example #6
0
def perform_colab_commit(project, privacy):
    if '/' not in project:
        project = get_current_user()['username'] + '/' + project

    data = {
        'project': project,
        'file_id': get_colab_file_id(),
        'visibility': privacy
    }

    if privacy == 'auto':
        data['public'] = True
    elif privacy == 'secret' or privacy == 'private':
        data['public'] = False

    auth_headers = _h()

    log("Uploading colab notebook to Jovian...")
    res = post(url=_u('/gist/colab-commit'), data=data, headers=auth_headers)

    if res.status_code == 200:
        data, warning = parse_success_response(res)
        if warning:
            log(warning, error=True)
        return data
    raise ApiError('Colab commit failed: ' + pretty(res))