test-hgweb-auth.py
114 lines
| 3.5 KiB
| text/x-python
|
PythonLexer
/ tests / test-hgweb-auth.py
|
r28748 | from __future__ import absolute_import, print_function | ||
|
r28747 | |||
|
r8333 | from mercurial import demandimport; demandimport.enable() | ||
|
r28747 | from mercurial import ( | ||
|
r28808 | error, | ||
|
r28807 | ui as uimod, | ||
|
r28747 | url, | ||
util, | ||||
) | ||||
|
r8333 | |||
|
r28883 | urlerr = util.urlerr | ||
urlreq = util.urlreq | ||||
|
r28807 | class myui(uimod.ui): | ||
|
r8333 | def interactive(self): | ||
return False | ||||
|
r30559 | origui = myui.load() | ||
|
r8333 | |||
def writeauth(items): | ||||
ui = origui.copy() | ||||
for name, value in items.iteritems(): | ||||
ui.setconfig('auth', name, value) | ||||
return ui | ||||
def dumpdict(dict): | ||||
|
r10282 | return '{' + ', '.join(['%s: %s' % (k, dict[k]) | ||
for k in sorted(dict.iterkeys())]) + '}' | ||||
|
r8333 | |||
|
r15005 | def test(auth, urls=None): | ||
|
r28748 | print('CFG:', dumpdict(auth)) | ||
|
r8333 | prefixes = set() | ||
for k in auth: | ||||
prefixes.add(k.split('.', 1)[0]) | ||||
for p in prefixes: | ||||
|
r15005 | for name in ('.username', '.password'): | ||
if (p + name) not in auth: | ||||
auth[p + name] = p | ||||
auth = dict((k, v) for k, v in auth.iteritems() if v is not None) | ||||
|
r8333 | |||
ui = writeauth(auth) | ||||
def _test(uri): | ||||
|
r28748 | print('URI:', uri) | ||
|
r8333 | try: | ||
|
r29377 | pm = url.passwordmgr(ui, urlreq.httppasswordmgrwithdefaultrealm()) | ||
|
r15025 | u, authinfo = util.url(uri).authinfo() | ||
|
r15005 | if authinfo is not None: | ||
pm.add_password(*authinfo) | ||||
|
r28748 | print(' ', pm.find_user_password('test', u)) | ||
|
r28808 | except error.Abort: | ||
|
r28748 | print(' ','abort') | ||
|
r8333 | |||
|
r15005 | if not urls: | ||
urls = [ | ||||
'http://example.org/foo', | ||||
'http://example.org/foo/bar', | ||||
'http://example.org/bar', | ||||
'https://example.org/foo', | ||||
'https://example.org/foo/bar', | ||||
'https://example.org/bar', | ||||
'https://x@example.org/bar', | ||||
'https://y@example.org/bar', | ||||
] | ||||
for u in urls: | ||||
_test(u) | ||||
|
r8333 | |||
|
r28748 | print('\n*** Test in-uri schemes\n') | ||
|
r8333 | test({'x.prefix': 'http://example.org'}) | ||
test({'x.prefix': 'https://example.org'}) | ||||
test({'x.prefix': 'http://example.org', 'x.schemes': 'https'}) | ||||
test({'x.prefix': 'https://example.org', 'x.schemes': 'http'}) | ||||
|
r28748 | print('\n*** Test separately configured schemes\n') | ||
|
r8333 | test({'x.prefix': 'example.org', 'x.schemes': 'http'}) | ||
test({'x.prefix': 'example.org', 'x.schemes': 'https'}) | ||||
test({'x.prefix': 'example.org', 'x.schemes': 'http https'}) | ||||
|
r28748 | print('\n*** Test prefix matching\n') | ||
|
r10282 | test({'x.prefix': 'http://example.org/foo', | ||
'y.prefix': 'http://example.org/bar'}) | ||||
test({'x.prefix': 'http://example.org/foo', | ||||
'y.prefix': 'http://example.org/foo/bar'}) | ||||
|
r8333 | test({'x.prefix': '*', 'y.prefix': 'https://example.org/bar'}) | ||
|
r15005 | |||
|
r28748 | print('\n*** Test user matching\n') | ||
|
r15005 | test({'x.prefix': 'http://example.org/foo', | ||
'x.username': None, | ||||
'x.password': 'xpassword'}, | ||||
urls=['http://y@example.org/foo']) | ||||
test({'x.prefix': 'http://example.org/foo', | ||||
'x.username': None, | ||||
'x.password': 'xpassword', | ||||
'y.prefix': 'http://example.org/foo', | ||||
'y.username': 'y', | ||||
'y.password': 'ypassword'}, | ||||
urls=['http://y@example.org/foo']) | ||||
test({'x.prefix': 'http://example.org/foo/bar', | ||||
'x.username': None, | ||||
'x.password': 'xpassword', | ||||
'y.prefix': 'http://example.org/foo', | ||||
'y.username': 'y', | ||||
'y.password': 'ypassword'}, | ||||
urls=['http://y@example.org/foo/bar']) | ||||
|
r15024 | |||
def testauthinfo(fullurl, authurl): | ||||
|
r28748 | print('URIs:', fullurl, authurl) | ||
|
r28883 | pm = urlreq.httppasswordmgrwithdefaultrealm() | ||
|
r15024 | pm.add_password(*util.url(fullurl).authinfo()[1]) | ||
|
r28748 | print(pm.find_user_password('test', authurl)) | ||
|
r15024 | |||
|
r28748 | print('\n*** Test urllib2 and util.url\n') | ||
|
r15024 | testauthinfo('http://user@example.com:8080/foo', 'http://example.com:8080/foo') | ||