import pytest import fetch @pytest.fixture(autouse=True) def _public_dns(monkeypatch): # Resolve every allowed host to a public address so tests need no network. monkeypatch.setattr(fetch.socket, "getaddrinfo", lambda host, *a, **k: [(None, None, None, None, ("142.250.1.1", 0))]) @pytest.mark.parametrize("url", [ "https://www.youtube.com/watch?v=abc", "https://youtu.be/abc", "https://m.youtube.com/watch?v=abc", "https://vimeo.com/12345", "https://www.facebook.com/x/videos/1", "https://fb.watch/abc/", "https://www.tiktok.com/@u/video/1", "https://x.com/u/status/1", "https://www.instagram.com/reel/abc/", ]) def test_allowed(url): assert fetch.check_url(url) is None @pytest.mark.parametrize("url", [ "ftp://youtube.com/x", "file:///etc/passwd", "https://example.com/video.mp4", "https://youtube.com.evil.net/x", "https://evilyoutube.com/x", "http://127.0.0.1/x", "http://[::1]/x", "http://169.254.169.254/latest/meta-data", "https://user:pw@youtube.com/x", "https://youtube.com:8080/x", "", "not a url", ]) def test_refused(url): assert fetch.check_url(url) is not None def test_private_resolution_refused(monkeypatch): monkeypatch.setattr(fetch.socket, "getaddrinfo", lambda host, *a, **k: [(None, None, None, None, ("10.0.0.5", 0))]) assert fetch.check_url("https://www.youtube.com/watch?v=abc") is not None def test_unresolvable_refused(monkeypatch): def boom(*a, **k): raise OSError("nxdomain") monkeypatch.setattr(fetch.socket, "getaddrinfo", boom) assert "can't be reached" in fetch.check_url("https://vimeo.com/1") def test_generic_extractor_not_allowed(): assert "generic" not in fetch.ALLOWED_EXTRACTORS or "generic:unsupported" in fetch.ALLOWED_EXTRACTORS assert not any(e == "generic" for e in fetch.ALLOWED_EXTRACTORS) def test_cookie_filter(monkeypatch, tmp_path): raw = ("# Netscape HTTP Cookie File\n" ".youtube.com\tTRUE\t/\tTRUE\t0\tSID\tabc\n" ".bank.example\tTRUE\t/\tTRUE\t0\tSESSION\tsecret\n" "accounts.google.com\tFALSE\t/\tTRUE\t0\tX\ty\n") monkeypatch.setenv("YT_DLP_COOKIES", raw) monkeypatch.delenv("YT_DLP_COOKIES_B64", raising=False) monkeypatch.setattr(fetch, "_cookie_path", None) p = fetch._cookiefile() assert p text = open(p, encoding="utf-8").read() assert "youtube.com" in text and "accounts.google.com" in text assert "bank.example" not in text def test_bad_token_short_circuits(monkeypatch): monkeypatch.setattr(fetch, "verify_usage_token", lambda t, s=None: False) assert fetch.fetch_video("https://youtu.be/x", "nope") == (None, "Not authorized.")