def mock_requests_get(url, *args, **kwargs): if url == "https://api-staging.jovian.ai/slack/integration_details": if kwargs["headers"]["Authorization"] == "Bearer fake_api_key": data = { "data": { "slackAccount": { "accountId": 47, "channel": "@rohit", "deleted": False, "id": 10, "updatedAt": "Fri, 13 Dec 2019 13:41:27 GMT", "workspace": "jovian.ai", } }, "success": True, } return MockResponse(data, status_code=200) elif kwargs["headers"]["Authorization"] == "Bearer fake_api_key_error": data = { "errors": [{"code": 200, "message": "Invalid guest key"}], "success": False, } return MockResponse(data, 200) else: data = { "errors": [{"code": 401, "message": "Missing Authorization Header"}], "success": False, } return MockResponse(data, 401)
def mocked_requests(url, *args, **kwargs): if url == 'http://someurl.com/test.json': return MockResponse({'key': 'value'}, 200) elif url == 'http://someurl.com/noauth': return MockResponse({'message': 'invalid creds'}, 401) elif url == 'http://someurl.com/create': return MockResponse({'message': 'created successfully'}, 201) return MockResponse(None, 404)
def mock_requests_get(url, *args, **kwargs): if url == 'https://jovian.ai/config.json': data = {"API_URL": "https://api.jovian.ai"} return MockResponse(data, status_code=200) elif url == 'https://fakecompany.jovian.ai/config.json': return MockResponse({"msg": "Request failed"}, status_code=500, text="Fake internal server error") elif url == 'https://jsonerror.jovian.ai/config.json': data = {"API_URL": "https://api.jovian.ai"} res = MockResponse(data, status_code=200, text="response of fake json decode error") res.json = json_decode_error_raiser return res elif url == 'https://no-api-key.jovian.ai/config.json': data = {} return MockResponse(data, status_code=200, text="response with no api key") elif url == 'https://api-staging.jovian.ai/user/profile': key = kwargs['headers']['Authorization'] if key == 'Bearer fake_correct_auth_key': return MockResponse({'key': 'value'}, 200) return MockResponse({'key': 'value'}, 401)
def mock_request_get(*args, **kwargs): return MockResponse({ "CONFIG_NAME": "staging", "APP_NAME": "Jovian (Staging)", "APP_DESCRIPTION": "Share Jupyter notebooks instantly", "IS_PRODUCTION": True, "IS_FLAVOR_PRO": False, "COMPANY_LOGO_URL": "https://i.imgur.com/iOPEHyK.png", "API_URL": "https://api-staging.jovian.ai", "API_URL_ALT": "https://staging.jovian.ai/api", "AUTH_ENV": "staging", "LOGIN_REDIRECT_PATH": "/login", }, 200)
def mock_request_get(*args, **kwargs): data = [{ "id": "6492b857-8ea0-45af-a1ab-8769dd066235", "path": "Untitled.ipynb", "name": "", "type": "notebook", "kernel": { "id": "ee27a181-852b-4f21-a2c0-a34eacef769c", "name": "python3", "last_activity": "2020-03-02T08:00:15.767476Z", "execution_state": "idle", "connections": 1 }, "notebook": { "path": "Fake-Notebook.ipynb", "name": "" } }] return MockResponse(data, 200, json.dumps(data))
assert _h(fresh=fresh) == expected_result @pytest.mark.parametrize("gist, called_with_url", [ ( 'aakashns/jovian-tutorial', 'https://api-staging.jovian.ai/user/aakashns/gist/jovian-tutorial?gist_version=3', ), ( 'fake_gist_slug', 'https://api-staging.jovian.ai/gist/fake_gist_slug?gist_version=3', ), ]) @mock.patch("jovian.utils.clone.get", return_value=MockResponse({'data': { 'key': 'value' }}, 200)) def test_get_gist(mock_get, gist, called_with_url): with fake_creds(): get_gist(gist, 3, fresh=True) mock_get.assert_called_with(called_with_url, headers=HEADERS) @mock.patch("jovian.utils.clone.get", return_value=MockResponse({'data': { 'key': 'value' }}, 404)) def test_get_gist_raises_exception(mock_get): with fake_creds(): with pytest.raises(Exception):
def mock_requests_get(url, *args, **kwargs): if url == "https://api-staging.jovian.ai/user/profile": if kwargs["headers"]["Authorization"] == "Bearer fake_api_key": data = {"data": {"id": 47, "username": "******"}, "success": True} return MockResponse(data, status_code=200) else: data = { "errors": [{"code": 401, "message": "Signature verification failed"}], "success": False, } return MockResponse(data, status_code=401) elif ( url == "https://api-staging.jovian.ai/user/rohit/gist/demo-notebook" or url == "https://api-staging.jovian.ai/user/rohit/gist/demo-notebook?gist_version=3" or url == "https://api-staging.jovian.ai/gist/f67108fc906341d8b15209ce88ebc3d2" ): data = { "data": { "archived": False, "clonesCount": 0, "createdAt": 1578718557994, "currentUser": {"id": 47, "username": "******"}, "deleted": False, "description": None, } } return MockResponse(data, status_code=200) # Dictionary containing mock responses response_dict = { "https://api-staging.jovian.ai/gist/fake_nonexistent_gist": MockResponse( {"errors": [{"code": 404, "message": "Gist not found"}], "success": False}, status_code=404, ), "https://api-staging.jovian.ai/gist/fake_gist_too_large": MockResponse( {"message": "Internal Server Error"}, status_code=500 ), "https://api-staging.jovian.ai/gist/f67108fc906341d8b15209ce88ebc3d2/check-access": MockResponse( { "data": { "currentUser": {"id": 47, "username": "******"}, "read": True, "slug": "f67108fc906341d8b15209ce88ebc3d2", "write": True, }, "success": True, }, status_code=200, ), "https://api-staging.jovian.ai/gist/fake_nonexistent_gist/check-access": MockResponse( {"errors": [{"code": 404, "message": "Gist not found"}], "success": False}, status_code=404, ), } return response_dict[url]
def mock_requests_post(url, *args, **kwargs): if ( url == "https://api-staging.jovian.ai/gist/create" or url == "https://api-staging.jovian.ai/gist/fake_gist_slug/upload" ): if kwargs["headers"]["Authorization"] == "Bearer fake_api_key": data = {"data": {"message": "Gist created successfully"}} return MockResponse(data, status_code=200) else: data = { "errors": [{"code": 404, "message": "Gist not found"}], "success": False, } return MockResponse(data, status_code=404) elif url == "https://api-staging.jovian.ai/data/record": if kwargs["headers"]["Authorization"] == "Bearer fake_api_key": data = {"data": {"message": "Data logged successfully"}} return MockResponse(data, status_code=200) else: data = { "errors": [{"code": 500, "message": "Internal Server Error"}], "success": False, } return MockResponse(data, status_code=500) elif url == "https://api-staging.jovian.ai/data/fake_gist_slug/commit": if kwargs["headers"]["Authorization"] == "Bearer fake_api_key": data = {"data": {"message": "Data logged successfully"}} return MockResponse(data, status_code=200) else: data = { "errors": [{"code": 404, "message": "Gist not found"}], "success": False, } return MockResponse(data, status_code=404) elif url == "https://api-staging.jovian.ai/slack/notify": if kwargs["headers"]["Authorization"] == "Bearer fake_api_key": data = { "data": { "messageId": "60023f08f3b54801bd58cf6b37067ed6", "messageSent": True, }, "success": True, } return MockResponse(data, 200) elif kwargs["headers"]["Authorization"] == "Bearer fake_invalid_api_key": data = { "errors": [{"code": 401, "message": "The token is invalid"}], "success": False, } return MockResponse(data, 401) elif kwargs["headers"]["Authorization"] == "Bearer fake_expired_api_key": data = { "errors": [{"code": 200, "message": "The token has expired"}], "success": False, } return MockResponse(data, 200)
"x-jovian-guest": "b6538d4dfde04fcf993463a828a9cec6", "x-jovian-org": "staging", }, json=None, ) assert (str( context.value) == "File upload failed: (HTTP 404) Gist not found") @mock.patch('requests.post', return_value=MockResponse( { "data": { "message": "Gist created successfully" }, "errors": [{ "message": "Uploaded gist has a warning" }] }, 200)) 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() @mock.patch("requests.post", side_effect=mock_requests_post) def test_upload_file(mock_requests_post):