def test_response_paged(server): mocked = { "result": [{ "username": "******" }, { "username": "******" }], "page": { "total_results": 52, "page_size": 2, "page_number": 1, "total_pages": 26, }, } server.mock_endpoint("/users/", json=mocked) client = Client("http://example.com/fasjson") response = client.list_users() assert response.result == mocked["result"] assert response.page == mocked["page"] assert str(response) == str(mocked["result"]) assert repr(response) == "<FASJSONResponse for list_users()>" with pytest.raises(PaginationError): response.prev_page() response.next_page() assert server.reqs.last_request.qs == {"page_size": ["2"], "page": ["2"]}
def test_response_single_page(server): mocked = {"result": []} server.mock_endpoint("/users/", json=mocked) client = Client("http://example.com/fasjson") response = client.list_users() assert response.result == mocked["result"] assert response.page is None with pytest.raises(PaginationError): response.prev_page() with pytest.raises(PaginationError): response.next_page()
def test_api_error_text(server): server.mock_endpoint( "/me/", status_code=500, reason="Server Error", text="Internal Server Error", ) with pytest.raises(APIError) as e: client = Client("http://example.com/fasjson") client.whoami() exc = e.value assert exc.code == 500 assert exc.message == "500 Server Error: Internal Server Error" assert exc.data.get("body") == "Internal Server Error"
def test_api_error(server): server.mock_endpoint( "/me/", status_code=500, reason="Server Error", json={"message": "Something's wrong"}, ) with pytest.raises(APIError) as e: client = Client("http://example.com/fasjson") client.whoami() exc = e.value assert exc.code == 500 assert exc.message == "Something's wrong" assert exc.data.get("body") == {"message": "Something's wrong"}
def test_client_wrong_url(): with requests_mock.mock() as m, pytest.raises(errors.ClientError) as e: m.get("http://example.com/specs/v1.json", status_code=404, reason="Not Found") Client("http://example.com/") err = e.value assert "error loading remote spec" == str(err) assert err.data["message"] == "404 Not Found"
def test_client_spec_invalid(): with requests_mock.mock() as m, pytest.raises(errors.ClientError) as e: m.get("http://example.com/specs/v1.json", text='{"foo": "bar"}') Client("http://example.com") err = e.value assert "schema validation failed" == str(err) assert err.code == 71
def test_client_spec_parse_error(): with requests_mock.mock() as m, pytest.raises(errors.ClientError) as e: m.get("http://example.com/specs/v1.json", text="{somethin: {wrong:") Client("http://example.com") err = e.value assert "remote data validation failed" == str(err) assert err.code == 71
def test_client_response_error(): with requests_mock.mock() as m, pytest.raises(errors.ClientError) as e: m.get("http://example.com/specs/v1.json", reason="Forbidden", status_code=403) Client("http://example.com") err = e.value assert "error loading remote spec" == str(err) assert err.data["status_code"] == 403 assert err.data["message"] == "403 Forbidden"
def test_client_conn_error(): with requests_mock.mock() as m, pytest.raises(errors.ClientError) as e: m.get( "http://example.com/specs/v1.json", exc=requests.exceptions.ConnectTimeout("A timeout occurred"), ) Client("http://example.com") err = e.value assert "error loading remote spec" == str(err)
def test_api_success(server): mocked = { "result": { "dn": "SRV/foo@bar,dc=example.test", "service": "SRV/foo" } } server.mock_endpoint("/me/", json=mocked) client = Client("http://example.com/fasjson") output = json.loads(client.me.whoami().response().result) assert mocked == output
def test_api_list_operations(server): client = Client("http://example.com/fasjson") assert client.operations == [ "sign_csr", "get_cert", "list_groups", "get_group", "get_group_members", "whoami", "list_users", "get_user", ]
def test_client_init(mocker): bravado_client = mocker.Mock(name="mock SwaggerClient") mocker.patch.object( bravado.client.SwaggerClient, "from_url", return_value=bravado_client ) c = Client("http://localhost/fasjson/") bravado.client.SwaggerClient.from_url.assert_called_once() call_args = bravado.client.SwaggerClient.from_url.call_args assert call_args[0] == ("http://localhost/fasjson/specs/v1.json",) assert call_args[1].get("config") == {} assert "http_client" in call_args[1] c.me.whoami() bravado_client.me.whoami.assert_called_once_with()
def test_response_wrapper(server): """Make sure attributes are forwarded to the operation""" client = Client("http://example.com/fasjson") operation = client.list_users assert operation.consumes == ["application/json"] assert operation.produces == ["application/json"]
def test_api_success(server): mocked = {"result": {"dn": "SRV/foo@bar,dc=example.test", "service": "SRV/foo"}} server.mock_endpoint("/me/", json=mocked) client = Client("http://example.com/fasjson") response = client.whoami() assert response.result == mocked["result"]
def test_api_unknown_operation(server): client = Client("http://example.com/fasjson") with pytest.raises(AttributeError) as e: client.unknown_op() assert str(e.value) == "No such operation: 'unknown_op'"
def test_client_normalize_url(mocker): mocker.patch.object(Client, "_make_bravado_client") c = Client("http://localhost/fasjson") assert c._spec_url == "http://localhost/fasjson/specs/v1.json"