def test_get_gist_access_raises_exception( mock_requests_get, mock_get_api_key ): with fake_creds(): with pytest.raises(Exception) as context: get_gist_access("fake_nonexistent_gist") mock_requests_get.assert_called_with( "https://api-staging.jovian.ai/gist/fake_nonexistent_gist/check-access", 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, ) assert ( str(context.value) == "Failed to retrieve access permission for notebook" + ' "fake_nonexistent_gist" (retry with new_project=True to create a new notebook):' + " (HTTP 404) Gist not found" )
def test_get_gist_access(self, mock_requests_get, mock_get_api_key): with fake_creds('.jovian', 'credentials.json'): get_gist_access('f67108fc906341d8b15209ce88ebc3d2') mock_requests_get.assert_called_with( 'https://api-staging.jovian.ai/gist/f67108fc906341d8b15209ce88ebc3d2/check-access', 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)
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
def test_get_gist_access_raises_exception(self, mock_requests_get, mock_get_api_key): with fake_creds('.jovian', 'credentials.json'): with self.assertRaises(Exception) as context: get_gist_access('fake_nonexistent_gist') mock_requests_get.assert_called_with( 'https://api-staging.jovian.ai/gist/fake_nonexistent_gist/check-access', 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) assert context.exception.args[0] == 'Failed to retrieve access permission for notebook' + \ ' "fake_nonexistent_gist" (retry with new_project=True to create a new notebook):' +\ ' (HTTP 404) Gist not found'