test_int_api_provider.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from http import HTTPStatus
  2. import pytest
  3. from clean_python import ctx
  4. from clean_python import Tenant
  5. from clean_python.api_client import ApiException
  6. from clean_python.api_client import ApiProvider
  7. async def fake_token():
  8. return {"Authorization": "Bearer token"}
  9. @pytest.fixture
  10. def provider(fastapi_example_app) -> ApiProvider:
  11. ctx.tenant = Tenant(id=2, name="")
  12. yield ApiProvider(fastapi_example_app + "/", fake_token)
  13. ctx.tenant = None
  14. async def test_request_params(provider: ApiProvider):
  15. response = await provider.request(
  16. "GET", "v1/books", params={"limit": 10, "offset": 2}
  17. )
  18. assert isinstance(response, dict)
  19. assert response["limit"] == 10
  20. assert response["offset"] == 2
  21. async def test_request_json_body(provider: ApiProvider):
  22. response = await provider.request(
  23. "POST", "v1/books", json={"title": "test_body", "author": {"name": "foo"}}
  24. )
  25. assert isinstance(response, dict)
  26. assert response["title"] == "test_body"
  27. assert response["author"] == {"name": "foo"}
  28. async def test_request_form_body(provider: ApiProvider):
  29. response = await provider.request("POST", "v1/form", fields={"name": "foo"})
  30. assert isinstance(response, dict)
  31. assert response["name"] == "foo"
  32. # files are not supported (yet)
  33. #
  34. # async def test_request_form_file(provider: ApiProvider):
  35. # response = await provider.request("POST", "v1/file", fields={"file": ("x.txt", b"foo")})
  36. # assert isinstance(response, dict)
  37. # assert response["x.txt"] == "foo"
  38. @pytest.fixture
  39. async def book(provider: ApiProvider):
  40. return await provider.request(
  41. "POST", "v1/books", json={"title": "fixture", "author": {"name": "foo"}}
  42. )
  43. async def test_no_content(provider: ApiProvider, book):
  44. response = await provider.request("DELETE", f"v1/books/{book['id']}")
  45. assert response is None
  46. async def test_not_found(provider: ApiProvider):
  47. with pytest.raises(ApiException) as e:
  48. await provider.request("GET", "v1/book")
  49. assert e.value.status is HTTPStatus.NOT_FOUND
  50. assert e.value.args[0] == {"detail": "Not Found"}
  51. async def test_bad_request(provider: ApiProvider):
  52. with pytest.raises(ApiException) as e:
  53. await provider.request("GET", "v1/books", params={"limit": "foo"})
  54. assert e.value.status is HTTPStatus.BAD_REQUEST
  55. assert e.value.args[0]["detail"][0]["loc"] == ["query", "limit"]
  56. async def test_no_json_response(provider: ApiProvider):
  57. with pytest.raises(ApiException) as e:
  58. await provider.request("GET", "v1/text")
  59. assert e.value.args[0] == "Unexpected content type 'text/plain; charset=utf-8'"
  60. async def test_urlencode(provider: ApiProvider):
  61. response = await provider.request("PUT", "v1/urlencode/x?")
  62. assert isinstance(response, dict)
  63. assert response["name"] == "x?"
  64. async def test_request_raw(provider: ApiProvider, book):
  65. response = await provider.request_raw("GET", f"v1/books/{book['id']}")
  66. assert response.status is HTTPStatus.OK
  67. assert len(response.data) > 0
  68. assert response.content_type == "application/json"