test_file_upload.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # Copyright 2021-2024 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 inspect
  12. import io
  13. import pathlib
  14. import tempfile
  15. import pytest
  16. from taipy.gui import Gui
  17. from taipy.gui.data.data_scope import _DataScopes
  18. from taipy.gui.utils import _get_non_existent_file_path
  19. def test_file_upload_no_varname(gui: Gui, helpers):
  20. gui.run(run_server=False)
  21. flask_client = gui._server.test_client()
  22. # Get the jsx once so that the page will be evaluated -> variable will be registered
  23. sid = helpers.create_scope_and_get_sid(gui)
  24. with pytest.warns(UserWarning):
  25. ret = flask_client.post(f"/taipy-uploads?client_id={sid}")
  26. assert ret.status_code == 400
  27. def test_file_upload_no_blob(gui: Gui, helpers):
  28. gui.run(run_server=False)
  29. flask_client = gui._server.test_client()
  30. # Get the jsx once so that the page will be evaluated -> variable will be registered
  31. sid = helpers.create_scope_and_get_sid(gui)
  32. with pytest.warns(UserWarning):
  33. ret = flask_client.post(f"/taipy-uploads?client_id={sid}", data={"var_name": "varname"})
  34. assert ret.status_code == 400
  35. def test_file_upload_no_filename(gui: Gui, helpers):
  36. gui.run(run_server=False)
  37. flask_client = gui._server.test_client()
  38. file = (io.BytesIO(b"abcdef"), "")
  39. # Get the jsx once so that the page will be evaluated -> variable will be registered
  40. sid = helpers.create_scope_and_get_sid(gui)
  41. with pytest.warns(UserWarning):
  42. ret = flask_client.post(f"/taipy-uploads?client_id={sid}", data={"var_name": "varname", "blob": file})
  43. assert ret.status_code == 400
  44. def test_file_upload_simple(gui: Gui, helpers):
  45. gui.run(run_server=False)
  46. flask_client = gui._server.test_client()
  47. # Get the jsx once so that the page will be evaluated -> variable will be registered
  48. sid = helpers.create_scope_and_get_sid(gui)
  49. file_name = "test.jpg"
  50. file = (io.BytesIO(b"abcdef"), file_name)
  51. upload_path = pathlib.Path(gui._get_config("upload_folder", tempfile.gettempdir()))
  52. file_name = _get_non_existent_file_path(upload_path, file_name).name
  53. ret = flask_client.post(
  54. f"/taipy-uploads?client_id={sid}",
  55. data={"var_name": "varname", "blob": file},
  56. content_type="multipart/form-data",
  57. )
  58. assert ret.status_code == 200
  59. created_file = upload_path / file_name
  60. assert created_file.exists()
  61. def test_file_upload_multi_part(gui: Gui, helpers):
  62. gui.run(run_server=False)
  63. flask_client = gui._server.test_client()
  64. # Get the jsx once so that the page will be evaluated -> variable will be registered
  65. sid = helpers.create_scope_and_get_sid(gui)
  66. file_name = "test2.jpg"
  67. file0 = (io.BytesIO(b"abcdef"), file_name)
  68. file1 = (io.BytesIO(b"abcdef"), file_name)
  69. upload_path = pathlib.Path(gui._get_config("upload_folder", tempfile.gettempdir()))
  70. file_name = _get_non_existent_file_path(upload_path, file_name).name
  71. ret = flask_client.post(
  72. f"/taipy-uploads?client_id={sid}",
  73. data={"var_name": "varname", "blob": file0, "total": "2", "part": "0"},
  74. content_type="multipart/form-data",
  75. )
  76. assert ret.status_code == 200
  77. file0_path = upload_path / f"{file_name}.part.0"
  78. assert file0_path.exists()
  79. ret = flask_client.post(
  80. f"/taipy-uploads?client_id={sid}",
  81. data={"var_name": "varname", "blob": file1, "total": "2", "part": "1"},
  82. content_type="multipart/form-data",
  83. )
  84. assert ret.status_code == 200
  85. file1_path = upload_path / f"{file_name}.part.1"
  86. assert file1_path.exists()
  87. file_path = upload_path / file_name
  88. assert file_path.exists()
  89. def test_file_upload_multiple(gui: Gui, helpers):
  90. var_name = "varname"
  91. gui._set_frame(inspect.currentframe())
  92. gui.run(run_server=False, single_client=True)
  93. flask_client = gui._server.test_client()
  94. with gui.get_flask_app().app_context():
  95. gui._bind_var_val(var_name, None)
  96. # Get the jsx once so that the page will be evaluated -> variable will be registered
  97. sid = _DataScopes._GLOBAL_ID
  98. file = (io.BytesIO(b"abcdef"), "test.jpg")
  99. ret = flask_client.post(
  100. f"/taipy-uploads?client_id={sid}", data={"var_name": var_name, "blob": file}, content_type="multipart/form-data"
  101. )
  102. assert ret.status_code == 200
  103. created_file = pathlib.Path(gui._get_config("upload_folder", tempfile.gettempdir())) / "test.jpg"
  104. assert created_file.exists()
  105. file2 = (io.BytesIO(b"abcdef"), "test2.jpg")
  106. ret = flask_client.post(
  107. f"/taipy-uploads?client_id={sid}",
  108. data={"var_name": var_name, "blob": file2, "multiple": "True"},
  109. content_type="multipart/form-data",
  110. )
  111. assert ret.status_code == 200
  112. created_file = pathlib.Path(gui._get_config("upload_folder", tempfile.gettempdir())) / "test2.jpg"
  113. assert created_file.exists()
  114. value = getattr(gui._bindings()._get_all_scopes()[sid], var_name)
  115. assert len(value) == 2