1
0

test_file_upload.py 5.6 KB

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