2022-02-16 16:38:55 +08:00
|
|
|
from app.config import ALLOWED_REDIRECT_DOMAINS
|
2022-02-15 22:16:31 +08:00
|
|
|
from app.utils import random_string, random_words, sanitize_next_url
|
2019-07-04 04:13:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_random_words():
|
|
|
|
s = random_words()
|
|
|
|
assert len(s) > 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_random_string():
|
|
|
|
s = random_string()
|
|
|
|
assert len(s) > 0
|
2022-02-15 22:16:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_sanitize_url():
|
|
|
|
cases = [
|
|
|
|
{"url": None, "expected": None},
|
|
|
|
{"url": "", "expected": None},
|
2022-02-16 16:38:55 +08:00
|
|
|
{"url": "http://unknown.domain", "expected": None},
|
2022-02-15 22:16:31 +08:00
|
|
|
{"url": "https://badzone.org", "expected": None},
|
|
|
|
{"url": "/", "expected": "/"},
|
|
|
|
{"url": "/auth", "expected": "/auth"},
|
|
|
|
{"url": "/some/path", "expected": "/some/path"},
|
|
|
|
]
|
|
|
|
|
2022-02-16 16:38:55 +08:00
|
|
|
for domain in ALLOWED_REDIRECT_DOMAINS:
|
|
|
|
cases.append({"url": f"http://{domain}", "expected": f"http://{domain}"})
|
|
|
|
cases.append({"url": f"https://{domain}", "expected": f"https://{domain}"})
|
|
|
|
cases.append({"url": domain, "expected": None})
|
|
|
|
|
2022-02-15 22:16:31 +08:00
|
|
|
for case in cases:
|
|
|
|
res = sanitize_next_url(case["url"])
|
|
|
|
assert res == case["expected"]
|