@@ -90,6 +90,16 @@ 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
+ @classmethod
95
+ def get_keyring (cls ):
96
+ if hasattr (cls , '_keyring' ):
97
+ return cls ._keyring
98
+
99
+ cls ._keyring = KeyringModuleV1 ()
100
+ return cls ._keyring
101
+
102
+
93
103
@pytest .mark .parametrize ('url, expect' , (
94
104
("http://example.com/path1" , (None , None )),
95
105
# path1 URLs will be resolved by netloc
@@ -99,20 +109,16 @@ def set_password(self, system, username, password):
99
109
("http://example.com/path2/path3" , (None , None )),
100
110
("http://[email protected] /path2/path3" , ("foo" , "foo!url" )),
101
111
))
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" ])
112
+ def test_keyring_get_password (url , expect ):
113
+ auth = MDBA_KeyringV1 (index_urls = ["http://example.com/path2" ])
106
114
107
115
actual = auth ._get_new_credentials (url , allow_netrc = False ,
108
116
allow_keyring = True )
109
117
assert actual == expect
110
118
111
119
112
120
def test_keyring_get_password_after_prompt (monkeypatch ):
113
- keyring = KeyringModuleV1 ()
114
- monkeypatch .setattr ('pip._internal.network.auth.keyring' , keyring )
115
- auth = MultiDomainBasicAuth ()
121
+ auth = MDBA_KeyringV1 ()
116
122
117
123
def ask_input (prompt ):
118
124
assert prompt == "User for example.com: "
@@ -124,9 +130,7 @@ def ask_input(prompt):
124
130
125
131
126
132
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 ()
133
+ auth = MDBA_KeyringV1 ()
130
134
131
135
def ask_input (prompt ):
132
136
assert prompt == "User for unknown.com: "
@@ -143,10 +147,8 @@ def ask_password(prompt):
143
147
assert actual == ("user" , "fake_password" , True )
144
148
145
149
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" ])
150
+ def test_keyring_get_password_username_in_index ():
151
+ auth = MDBA_KeyringV1 (
index_urls = [
"http://[email protected] /path2" ])
150
152
get = functools .partial (
151
153
auth ._get_new_credentials ,
152
154
allow_netrc = False ,
@@ -164,9 +166,7 @@ def test_keyring_get_password_username_in_index(monkeypatch):
164
166
))
165
167
def test_keyring_set_password (monkeypatch , response_status , creds ,
166
168
expect_save ):
167
- keyring = KeyringModuleV1 ()
168
- monkeypatch .setattr ('pip._internal.network.auth.keyring' , keyring )
169
- auth = MultiDomainBasicAuth (prompting = True )
169
+ auth = MDBA_KeyringV1 (prompting = True )
170
170
monkeypatch .setattr (auth , '_get_url_and_credentials' ,
171
171
lambda u : (u , None , None ))
172
172
monkeypatch .setattr (auth , '_prompt_for_password' , lambda * a : creds )
@@ -203,6 +203,7 @@ def _send(sent_req, **kwargs):
203
203
204
204
auth .handle_401 (resp )
205
205
206
+ keyring = auth .get_keyring ()
206
207
if expect_save :
207
208
assert keyring .saved_passwords == [("example.com" , creds [0 ], creds [1 ])]
208
209
else :
@@ -228,16 +229,23 @@ def get_credential(self, system, username):
228
229
return None
229
230
230
231
232
+ class MDBA_KeyringV2 (MultiDomainBasicAuth ):
233
+ @classmethod
234
+ def get_keyring (cls ):
235
+ if hasattr (cls , '_keyring' ):
236
+ return cls ._keyring
237
+
238
+ cls ._keyring = KeyringModuleV2 ()
239
+ return cls ._keyring
240
+
241
+
231
242
@pytest .mark .parametrize ('url, expect' , (
232
243
("http://example.com/path1" , ("username" , "netloc" )),
233
244
("http://example.com/path2/path3" , ("username" , "url" )),
234
245
("http://[email protected] /path2/path3" , ("username" , "url" )),
235
246
))
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" ])
247
+ def test_keyring_get_credential (url , expect ):
248
+ auth = MDBA_KeyringV1 (index_urls = ["http://example.com/path2" ])
241
249
242
250
assert auth ._get_new_credentials (
243
251
url , allow_netrc = False , allow_keyring = True
@@ -255,11 +263,19 @@ def get_credential(self, system, username):
255
263
raise Exception ("This keyring is broken!" )
256
264
257
265
258
- def test_broken_keyring_disables_keyring (monkeypatch ):
259
- keyring_broken = KeyringModuleBroken ()
260
- monkeypatch .setattr (pip ._internal .network .auth , 'keyring' , keyring_broken )
266
+ class MDBA_KeyringBroken (MultiDomainBasicAuth ):
267
+ @classmethod
268
+ def get_keyring (cls ):
269
+ if hasattr (cls , '_keyring' ):
270
+ return cls ._keyring
261
271
272
+ cls ._keyring = KeyringModuleBroken ()
273
+ return cls ._keyring
274
+
275
+
276
+ def test_broken_keyring_disables_keyring (monkeypatch ):
262
277
auth = MultiDomainBasicAuth (index_urls = ["http://example.com/" ])
278
+ keyring_broken = auth .get_keyring ()
263
279
264
280
assert keyring_broken ._call_count == 0
265
281
for i in range (5 ):
0 commit comments