test_upload.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. """Integration tests for file upload."""
  2. from __future__ import annotations
  3. import asyncio
  4. import time
  5. from typing import Generator
  6. import pytest
  7. from selenium.webdriver.common.by import By
  8. from reflex.testing import AppHarness, WebDriver
  9. def UploadFile():
  10. """App for testing dynamic routes."""
  11. from typing import Dict, List
  12. import reflex_chakra as rc
  13. import reflex as rx
  14. class UploadState(rx.State):
  15. _file_data: Dict[str, str] = {}
  16. event_order: List[str] = []
  17. progress_dicts: List[dict] = []
  18. async def handle_upload(self, files: List[rx.UploadFile]):
  19. for file in files:
  20. upload_data = await file.read()
  21. self._file_data[file.filename or ""] = upload_data.decode("utf-8")
  22. async def handle_upload_secondary(self, files: List[rx.UploadFile]):
  23. for file in files:
  24. upload_data = await file.read()
  25. self._file_data[file.filename or ""] = upload_data.decode("utf-8")
  26. yield UploadState.chain_event
  27. def upload_progress(self, progress):
  28. assert progress
  29. self.event_order.append("upload_progress")
  30. self.progress_dicts.append(progress)
  31. def chain_event(self):
  32. self.event_order.append("chain_event")
  33. def index():
  34. return rx.vstack(
  35. rc.input(
  36. value=UploadState.router.session.client_token,
  37. is_read_only=True,
  38. id="token",
  39. ),
  40. rx.heading("Default Upload"),
  41. rx.upload.root(
  42. rx.vstack(
  43. rx.button("Select File"),
  44. rx.text("Drag and drop files here or click to select files"),
  45. ),
  46. ),
  47. rx.button(
  48. "Upload",
  49. on_click=lambda: UploadState.handle_upload(rx.upload_files()), # type: ignore
  50. id="upload_button",
  51. ),
  52. rx.box(
  53. rx.foreach(
  54. rx.selected_files,
  55. lambda f: rx.text(f, as_="p"),
  56. ),
  57. id="selected_files",
  58. ),
  59. rx.button(
  60. "Clear",
  61. on_click=rx.clear_selected_files,
  62. id="clear_button",
  63. ),
  64. rx.heading("Secondary Upload"),
  65. rx.upload.root(
  66. rx.vstack(
  67. rx.button("Select File"),
  68. rx.text("Drag and drop files here or click to select files"),
  69. ),
  70. id="secondary",
  71. ),
  72. rx.button(
  73. "Upload",
  74. on_click=UploadState.handle_upload_secondary( # type: ignore
  75. rx.upload_files(
  76. upload_id="secondary",
  77. on_upload_progress=UploadState.upload_progress,
  78. ),
  79. ),
  80. id="upload_button_secondary",
  81. ),
  82. rx.box(
  83. rx.foreach(
  84. rx.selected_files("secondary"),
  85. lambda f: rx.text(f, as_="p"),
  86. ),
  87. id="selected_files_secondary",
  88. ),
  89. rx.button(
  90. "Clear",
  91. on_click=rx.clear_selected_files("secondary"),
  92. id="clear_button_secondary",
  93. ),
  94. rx.vstack(
  95. rx.foreach(
  96. UploadState.progress_dicts, # type: ignore
  97. lambda d: rx.text(d.to_string()),
  98. )
  99. ),
  100. rx.button(
  101. "Cancel",
  102. on_click=rx.cancel_upload("secondary"),
  103. id="cancel_button_secondary",
  104. ),
  105. )
  106. app = rx.App(state=rx.State)
  107. app.add_page(index)
  108. @pytest.fixture(scope="module")
  109. def upload_file(tmp_path_factory) -> Generator[AppHarness, None, None]:
  110. """Start UploadFile app at tmp_path via AppHarness.
  111. Args:
  112. tmp_path_factory: pytest tmp_path_factory fixture
  113. Yields:
  114. running AppHarness instance
  115. """
  116. with AppHarness.create(
  117. root=tmp_path_factory.mktemp("upload_file"),
  118. app_source=UploadFile, # type: ignore
  119. ) as harness:
  120. yield harness
  121. @pytest.fixture
  122. def driver(upload_file: AppHarness):
  123. """Get an instance of the browser open to the upload_file app.
  124. Args:
  125. upload_file: harness for DynamicRoute app
  126. Yields:
  127. WebDriver instance.
  128. """
  129. assert upload_file.app_instance is not None, "app is not running"
  130. driver = upload_file.frontend()
  131. try:
  132. yield driver
  133. finally:
  134. driver.quit()
  135. @pytest.mark.parametrize("secondary", [False, True])
  136. @pytest.mark.asyncio
  137. async def test_upload_file(
  138. tmp_path, upload_file: AppHarness, driver: WebDriver, secondary: bool
  139. ):
  140. """Submit a file upload and check that it arrived on the backend.
  141. Args:
  142. tmp_path: pytest tmp_path fixture
  143. upload_file: harness for UploadFile app.
  144. driver: WebDriver instance.
  145. secondary: whether to use the secondary upload form
  146. """
  147. assert upload_file.app_instance is not None
  148. token_input = driver.find_element(By.ID, "token")
  149. assert token_input
  150. # wait for the backend connection to send the token
  151. token = upload_file.poll_for_value(token_input)
  152. assert token is not None
  153. full_state_name = upload_file.get_full_state_name(["_upload_state"])
  154. state_name = upload_file.get_state_name("_upload_state")
  155. substate_token = f"{token}_{full_state_name}"
  156. suffix = "_secondary" if secondary else ""
  157. upload_box = driver.find_elements(By.XPATH, "//input[@type='file']")[
  158. 1 if secondary else 0
  159. ]
  160. assert upload_box
  161. upload_button = driver.find_element(By.ID, f"upload_button{suffix}")
  162. assert upload_button
  163. exp_name = "test.txt"
  164. exp_contents = "test file contents!"
  165. target_file = tmp_path / exp_name
  166. target_file.write_text(exp_contents)
  167. upload_box.send_keys(str(target_file))
  168. upload_button.click()
  169. # look up the backend state and assert on uploaded contents
  170. async def get_file_data():
  171. return (
  172. (await upload_file.get_state(substate_token))
  173. .substates[state_name]
  174. ._file_data
  175. )
  176. file_data = await AppHarness._poll_for_async(get_file_data)
  177. assert isinstance(file_data, dict)
  178. assert file_data[exp_name] == exp_contents
  179. # check that the selected files are displayed
  180. selected_files = driver.find_element(By.ID, f"selected_files{suffix}")
  181. assert selected_files.text == exp_name
  182. state = await upload_file.get_state(substate_token)
  183. if secondary:
  184. # only the secondary form tracks progress and chain events
  185. assert state.substates[state_name].event_order.count("upload_progress") == 1
  186. assert state.substates[state_name].event_order.count("chain_event") == 1
  187. @pytest.mark.asyncio
  188. async def test_upload_file_multiple(tmp_path, upload_file: AppHarness, driver):
  189. """Submit several file uploads and check that they arrived on the backend.
  190. Args:
  191. tmp_path: pytest tmp_path fixture
  192. upload_file: harness for UploadFile app.
  193. driver: WebDriver instance.
  194. """
  195. assert upload_file.app_instance is not None
  196. token_input = driver.find_element(By.ID, "token")
  197. assert token_input
  198. # wait for the backend connection to send the token
  199. token = upload_file.poll_for_value(token_input)
  200. assert token is not None
  201. full_state_name = upload_file.get_full_state_name(["_upload_state"])
  202. state_name = upload_file.get_state_name("_upload_state")
  203. substate_token = f"{token}_{full_state_name}"
  204. upload_box = driver.find_element(By.XPATH, "//input[@type='file']")
  205. assert upload_box
  206. upload_button = driver.find_element(By.ID, "upload_button")
  207. assert upload_button
  208. exp_files = {
  209. "test1.txt": "test file contents!",
  210. "test2.txt": "this is test file number 2!",
  211. "reflex.txt": "reflex is awesome!",
  212. }
  213. for exp_name, exp_contents in exp_files.items():
  214. target_file = tmp_path / exp_name
  215. target_file.write_text(exp_contents)
  216. upload_box.send_keys(str(target_file))
  217. time.sleep(0.2)
  218. # check that the selected files are displayed
  219. selected_files = driver.find_element(By.ID, "selected_files")
  220. assert selected_files.text == "\n".join(exp_files)
  221. # do the upload
  222. upload_button.click()
  223. # look up the backend state and assert on uploaded contents
  224. async def get_file_data():
  225. return (
  226. (await upload_file.get_state(substate_token))
  227. .substates[state_name]
  228. ._file_data
  229. )
  230. file_data = await AppHarness._poll_for_async(get_file_data)
  231. assert isinstance(file_data, dict)
  232. for exp_name, exp_contents in exp_files.items():
  233. assert file_data[exp_name] == exp_contents
  234. @pytest.mark.parametrize("secondary", [False, True])
  235. def test_clear_files(
  236. tmp_path, upload_file: AppHarness, driver: WebDriver, secondary: bool
  237. ):
  238. """Select then clear several file uploads and check that they are cleared.
  239. Args:
  240. tmp_path: pytest tmp_path fixture
  241. upload_file: harness for UploadFile app.
  242. driver: WebDriver instance.
  243. secondary: whether to use the secondary upload form.
  244. """
  245. assert upload_file.app_instance is not None
  246. token_input = driver.find_element(By.ID, "token")
  247. assert token_input
  248. # wait for the backend connection to send the token
  249. token = upload_file.poll_for_value(token_input)
  250. assert token is not None
  251. suffix = "_secondary" if secondary else ""
  252. upload_box = driver.find_elements(By.XPATH, "//input[@type='file']")[
  253. 1 if secondary else 0
  254. ]
  255. assert upload_box
  256. upload_button = driver.find_element(By.ID, f"upload_button{suffix}")
  257. assert upload_button
  258. exp_files = {
  259. "test1.txt": "test file contents!",
  260. "test2.txt": "this is test file number 2!",
  261. "reflex.txt": "reflex is awesome!",
  262. }
  263. for exp_name, exp_contents in exp_files.items():
  264. target_file = tmp_path / exp_name
  265. target_file.write_text(exp_contents)
  266. upload_box.send_keys(str(target_file))
  267. time.sleep(0.2)
  268. # check that the selected files are displayed
  269. selected_files = driver.find_element(By.ID, f"selected_files{suffix}")
  270. assert selected_files.text == "\n".join(exp_files)
  271. clear_button = driver.find_element(By.ID, f"clear_button{suffix}")
  272. assert clear_button
  273. clear_button.click()
  274. # check that the selected files are cleared
  275. selected_files = driver.find_element(By.ID, f"selected_files{suffix}")
  276. assert selected_files.text == ""
  277. # TODO: drag and drop directory
  278. # https://gist.github.com/florentbr/349b1ab024ca9f3de56e6bf8af2ac69e
  279. @pytest.mark.asyncio
  280. async def test_cancel_upload(tmp_path, upload_file: AppHarness, driver: WebDriver):
  281. """Submit a large file upload and cancel it.
  282. Args:
  283. tmp_path: pytest tmp_path fixture
  284. upload_file: harness for UploadFile app.
  285. driver: WebDriver instance.
  286. """
  287. assert upload_file.app_instance is not None
  288. token_input = driver.find_element(By.ID, "token")
  289. assert token_input
  290. # wait for the backend connection to send the token
  291. token = upload_file.poll_for_value(token_input)
  292. assert token is not None
  293. state_name = upload_file.get_state_name("_upload_state")
  294. state_full_name = upload_file.get_full_state_name(["_upload_state"])
  295. substate_token = f"{token}_{state_full_name}"
  296. upload_box = driver.find_elements(By.XPATH, "//input[@type='file']")[1]
  297. upload_button = driver.find_element(By.ID, f"upload_button_secondary")
  298. cancel_button = driver.find_element(By.ID, f"cancel_button_secondary")
  299. exp_name = "large.txt"
  300. target_file = tmp_path / exp_name
  301. with target_file.open("wb") as f:
  302. f.seek(1024 * 1024 * 256)
  303. f.write(b"0")
  304. upload_box.send_keys(str(target_file))
  305. upload_button.click()
  306. await asyncio.sleep(0.3)
  307. cancel_button.click()
  308. # look up the backend state and assert on progress
  309. state = await upload_file.get_state(substate_token)
  310. assert state.substates[state_name].progress_dicts
  311. assert exp_name not in state.substates[state_name]._file_data
  312. target_file.unlink()