helpers.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from collections import namedtuple
  2. from os import path
  3. import pytest
  4. from responses import RequestsMock, Response
  5. @pytest.fixture(scope="function")
  6. def responses():
  7. mock = RequestsMock(assert_all_requests_are_fired=True)
  8. with mock:
  9. yield mock
  10. _here = __file__
  11. ImageResponse = namedtuple("ImageResponse", ["raw", "url", "headers"])
  12. def _get_image_bytes(which):
  13. img_path = path.join(path.realpath(path.dirname(_here)), "images", which)
  14. with open(img_path, "rb") as fh:
  15. return fh.read()
  16. def _get_image_resp(which, mime):
  17. raw = _get_image_bytes(which)
  18. return Response(
  19. method="GET",
  20. body=raw,
  21. url="http://example/{}".format(which),
  22. headers={"Content-Type": mime, "Content-Length": str(len(raw))},
  23. stream=True,
  24. )
  25. @pytest.fixture(scope="function")
  26. def image_just_right():
  27. return _get_image_resp("good_image.png", "image/png")
  28. @pytest.fixture(scope="function")
  29. def image_too_big():
  30. return _get_image_resp("too_big.gif", "image/gif")
  31. @pytest.fixture(scope="function")
  32. def image_too_tall():
  33. return _get_image_resp("too_tall.png", "image/png")
  34. @pytest.fixture(scope="function")
  35. def image_too_wide():
  36. return _get_image_resp("too_wide.png", "image/png")
  37. @pytest.fixture(scope="function")
  38. def image_wrong_mime():
  39. return _get_image_resp("wrong_mime.svg", "image/svg+xml")
  40. @pytest.fixture(scope="function")
  41. def image_jpg():
  42. return _get_image_resp("image.jpg", "image/jpeg")
  43. @pytest.fixture(scope="function")
  44. def image_gif():
  45. return _get_image_resp("image.gif", "image/gif")
  46. @pytest.fixture(scope="function")
  47. def image_png():
  48. return _get_image_resp("image.png", "image/png")