test-hgweb-auth.py
207 lines
| 5.5 KiB
| text/x-python
|
PythonLexer
/ tests / test-hgweb-auth.py
|
r28748 | from __future__ import absolute_import, print_function | ||
|
r28747 | |||
|
r43346 | from mercurial import demandimport | ||
demandimport.enable() | ||||
|
r28747 | from mercurial import ( | ||
|
r28808 | error, | ||
|
r37959 | pycompat, | ||
|
r28807 | ui as uimod, | ||
|
r28747 | url, | ||
util, | ||||
) | ||||
|
r43346 | from mercurial.utils import stringutil | ||
|
r8333 | |||
|
r28883 | urlerr = util.urlerr | ||
urlreq = util.urlreq | ||||
|
r43346 | |||
|
r28807 | class myui(uimod.ui): | ||
|
r8333 | def interactive(self): | ||
return False | ||||
|
r43346 | |||
|
r30559 | origui = myui.load() | ||
|
r8333 | |||
|
r43346 | |||
|
r8333 | def writeauth(items): | ||
ui = origui.copy() | ||||
|
r36345 | for name, value in items.items(): | ||
|
r41494 | ui.setconfig(b'auth', name, value) | ||
|
r8333 | return ui | ||
|
r43346 | |||
|
r41494 | def _stringifyauthinfo(ai): | ||
if ai is None: | ||||
return ai | ||||
realm, authuris, user, passwd = ai | ||||
|
r43346 | return ( | ||
pycompat.strurl(realm), | ||||
[pycompat.strurl(u) for u in authuris], | ||||
pycompat.strurl(user), | ||||
pycompat.strurl(passwd), | ||||
|
r41494 | ) | ||
|
r43346 | |||
|
r15005 | def test(auth, urls=None): | ||
|
r37961 | print('CFG:', pycompat.sysstr(stringutil.pprint(auth, bprefix=True))) | ||
|
r8333 | prefixes = set() | ||
for k in auth: | ||||
|
r41494 | prefixes.add(k.split(b'.', 1)[0]) | ||
|
r8333 | for p in prefixes: | ||
|
r41494 | for name in (b'.username', b'.password'): | ||
|
r15005 | if (p + name) not in auth: | ||
auth[p + name] = p | ||||
|
r44937 | auth = {k: v for k, v in auth.items() if v is not None} | ||
|
r8333 | |||
ui = writeauth(auth) | ||||
def _test(uri): | ||||
|
r41494 | print('URI:', pycompat.strurl(uri)) | ||
|
r8333 | try: | ||
|
r29377 | pm = url.passwordmgr(ui, urlreq.httppasswordmgrwithdefaultrealm()) | ||
|
r15025 | u, authinfo = util.url(uri).authinfo() | ||
|
r15005 | if authinfo is not None: | ||
|
r41494 | pm.add_password(*_stringifyauthinfo(authinfo)) | ||
|
r43346 | print( | ||
' ', | ||||
tuple( | ||||
pycompat.strurl(a) | ||||
for a in pm.find_user_password('test', pycompat.strurl(u)) | ||||
), | ||||
) | ||||
|
r28808 | except error.Abort: | ||
|
r43346 | print(' ', 'abort') | ||
|
r8333 | |||
|
r15005 | if not urls: | ||
urls = [ | ||||
|
r41494 | b'http://example.org/foo', | ||
b'http://example.org/foo/bar', | ||||
b'http://example.org/bar', | ||||
b'https://example.org/foo', | ||||
b'https://example.org/foo/bar', | ||||
b'https://example.org/bar', | ||||
b'https://x@example.org/bar', | ||||
b'https://y@example.org/bar', | ||||
|
r43346 | ] | ||
|
r15005 | for u in urls: | ||
_test(u) | ||||
|
r8333 | |||
|
r28748 | print('\n*** Test in-uri schemes\n') | ||
|
r41494 | test({b'x.prefix': b'http://example.org'}) | ||
test({b'x.prefix': b'https://example.org'}) | ||||
test({b'x.prefix': b'http://example.org', b'x.schemes': b'https'}) | ||||
test({b'x.prefix': b'https://example.org', b'x.schemes': b'http'}) | ||||
|
r8333 | |||
|
r28748 | print('\n*** Test separately configured schemes\n') | ||
|
r41494 | test({b'x.prefix': b'example.org', b'x.schemes': b'http'}) | ||
test({b'x.prefix': b'example.org', b'x.schemes': b'https'}) | ||||
test({b'x.prefix': b'example.org', b'x.schemes': b'http https'}) | ||||
|
r8333 | |||
|
r28748 | print('\n*** Test prefix matching\n') | ||
|
r43346 | test( | ||
{ | ||||
b'x.prefix': b'http://example.org/foo', | ||||
b'y.prefix': b'http://example.org/bar', | ||||
} | ||||
) | ||||
test( | ||||
{ | ||||
b'x.prefix': b'http://example.org/foo', | ||||
b'y.prefix': b'http://example.org/foo/bar', | ||||
} | ||||
) | ||||
|
r41494 | test({b'x.prefix': b'*', b'y.prefix': b'https://example.org/bar'}) | ||
|
r15005 | |||
|
r28748 | print('\n*** Test user matching\n') | ||
|
r43346 | test( | ||
{ | ||||
b'x.prefix': b'http://example.org/foo', | ||||
b'x.username': None, | ||||
b'x.password': b'xpassword', | ||||
}, | ||||
urls=[b'http://y@example.org/foo'], | ||||
) | ||||
test( | ||||
{ | ||||
b'x.prefix': b'http://example.org/foo', | ||||
b'x.username': None, | ||||
b'x.password': b'xpassword', | ||||
b'y.prefix': b'http://example.org/foo', | ||||
b'y.username': b'y', | ||||
b'y.password': b'ypassword', | ||||
}, | ||||
urls=[b'http://y@example.org/foo'], | ||||
) | ||||
test( | ||||
{ | ||||
b'x.prefix': b'http://example.org/foo/bar', | ||||
b'x.username': None, | ||||
b'x.password': b'xpassword', | ||||
b'y.prefix': b'http://example.org/foo', | ||||
b'y.username': b'y', | ||||
b'y.password': b'ypassword', | ||||
}, | ||||
urls=[b'http://y@example.org/foo/bar'], | ||||
) | ||||
|
r15024 | |||
|
r40699 | print('\n*** Test user matching with name in prefix\n') | ||
# prefix, username and URL have the same user | ||||
|
r43346 | test( | ||
{ | ||||
b'x.prefix': b'https://example.org/foo', | ||||
b'x.username': None, | ||||
b'x.password': b'xpassword', | ||||
b'y.prefix': b'http://y@example.org/foo', | ||||
b'y.username': b'y', | ||||
b'y.password': b'ypassword', | ||||
}, | ||||
urls=[b'http://y@example.org/foo'], | ||||
) | ||||
|
r40699 | # Prefix has a different user from username and URL | ||
|
r43346 | test( | ||
{ | ||||
b'y.prefix': b'http://z@example.org/foo', | ||||
b'y.username': b'y', | ||||
b'y.password': b'ypassword', | ||||
}, | ||||
urls=[b'http://y@example.org/foo'], | ||||
) | ||||
|
r40699 | # Prefix has a different user from URL; no username | ||
|
r43346 | test( | ||
{b'y.prefix': b'http://z@example.org/foo', b'y.password': b'ypassword'}, | ||||
urls=[b'http://y@example.org/foo'], | ||||
) | ||||
|
r40699 | # Prefix and URL have same user, but doesn't match username | ||
|
r43346 | test( | ||
{ | ||||
b'y.prefix': b'http://y@example.org/foo', | ||||
b'y.username': b'z', | ||||
b'y.password': b'ypassword', | ||||
}, | ||||
urls=[b'http://y@example.org/foo'], | ||||
) | ||||
|
r40699 | # Prefix and URL have the same user; no username | ||
|
r43346 | test( | ||
{b'y.prefix': b'http://y@example.org/foo', b'y.password': b'ypassword'}, | ||||
urls=[b'http://y@example.org/foo'], | ||||
) | ||||
|
r40699 | # Prefix user, but no URL user or username | ||
|
r43346 | test( | ||
{b'y.prefix': b'http://y@example.org/foo', b'y.password': b'ypassword'}, | ||||
urls=[b'http://example.org/foo'], | ||||
) | ||||
|
r40699 | |||
|
r15024 | def testauthinfo(fullurl, authurl): | ||
|
r28748 | print('URIs:', fullurl, authurl) | ||
|
r28883 | pm = urlreq.httppasswordmgrwithdefaultrealm() | ||
|
r41494 | ai = _stringifyauthinfo(util.url(pycompat.bytesurl(fullurl)).authinfo()[1]) | ||
pm.add_password(*ai) | ||||
|
r28748 | print(pm.find_user_password('test', authurl)) | ||
|
r15024 | |||
|
r43346 | |||
|
r28748 | print('\n*** Test urllib2 and util.url\n') | ||
|
r15024 | testauthinfo('http://user@example.com:8080/foo', 'http://example.com:8080/foo') | ||