|
8 | 8 | from collections.abc import Generator |
9 | 9 | from pathlib import Path |
10 | 10 | from typing import Any |
11 | | -from urllib.parse import urlsplit |
12 | 11 |
|
13 | 12 | import pytest |
14 | 13 | from reflex_base.constants.event import Endpoint |
| 14 | +from selenium.common.exceptions import NoAlertPresentException |
15 | 15 | from selenium.webdriver.common.by import By |
16 | 16 |
|
17 | 17 | import reflex as rx |
18 | 18 | from reflex.testing import AppHarness, WebDriver |
19 | 19 |
|
20 | | -from .utils import poll_for_navigation |
21 | | - |
22 | 20 |
|
23 | 21 | def UploadFile(): |
24 | 22 | """App for testing dynamic routes.""" |
@@ -723,24 +721,32 @@ def test_upload_download_file( |
723 | 721 | upload_box.send_keys(str(target_file)) |
724 | 722 | upload_button.click() |
725 | 723 |
|
| 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 | + |
726 | 738 | # Download via event embedded in frontend code. |
727 | 739 | 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() |
737 | 744 |
|
738 | 745 | # Download via backend event handler. |
739 | 746 | 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 |
744 | 750 |
|
745 | 751 |
|
746 | 752 | def test_uploaded_file_security_headers( |
@@ -785,19 +791,28 @@ def test_uploaded_file_security_headers( |
785 | 791 | assert resp.headers["content-disposition"] == "attachment" |
786 | 792 | assert resp.headers["x-content-type-options"] == "nosniff" |
787 | 793 |
|
| 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 | + |
788 | 804 | # Navigate to the uploaded HTML file in the browser and verify the script |
789 | 805 | # does not execute (Content-Disposition: attachment prevents rendering). |
790 | 806 | driver.get(upload_url) |
791 | 807 | # If the browser rendered the HTML, an alert('xss') dialog would appear. |
792 | 808 | # 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): |
796 | 810 | alert = driver.switch_to.alert |
797 | 811 | 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 |
801 | 816 |
|
802 | 817 |
|
803 | 818 | def test_on_drop( |
|
0 commit comments