Skip to content

Commit 10b0c45

Browse files
committed
test_upload: assert that files are properly downloaded
1 parent a09d416 commit 10b0c45

1 file changed

Lines changed: 37 additions & 22 deletions

File tree

tests/integration/test_upload.py

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@
88
from collections.abc import Generator
99
from pathlib import Path
1010
from typing import Any
11-
from urllib.parse import urlsplit
1211

1312
import pytest
1413
from reflex_base.constants.event import Endpoint
14+
from selenium.common.exceptions import NoAlertPresentException
1515
from selenium.webdriver.common.by import By
1616

1717
import reflex as rx
1818
from reflex.testing import AppHarness, WebDriver
1919

20-
from .utils import poll_for_navigation
21-
2220

2321
def UploadFile():
2422
"""App for testing dynamic routes."""
@@ -723,24 +721,32 @@ def test_upload_download_file(
723721
upload_box.send_keys(str(target_file))
724722
upload_button.click()
725723

724+
# Wait for the upload to complete.
725+
upload_done = driver.find_element(By.ID, "upload_done")
726+
assert upload_file.poll_for_value(upload_done, exp_not_equal="false") == "true"
727+
728+
# Configure the download directory using CDP.
729+
download_dir = tmp_path / "downloads"
730+
download_dir.mkdir()
731+
driver.execute_cdp_cmd(
732+
"Page.setDownloadBehavior",
733+
{"behavior": "allow", "downloadPath": str(download_dir)},
734+
)
735+
736+
downloaded_file = download_dir / exp_name
737+
726738
# Download via event embedded in frontend code.
727739
download_frontend = driver.find_element(By.ID, "download-frontend")
728-
with poll_for_navigation(driver):
729-
download_frontend.click()
730-
assert urlsplit(driver.current_url).path == f"/{Endpoint.UPLOAD.value}/test.txt"
731-
assert driver.find_element(by=By.TAG_NAME, value="body").text == exp_contents
732-
733-
# Go back and wait for the app to reload.
734-
with poll_for_navigation(driver):
735-
driver.back()
736-
poll_for_token(driver, upload_file)
740+
download_frontend.click()
741+
AppHarness.expect(lambda: downloaded_file.exists())
742+
assert downloaded_file.read_text() == exp_contents
743+
downloaded_file.unlink()
737744

738745
# Download via backend event handler.
739746
download_backend = driver.find_element(By.ID, "download-backend")
740-
with poll_for_navigation(driver):
741-
download_backend.click()
742-
assert urlsplit(driver.current_url).path == f"/{Endpoint.UPLOAD.value}/test.txt"
743-
assert driver.find_element(by=By.TAG_NAME, value="body").text == exp_contents
747+
download_backend.click()
748+
AppHarness.expect(lambda: downloaded_file.exists())
749+
assert downloaded_file.read_text() == exp_contents
744750

745751

746752
def test_uploaded_file_security_headers(
@@ -785,19 +791,28 @@ def test_uploaded_file_security_headers(
785791
assert resp.headers["content-disposition"] == "attachment"
786792
assert resp.headers["x-content-type-options"] == "nosniff"
787793

794+
# Configure the download directory using CDP.
795+
download_dir = tmp_path / "downloads"
796+
download_dir.mkdir()
797+
driver.execute_cdp_cmd(
798+
"Page.setDownloadBehavior",
799+
{"behavior": "allow", "downloadPath": str(download_dir)},
800+
)
801+
802+
downloaded_file = download_dir / exp_name
803+
788804
# Navigate to the uploaded HTML file in the browser and verify the script
789805
# does not execute (Content-Disposition: attachment prevents rendering).
790806
driver.get(upload_url)
791807
# If the browser rendered the HTML, an alert('xss') dialog would appear.
792808
# Verify no alert is present — the file should be downloaded, not rendered.
793-
from selenium.common.exceptions import NoAlertPresentException
794-
795-
try:
809+
with pytest.raises(NoAlertPresentException):
796810
alert = driver.switch_to.alert
797811
alert.dismiss()
798-
pytest.fail("Browser rendered the HTML and triggered an alert dialog")
799-
except NoAlertPresentException:
800-
pass # Expected: no alert because the file was downloaded, not rendered
812+
813+
# Also verify the file was downloaded with the correct contents.
814+
AppHarness.expect(lambda: downloaded_file.exists())
815+
assert downloaded_file.read_text() == exp_contents
801816

802817

803818
def test_on_drop(

0 commit comments

Comments
 (0)