test_upload.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. """Integration tests for file upload."""
  2. from __future__ import annotations
  3. import time
  4. from typing import Generator
  5. import pytest
  6. from selenium.webdriver.common.by import By
  7. from reflex.testing import AppHarness
  8. def UploadFile():
  9. """App for testing dynamic routes."""
  10. import reflex as rx
  11. class UploadState(rx.State):
  12. _file_data: dict[str, str] = {}
  13. async def handle_upload(self, files: list[rx.UploadFile]):
  14. for file in files:
  15. upload_data = await file.read()
  16. self._file_data[file.filename or ""] = upload_data.decode("utf-8")
  17. def index():
  18. return rx.vstack(
  19. rx.input(
  20. value=UploadState.router.session.client_token,
  21. is_read_only=True,
  22. id="token",
  23. ),
  24. rx.upload(
  25. rx.vstack(
  26. rx.button("Select File"),
  27. rx.text("Drag and drop files here or click to select files"),
  28. ),
  29. ),
  30. rx.button(
  31. "Upload",
  32. on_click=lambda: UploadState.handle_upload(rx.upload_files()), # type: ignore
  33. id="upload_button",
  34. ),
  35. rx.box(
  36. rx.foreach(
  37. rx.selected_files,
  38. lambda f: rx.text(f),
  39. ),
  40. id="selected_files",
  41. ),
  42. rx.button(
  43. "Clear",
  44. on_click=rx.clear_selected_files,
  45. id="clear_button",
  46. ),
  47. )
  48. app = rx.App(state=UploadState)
  49. app.add_page(index)
  50. app.compile()
  51. @pytest.fixture(scope="session")
  52. def upload_file(tmp_path_factory) -> Generator[AppHarness, None, None]:
  53. """Start UploadFile app at tmp_path via AppHarness.
  54. Args:
  55. tmp_path_factory: pytest tmp_path_factory fixture
  56. Yields:
  57. running AppHarness instance
  58. """
  59. with AppHarness.create(
  60. root=tmp_path_factory.mktemp("upload_file"),
  61. app_source=UploadFile, # type: ignore
  62. ) as harness:
  63. yield harness
  64. @pytest.fixture
  65. def driver(upload_file: AppHarness):
  66. """Get an instance of the browser open to the upload_file app.
  67. Args:
  68. upload_file: harness for DynamicRoute app
  69. Yields:
  70. WebDriver instance.
  71. """
  72. assert upload_file.app_instance is not None, "app is not running"
  73. driver = upload_file.frontend()
  74. try:
  75. yield driver
  76. finally:
  77. driver.quit()
  78. @pytest.mark.asyncio
  79. async def test_upload_file(tmp_path, upload_file: AppHarness, driver):
  80. """Submit a file upload and check that it arrived on the backend.
  81. Args:
  82. tmp_path: pytest tmp_path fixture
  83. upload_file: harness for UploadFile app.
  84. driver: WebDriver instance.
  85. """
  86. assert upload_file.app_instance is not None
  87. token_input = driver.find_element(By.ID, "token")
  88. assert token_input
  89. # wait for the backend connection to send the token
  90. token = upload_file.poll_for_value(token_input)
  91. assert token is not None
  92. upload_box = driver.find_element(By.XPATH, "//input[@type='file']")
  93. assert upload_box
  94. upload_button = driver.find_element(By.ID, "upload_button")
  95. assert upload_button
  96. exp_name = "test.txt"
  97. exp_contents = "test file contents!"
  98. target_file = tmp_path / exp_name
  99. target_file.write_text(exp_contents)
  100. upload_box.send_keys(str(target_file))
  101. upload_button.click()
  102. # look up the backend state and assert on uploaded contents
  103. async def get_file_data():
  104. return (await upload_file.get_state(token))._file_data
  105. file_data = await AppHarness._poll_for_async(get_file_data)
  106. assert isinstance(file_data, dict)
  107. assert file_data[exp_name] == exp_contents
  108. # check that the selected files are displayed
  109. selected_files = driver.find_element(By.ID, "selected_files")
  110. assert selected_files.text == exp_name
  111. @pytest.mark.asyncio
  112. async def test_upload_file_multiple(tmp_path, upload_file: AppHarness, driver):
  113. """Submit several file uploads and check that they arrived on the backend.
  114. Args:
  115. tmp_path: pytest tmp_path fixture
  116. upload_file: harness for UploadFile app.
  117. driver: WebDriver instance.
  118. """
  119. assert upload_file.app_instance is not None
  120. token_input = driver.find_element(By.ID, "token")
  121. assert token_input
  122. # wait for the backend connection to send the token
  123. token = upload_file.poll_for_value(token_input)
  124. assert token is not None
  125. upload_box = driver.find_element(By.XPATH, "//input[@type='file']")
  126. assert upload_box
  127. upload_button = driver.find_element(By.ID, "upload_button")
  128. assert upload_button
  129. exp_files = {
  130. "test1.txt": "test file contents!",
  131. "test2.txt": "this is test file number 2!",
  132. "reflex.txt": "reflex is awesome!",
  133. }
  134. for exp_name, exp_contents in exp_files.items():
  135. target_file = tmp_path / exp_name
  136. target_file.write_text(exp_contents)
  137. upload_box.send_keys(str(target_file))
  138. time.sleep(0.2)
  139. # check that the selected files are displayed
  140. selected_files = driver.find_element(By.ID, "selected_files")
  141. assert selected_files.text == "\n".join(exp_files)
  142. # do the upload
  143. upload_button.click()
  144. # look up the backend state and assert on uploaded contents
  145. async def get_file_data():
  146. return (await upload_file.get_state(token))._file_data
  147. file_data = await AppHarness._poll_for_async(get_file_data)
  148. assert isinstance(file_data, dict)
  149. for exp_name, exp_contents in exp_files.items():
  150. assert file_data[exp_name] == exp_contents
  151. def test_clear_files(tmp_path, upload_file: AppHarness, driver):
  152. """Select then clear several file uploads and check that they are cleared.
  153. Args:
  154. tmp_path: pytest tmp_path fixture
  155. upload_file: harness for UploadFile app.
  156. driver: WebDriver instance.
  157. """
  158. assert upload_file.app_instance is not None
  159. token_input = driver.find_element(By.ID, "token")
  160. assert token_input
  161. # wait for the backend connection to send the token
  162. token = upload_file.poll_for_value(token_input)
  163. assert token is not None
  164. upload_box = driver.find_element(By.XPATH, "//input[@type='file']")
  165. assert upload_box
  166. upload_button = driver.find_element(By.ID, "upload_button")
  167. assert upload_button
  168. exp_files = {
  169. "test1.txt": "test file contents!",
  170. "test2.txt": "this is test file number 2!",
  171. "reflex.txt": "reflex is awesome!",
  172. }
  173. for exp_name, exp_contents in exp_files.items():
  174. target_file = tmp_path / exp_name
  175. target_file.write_text(exp_contents)
  176. upload_box.send_keys(str(target_file))
  177. time.sleep(0.2)
  178. # check that the selected files are displayed
  179. selected_files = driver.find_element(By.ID, "selected_files")
  180. assert selected_files.text == "\n".join(exp_files)
  181. clear_button = driver.find_element(By.ID, "clear_button")
  182. assert clear_button
  183. clear_button.click()
  184. # check that the selected files are cleared
  185. selected_files = driver.find_element(By.ID, "selected_files")
  186. assert selected_files.text == ""