Skip to content

💫 Toolkit to help you get started with SDD or any other process!

31.4 KB
Raw Blame
"""Tests for GitHub-authenticated HTTP request helpers."""

import io
import json
import os
from contextlib import contextmanager
from unittest.mock import MagicMock, patch
from urllib.request import Request

import pytest

from specify_cli.authentication.github_http import (
    GITHUB_HOSTS,
    build_github_request,
    resolve_github_release_asset_api_url,
)
from specify_cli.authentication.http import _StripAuthOnRedirect


class TestBuildGitHubRequest:
    """Tests for build_github_request() URL validation and auth handling."""

    # --- URL Validation Tests ---

    def test_empty_url_raises_value_error(self):
        """build_github_request() must reject an empty string URL."""
        with pytest.raises(ValueError, match="url must not be empty"):
            build_github_request("")

    def test_whitespace_url_raises_value_error(self):
        """build_github_request() must reject a whitespace-only URL."""
        with pytest.raises(ValueError, match="url must not be empty"):
            build_github_request("   ")

    def test_non_http_url_raises_value_error(self):
        """build_github_request() must reject URLs without http/https scheme."""
        with pytest.raises(ValueError, match="url must start with http"):
            build_github_request("not-a-url")

    def test_ftp_url_raises_value_error(self):
        """build_github_request() must reject ftp:// URLs."""
        with pytest.raises(ValueError, match="url must start with http"):
            build_github_request("ftp://github.com/file.zip")

    @pytest.mark.parametrize(
        "url", ["https://github.com:notaport/file", "https://github.com:65536/file"]
    )
    def test_malformed_explicit_port_raises_before_request_construction(self, url):
        """Malformed explicit ports are rejected before creating a Request."""
        with patch(
            "specify_cli.authentication.github_http.urllib.request.Request"
        ) as request:
            with pytest.raises(ValueError):
                build_github_request(url)
        request.assert_not_called()

    # --- Valid URL Tests ---

    def test_valid_https_url_returns_request(self):
        """build_github_request() must return a Request for a valid https URL."""
        req = build_github_request("https://github.com/github/spec-kit")
        assert req.full_url == "https://github.com/github/spec-kit"

    def test_valid_http_url_returns_request(self):
        """build_github_request() must accept http:// URLs."""
        req = build_github_request("http://example.com/file")
        assert req.full_url == "http://example.com/file"

    def test_valid_explicit_port_retains_url_method_and_github_auth(self):
        """A valid explicit port retains normal GitHub request behavior."""
        with patch.dict(os.environ, {"GITHUB_TOKEN": "test-token", "GH_TOKEN": ""}):
            req = build_github_request("https://github.com:8443/github/spec-kit")
        assert req.full_url == "https://github.com:8443/github/spec-kit"
        assert req.get_method() == "GET"
        assert req.get_header("Authorization") == "Bearer test-token"

    # --- Auth Header Tests ---

    def test_github_token_added_for_github_host(self):
        """Authorization header is set when GITHUB_TOKEN is present."""
        with patch.dict(os.environ, {"GITHUB_TOKEN": "test-token", "GH_TOKEN": ""}):
            req = build_github_request("https://github.com/github/spec-kit")
        assert req.get_header("Authorization") == "Bearer test-token"

    def test_gh_token_used_as_fallback(self):
        """GH_TOKEN is used when GITHUB_TOKEN is absent."""
        with patch.dict(os.environ, {"GITHUB_TOKEN": "", "GH_TOKEN": "fallback-token"}):
            req = build_github_request("https://github.com/github/spec-kit")
        assert req.get_header("Authorization") == "Bearer fallback-token"

    def test_no_auth_header_for_non_github_host(self):
        """Authorization header must NOT be set for non-GitHub URLs."""
        with patch.dict(os.environ, {"GITHUB_TOKEN": "test-token"}):
            req = build_github_request("https://example.com/file")
        assert req.get_header("Authorization") is None

    def test_no_auth_header_when_no_token(self):
        """No Authorization header when no token is set in environment."""
        with patch.dict(os.environ, {}, clear=True):
            req = build_github_request("https://github.com/github/spec-kit")
        assert req.get_header("Authorization") is None

    def test_missing_hostname_raises_value_error(self):
        """build_github_request() must reject URLs with valid scheme but no hostname."""
        with pytest.raises(ValueError, match="url must include a hostname"):
            build_github_request("http://")


class TestResolveGitHubReleaseAssetApiUrl:
    """Tests for resolve_github_release_asset_api_url()."""

    def _make_open_url_fn(self, release_json):
        """Create a fake open_url_fn that returns release JSON."""
        @contextmanager
        def fake_open(url, timeout=None, extra_headers=None):
            resp = MagicMock()
            resp.read.side_effect = io.BytesIO(json.dumps(release_json).encode()).read
            yield resp
        return fake_open

    def test_returns_none_for_non_github_url(self):
        """Non-GitHub URLs should return None."""
        result = resolve_github_release_asset_api_url(
            "https://example.com/file.zip", lambda *a, **kw: None
        )
        assert result is None

    def test_returns_none_for_non_release_github_url(self):
        """GitHub URLs that aren't release downloads return None."""
        result = resolve_github_release_asset_api_url(
            "https://github.com/org/repo/archive/refs/tags/v1.zip",
            lambda *a, **kw: None,
        )
        assert result is None

    def test_passthrough_for_existing_api_asset_url(self):
        """Already-resolved REST API asset URLs are returned as-is."""
        url = "https://api.github.com/repos/org/repo/releases/assets/12345"
        result = resolve_github_release_asset_api_url(url, lambda *a, **kw: None)
        assert result == url

    def test_resolves_browser_url_to_api_url(self):
        """Browser release URL resolves to REST API asset URL."""
        release_json = {
            "assets": [
                {"name": "pack.zip", "url": "https://api.github.com/repos/org/repo/releases/assets/99"}
            ]
        }
        result = resolve_github_release_asset_api_url(
            "https://github.com/org/repo/releases/download/v1.0/pack.zip",
            self._make_open_url_fn(release_json),
        )
        assert result == "https://api.github.com/repos/org/repo/releases/assets/99"

    def test_accepts_case_variant_public_metadata_repository_identity(self):
        """GitHub owner and repository names are case-insensitive identities."""
        asset_url = "https://api.github.com/repos/owner/repository/releases/assets/99"
        result = resolve_github_release_asset_api_url(
            "https://github.com/Owner/Repository/releases/download/v1.0/pack.zip",
            self._make_open_url_fn(
                {"assets": [{"name": "pack.zip", "url": asset_url}]}
            ),
        )
        assert result == asset_url

    @pytest.mark.parametrize(
        "asset_url",
        [
            None,
            99,
            "http://metadata.example/repos/org/repo/releases/assets/99",
            "https://wrong.example/repos/org/repo/releases/assets/99",
            "http://api.github.com/repos/org/repo/releases/assets/99",
            "https://api.github.com:8443/repos/org/repo/releases/assets/99",
            "https://api.github.com:notaport/repos/org/repo/releases/assets/99",
            "https://[not-an-ip]/repos/org/repo/releases/assets/99",
            "https:///repos/org/repo/releases/assets/99",
            "ftp://api.github.com/repos/org/repo/releases/assets/99",
            "https://api.github.com/repos/other/repo/releases/assets/99",
            "https://api.github.com/repos/org/other/releases/assets/99",
            "https://api.github.com/repos/org/repo/releases/download/99",
            "https://api.github.com/repos/org/repo/releases/assets/not-a-number",
            "https://api.github.com/repos/org/repo/releases/assets/99/extra",
            "https://api.github.com/repos/org/repo/releases/assets/99?download=1",
            "https://api.github.com/repos/org/repo/releases/assets/99#fragment",
            "https://[email protected]/repos/org/repo/releases/assets/99",
            "https://api.github.com/repos/org/repo/releases/assets/99\n",
            "https://api.github.com/repos/org/repo/releases/assets/99\t",
            " https://api.github.com/repos/org/repo/releases/assets/99",
            "https://api.github.com/repos/org/repo/releases/assets/99 ",
            "https://api.github.com/repos/org/repo/releases/assets/99;",
            "https://api.github.com/repos/org/repo/releases/assets/99?",
            "https://api.github.com/repos/org/repo/releases/assets/99#",
            "https://api.github.com/repos/org/repo/releases/assets/%",
            "https://api.github.com/repos/org/repo/releases/assets/%9",
            "https://api.github.com/repos/org/repo/releases/assets/%ZZ",
            "https://api.github.com:65536/repos/org/repo/releases/assets/99",
        ],
    )
    def test_rejects_invalid_public_metadata_asset_url(self, asset_url):
        """Metadata cannot replace a browser URL with a noncanonical API URL."""
        result = resolve_github_release_asset_api_url(
            "https://github.com/org/repo/releases/download/v1.0/pack.zip",
            self._make_open_url_fn(
                {"assets": [{"name": "pack.zip", "url": asset_url}]}
            ),
        )
        assert result is None

    def test_returns_none_when_asset_not_found(self):
        """Returns None when the release exists but asset name doesn't match."""
        release_json = {"assets": [{"name": "other.zip", "url": "https://api.github.com/repos/org/repo/releases/assets/1"}]}
        result = resolve_github_release_asset_api_url(
            "https://github.com/org/repo/releases/download/v1/missing.zip",
            self._make_open_url_fn(release_json),
        )
        assert result is None

    def test_returns_none_on_network_error(self):
        """Returns None when the API request fails."""
        import urllib.error

        @contextmanager
        def failing_open(url, timeout=None, extra_headers=None):
            raise urllib.error.URLError("network error")
            yield  # pragma: no cover

        result = resolve_github_release_asset_api_url(
            "https://github.com/org/repo/releases/download/v1/pack.zip",
            failing_open,
        )
        assert result is None

    @pytest.mark.parametrize("release_json", [[], {"assets": {}}])
    def test_returns_none_for_invalid_release_metadata(self, release_json):
        """Malformed release metadata retains the existing None fallback."""
        result = resolve_github_release_asset_api_url(
            "https://github.com/org/repo/releases/download/v1/pack.zip",
            self._make_open_url_fn(release_json),
        )
        assert result is None

    def test_metadata_lookup_is_bounded_and_redirect_validated(self):
        """Release metadata reads stay bounded and use the caller's policy."""
        captured = {}

        class OversizedResponse:
            def read(self, amount=None):
                captured["read_amount"] = amount
                return b"x" * amount

            def __enter__(self):
                return self

            def __exit__(self, *args):
                return False

        def redirect_validator(old_url, new_url):
            return None

        def fake_open(
            url,
            timeout=None,
            extra_headers=None,
            redirect_validator=None,
        ):
            captured["redirect_validator"] = redirect_validator
            return OversizedResponse()

        result = resolve_github_release_asset_api_url(
            "https://github.com/org/repo/releases/download/v1/pack.zip",
            fake_open,
            redirect_validator=redirect_validator,
            max_metadata_bytes=8,
        )

        assert result is None
        assert captured["read_amount"] == 9
        assert captured["redirect_validator"] is redirect_validator

    def test_tag_with_special_characters_is_url_encoded(self):
        """Tags with reserved characters (e.g. '/') are encoded in the API URL."""
        captured_urls = []

        @contextmanager
        def capturing_open(url, timeout=None, extra_headers=None):
            captured_urls.append(url)
            resp = MagicMock()
            resp.read.side_effect = io.BytesIO(json.dumps({"assets": []}).encode()).read
            yield resp

        resolve_github_release_asset_api_url(
            "https://github.com/org/repo/releases/download/feature%2Fv1/pack.zip",
            capturing_open,
        )
        # The tag "feature/v1" (decoded from %2F) must be re-encoded as "feature%2Fv1"
        assert len(captured_urls) == 1
        assert "releases/tags/feature%2Fv1" in captured_urls[0]

    def test_tag_with_hash_is_url_encoded(self):
        """Tags with '#' character are properly encoded."""
        captured_urls = []

        @contextmanager
        def capturing_open(url, timeout=None, extra_headers=None):
            captured_urls.append(url)
            resp = MagicMock()
            resp.read.side_effect = io.BytesIO(json.dumps({"assets": []}).encode()).read
            yield resp

        resolve_github_release_asset_api_url(
            "https://github.com/org/repo/releases/download/v1%23beta/pack.zip",
            capturing_open,
        )
        assert len(captured_urls) == 1
        assert "releases/tags/v1%23beta" in captured_urls[0]

    # --- GHE.com (GitHub Enterprise Cloud with data residency) ---

    def test_resolves_ghecom_browser_url_to_api_url(self):
        """A GHE.com browser URL resolves through its paired API subdomain."""
        asset_url = "https://api.msft.ghe.com/repos/org/repo/releases/assets/42"
        captured = []

        @contextmanager
        def capturing_open(url, timeout=None, extra_headers=None):
            captured.append(url)
            resp = MagicMock()
            resp.read.side_effect = io.BytesIO(
                json.dumps(
                    {"assets": [{"name": "bundle.zip", "url": asset_url}]}
                ).encode()
            ).read
            yield resp

        result = resolve_github_release_asset_api_url(
            "https://msft.ghe.com/org/repo/releases/download/v1.0/bundle.zip",
            capturing_open,
            github_hosts=("msft.ghe.com", "api.msft.ghe.com"),
        )

        assert result == asset_url
        assert captured == [
            "https://api.msft.ghe.com/repos/org/repo/releases/tags/v1.0"
        ]

    def test_passthrough_for_trusted_ghecom_api_asset_url(self):
        """A trusted direct GHE.com API asset URL receives asset treatment."""
        url = "https://api.msft.ghe.com/repos/org/repo/releases/assets/42"
        result = resolve_github_release_asset_api_url(
            url,
            lambda *a, **kw: None,
            github_hosts=("msft.ghe.com", "api.msft.ghe.com"),
        )
        assert result == url

    @pytest.mark.parametrize(
        "url",
        [
            "https://api.msft.ghe.com/repos/org/repo/releases/assets/42?",
            "https://api.msft.ghe.com/repos/org/repo/releases/assets/42#",
            "https://api.msft.ghe.com/repos/org/repo/releases/assets/42/",
            "https://api.msft.ghe.com/repos/org/repo/releases/assets/%34%32",
            "https://api.msft.ghe.com/repos/org/repo/releases/assets/42 ",
            "https://api.msft.ghe.com/repos/org/repo/releases/assets/42;",
            "https://api.msft.ghe.com/repos/org/repo/releases/assets/%ZZ",
        ],
    )
    def test_rejects_noncanonical_direct_ghecom_api_asset_url(self, url):
        """Direct GHE.com asset URLs retain strict raw-spelling validation."""
        result = resolve_github_release_asset_api_url(
            url,
            lambda *a, **kw: None,
            github_hosts=("msft.ghe.com", "api.msft.ghe.com"),
        )
        assert result is None

    def test_rejects_direct_ghecom_api_asset_url_without_trusted_pair(self):
        """A direct GHE.com API URL needs both trusted tenant hostnames."""
        url = "https://api.msft.ghe.com/repos/org/repo/releases/assets/42"
        result = resolve_github_release_asset_api_url(
            url,
            lambda *a, **kw: None,
            github_hosts=("api.msft.ghe.com",),
        )
        assert result is None

    @pytest.mark.parametrize(
        "github_hosts",
        [
            (),
            ("msft.ghe.com", "api.msft.ghe.com"),
        ],
    )
    def test_rejects_direct_ghecom_api_asset_url_with_ghes_path(
        self, github_hosts
    ):
        """A GHE.com API host never accepts the legacy GHES /api/v3 path."""
        url = (
            "https://api.msft.ghe.com/api/v3/repos/org/repo/"
            "releases/assets/42"
        )
        result = resolve_github_release_asset_api_url(
            url,
            lambda *a, **kw: None,
            github_hosts=github_hosts,
        )
        assert result is None

    @pytest.mark.parametrize(
        "github_hosts",
        [
            ("msft.ghe.com",),
            ("api.msft.ghe.com",),
            ("other.ghe.com", "api.other.ghe.com"),
        ],
    )
    def test_ghecom_resolution_requires_trusted_web_and_api_hosts(self, github_hosts):
        """GHE.com resolution requires both members of the tenant host pair."""
        called = []

        @contextmanager
        def recording_open(url, timeout=None, extra_headers=None):
            called.append(url)
            resp = MagicMock()
            resp.read.side_effect = io.BytesIO(b"{}").read
            yield resp

        result = resolve_github_release_asset_api_url(
            "https://msft.ghe.com/org/repo/releases/download/v1.0/bundle.zip",
            recording_open,
            github_hosts=github_hosts,
        )

        assert result is None
        assert called == []

    @pytest.mark.parametrize(
        "asset_url",
        [
            "https://api.other.ghe.com/repos/org/repo/releases/assets/42",
            "http://api.msft.ghe.com/repos/org/repo/releases/assets/42",
            "https://msft.ghe.com/api/v3/repos/org/repo/releases/assets/42",
            "https://api.msft.ghe.com/repos/other/repo/releases/assets/42",
            "https://api.msft.ghe.com/repos/org/other/releases/assets/42",
            "https://api.msft.ghe.com/repos/org/repo/releases/assets/not-a-number",
            "https://api.msft.ghe.com/repos/org/repo/releases/assets/42?download=1",
        ],
    )
    def test_rejects_wrong_origin_or_path_for_ghecom_metadata_asset_url(
        self, asset_url
    ):
        """GHE.com metadata must identify the paired tenant and repository."""
        result = resolve_github_release_asset_api_url(
            "https://msft.ghe.com/org/repo/releases/download/v1.0/bundle.zip",
            self._make_open_url_fn(
                {"assets": [{"name": "bundle.zip", "url": asset_url}]}
            ),
            github_hosts=("msft.ghe.com", "api.msft.ghe.com"),
        )
        assert result is None

    # --- GHES (GitHub Enterprise Server) ---

    def test_resolves_ghes_browser_url_to_api_url(self):
        """A GHES browser release URL resolves to the /api/v3 asset URL."""
        release_json = {
            "assets": [
                {"name": "ext.zip",
                 "url": "https://ghes.example/api/v3/repos/o/r/releases/assets/7"}
            ]
        }
        result = resolve_github_release_asset_api_url(
            "https://ghes.example/o/r/releases/download/v1/ext.zip",
            self._make_open_url_fn(release_json),
            github_hosts=("ghes.example",),
        )
        assert result == "https://ghes.example/api/v3/repos/o/r/releases/assets/7"

    def test_accepts_case_variant_ghes_metadata_repository_identity(self):
        """GHES owner and repository names are case-insensitive identities."""
        asset_url = (
            "https://ghes.example/api/v3/repos/owner/repository/releases/assets/7"
        )
        result = resolve_github_release_asset_api_url(
            "https://ghes.example/Owner/Repository/releases/download/v1/ext.zip",
            self._make_open_url_fn({"assets": [{"name": "ext.zip", "url": asset_url}]}),
            github_hosts=("ghes.example",),
        )
        assert result == asset_url

    def test_passthrough_for_existing_ghes_api_asset_url(self):
        """An already-resolved GHES /api/v3 asset URL is returned as-is."""
        url = "https://ghes.example/api/v3/repos/o/r/releases/assets/7"
        result = resolve_github_release_asset_api_url(
            url, lambda *a, **kw: None, github_hosts=("ghes.example",)
        )
        assert result == url

    def test_returns_none_for_ghes_host_not_in_allowlist(self):
        """Unlisted hosts get no GHES treatment and trigger no API call (anti-SSRF)."""
        called = []

        @contextmanager
        def recording_open(url, timeout=None, extra_headers=None):
            called.append(url)
            resp = MagicMock()
            resp.read.side_effect = io.BytesIO(b"{}").read
            yield resp

        result = resolve_github_release_asset_api_url(
            "https://ghes.example/o/r/releases/download/v1/ext.zip",
            recording_open,
            github_hosts=("other.example",),
        )
        assert result is None
        assert called == []

    def test_returns_none_on_malformed_ghes_port(self):
        """A malformed port on an allowlisted GHES host returns None, not a
        ValueError (contract: resolve or return None, never raise)."""
        called = []

        def open_never(url, timeout=None, extra_headers=None):
            called.append(url)
            raise AssertionError("open_url_fn must not be called")

        result = resolve_github_release_asset_api_url(
            "https://ghes.example:notaport/o/r/releases/download/v1/ext.zip",
            open_never,
            github_hosts=("ghes.example",),
        )
        assert result is None
        assert called == []

    def test_returns_none_on_malformed_host(self):
        """A malformed authority (e.g. an invalid bracketed IPv6 host) returns
        None, not a ValueError (contract: resolve or return None, never raise)."""
        called = []

        def open_never(url, timeout=None, extra_headers=None):
            called.append(url)
            raise AssertionError("open_url_fn must not be called")

        result = resolve_github_release_asset_api_url(
            "https://[not-an-ip]/o/r/releases/download/v1/ext.zip",
            open_never,
            github_hosts=("ghes.example",),
        )
        assert result is None
        assert called == []

    def test_passthrough_for_unlisted_ghes_api_asset_url(self):
        """A direct GHES /api/v3 asset URL passes through even when the host is
        not allowlisted: passthrough issues no API request, and the download
        helper gates the token independently, so octet-stream resolution must
        not be withheld."""
        called = []

        @contextmanager
        def recording_open(url, timeout=None, extra_headers=None):
            called.append(url)
            resp = MagicMock()
            resp.read.side_effect = io.BytesIO(b"{}").read
            yield resp

        url = "https://ghes.example/api/v3/repos/o/r/releases/assets/7"
        result = resolve_github_release_asset_api_url(
            url, recording_open, github_hosts=("other.example",)
        )
        assert result == url
        assert called == []

    def test_ghes_api_base_preserves_scheme_and_port(self):
        """The GHES API base mirrors the URL scheme and keeps a non-standard port."""
        captured = []

        @contextmanager
        def capturing_open(url, timeout=None, extra_headers=None):
            captured.append(url)
            resp = MagicMock()
            resp.read.side_effect = io.BytesIO(json.dumps({"assets": []}).encode()).read
            yield resp

        resolve_github_release_asset_api_url(
            "http://localhost:8000/o/r/releases/download/v1/ext.zip",
            capturing_open,
            github_hosts=("localhost",),
        )
        assert captured == ["http://localhost:8000/api/v3/repos/o/r/releases/tags/v1"]

    @pytest.mark.parametrize(
        ("download_url", "asset_url", "expected_lookup_url"),
        [
            (
                "http://[::1]/o/r/releases/download/v1/ext.zip",
                "http://[::1]/api/v3/repos/o/r/releases/assets/7",
                "http://[::1]/api/v3/repos/o/r/releases/tags/v1",
            ),
            (
                "https://[::1]:8443/o/r/releases/download/v1/ext.zip",
                "https://[0:0:0:0:0:0:0:1]:8443/api/v3/repos/o/r/releases/assets/7",
                "https://[::1]:8443/api/v3/repos/o/r/releases/tags/v1",
            ),
        ],
    )
    def test_ghes_ipv6_api_base_and_metadata_origin(
        self, download_url, asset_url, expected_lookup_url
    ):
        """GHES IPv6 API lookups retain brackets and compare equivalent literals."""
        captured = []

        @contextmanager
        def capturing_open(url, timeout=None, extra_headers=None):
            captured.append(url)
            resp = MagicMock()
            resp.read.side_effect = io.BytesIO(json.dumps({
                "assets": [{"name": "ext.zip", "url": asset_url}]
            }).encode()).read
            yield resp

        result = resolve_github_release_asset_api_url(
            download_url,
            capturing_open,
            github_hosts=("::1",),
        )
        assert result == asset_url
        assert captured == [expected_lookup_url]

    @pytest.mark.parametrize(
        ("download_url", "asset_url", "github_hosts"),
        [
            (
                "https://ghes.example/o/r/releases/download/v1/ext.zip",
                "https://ghes.example/api/v3/repos/o/r/releases/assets/7",
                ("ghes.example",),
            ),
            (
                "https://ghes.example:8443/o/r/releases/download/v1/ext.zip",
                "https://GHES.EXAMPLE:8443/api/v3/repos/o/r/releases/assets/7",
                ("ghes.example",),
            ),
            (
                "http://localhost:8000/o/r/releases/download/v1/ext.zip",
                "http://localhost:8000/api/v3/repos/o/r/releases/assets/7",
                ("localhost",),
            ),
            (
                "http://localhost/o/r/releases/download/v1/ext.zip",
                "http://LOCALHOST:80/api/v3/repos/o/r/releases/assets/7",
                ("localhost",),
            ),
        ],
    )
    def test_accepts_same_origin_ghes_metadata_asset_url(
        self, download_url, asset_url, github_hosts
    ):
        """GHES metadata URLs retain their derived scheme, host, and port."""
        result = resolve_github_release_asset_api_url(
            download_url,
            self._make_open_url_fn({"assets": [{"name": "ext.zip", "url": asset_url}]}),
            github_hosts=github_hosts,
        )
        assert result == asset_url

    @pytest.mark.parametrize(
        "asset_url",
        [
            "https://other.example/api/v3/repos/o/r/releases/assets/7",
            "http://ghes.example/api/v3/repos/o/r/releases/assets/7",
            "https://ghes.example:8443/api/v3/repos/o/r/releases/assets/7",
            "https://ghes.example/api/v3/repos/o/other/releases/assets/7",
            "https://ghes.example/api/v3/repos/o/r/releases/assets/7/extra",
        ],
    )
    def test_rejects_wrong_origin_or_path_for_ghes_metadata_asset_url(self, asset_url):
        """GHES metadata URLs must match the derived API origin and endpoint."""
        result = resolve_github_release_asset_api_url(
            "https://ghes.example/o/r/releases/download/v1/ext.zip",
            self._make_open_url_fn({"assets": [{"name": "ext.zip", "url": asset_url}]}),
            github_hosts=("ghes.example",),
        )
        assert result is None

    def test_ghes_wildcard_does_not_match_bare_host(self):
        """A '*.suffix' pattern does not match the bare host (must list it explicitly)."""
        result = resolve_github_release_asset_api_url(
            "https://ghes.example/o/r/releases/download/v1/ext.zip",
            lambda *a, **kw: None,
            github_hosts=("*.ghes.example",),
        )
        assert result is None

    def test_public_github_url_unaffected_by_github_hosts(self):
        """Public github.com still resolves via api.github.com even with github_hosts set."""
        captured = []

        @contextmanager
        def capturing_open(url, timeout=None, extra_headers=None):
            captured.append(url)
            resp = MagicMock()
            resp.read.side_effect = io.BytesIO(json.dumps({
                "assets": [{"name": "pack.zip",
                            "url": "https://api.github.com/repos/org/repo/releases/assets/99"}]
            }).encode()).read
            yield resp

        result = resolve_github_release_asset_api_url(
            "https://github.com/org/repo/releases/download/v1.0/pack.zip",
            capturing_open,
            github_hosts=("ghes.example",),
        )
        assert result == "https://api.github.com/repos/org/repo/releases/assets/99"
        assert captured == ["https://api.github.com/repos/org/repo/releases/tags/v1.0"]

    def test_tag_with_literal_slash_in_path(self):
        """A tag containing a literal '/' (e.g. feature/v1.0.0) splits across
        multiple URL path segments. The implementation must join all segments
        between 'download/' and the asset name to reconstruct the full tag."""
        captured_urls = []
        asset_url = "https://api.github.com/repos/org/repo/releases/assets/77"

        @contextmanager
        def capturing_open(url, timeout=None, extra_headers=None):
            captured_urls.append(url)
            resp = MagicMock()
            resp.read.side_effect = io.BytesIO(json.dumps({
                "assets": [{"name": "asset.zip", "url": asset_url}]
            }).encode()).read
            yield resp

        result = resolve_github_release_asset_api_url(
            "https://github.com/org/repo/releases/download/feature/v1.0.0/asset.zip",
            capturing_open,
        )
        assert result == asset_url
        # Tag must be the full "feature/v1.0.0", not just "v1.0.0"
        assert len(captured_urls) == 1
        assert "releases/tags/feature%2Fv1.0.0" in captured_urls[0]


class TestGitHubRedirectAuth:
    """Tests for GitHub-owned redirect auth handling."""

    def test_multi_hop_github_redirect_preserves_unredirected_auth(self):
        """Auth survives a multi-hop redirect chain within GitHub hosts."""
        handler = _StripAuthOnRedirect(tuple(GITHUB_HOSTS))
        req1 = Request(
            "https://github.com/org/repo",
            headers={"Authorization": "Bearer tok"},
        )

        req2 = handler.redirect_request(
            req1,
            io.BytesIO(b""),
            302,
            "Found",
            {},
            "https://codeload.github.com/org/repo/zip",
        )
        assert req2 is not None
        auth2 = req2.get_header("Authorization") or req2.unredirected_hdrs.get(
            "Authorization"
        )
        assert auth2 == "Bearer tok"

        req3 = handler.redirect_request(
            req2,
            io.BytesIO(b""),
            302,
            "Found",
            {},
            "https://raw.githubusercontent.com/org/repo/main/file",
        )
        assert req3 is not None
        auth3 = req3.get_header("Authorization") or req3.unredirected_hdrs.get(
            "Authorization"
        )
        assert auth3 == "Bearer tok"