def test_post_records_raises_api_error(mock_requests_post, mock_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(ApiError) as context: post_records("fake_gist_slug", {"key": "value"}) mock_requests_post.assert_called_with( "https://api-staging.jovian.ai/data/fake_gist_slug/commit", data=None, headers={ "Authorization": "Bearer fake_invalid_api_key", "x-jovian-source": "library", "x-jovian-library-version": __version__, "x-jovian-guest": "b6538d4dfde04fcf993463a828a9cec6", "x-jovian-org": "staging", }, json={"key": "value"}, ) assert ( str(context.value) == "Data logging failed: (HTTP 404) Gist not found" )
def test_post_slack_message_safe( mock_requests_post, 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) data = {"key": "value"} assert post_slack_message(data, safe=True) == { "data": {"messageSent": False} } mock_requests_post.assert_called_with( "https://api-staging.jovian.ai/slack/notify", data=None, headers={ "Authorization": "Bearer fake_invalid_api_key", "x-jovian-source": "library", "x-jovian-library-version": __version__, "x-jovian-guest": "b6538d4dfde04fcf993463a828a9cec6", "x-jovian-org": "staging", }, json={"key": "value"}, )
def test_upload_file(mock_requests_post): with fake_creds() as dir: with open( os.path.join(dir, ".jovian/credentials.json"), "rb" ) as f: upload_file( gist_slug="fake_gist_slug", file=("credentials.json", f), folder=".jovian", artifact=True, version_title="fake_version_title", ) mock_requests_post.assert_called_with( "https://api-staging.jovian.ai/gist/fake_gist_slug/upload", data={ "artifact": "true", "folder": ".jovian", "version_title": "fake_version_title", }, files={"files": ("credentials.json", ANY)}, headers={ "Authorization": "Bearer fake_api_key", "x-jovian-source": "library", "x-jovian-library-version": __version__, "x-jovian-guest": "b6538d4dfde04fcf993463a828a9cec6", "x-jovian-org": "staging", }, json=None, )
def test_post_blocks_raises_api_error(mock_requests_post, mock_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) blocks = [{"data": {"key": "value"}, "record_type": "metrics"}] with pytest.raises(ApiError) as context: post_blocks(blocks) mock_requests_post.assert_called_with( "https://api-staging.jovian.ai/data/record", data=None, headers={ "Authorization": "Bearer fake_invalid_api_key", "x-jovian-source": "library", "x-jovian-library-version": __version__, "x-jovian-guest": "b6538d4dfde04fcf993463a828a9cec6", "x-jovian-org": "staging", }, json=[{"data": {"key": "value"}, "record_type": "metrics"}], ) assert ( str(context.value) == "Data logging failed: (HTTP 500) Internal Server Error" )
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" )
def test_create_gist_simple_with_gist_slug(mock_requests_post): with fake_creds() as dir: create_gist_simple( filename=os.path.join(dir, ".jovian/credentials.json"), gist_slug="fake_gist_slug", title="Credentials", version_title="first version", ) mock_requests_post.assert_called_with( "https://api-staging.jovian.ai/gist/fake_gist_slug/upload", data={"version_title": "first version"}, files={ "files": ( os.path.join(dir, ".jovian/credentials.json"), ANY, ) }, headers={ "Authorization": "Bearer fake_api_key", "x-jovian-source": "library", "x-jovian-library-version": __version__, "x-jovian-guest": "b6538d4dfde04fcf993463a828a9cec6", "x-jovian-org": "staging", }, json=None, )
def test_purge_creds(): with fake_creds() as dir: assert os.path.exists(os.path.join(dir, '.jovian/credentials.json')) == True purge_creds() assert os.path.exists(os.path.join( dir, '.jovian/credentials.json')) == False
def test_capture_environment_from_config_none(mock_upload_conda_env, mock_upload_pip_env): with fake_creds(extra={"DEFAULT_CONFIG": {"environment": None}}): _capture_environment("auto", "fake_gist_slug", 2) mock_upload_conda_env.assert_not_called() mock_upload_pip_env.assert_not_called()
def test_attach_files(mock_attach_file, attach_files_kwargs, extra_config, mock_calls, capsys): with fake_creds(extra=extra_config): os.makedirs('tempdir/subdir') os.system('touch tempdir/file.txt && touch tempdir/subdir/file1.txt') # current notebook os.system('touch notebook.ipynb') # valid files os.mkdir('valid') valid_files = [ "file.txt", "file.py", "file.csv", "file.yaml", "file.yml", "file.ipynb", "file.tsv" ] for file in valid_files: os.system("touch valid/{}".format(file)) # invalid files os.mkdir('invalid') invalid_files = ["file.file", "no_extension", "file.pyc"] for file in invalid_files: os.system("touch invalid/{}".format(file)) _attach_files(gist_slug='fake_gist_slug', version=2, **attach_files_kwargs) mock_attach_file.assert_has_calls(mock_calls, any_order=True)
def test_parse_project_from_rcfile(mock_get_gist_access, mock_get_gist, mock_get_current_user, mock_get_notebook_slug): with fake_creds(): assert _parse_project(project=None, filename="file.ipynb", new_project=False) == ('demo-notebook', None)
def test_create_gist_simple_has_warning(mock_post_request, capsys): with fake_creds() as dir: create_gist_simple( filename=os.path.join(dir, ".jovian/credentials.json")) assert capsys.readouterr().err.strip( ) == '[jovian] Error: Uploaded gist has a warning'.strip()
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_reset_config_no_prompt_confirmation(capsys): with fake_creds(): reset_config(confirm=False) assert creds_exist() == False expected_result = "[jovian] Removing existing configuration. Run \"jovian configure\" to set up Jovian" captured = capsys.readouterr() assert captured.out.strip() == expected_result
def test_purge_api_key(): with fake_creds(): assert read_cred(API_TOKEN_KEY) == 'fake_api_key' purge_api_key() with pytest.raises(KeyError): read_cred(API_TOKEN_KEY)
def test_upload_file_has_warning(mock_post_request, capsys): with fake_creds() as dir: with open(os.path.join(dir, ".jovian/credentials.json"), "rb") as f: upload_file(gist_slug="fake_gist_slug", file=("credentials.json", f)) assert capsys.readouterr().err.strip( ) == '[jovian] Error: Uploaded gist has a warning'.strip()
def test_reset_config_confirm_false(mock_purge_creds, mock_confirm, capsys): with fake_creds(): reset_config() mock_purge_creds.assert_not_called() expected_result = "[jovian] Skipping.." captured = capsys.readouterr() assert captured.out.strip() == expected_result
def test_get_gist_raises_exception(mock_requests_get, mock_get_api_key): with fake_creds(): with pytest.raises(Exception) as context: get_gist("fake_gist_too_large") assert ( str(context.value) == 'Failed to retrieve metadata for notebook "fake_gist_too_large":' + " (HTTP 500) Internal Server Error")
def test_reset_config_no_creds(capsys): with fake_creds(): purge_creds() reset_config() expected_result = "[jovian] Jovian is not configured yet. Run \"jovian configure\" to set it up." captured = capsys.readouterr() assert captured.out.strip() == expected_result
def test_read_creds_folder_exists(): with fake_creds(): expected_result = { "WEBAPP_URL": "https://staging.jovian.ai/", "GUEST_KEY": "b6538d4dfde04fcf993463a828a9cec6", "ORG_ID": "staging", "API_URL": "https://api-staging.jovian.ai", "API_KEY": "fake_api_key" } assert read_creds() == expected_result
def test_get_api_key_request_once(mock_validate_api_key, mock_prompt): with fake_creds(): creds = { "WEBAPP_URL": "https://staging.jovian.ai/", "ORG_ID": "staging", "API_URL": "https://api-staging.jovian.ai" } write_creds(creds) assert get_api_key() == "fake_api_key"
def test_get_guest_key_generate_key(mock_uuid4): with fake_creds(): creds = { "WEBAPP_URL": "https://staging.jovian.ai/", "ORG_ID": "staging", "API_URL": "https://api-staging.jovian.ai" } write_creds(creds) assert get_guest_key() == "b66406dc02c3471bac27d923fb4c6b1e"
def test_h(): with fake_creds(): expected_result = { "Authorization": "Bearer fake_api_key", "x-jovian-source": "library", "x-jovian-library-version": __version__, "x-jovian-guest": "b6538d4dfde04fcf993463a828a9cec6", "x-jovian-org": "staging", } assert _h() == expected_result
def test_get_api_key_api_error(mock_validate_api_key, mock_prompt): with fake_creds(): creds = { "WEBAPP_URL": "https://staging.jovian.ai/", "ORG_ID": "staging", "API_URL": "https://api-staging.jovian.ai" } write_creds(creds) with pytest.raises(ApiError): get_api_key()
def test_write_creds(): with fake_creds(): creds = { "WEBAPP_URL": "https://staging.jovian.ai/", "GUEST_KEY": "b6538d4dfde04fcf993463a828a9cec6", "ORG_ID": "staging", "API_URL": "https://api-staging.jovian.ai" } write_creds(creds) assert read_creds() == creds
def test_get_api_key(mock_validate_api_key): with fake_creds(): creds = { "WEBAPP_URL": "https://staging.jovian.ml/", "ORG_ID": "staging", "API_KEY": "fake_api_key", "API_URL": "https://api-staging.jovian.ai" } write_creds(creds) assert get_api_key() == "fake_api_key"
def test_ensure_org_pro_raises_error(mock_is_flavor_pro, get_side_effect, request_org_id, creds, msg): with fake_creds(), \ mock.patch("jovian.utils.credentials.request_org_id", return_value=request_org_id), \ mock.patch("requests.get", side_effect=get_side_effect): write_creds(creds) with pytest.raises(ConfigError) as context: ensure_org() assert msg in str(context.value)
def test_add_slack_api_error(mock_get): with fake_creds(): 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(ApiError): add_slack()
def test_get_gist_access(mock_requests_get, mock_get_api_key): with fake_creds(): 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 test_write_cred_already_exists(): with fake_creds(): creds = { "WEBAPP_URL": "https://staging.jovian.ai/", "GUEST_KEY": "b6538d4dfde04fcf993463a828a9cec6", "ORG_ID": "staging", "API_URL": "https://api-staging.jovian.ai" } write_creds(creds) write_cred('ORG_ID', 'staging') expected_result = creds assert read_creds() == expected_result
def test_configure_confirm_no(mock_confirm, capsys): with fake_creds(): creds = read_creds() configure() # Check that creds were not modified assert read_creds() == creds expected_result = dedent(""" [jovian] It looks like Jovian is already configured ( check ~/.jovian/credentials.json ). [jovian] Skipping.. """).strip() captured = capsys.readouterr() assert captured.out.strip() == expected_result.strip()