File size: 1,241 Bytes
fc82490
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87aa1ef
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import asyncio

import pytest

from telesave.core.errors import InvalidUrlError, UnsafeUrlError
from telesave.core.security import assert_public_url, normalize_and_validate_url


def test_normalize_accepts_http_url() -> None:
    assert normalize_and_validate_url("https://example.com/file.mp4") == "https://example.com/file.mp4"


@pytest.mark.parametrize("url", ["ftp://example.com/file", "javascript:alert(1)", "not a url"])
def test_normalize_rejects_invalid_schemes(url: str) -> None:
    with pytest.raises(InvalidUrlError):
        normalize_and_validate_url(url)


def test_normalize_rejects_credentials() -> None:
    with pytest.raises(UnsafeUrlError):
        normalize_and_validate_url("https://user:pass@example.com/file")


def test_assert_public_url_rejects_loopback_ip() -> None:
    with pytest.raises(UnsafeUrlError):
        asyncio.run(assert_public_url("http://127.0.0.1/private"))


def test_normalize_rejects_out_of_range_port() -> None:
    with pytest.raises(InvalidUrlError):
        normalize_and_validate_url("http://example.com:99999/")


def test_assert_public_url_rejects_out_of_range_port() -> None:
    with pytest.raises(InvalidUrlError):
        asyncio.run(assert_public_url("http://example.com:99999/"))