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'
def test_create_gist_simple_with_gist_slug(self, mock_requests_post): with fake_creds('.jovian', 'credentials.json'): create_gist_simple( filename= 'jovian/tests/resources/creds/.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': ('jovian/tests/resources/creds/.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_upload_file(self, mock_requests_post): with fake_creds('.jovian', 'credentials.json'): with open('jovian/tests/resources/creds/.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_records_raises_api_error(self, mock_requests_post, mock_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(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 context.exception.args[ 0] == 'Data logging failed: (HTTP 404) Gist not found'
def test_post_slack_message_safe(self, mock_requests_post, mock_request_get_api_key): with fake_creds('.jovian-notify', '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) 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_purge_api_key(self): with fake_creds('.jovian', 'credentials.json'): assert read_cred(API_TOKEN_KEY) == 'fake_api_key' purge_api_key() with self.assertRaises(KeyError): read_cred(API_TOKEN_KEY)
def test_get_gist_raises_exception(self, mock_requests_get, mock_get_api_key): with fake_creds('.jovian', 'credentials.json'): with self.assertRaises(Exception) as context: get_gist('fake_gist_too_large') assert context.exception.args[0] == 'Failed to retrieve metadata for notebook "fake_gist_too_large":' + \ ' (HTTP 500) Internal Server Error'
def test_read_creds_folder_exists(): with fake_creds('.jovian', 'credentials.json'): expected_result = { "WEBAPP_URL": "https://staging.jovian.ml/", "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_guest_key_generate_key(mock_uuid4): with fake_creds('.jovian-get-guest-key', 'credentials.json'): creds = { "WEBAPP_URL": "https://staging.jovian.ml/", "ORG_ID": "staging", "API_URL": "https://api-staging.jovian.ai" } write_creds(creds) assert get_guest_key() == "b66406dc02c3471bac27d923fb4c6b1e"
def test_write_creds(): with fake_creds('.jovian-write-creds', 'credentials.json'): creds = { "WEBAPP_URL": "https://staging.jovian.ml/", "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_request_once(self, mock_validate_api_key, mock_prompt): with fake_creds('.jovian-get-api-key', 'credentials.json'): creds = { "WEBAPP_URL": "https://staging.jovian.ml/", "ORG_ID": "staging", "API_URL": "https://api-staging.jovian.ai" } write_creds(creds) assert get_api_key() == "fake_api_key"
def test_get_api_key_api_error(self, mock_validate_api_key, mock_prompt): with fake_creds('.jovian-get-api-key', 'credentials.json'): creds = { "WEBAPP_URL": "https://staging.jovian.ml/", "ORG_ID": "staging", "API_URL": "https://api-staging.jovian.ai" } write_creds(creds) with self.assertRaises(ApiError): get_api_key()
def test_h(): with fake_creds('.jovian', 'credentials.json'): 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_purge_creds(): with fake_creds('.jovian-purge-creds', 'credentials.json'): os.makedirs(credentials.CONFIG_DIR, exist_ok=True) os.system( 'touch jovian/tests/resources/creds/.jovian-purge-creds/credentials.json' ) assert os.path.exists( 'jovian/tests/resources/creds/.jovian-purge-creds/credentials.json' ) == True purge_creds() assert os.path.exists( 'jovian/tests/resources/creds/.jovian-purge-creds/credentials.json' ) == False
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 test_write_cred_already_exists(): with fake_creds('.jovian-write-cred', 'credentials.json'): creds = { "WEBAPP_URL": "https://staging.jovian.ml/", "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_post_records(self, mock_requests_post): with fake_creds('.jovian', 'credentials.json'): 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_api_key", "x-jovian-source": "library", "x-jovian-library-version": __version__, "x-jovian-guest": "b6538d4dfde04fcf993463a828a9cec6", "x-jovian-org": "staging" }, json={'key': 'value'})
def test_add_slack_api_error(self, mock_get): with fake_creds('.jovian-add-slack', '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(ApiError): add_slack()
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)
def test_notify_safe_false_raises_api_error(self, mock_requests_post, mock_get_api_key): with fake_creds('.jovian-notify', '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) data = {'key': 'value'} with self.assertRaises(ApiError): notify(data)
def test_ensure_org_api_url_key_error(self, mock_is_flavor_pro, mock_request_org_id, mock_requests_get): with fake_creds('.jovian-api-key-error', 'credentials.json'): # setUp creds = { "WEBAPP_URL": "https://staging.jovian.ml/", "GUEST_KEY": "b6538d4dfde04fcf993463a828a9cec6" } write_creds(creds) with self.assertRaises(ConfigError) as context: ensure_org() msg = "Failed to extract API_URL from JSON configuration file https://no-api-key.jovian.ml/config.json" self.assertTrue(msg in context.exception.args[0])
def test_ensure_org_some_creds_exist_default_org_id( self, mock_is_flavor_pro, mock_request_org_id, mock_requests_get): with fake_creds('.jovian-some-creds', 'credentials.json'): # setUp creds = { "WEBAPP_URL": "https://staging.jovian.ml/", "GUEST_KEY": "b6538d4dfde04fcf993463a828a9cec6", "API_URL": "https://api-staging.jovian.ai" } write_creds(creds) ensure_org() assert read_api_url() == "https://api.jovian.ai" assert read_org_id() == "public" assert read_webapp_url() == "https://jovian.ml/"
def test_add_slack_errors(mock_get, capsys): with fake_creds('.jovian-add-slack', 'credentials.json'): # setUp creds = { "WEBAPP_URL": "https://staging.jovian.ml/", "GUEST_KEY": "b6538d4dfde04fcf993463a828a9cec6", "API_URL": "https://api-staging.jovian.ai", "API_KEY": "fake_api_key_error", "ORG_ID": "staging" } write_creds(creds) add_slack() captured = capsys.readouterr() assert captured.out.strip() == "[jovian] Invalid guest key"
def test_ensure_org_with_json_decode_error(self, mock_is_flavor_pro, mock_request_org_id, mock_requests_get): with fake_creds('.jovian-decode-error', 'credentials.json'): # setUp creds = { "WEBAPP_URL": "https://staging.jovian.ml/", "GUEST_KEY": "b6538d4dfde04fcf993463a828a9cec6", "API_URL": "https://api-staging.jovian.ai" } write_creds(creds) with self.assertRaises(ConfigError) as context: ensure_org() msg = "Failed to parse JSON configuration file from https://jsonerror.jovian.ml/config.json" self.assertTrue(msg in context.exception.args[0])
def test_add_slack(mock_get, capsys): with fake_creds('.jovian-add-slack', 'credentials.json'): # setUp creds = { "WEBAPP_URL": "https://staging.jovian.ml/", "GUEST_KEY": "b6538d4dfde04fcf993463a828a9cec6", "API_URL": "https://api-staging.jovian.ai", "API_KEY": "fake_api_key", "ORG_ID": "staging" } write_creds(creds) add_slack() captured = capsys.readouterr() assert captured.out.strip() == "[jovian] Slack already connected." + \ " \nWorkspace: jovian.ml\nConnected Channel: @rohit"
def test_ensure_org_with_unsuccessful_response(self, mock_is_flavor_pro, mock_request_org_id, mock_requests_get): with fake_creds('.jovian-unsuccessful-response', 'credentials.json'): # setUp creds = { "WEBAPP_URL": "https://staging.jovian.ml/", "GUEST_KEY": "b6538d4dfde04fcf993463a828a9cec6", "API_URL": "https://api-staging.jovian.ai" } write_creds(creds) with self.assertRaises(ConfigError) as context: ensure_org() msg = "Request to retrieve configuration file https://fakecompany.jovian.ml/config.json failed with " + \ "status_code 500 . Looks like there's something wrong with your setup." self.assertTrue(msg in context.exception.args[0])
def test_ensure_org_with_connection_error(self, mock_is_flavor_pro, mock_request_org_id, mock_requests_get): with fake_creds('.jovian-connection-error', 'credentials.json'): # setUp creds = { "WEBAPP_URL": "https://staging.jovian.ml/", "GUEST_KEY": "b6538d4dfde04fcf993463a828a9cec6", "API_URL": "https://api-staging.jovian.ai" } write_creds(creds) with self.assertRaises(ConfigError) as context: ensure_org() msg = "Failed to connect to https://fakecompany.jovian.ml/ . Please verify your organization ID and " + \ "ensure you are connected to the internet." self.assertTrue(msg in context.exception.args[0])
def test_notify_safe_true(mock_requests_post, mock_get_api_key, capsys): with fake_creds('.jovian-notify', '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) data = {'key': 'value'} notify(data, safe=True) captured = capsys.readouterr() assert captured.out.strip() == "[jovian] message_sent:False"
def test_notify_log_error(mock_requests_post, capsys): with fake_creds('.jovian-notify', 'credentials.json'): # setUp creds = { "WEBAPP_URL": "https://staging.jovian.ml/", "GUEST_KEY": "b6538d4dfde04fcf993463a828a9cec6", "API_URL": "https://api-staging.jovian.ai", "API_KEY": "fake_expired_api_key", "ORG_ID": "staging" } write_creds(creds) data = {'key': 'value'} notify(data) captured = capsys.readouterr() assert captured.err.strip() == "[jovian] Error: The token has expired"
def test_create_gist_simple_raises_api_error(self, mock_requests_post): with fake_creds('.jovian', '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(ApiError) as context: create_gist_simple( filename= 'jovian/tests/resources/creds/.jovian/credentials.json', title='Credentials', version_title='first version') mock_requests_post.assert_called_with( 'https://api-staging.jovian.ai/gist/create', data={ 'visibility': 'auto', 'public': True, 'title': 'Credentials', 'version_title': 'first version' }, files={ 'files': ('jovian/tests/resources/creds/.jovian/credentials.json', ANY) }, 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=None) assert context.exception.args[ 0] == 'File upload failed: (HTTP 404) Gist not found'