tests/test-hgweb-auth.py
changeset 8333 89c80c3dc584
child 10282 08a0f04b56bd
equal deleted inserted replaced
8332:3e544c074459 8333:89c80c3dc584
       
     1 from mercurial import demandimport; demandimport.enable()
       
     2 from mercurial import ui
       
     3 from mercurial import url
       
     4 from mercurial.error import Abort
       
     5 
       
     6 class myui(ui.ui):
       
     7     def interactive(self):
       
     8         return False
       
     9 
       
    10 origui = myui()
       
    11 
       
    12 def writeauth(items):
       
    13     ui = origui.copy()
       
    14     for name, value in items.iteritems():
       
    15         ui.setconfig('auth', name, value)
       
    16     return ui
       
    17 
       
    18 def dumpdict(dict):
       
    19     return '{' + ', '.join(['%s: %s' % (k, dict[k]) for k in sorted(dict.iterkeys())]) + '}'
       
    20 
       
    21 def test(auth):
       
    22     print 'CFG:', dumpdict(auth)
       
    23     prefixes = set()
       
    24     for k in auth:
       
    25         prefixes.add(k.split('.', 1)[0])
       
    26     for p in prefixes:
       
    27         auth.update({p + '.username': p, p + '.password': p})
       
    28 
       
    29     ui = writeauth(auth)
       
    30 
       
    31     def _test(uri):
       
    32         print 'URI:', uri
       
    33         try:
       
    34             pm = url.passwordmgr(ui)
       
    35             print '    ', pm.find_user_password('test', uri)
       
    36         except Abort, e:
       
    37             print 'abort'
       
    38 
       
    39     _test('http://example.org/foo')
       
    40     _test('http://example.org/foo/bar')
       
    41     _test('http://example.org/bar')
       
    42     _test('https://example.org/foo')
       
    43     _test('https://example.org/foo/bar')
       
    44     _test('https://example.org/bar')
       
    45 
       
    46 
       
    47 print '\n*** Test in-uri schemes\n'
       
    48 test({'x.prefix': 'http://example.org'})
       
    49 test({'x.prefix': 'https://example.org'})
       
    50 test({'x.prefix': 'http://example.org', 'x.schemes': 'https'})
       
    51 test({'x.prefix': 'https://example.org', 'x.schemes': 'http'})
       
    52 
       
    53 print '\n*** Test separately configured schemes\n'
       
    54 test({'x.prefix': 'example.org', 'x.schemes': 'http'})
       
    55 test({'x.prefix': 'example.org', 'x.schemes': 'https'})
       
    56 test({'x.prefix': 'example.org', 'x.schemes': 'http https'})
       
    57 
       
    58 print '\n*** Test prefix matching\n'
       
    59 test({'x.prefix': 'http://example.org/foo', 'y.prefix': 'http://example.org/bar'})
       
    60 test({'x.prefix': 'http://example.org/foo', 'y.prefix': 'http://example.org/foo/bar'})
       
    61 test({'x.prefix': '*', 'y.prefix': 'https://example.org/bar'})