test_upload.py 6.9 KB

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