@@ -90,6 +90,10 @@ def set_password(self, system, username, password):
90
90
self .saved_passwords .append ((system , username , password ))
91
91
92
92
93
+ class MDBA_KeyringV1 (MultiDomainBasicAuth ):
94
+ _keyring = KeyringModuleV1 ()
95
+
96
+
93
97
@pytest .mark .parametrize ('url, expect' , (
94
98
("http://example.com/path1" , (None , None )),
95
99
# path1 URLs will be resolved by netloc
@@ -99,20 +103,16 @@ def set_password(self, system, username, password):
99
103
("http://example.com/path2/path3" , (None , None )),
100
104
("http://[email protected] /path2/path3" , ("foo" , "foo!url" )),
101
105
))
102
- def test_keyring_get_password (monkeypatch , url , expect ):
103
- keyring = KeyringModuleV1 ()
104
- monkeypatch .setattr ('pip._internal.network.auth.keyring' , keyring )
105
- auth = MultiDomainBasicAuth (index_urls = ["http://example.com/path2" ])
106
+ def test_keyring_get_password (url , expect ):
107
+ auth = MDBA_KeyringV1 (index_urls = ["http://example.com/path2" ])
106
108
107
109
actual = auth ._get_new_credentials (url , allow_netrc = False ,
108
110
allow_keyring = True )
109
111
assert actual == expect
110
112
111
113
112
114
def test_keyring_get_password_after_prompt (monkeypatch ):
113
- keyring = KeyringModuleV1 ()
114
- monkeypatch .setattr ('pip._internal.network.auth.keyring' , keyring )
115
- auth = MultiDomainBasicAuth ()
115
+ auth = MDBA_KeyringV1 ()
116
116
117
117
def ask_input (prompt ):
118
118
assert prompt == "User for example.com: "
@@ -124,9 +124,7 @@ def ask_input(prompt):
124
124
125
125
126
126
def test_keyring_get_password_after_prompt_when_none (monkeypatch ):
127
- keyring = KeyringModuleV1 ()
128
- monkeypatch .setattr ('pip._internal.network.auth.keyring' , keyring )
129
- auth = MultiDomainBasicAuth ()
127
+ auth = MDBA_KeyringV1 ()
130
128
131
129
def ask_input (prompt ):
132
130
assert prompt == "User for unknown.com: "
@@ -143,10 +141,8 @@ def ask_password(prompt):
143
141
assert actual == ("user" , "fake_password" , True )
144
142
145
143
146
- def test_keyring_get_password_username_in_index (monkeypatch ):
147
- keyring = KeyringModuleV1 ()
148
- monkeypatch .setattr ('pip._internal.network.auth.keyring' , keyring )
149
- auth = MultiDomainBasicAuth (
index_urls = [
"http://[email protected] /path2" ])
144
+ def test_keyring_get_password_username_in_index ():
145
+ auth = MDBA_KeyringV1 (
index_urls = [
"http://[email protected] /path2" ])
150
146
get = functools .partial (
151
147
auth ._get_new_credentials ,
152
148
allow_netrc = False ,
@@ -164,9 +160,7 @@ def test_keyring_get_password_username_in_index(monkeypatch):
164
160
))
165
161
def test_keyring_set_password (monkeypatch , response_status , creds ,
166
162
expect_save ):
167
- keyring = KeyringModuleV1 ()
168
- monkeypatch .setattr ('pip._internal.network.auth.keyring' , keyring )
169
- auth = MultiDomainBasicAuth (prompting = True )
163
+ auth = MDBA_KeyringV1 (prompting = True )
170
164
monkeypatch .setattr (auth , '_get_url_and_credentials' ,
171
165
lambda u : (u , None , None ))
172
166
monkeypatch .setattr (auth , '_prompt_for_password' , lambda * a : creds )
@@ -203,6 +197,7 @@ def _send(sent_req, **kwargs):
203
197
204
198
auth .handle_401 (resp )
205
199
200
+ keyring = auth .get_keyring ()
206
201
if expect_save :
207
202
assert keyring .saved_passwords == [("example.com" , creds [0 ], creds [1 ])]
208
203
else :
@@ -228,16 +223,17 @@ def get_credential(self, system, username):
228
223
return None
229
224
230
225
226
+ class MDBA_KeyringV2 (MultiDomainBasicAuth ):
227
+ _keyring = KeyringModuleV2 ()
228
+
229
+
231
230
@pytest .mark .parametrize ('url, expect' , (
232
231
("http://example.com/path1" , ("username" , "netloc" )),
233
232
("http://example.com/path2/path3" , ("username" , "url" )),
234
233
("http://[email protected] /path2/path3" , ("username" , "url" )),
235
234
))
236
- def test_keyring_get_credential (monkeypatch , url , expect ):
237
- monkeypatch .setattr (
238
- pip ._internal .network .auth , 'keyring' , KeyringModuleV2 ()
239
- )
240
- auth = MultiDomainBasicAuth (index_urls = ["http://example.com/path2" ])
235
+ def test_keyring_get_credential (url , expect ):
236
+ auth = MDBA_KeyringV1 (index_urls = ["http://example.com/path2" ])
241
237
242
238
assert auth ._get_new_credentials (
243
239
url , allow_netrc = False , allow_keyring = True
@@ -255,11 +251,13 @@ def get_credential(self, system, username):
255
251
raise Exception ("This keyring is broken!" )
256
252
257
253
258
- def test_broken_keyring_disables_keyring (monkeypatch ):
259
- keyring_broken = KeyringModuleBroken ()
260
- monkeypatch .setattr (pip ._internal .network .auth , 'keyring' , keyring_broken )
254
+ class MDBA_KeyringBroken (MultiDomainBasicAuth ):
255
+ _keyring = KeyringModuleBroken ()
261
256
257
+
258
+ def test_broken_keyring_disables_keyring (monkeypatch ):
262
259
auth = MultiDomainBasicAuth (index_urls = ["http://example.com/" ])
260
+ keyring_broken = auth .get_keyring ()
263
261
264
262
assert keyring_broken ._call_count == 0
265
263
for i in range (5 ):
0 commit comments