Exemplo n.º 1
0
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"
        )
Exemplo n.º 2
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'
Exemplo n.º 3
0
def test_post_slack_message_raises_api_error(
    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"}

        with pytest.raises(ApiError):
            post_slack_message(data)

        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"},
        )
Exemplo n.º 4
0
    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'
Exemplo n.º 5
0
    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'})
Exemplo n.º 6
0
def fake_creds(config_dir='.jovian',
               creds_filename='credentials.json',
               extra=None):
    with temp_directory() as dir:
        _d, _f = credentials.CONFIG_DIR, credentials.CREDS_FNAME
        credentials.CONFIG_DIR = os.path.join(dir, config_dir)
        credentials.CREDS_FNAME = creds_filename
        creds = {
            "GUEST_KEY": "b6538d4dfde04fcf993463a828a9cec6",
            "API_URL": "https://api-staging.jovian.ai",
            "WEBAPP_URL": "https://staging.jovian.ai/",
            "ORG_ID": "staging",
            "API_KEY": "fake_api_key",
        }

        if extra and isinstance(extra, dict):
            creds.update(extra)

        write_creds(creds)
        try:
            yield dir
        finally:
            purge_config()
        credentials.CONFIG_DIR = _d
        credentials.CREDS_FNAME = _f
Exemplo n.º 7
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"
        )
Exemplo n.º 8
0
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"
        )
Exemplo n.º 9
0
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"
Exemplo n.º 10
0
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"
Exemplo n.º 11
0
    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()
Exemplo n.º 12
0
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"
Exemplo n.º 13
0
    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"
Exemplo n.º 14
0
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
Exemplo n.º 15
0
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()
Exemplo n.º 16
0
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)
Exemplo n.º 17
0
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()
Exemplo n.º 18
0
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
Exemplo n.º 19
0
    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()
Exemplo n.º 20
0
def test_add_slack(mock_requests_get, api_key, expected_result, capsys):
    with fake_creds():
        creds = {
            "WEBAPP_URL": "https://staging.jovian.ml/",
            "GUEST_KEY": "b6538d4dfde04fcf993463a828a9cec6",
            "API_URL": "https://api-staging.jovian.ai",
            "API_KEY": api_key,
            "ORG_ID": "staging",
        }
        write_creds(creds)

        add_slack()

        captured = capsys.readouterr()
        assert captured.out.strip() == expected_result
Exemplo n.º 21
0
    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)
Exemplo n.º 22
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/"
Exemplo n.º 23
0
    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])
Exemplo n.º 24
0
def test_create_gist_simple_raises_api_error(mock_requests_post):
    with fake_creds() as dir:
        # 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:
            create_gist_simple(
                filename=os.path.join(dir, ".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": (
                    os.path.join(dir, ".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 (
            str(context.value)
            == "File upload failed: (HTTP 404) Gist not found"
        )
Exemplo n.º 25
0
    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])
Exemplo n.º 26
0
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"
Exemplo n.º 27
0
def test_upload_file_raises_api_error(mock_requests_post, mock_get_api_key):
    with fake_creds() as dir:
        # 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:
            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_invalid_api_key",
                "x-jovian-source": "library",
                "x-jovian-library-version": __version__,
                "x-jovian-guest": "b6538d4dfde04fcf993463a828a9cec6",
                "x-jovian-org": "staging",
            },
            json=None,
        )

        assert (
            str(context.value)
            == "File upload failed: (HTTP 404) Gist not found"
        )
Exemplo n.º 28
0
def fake_creds(config_dir, creds_filename):
    _d, _f = credentials.CONFIG_DIR, credentials.CREDS_FNAME
    credentials.CONFIG_DIR = 'jovian/tests/resources/creds/' + config_dir
    credentials.CREDS_FNAME = creds_filename
    creds = {
        "GUEST_KEY": "b6538d4dfde04fcf993463a828a9cec6",
        "API_URL": "https://api-staging.jovian.ai",
        "WEBAPP_URL": "https://staging.jovian.ml/",
        "ORG_ID": "staging",
        "API_KEY": "fake_api_key"
    }
    write_creds(creds)
    try:
        yield
    finally:
        purge_config()
    credentials.CONFIG_DIR = _d
    credentials.CREDS_FNAME = _f
Exemplo n.º 29
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])
Exemplo n.º 30
0
    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])