1
1
import functools
2
- from typing import Any , List , Optional , Tuple
2
+ import sys
3
+ from typing import Any , Iterable , List , Optional , Tuple
3
4
4
5
import pytest
5
6
8
9
from tests .lib .requests_mocks import MockConnection , MockRequest , MockResponse
9
10
10
11
12
+ @pytest .fixture (scope = "function" , autouse = True )
13
+ def reset_keyring () -> Iterable [None ]:
14
+ yield None
15
+ # Reset the state of the module between tests
16
+ pip ._internal .network .auth .KEYRING_DISABLED = False
17
+ pip ._internal .network .auth .get_keyring_provider .cache_clear ()
18
+
19
+
11
20
@pytest .mark .parametrize (
12
21
["input_url" , "url" , "username" , "password" ],
13
22
[
@@ -138,7 +147,7 @@ def test_keyring_get_password(
138
147
expect : Tuple [Optional [str ], Optional [str ]],
139
148
) -> None :
140
149
keyring = KeyringModuleV1 ()
141
- monkeypatch .setattr ( "pip._internal.network.auth. keyring" , keyring )
150
+ monkeypatch .setitem ( sys . modules , " keyring" , keyring ) # type: ignore[misc]
142
151
auth = MultiDomainBasicAuth (index_urls = ["http://example.com/path2" ])
143
152
144
153
actual = auth ._get_new_credentials (url , allow_netrc = False , allow_keyring = True )
@@ -147,7 +156,7 @@ def test_keyring_get_password(
147
156
148
157
def test_keyring_get_password_after_prompt (monkeypatch : pytest .MonkeyPatch ) -> None :
149
158
keyring = KeyringModuleV1 ()
150
- monkeypatch .setattr ( "pip._internal.network.auth. keyring" , keyring )
159
+ monkeypatch .setitem ( sys . modules , " keyring" , keyring ) # type: ignore[misc]
151
160
auth = MultiDomainBasicAuth ()
152
161
153
162
def ask_input (prompt : str ) -> str :
@@ -163,7 +172,7 @@ def test_keyring_get_password_after_prompt_when_none(
163
172
monkeypatch : pytest .MonkeyPatch ,
164
173
) -> None :
165
174
keyring = KeyringModuleV1 ()
166
- monkeypatch .setattr ( "pip._internal.network.auth. keyring" , keyring )
175
+ monkeypatch .setitem ( sys . modules , " keyring" , keyring ) # type: ignore[misc]
167
176
auth = MultiDomainBasicAuth ()
168
177
169
178
def ask_input (prompt : str ) -> str :
@@ -184,7 +193,7 @@ def test_keyring_get_password_username_in_index(
184
193
monkeypatch : pytest .MonkeyPatch ,
185
194
) -> None :
186
195
keyring = KeyringModuleV1 ()
187
- monkeypatch .setattr ( "pip._internal.network.auth. keyring" , keyring )
196
+ monkeypatch .setitem ( sys . modules , " keyring" , keyring ) # type: ignore[misc]
188
197
auth = MultiDomainBasicAuth (
index_urls = [
"http://[email protected] /path2" ])
189
198
get = functools .partial (
190
199
auth ._get_new_credentials , allow_netrc = False , allow_keyring = True
@@ -217,7 +226,7 @@ def test_keyring_set_password(
217
226
expect_save : bool ,
218
227
) -> None :
219
228
keyring = KeyringModuleV1 ()
220
- monkeypatch .setattr ( "pip._internal.network.auth. keyring" , keyring )
229
+ monkeypatch .setitem ( sys . modules , " keyring" , keyring ) # type: ignore[misc]
221
230
auth = MultiDomainBasicAuth (prompting = True )
222
231
monkeypatch .setattr (auth , "_get_url_and_credentials" , lambda u : (u , None , None ))
223
232
monkeypatch .setattr (auth , "_prompt_for_password" , lambda * a : creds )
@@ -293,7 +302,7 @@ def get_credential(self, system: str, username: str) -> Optional[Credential]:
293
302
def test_keyring_get_credential (
294
303
monkeypatch : pytest .MonkeyPatch , url : str , expect : str
295
304
) -> None :
296
- monkeypatch .setattr ( pip . _internal . network . auth , "keyring" , KeyringModuleV2 ())
305
+ monkeypatch .setitem ( sys . modules , "keyring" , KeyringModuleV2 ()) # type: ignore[misc]
297
306
auth = MultiDomainBasicAuth (index_urls = ["http://example.com/path2" ])
298
307
299
308
assert (
@@ -314,7 +323,7 @@ def get_credential(self, system: str, username: str) -> None:
314
323
315
324
def test_broken_keyring_disables_keyring (monkeypatch : pytest .MonkeyPatch ) -> None :
316
325
keyring_broken = KeyringModuleBroken ()
317
- monkeypatch .setattr ( pip . _internal . network . auth , "keyring" , keyring_broken )
326
+ monkeypatch .setitem ( sys . modules , "keyring" , keyring_broken ) # type: ignore[misc]
318
327
319
328
auth = MultiDomainBasicAuth (index_urls = ["http://example.com/" ])
320
329
0 commit comments