@@ -102,7 +102,12 @@ def test_get_credentials_uses_cached_credentials_only_username() -> None:
102
102
103
103
104
104
def test_get_index_url_credentials () -> None :
105
- auth = MultiDomainBasicAuth (
index_urls = [
"http://foo:[email protected] /path" ])
105
+ auth = MultiDomainBasicAuth (
106
+ index_urls = [
107
+ "http://example.com/" ,
108
+ "http://foo:[email protected] /path" ,
109
+ ]
110
+ )
106
111
get = functools .partial (
107
112
auth ._get_new_credentials , allow_netrc = False , allow_keyring = False
108
113
)
@@ -112,6 +117,45 @@ def test_get_index_url_credentials() -> None:
112
117
assert get ("http://example.com/path3/path2" ) == (None , None )
113
118
114
119
120
+ def test_prioritize_longest_path_prefix_match_organization () -> None :
121
+ auth = MultiDomainBasicAuth (
122
+ index_urls = [
123
+ "http://foo:[email protected] /org-name-alpha/repo-alias/simple" ,
124
+ "http://bar:[email protected] /org-name-beta/repo-alias/simple" ,
125
+ ]
126
+ )
127
+ get = functools .partial (
128
+ auth ._get_new_credentials , allow_netrc = False , allow_keyring = False
129
+ )
130
+
131
+ # Inspired by Azure DevOps URL structure, GitLab should look similar
132
+ assert get ("http://example.com/org-name-alpha/repo-guid/dowbload/" ) == (
133
+ "foo" ,
134
+ "bar" ,
135
+ )
136
+ assert get ("http://example.com/org-name-beta/repo-guid/dowbload/" ) == ("bar" , "foo" )
137
+
138
+
139
+ def test_prioritize_longest_path_prefix_match_project () -> None :
140
+ auth = MultiDomainBasicAuth (
141
+ index_urls = [
142
+ "http://foo:[email protected] /org-alpha/project-name-alpha/repo-alias/simple" ,
143
+ "http://bar:[email protected] /org-alpha/project-name-beta/repo-alias/simple" ,
144
+ ]
145
+ )
146
+ get = functools .partial (
147
+ auth ._get_new_credentials , allow_netrc = False , allow_keyring = False
148
+ )
149
+
150
+ # Inspired by Azure DevOps URL structure, GitLab should look similar
151
+ assert get (
152
+ "http://example.com/org-alpha/project-name-alpha/repo-guid/dowbload/"
153
+ ) == ("foo" , "bar" )
154
+ assert get (
155
+ "http://example.com/org-alpha/project-name-beta/repo-guid/dowbload/"
156
+ ) == ("bar" , "foo" )
157
+
158
+
115
159
class KeyringModuleV1 :
116
160
"""Represents the supported API of keyring before get_credential
117
161
was added.
@@ -123,7 +167,7 @@ def __init__(self) -> None:
123
167
def get_password (self , system : str , username : str ) -> Optional [str ]:
124
168
if system == "example.com" and username :
125
169
return username + "!netloc"
126
- if system == "http://example.com/path2" and username :
170
+ if system == "http://example.com/path2/ " and username :
127
171
return username + "!url"
128
172
return None
129
173
@@ -136,8 +180,8 @@ def set_password(self, system: str, username: str, password: str) -> None:
136
180
(
137
181
("http://example.com/path1" , (None , None )),
138
182
# path1 URLs will be resolved by netloc
139
- ("http://[email protected] /path1 " , ("user" , "user!netloc" )),
140
- ("http://[email protected] /path1 " , ("user2" , "user2!netloc" )),
183
+ ("http://[email protected] /path3 " , ("user" , "user!netloc" )),
184
+ ("http://[email protected] /path3 " , ("user2" , "user2!netloc" )),
141
185
# path2 URLs will be resolved by index URL
142
186
("http://example.com/path2/path3" , (None , None )),
143
187
("http://[email protected] /path2/path3" , ("foo" , "foo!url" )),
@@ -151,7 +195,8 @@ def test_keyring_get_password(
151
195
keyring = KeyringModuleV1 ()
152
196
monkeypatch .setitem (sys .modules , "keyring" , keyring ) # type: ignore[misc]
153
197
auth = MultiDomainBasicAuth (
154
- index_urls = ["http://example.com/path2" ], keyring_provider = "import"
198
+ index_urls = ["http://example.com/path2" , "http://example.com/path3" ],
199
+ keyring_provider = "import" ,
155
200
)
156
201
157
202
actual = auth ._get_new_credentials (url , allow_netrc = False , allow_keyring = True )
@@ -199,7 +244,8 @@ def test_keyring_get_password_username_in_index(
199
244
keyring = KeyringModuleV1 ()
200
245
monkeypatch .setitem (sys .modules , "keyring" , keyring ) # type: ignore[misc]
201
246
auth = MultiDomainBasicAuth (
202
- index_urls = [
"http://[email protected] /path2" ],
keyring_provider = "import"
247
+ index_urls = [
"http://[email protected] /path2" ,
"http://example.com/path4" ],
248
+ keyring_provider = "import" ,
203
249
)
204
250
get = functools .partial (
205
251
auth ._get_new_credentials , allow_netrc = False , allow_keyring = True
@@ -290,7 +336,7 @@ def get_password(self, system: str, username: str) -> None:
290
336
assert False , "get_password should not ever be called"
291
337
292
338
def get_credential (self , system : str , username : str ) -> Optional [Credential ]:
293
- if system == "http://example.com/path2" :
339
+ if system == "http://example.com/path2/ " :
294
340
return self .Credential ("username" , "url" )
295
341
if system == "example.com" :
296
342
return self .Credential ("username" , "netloc" )
@@ -310,7 +356,8 @@ def test_keyring_get_credential(
310
356
) -> None :
311
357
monkeypatch .setitem (sys .modules , "keyring" , KeyringModuleV2 ()) # type: ignore[misc]
312
358
auth = MultiDomainBasicAuth (
313
- index_urls = ["http://example.com/path2" ], keyring_provider = "import"
359
+ index_urls = ["http://example.com/path1" , "http://example.com/path2" ],
360
+ keyring_provider = "import" ,
314
361
)
315
362
316
363
assert (
@@ -375,6 +422,7 @@ def __call__(
375
422
self .returncode = 1
376
423
else :
377
424
# Passwords are returned encoded with a newline appended
425
+ self .returncode = 0
378
426
self .stdout = (password + os .linesep ).encode ("utf-8" )
379
427
380
428
if cmd [1 ] == "set" :
@@ -400,8 +448,8 @@ def check_returncode(self) -> None:
400
448
(
401
449
("http://example.com/path1" , (None , None )),
402
450
# path1 URLs will be resolved by netloc
403
- ("http://[email protected] /path1 " , ("user" , "user!netloc" )),
404
- ("http://[email protected] /path1 " , ("user2" , "user2!netloc" )),
451
+ ("http://[email protected] /path3 " , ("user" , "user!netloc" )),
452
+ ("http://[email protected] /path3 " , ("user2" , "user2!netloc" )),
405
453
# path2 URLs will be resolved by index URL
406
454
("http://example.com/path2/path3" , (None , None )),
407
455
("http://[email protected] /path2/path3" , ("foo" , "foo!url" )),
@@ -417,7 +465,8 @@ def test_keyring_cli_get_password(
417
465
pip ._internal .network .auth .subprocess , "run" , KeyringSubprocessResult ()
418
466
)
419
467
auth = MultiDomainBasicAuth (
420
- index_urls = ["http://example.com/path2" ], keyring_provider = "subprocess"
468
+ index_urls = ["http://example.com/path2" , "http://example.com/path3" ],
469
+ keyring_provider = "subprocess" ,
421
470
)
422
471
423
472
actual = auth ._get_new_credentials (url , allow_netrc = False , allow_keyring = True )
0 commit comments