tests/test-hgweb-auth.py
changeset 28748 c2ba5a810fa1
parent 28747 779addce6910
child 28807 736f64b23a24
equal deleted inserted replaced
28747:779addce6910 28748:c2ba5a810fa1
     1 from __future__ import absolute_import
     1 from __future__ import absolute_import, print_function
     2 
     2 
     3 from mercurial import demandimport; demandimport.enable()
     3 from mercurial import demandimport; demandimport.enable()
     4 import urllib2
     4 import urllib2
     5 from mercurial import (
     5 from mercurial import (
     6     ui,
     6     ui,
    26 def dumpdict(dict):
    26 def dumpdict(dict):
    27     return '{' + ', '.join(['%s: %s' % (k, dict[k])
    27     return '{' + ', '.join(['%s: %s' % (k, dict[k])
    28                             for k in sorted(dict.iterkeys())]) + '}'
    28                             for k in sorted(dict.iterkeys())]) + '}'
    29 
    29 
    30 def test(auth, urls=None):
    30 def test(auth, urls=None):
    31     print 'CFG:', dumpdict(auth)
    31     print('CFG:', dumpdict(auth))
    32     prefixes = set()
    32     prefixes = set()
    33     for k in auth:
    33     for k in auth:
    34         prefixes.add(k.split('.', 1)[0])
    34         prefixes.add(k.split('.', 1)[0])
    35     for p in prefixes:
    35     for p in prefixes:
    36         for name in ('.username', '.password'):
    36         for name in ('.username', '.password'):
    39     auth = dict((k, v) for k, v in auth.iteritems() if v is not None)
    39     auth = dict((k, v) for k, v in auth.iteritems() if v is not None)
    40 
    40 
    41     ui = writeauth(auth)
    41     ui = writeauth(auth)
    42 
    42 
    43     def _test(uri):
    43     def _test(uri):
    44         print 'URI:', uri
    44         print('URI:', uri)
    45         try:
    45         try:
    46             pm = url.passwordmgr(ui)
    46             pm = url.passwordmgr(ui)
    47             u, authinfo = util.url(uri).authinfo()
    47             u, authinfo = util.url(uri).authinfo()
    48             if authinfo is not None:
    48             if authinfo is not None:
    49                 pm.add_password(*authinfo)
    49                 pm.add_password(*authinfo)
    50             print '    ', pm.find_user_password('test', u)
    50             print('    ', pm.find_user_password('test', u))
    51         except Abort:
    51         except Abort:
    52             print 'abort'
    52             print('    ','abort')
    53 
    53 
    54     if not urls:
    54     if not urls:
    55         urls = [
    55         urls = [
    56             'http://example.org/foo',
    56             'http://example.org/foo',
    57             'http://example.org/foo/bar',
    57             'http://example.org/foo/bar',
    64             ]
    64             ]
    65     for u in urls:
    65     for u in urls:
    66         _test(u)
    66         _test(u)
    67 
    67 
    68 
    68 
    69 print '\n*** Test in-uri schemes\n'
    69 print('\n*** Test in-uri schemes\n')
    70 test({'x.prefix': 'http://example.org'})
    70 test({'x.prefix': 'http://example.org'})
    71 test({'x.prefix': 'https://example.org'})
    71 test({'x.prefix': 'https://example.org'})
    72 test({'x.prefix': 'http://example.org', 'x.schemes': 'https'})
    72 test({'x.prefix': 'http://example.org', 'x.schemes': 'https'})
    73 test({'x.prefix': 'https://example.org', 'x.schemes': 'http'})
    73 test({'x.prefix': 'https://example.org', 'x.schemes': 'http'})
    74 
    74 
    75 print '\n*** Test separately configured schemes\n'
    75 print('\n*** Test separately configured schemes\n')
    76 test({'x.prefix': 'example.org', 'x.schemes': 'http'})
    76 test({'x.prefix': 'example.org', 'x.schemes': 'http'})
    77 test({'x.prefix': 'example.org', 'x.schemes': 'https'})
    77 test({'x.prefix': 'example.org', 'x.schemes': 'https'})
    78 test({'x.prefix': 'example.org', 'x.schemes': 'http https'})
    78 test({'x.prefix': 'example.org', 'x.schemes': 'http https'})
    79 
    79 
    80 print '\n*** Test prefix matching\n'
    80 print('\n*** Test prefix matching\n')
    81 test({'x.prefix': 'http://example.org/foo',
    81 test({'x.prefix': 'http://example.org/foo',
    82       'y.prefix': 'http://example.org/bar'})
    82       'y.prefix': 'http://example.org/bar'})
    83 test({'x.prefix': 'http://example.org/foo',
    83 test({'x.prefix': 'http://example.org/foo',
    84       'y.prefix': 'http://example.org/foo/bar'})
    84       'y.prefix': 'http://example.org/foo/bar'})
    85 test({'x.prefix': '*', 'y.prefix': 'https://example.org/bar'})
    85 test({'x.prefix': '*', 'y.prefix': 'https://example.org/bar'})
    86 
    86 
    87 print '\n*** Test user matching\n'
    87 print('\n*** Test user matching\n')
    88 test({'x.prefix': 'http://example.org/foo',
    88 test({'x.prefix': 'http://example.org/foo',
    89       'x.username': None,
    89       'x.username': None,
    90       'x.password': 'xpassword'},
    90       'x.password': 'xpassword'},
    91      urls=['http://y@example.org/foo'])
    91      urls=['http://y@example.org/foo'])
    92 test({'x.prefix': 'http://example.org/foo',
    92 test({'x.prefix': 'http://example.org/foo',
   103       'y.username': 'y',
   103       'y.username': 'y',
   104       'y.password': 'ypassword'},
   104       'y.password': 'ypassword'},
   105      urls=['http://y@example.org/foo/bar'])
   105      urls=['http://y@example.org/foo/bar'])
   106 
   106 
   107 def testauthinfo(fullurl, authurl):
   107 def testauthinfo(fullurl, authurl):
   108     print 'URIs:', fullurl, authurl
   108     print('URIs:', fullurl, authurl)
   109     pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
   109     pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
   110     pm.add_password(*util.url(fullurl).authinfo()[1])
   110     pm.add_password(*util.url(fullurl).authinfo()[1])
   111     print pm.find_user_password('test', authurl)
   111     print(pm.find_user_password('test', authurl))
   112 
   112 
   113 print '\n*** Test urllib2 and util.url\n'
   113 print('\n*** Test urllib2 and util.url\n')
   114 testauthinfo('http://user@example.com:8080/foo', 'http://example.com:8080/foo')
   114 testauthinfo('http://user@example.com:8080/foo', 'http://example.com:8080/foo')