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