initial attempt at writing test for urls (#2689)
* initial attempt at writing test for urls * turn it into a pytest test * fix precommit and add wrong url to make sure test work * fix darglint error and fix url set error * black error * add to test .md files in reflex as well * update to fix masen comment * black formatting * Fix trailing slash in reflex/state.py --------- Co-authored-by: Tom Gotsman <tomgotsman@Toms-MacBook-Pro.local> Co-authored-by: Tom Gotsman <tomgotsman@toms-mbp.lan> Co-authored-by: Masen Furer <m_github@0x26.net>
This commit is contained in:
parent
0a18eaa28b
commit
93fc269860
67
integration/test_urls.py
Executable file
67
integration/test_urls.py
Executable file
@ -0,0 +1,67 @@
|
|||||||
|
"""Integration tests for all urls in Reflex."""
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
import requests
|
||||||
|
|
||||||
|
|
||||||
|
def check_urls(repo_dir):
|
||||||
|
"""Check that all URLs in the repo are valid and secure.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
repo_dir: The directory of the repo.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A list of errors.
|
||||||
|
"""
|
||||||
|
url_pattern = re.compile(r'http[s]?://reflex\.dev[^\s")]*')
|
||||||
|
errors = []
|
||||||
|
|
||||||
|
for root, _dirs, files in os.walk(repo_dir):
|
||||||
|
if "__pycache__" in root:
|
||||||
|
continue
|
||||||
|
|
||||||
|
for file_name in files:
|
||||||
|
if not file_name.endswith(".py") and not file_name.endswith(".md"):
|
||||||
|
continue
|
||||||
|
|
||||||
|
file_path = os.path.join(root, file_name)
|
||||||
|
try:
|
||||||
|
with open(file_path, "r", encoding="utf-8", errors="ignore") as file:
|
||||||
|
for line in file:
|
||||||
|
urls = url_pattern.findall(line)
|
||||||
|
for url in set(urls):
|
||||||
|
if url.startswith("http://"):
|
||||||
|
errors.append(
|
||||||
|
f"Found insecure HTTP URL: {url} in {file_path}"
|
||||||
|
)
|
||||||
|
url = url.strip('"\n')
|
||||||
|
try:
|
||||||
|
response = requests.head(
|
||||||
|
url, allow_redirects=True, timeout=5
|
||||||
|
)
|
||||||
|
response.raise_for_status()
|
||||||
|
except requests.RequestException as e:
|
||||||
|
errors.append(
|
||||||
|
f"Error accessing URL: {url} in {file_path} | Error: {e}, , Check your path ends with a /"
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
errors.append(f"Error reading file: {file_path} | Error: {e}")
|
||||||
|
|
||||||
|
return errors
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"repo_dir",
|
||||||
|
[Path(__file__).resolve().parent.parent / "reflex"],
|
||||||
|
)
|
||||||
|
def test_find_and_check_urls(repo_dir):
|
||||||
|
"""Test that all URLs in the repo are valid and secure.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
repo_dir: The directory of the repo.
|
||||||
|
"""
|
||||||
|
errors = check_urls(repo_dir)
|
||||||
|
assert not errors, "\n".join(errors)
|
@ -4,7 +4,7 @@ from rxconfig import config
|
|||||||
|
|
||||||
import reflex as rx
|
import reflex as rx
|
||||||
|
|
||||||
docs_url = "https://reflex.dev/docs/getting-started/introduction"
|
docs_url = "https://reflex.dev/docs/getting-started/introduction/"
|
||||||
filename = f"{config.app_name}/{config.app_name}.py"
|
filename = f"{config.app_name}/{config.app_name}.py"
|
||||||
|
|
||||||
|
|
||||||
|
@ -62,6 +62,8 @@ In this template, we have a sidebar component in `{your_app}/components/sidebar.
|
|||||||
|
|
||||||
### Adding State
|
### Adding State
|
||||||
|
|
||||||
As your app grows, we recommend using [substates](https://reflex.dev/docs/substates/overview/) to organize your state.
|
As your app grows, we recommend using [substates](https://reflex.dev/docs/substates/overview/)
|
||||||
|
to organize your state.
|
||||||
|
|
||||||
You can either define substates in their own files, or if the state is
|
You can either define substates in their own files, or if the state is
|
||||||
specific to a page, you can define it in the page file itself.
|
specific to a page, you can define it in the page file itself.
|
||||||
|
@ -379,7 +379,7 @@ class App(Base):
|
|||||||
raise TypeError(
|
raise TypeError(
|
||||||
"You may be trying to use an invalid Python function on a state var. "
|
"You may be trying to use an invalid Python function on a state var. "
|
||||||
"When referencing a var inside your render code, only limited var operations are supported. "
|
"When referencing a var inside your render code, only limited var operations are supported. "
|
||||||
"See the var operation docs here: https://reflex.dev/docs/state/vars/#var-operations"
|
"See the var operation docs here: https://reflex.dev/docs/vars/var-operations/"
|
||||||
) from e
|
) from e
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
@ -278,7 +278,7 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
|
|||||||
if not _reflex_internal_init and not is_testing_env():
|
if not _reflex_internal_init and not is_testing_env():
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
"State classes should not be instantiated directly in a Reflex app. "
|
"State classes should not be instantiated directly in a Reflex app. "
|
||||||
"See https://reflex.dev/docs/state for further information."
|
"See https://reflex.dev/docs/state/ for further information."
|
||||||
)
|
)
|
||||||
kwargs["parent_state"] = parent_state
|
kwargs["parent_state"] = parent_state
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
@ -1036,7 +1036,7 @@ def show_rx_chakra_migration_instructions():
|
|||||||
)
|
)
|
||||||
console.log("")
|
console.log("")
|
||||||
console.log(
|
console.log(
|
||||||
"For more details, please see https://reflex.dev/blog/2024-02-16-reflex-v0.4.0"
|
"For more details, please see https://reflex.dev/blog/2024-02-16-reflex-v0.4.0/"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user