Debian Codegraph @

tests/test_download.py

raw
1 """Tests for the internal mkdocstrings _download module."""
2
3 from __future__ import annotations
4
5 import logging
6 from typing import TYPE_CHECKING
7
8 import pytest
9
10 from mkdocstrings._internal import download
11
12 if TYPE_CHECKING:
13 from collections.abc import Mapping
14
15
16 @pytest.mark.parametrize(
17 ("credential", "expected", "env"),
18 [
19 ("USER", "USER", {"USER": "testuser"}),
20 ("$USER", "$USER", {"USER": "testuser"}),
21 ("${USER", "${USER", {"USER": "testuser"}),
22 ("$USER}", "$USER}", {"USER": "testuser"}),
23 ("${TOKEN}", "testtoken", {"TOKEN": "testtoken"}),
24 ("${USER}:${PASSWORD}", "${USER}:testpass", {"PASSWORD": "testpass"}),
25 ("${USER}:${PASSWORD}", "testuser:testpass", {"USER": "testuser", "PASSWORD": "testpass"}),
26 (
27 "user_prefix_${USER}_user_$uffix:pwd_prefix_${PASSWORD}_pwd_${uffix",
28 "user_prefix_testuser_user_$uffix:pwd_prefix_testpass_pwd_${uffix",
29 {"USER": "testuser", "PASSWORD": "testpass"},
30 ),
31 ],
32 )
33 def test_expand_env_vars(credential: str, expected: str, env: Mapping[str, str]) -> None:
34 """Test expanding environment variables."""
35 assert download._expand_env_vars(credential, url="https://test.example.com", env=env) == expected
36
37
38 def test_expand_env_vars_with_missing_env_var(caplog: pytest.LogCaptureFixture) -> None:
39 """Test expanding environment variables with a missing environment variable."""
40 caplog.set_level(logging.WARNING, logger="mkdocs.plugins.mkdocstrings._download")
41
42 credential = "${USER}"
43 env: dict[str, str] = {}
44 assert download._expand_env_vars(credential, url="https://test.example.com", env=env) == "${USER}"
45
46 output = caplog.records[0].getMessage()
47 assert "'USER' is not set" in output
48
49
50 @pytest.mark.parametrize(
51 ("url", "expected_url"),
52 [
53 ("http://host/path", "http://host/path"),
54 ("http://token@host/path", "http://host/path"),
55 ("http://${token}@host/path", "http://host/path"),
56 ("http://username:password@host/path", "http://host/path"),
57 ("http://username:${PASSWORD}@host/path", "http://host/path"),
58 ("http://${USERNAME}:${PASSWORD}@host/path", "http://host/path"),
59 ("http://prefix${USERNAME}suffix:prefix${PASSWORD}suffix@host/path", "http://host/path"),
60 ],
61 )
62 def test_extract_auth_from_url(monkeypatch: pytest.MonkeyPatch, url: str, expected_url: str) -> None:
63 """Test extracting the auth part from the URL."""
64 monkeypatch.setattr(download, "_create_auth_header", lambda *args, **kwargs: {})
65 result_url, _result_auth_header = download._extract_auth_from_url(url)
66 assert result_url == expected_url
67
68
69 def test_create_auth_header_basic_auth() -> None:
70 """Test creating the Authorization header for basic authentication."""
71 auth_header = download._create_auth_header(credential="testuser:testpass", url="https://test.example.com")
72 assert auth_header == {"Authorization": "Basic dGVzdHVzZXI6dGVzdHBhc3M="}
73
74
75 def test_create_auth_header_bearer_auth() -> None:
76 """Test creating the Authorization header for bearer token authentication."""
77 auth_header = download._create_auth_header(credential="token123", url="https://test.example.com")
78 assert auth_header == {"Authorization": "Bearer token123"}
79
80
81 @pytest.mark.parametrize(
82 ("var", "match"),
83 [
84 ("${var}", "var"),
85 ("${VAR}", "VAR"),
86 ("${_}", "_"),
87 ("${_VAR}", "_VAR"),
88 ("${VAR123}", "VAR123"),
89 ("${VAR123_}", "VAR123_"),
90 ("VAR", None),
91 ("$1VAR", None),
92 ("${1VAR}", None),
93 ("${}", None),
94 ("${ }", None),
95 ],
96 )
97 def test_env_var_pattern(var: str, match: str | None) -> None:
98 """Test the environment variable regex pattern."""
99 _match = download._ENV_VAR_PATTERN.match(var)
100 if _match is None:
101 assert match is _match
102 else:
103 assert _match.group(1) == match