test_image_path.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # Copyright 2023 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import pathlib
  12. from unittest.mock import patch
  13. from taipy.gui import Gui
  14. def test_image_path_not_found(gui: Gui, helpers):
  15. with patch("sys.argv", ["prog"]):
  16. gui.run(run_server=False)
  17. flask_client = gui._server.test_client()
  18. # Get the jsx once so that the page will be evaluated -> variable will be registered
  19. sid = helpers.create_scope_and_get_sid(gui)
  20. ret = flask_client.get(f"/taipy-images/images/img.png?client_id={sid}")
  21. assert ret.status_code == 404
  22. def test_image_path_found(gui: Gui, helpers):
  23. url = gui._get_content(
  24. "img", str((pathlib.Path(__file__).parent.parent.parent / "resources" / "fred.png").resolve()), True
  25. )
  26. with patch("sys.argv", ["prog"]):
  27. gui.run(run_server=False)
  28. flask_client = gui._server.test_client()
  29. # Get the jsx once so that the page will be evaluated -> variable will be registered
  30. sid = helpers.create_scope_and_get_sid(gui)
  31. ret = flask_client.get(f"{url}?client_id={sid}")
  32. assert ret.status_code == 200
  33. def test_image_data_too_big(gui: Gui, helpers):
  34. with open((pathlib.Path(__file__).parent.parent.parent / "resources" / "taipan.jpg"), "rb") as big_file:
  35. url = gui._get_content("img", big_file.read(), True)
  36. assert not url.startswith("data:")