|
1 | 1 | import functools
|
2 | 2 | from typing import Any, List, Optional, Tuple
|
| 3 | +from unittest.mock import Mock, PropertyMock |
3 | 4 |
|
4 | 5 | import pytest
|
5 | 6 |
|
6 | 7 | import pip._internal.network.auth
|
7 | 8 | from pip._internal.network.auth import MultiDomainBasicAuth
|
| 9 | +from src.pip._vendor.requests import PreparedRequest |
8 | 10 | from tests.lib.requests_mocks import MockConnection, MockRequest, MockResponse
|
9 | 11 |
|
10 | 12 |
|
@@ -50,40 +52,143 @@ def test_get_credentials_parses_correctly(
|
50 | 52 | (username is None and password is None)
|
51 | 53 | or
|
52 | 54 | # Credentials were found and "cached" appropriately
|
53 |
| - auth.passwords["example.com"] == (username, password) |
| 55 | + (url, (username, password)) in auth.passwords |
54 | 56 | )
|
55 | 57 |
|
56 | 58 |
|
| 59 | +def test_handle_prompt_for_password_successful() -> None: |
| 60 | + auth = MultiDomainBasicAuth() |
| 61 | + resp = Mock() |
| 62 | + resp.status_code = 401 |
| 63 | + resp.url = "http://example.com" |
| 64 | + resp.raw = Mock() |
| 65 | + resp.content = PropertyMock() |
| 66 | + resp.content = "" |
| 67 | + resp.request = PreparedRequest() |
| 68 | + resp.request.headers = {} |
| 69 | + auth._prompt_for_password = Mock() # type: ignore[assignment] |
| 70 | + auth._prompt_for_password.return_value = ("user", "password", True) |
| 71 | + auth.handle_401(resp) |
| 72 | + auth._prompt_for_password.assert_called_with("example.com") |
| 73 | + expected = ("http://example.com", ("user", "password")) |
| 74 | + assert len(auth.passwords) == 1 |
| 75 | + assert auth.passwords[0] == expected |
| 76 | + |
| 77 | + |
| 78 | +def test_handle_prompt_for_password_unsuccessful() -> None: |
| 79 | + auth = MultiDomainBasicAuth() |
| 80 | + resp = Mock() |
| 81 | + resp.status_code = 401 |
| 82 | + resp.url = "http://example.com" |
| 83 | + resp.raw = Mock() |
| 84 | + resp.content = PropertyMock() |
| 85 | + resp.content = "" |
| 86 | + resp.request = PreparedRequest() |
| 87 | + resp.request.headers = {} |
| 88 | + auth._prompt_for_password = Mock() # type: ignore[assignment] |
| 89 | + auth._prompt_for_password.return_value = (None, None, False) |
| 90 | + auth.handle_401(resp) |
| 91 | + auth._prompt_for_password.assert_called_with("example.com") |
| 92 | + assert len(auth.passwords) == 0 |
| 93 | + |
| 94 | + |
57 | 95 | def test_get_credentials_not_to_uses_cached_credentials() -> None:
|
58 | 96 | auth = MultiDomainBasicAuth()
|
59 |
| - auth.passwords["example.com"] = ("user", "pass") |
| 97 | + auth.passwords.append(("http://example.com", ("user", "pass"))) |
60 | 98 |
|
61 | 99 | got = auth. _get_url_and_credentials( "http://foo:[email protected]/path")
|
62 | 100 | expected = ("http://example.com/path", "foo", "bar")
|
63 | 101 | assert got == expected
|
64 | 102 |
|
65 | 103 |
|
66 |
| -def test_get_credentials_not_to_uses_cached_credentials_only_username() -> None: |
| 104 | +def test_get_credentials_not_to_use_cached_credentials_only_username() -> None: |
| 105 | + auth = MultiDomainBasicAuth() |
| 106 | + auth.passwords.append(("https://example.com", ("user", "pass"))) |
| 107 | + |
| 108 | + got = auth. _get_url_and_credentials( "https://[email protected]/path") |
| 109 | + expected = ("https://example.com/path", "foo", "") |
| 110 | + assert got == expected |
| 111 | + |
| 112 | + |
| 113 | +def test_multi_domain_credentials_match() -> None: |
| 114 | + auth = MultiDomainBasicAuth() |
| 115 | + auth.passwords.append(("http://example.com", ("user", "pass"))) |
| 116 | + auth.passwords.append(("http://example.com/path", ("user", "pass2"))) |
| 117 | + |
| 118 | + got = auth. _get_url_and_credentials( "http://[email protected]/path") |
| 119 | + expected = ("http://example.com/path", "user", "pass2") |
| 120 | + assert got == expected |
| 121 | + |
| 122 | + |
| 123 | +def test_multi_domain_credentials_longest_match() -> None: |
| 124 | + auth = MultiDomainBasicAuth() |
| 125 | + auth.passwords.append(("http://example.com", ("user", "pass"))) |
| 126 | + auth.passwords.append(("http://example.com/path", ("user", "pass2"))) |
| 127 | + auth.passwords.append(("http://example.com/path/subpath", ("user", "pass3"))) |
| 128 | + |
| 129 | + got = auth. _get_url_and_credentials( "http://[email protected]/path") |
| 130 | + expected = ("http://example.com/path", "user", "pass2") |
| 131 | + assert got == expected |
| 132 | + |
| 133 | + |
| 134 | +def test_multi_domain_credentials_partial_match_only() -> None: |
67 | 135 | auth = MultiDomainBasicAuth()
|
68 |
| - auth.passwords["example.com"] = ("user", "pass") |
| 136 | + auth.passwords.append(("http://example.com/path1", ("user", "pass"))) |
69 | 137 |
|
70 |
| - got = auth._get_url_and_credentials("http://foo@example.com/path") |
71 |
| - expected = ("http://example.com/path", "foo", "") |
| 138 | + got = auth._get_url_and_credentials("http://example.com/path2") |
| 139 | + expected = ("http://example.com/path2", None, None) |
72 | 140 | assert got == expected
|
73 | 141 |
|
74 | 142 |
|
75 | 143 | def test_get_credentials_uses_cached_credentials() -> None:
|
76 | 144 | auth = MultiDomainBasicAuth()
|
77 |
| - auth.passwords["example.com"] = ("user", "pass") |
| 145 | + auth.passwords.append(("https://example.com", ("user", "pass"))) |
| 146 | + |
| 147 | + got = auth._get_url_and_credentials("https://example.com/path") |
| 148 | + expected = ("https://example.com/path", "user", "pass") |
| 149 | + assert got == expected |
| 150 | + |
| 151 | + |
| 152 | +def test_get_credentials_not_uses_cached_credentials_different_scheme_http() -> None: |
| 153 | + auth = MultiDomainBasicAuth() |
| 154 | + auth.passwords.append(("http://example.com", ("user", "pass"))) |
| 155 | + |
| 156 | + got = auth._get_url_and_credentials("https://example.com/path") |
| 157 | + expected = ("https://example.com/path", None, None) |
| 158 | + assert got == expected |
| 159 | + |
| 160 | + |
| 161 | +def test_get_credentials_not_uses_cached_credentials_different_scheme_https() -> None: |
| 162 | + auth = MultiDomainBasicAuth() |
| 163 | + auth.passwords.append(("https://example.com", ("user", "pass"))) |
78 | 164 |
|
79 | 165 | got = auth._get_url_and_credentials("http://example.com/path")
|
80 |
| - expected = ("http://example.com/path", "user", "pass") |
| 166 | + expected = ("http://example.com/path", None, None) |
81 | 167 | assert got == expected
|
82 | 168 |
|
83 | 169 |
|
84 | 170 | def test_get_credentials_uses_cached_credentials_only_username() -> None:
|
85 | 171 | auth = MultiDomainBasicAuth()
|
86 |
| - auth.passwords["example.com"] = ("user", "pass") |
| 172 | + auth.passwords.append(("http://example.com", ("user", "pass"))) |
| 173 | + |
| 174 | + got = auth. _get_url_and_credentials( "http://[email protected]/path") |
| 175 | + expected = ("http://example.com/path", "user", "pass") |
| 176 | + assert got == expected |
| 177 | + |
| 178 | + |
| 179 | +def test_get_credentials_uses_cached_credentials_wrong_username() -> None: |
| 180 | + auth = MultiDomainBasicAuth() |
| 181 | + auth.passwords.append(("http://example.com", ("user", "pass"))) |
| 182 | + |
| 183 | + got = auth. _get_url_and_credentials( "http://[email protected]/path") |
| 184 | + expected = ("http://example.com/path", "user2", "") |
| 185 | + assert got == expected |
| 186 | + |
| 187 | + |
| 188 | +def test_get_credentials_added_multiple_times() -> None: |
| 189 | + auth = MultiDomainBasicAuth() |
| 190 | + auth.passwords.append(("http://example.com", ("user", "pass"))) |
| 191 | + auth.passwords.append(("http://example.com", ("user", "pass2"))) |
87 | 192 |
|
88 | 193 | got = auth. _get_url_and_credentials( "http://[email protected]/path")
|
89 | 194 | expected = ("http://example.com/path", "user", "pass")
|
|
0 commit comments