tests/test-hgweb-auth.py
changeset 43076 2372284d9457
parent 41451 30dd20a56f3e
child 44452 9d2b2df2c2ba
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
     1 from __future__ import absolute_import, print_function
     1 from __future__ import absolute_import, print_function
     2 
     2 
     3 from mercurial import demandimport; demandimport.enable()
     3 from mercurial import demandimport
       
     4 
       
     5 demandimport.enable()
     4 from mercurial import (
     6 from mercurial import (
     5     error,
     7     error,
     6     pycompat,
     8     pycompat,
     7     ui as uimod,
     9     ui as uimod,
     8     url,
    10     url,
     9     util,
    11     util,
    10 )
    12 )
    11 from mercurial.utils import (
    13 from mercurial.utils import stringutil
    12     stringutil,
       
    13 )
       
    14 
    14 
    15 urlerr = util.urlerr
    15 urlerr = util.urlerr
    16 urlreq = util.urlreq
    16 urlreq = util.urlreq
       
    17 
    17 
    18 
    18 class myui(uimod.ui):
    19 class myui(uimod.ui):
    19     def interactive(self):
    20     def interactive(self):
    20         return False
    21         return False
    21 
    22 
       
    23 
    22 origui = myui.load()
    24 origui = myui.load()
       
    25 
    23 
    26 
    24 def writeauth(items):
    27 def writeauth(items):
    25     ui = origui.copy()
    28     ui = origui.copy()
    26     for name, value in items.items():
    29     for name, value in items.items():
    27         ui.setconfig(b'auth', name, value)
    30         ui.setconfig(b'auth', name, value)
    28     return ui
    31     return ui
    29 
    32 
       
    33 
    30 def _stringifyauthinfo(ai):
    34 def _stringifyauthinfo(ai):
    31     if ai is None:
    35     if ai is None:
    32         return ai
    36         return ai
    33     realm, authuris, user, passwd = ai
    37     realm, authuris, user, passwd = ai
    34     return (pycompat.strurl(realm),
    38     return (
    35             [pycompat.strurl(u) for u in authuris],
    39         pycompat.strurl(realm),
    36             pycompat.strurl(user),
    40         [pycompat.strurl(u) for u in authuris],
    37             pycompat.strurl(passwd),
    41         pycompat.strurl(user),
       
    42         pycompat.strurl(passwd),
    38     )
    43     )
       
    44 
    39 
    45 
    40 def test(auth, urls=None):
    46 def test(auth, urls=None):
    41     print('CFG:', pycompat.sysstr(stringutil.pprint(auth, bprefix=True)))
    47     print('CFG:', pycompat.sysstr(stringutil.pprint(auth, bprefix=True)))
    42     prefixes = set()
    48     prefixes = set()
    43     for k in auth:
    49     for k in auth:
    55         try:
    61         try:
    56             pm = url.passwordmgr(ui, urlreq.httppasswordmgrwithdefaultrealm())
    62             pm = url.passwordmgr(ui, urlreq.httppasswordmgrwithdefaultrealm())
    57             u, authinfo = util.url(uri).authinfo()
    63             u, authinfo = util.url(uri).authinfo()
    58             if authinfo is not None:
    64             if authinfo is not None:
    59                 pm.add_password(*_stringifyauthinfo(authinfo))
    65                 pm.add_password(*_stringifyauthinfo(authinfo))
    60             print('    ', tuple(pycompat.strurl(a) for a in
    66             print(
    61                                 pm.find_user_password('test',
    67                 '    ',
    62                                                       pycompat.strurl(u))))
    68                 tuple(
       
    69                     pycompat.strurl(a)
       
    70                     for a in pm.find_user_password('test', pycompat.strurl(u))
       
    71                 ),
       
    72             )
    63         except error.Abort:
    73         except error.Abort:
    64             print('    ','abort')
    74             print('    ', 'abort')
    65 
    75 
    66     if not urls:
    76     if not urls:
    67         urls = [
    77         urls = [
    68             b'http://example.org/foo',
    78             b'http://example.org/foo',
    69             b'http://example.org/foo/bar',
    79             b'http://example.org/foo/bar',
    71             b'https://example.org/foo',
    81             b'https://example.org/foo',
    72             b'https://example.org/foo/bar',
    82             b'https://example.org/foo/bar',
    73             b'https://example.org/bar',
    83             b'https://example.org/bar',
    74             b'https://x@example.org/bar',
    84             b'https://x@example.org/bar',
    75             b'https://y@example.org/bar',
    85             b'https://y@example.org/bar',
    76             ]
    86         ]
    77     for u in urls:
    87     for u in urls:
    78         _test(u)
    88         _test(u)
    79 
    89 
    80 
    90 
    81 print('\n*** Test in-uri schemes\n')
    91 print('\n*** Test in-uri schemes\n')
    88 test({b'x.prefix': b'example.org', b'x.schemes': b'http'})
    98 test({b'x.prefix': b'example.org', b'x.schemes': b'http'})
    89 test({b'x.prefix': b'example.org', b'x.schemes': b'https'})
    99 test({b'x.prefix': b'example.org', b'x.schemes': b'https'})
    90 test({b'x.prefix': b'example.org', b'x.schemes': b'http https'})
   100 test({b'x.prefix': b'example.org', b'x.schemes': b'http https'})
    91 
   101 
    92 print('\n*** Test prefix matching\n')
   102 print('\n*** Test prefix matching\n')
    93 test({b'x.prefix': b'http://example.org/foo',
   103 test(
    94       b'y.prefix': b'http://example.org/bar'})
   104     {
    95 test({b'x.prefix': b'http://example.org/foo',
   105         b'x.prefix': b'http://example.org/foo',
    96       b'y.prefix': b'http://example.org/foo/bar'})
   106         b'y.prefix': b'http://example.org/bar',
       
   107     }
       
   108 )
       
   109 test(
       
   110     {
       
   111         b'x.prefix': b'http://example.org/foo',
       
   112         b'y.prefix': b'http://example.org/foo/bar',
       
   113     }
       
   114 )
    97 test({b'x.prefix': b'*', b'y.prefix': b'https://example.org/bar'})
   115 test({b'x.prefix': b'*', b'y.prefix': b'https://example.org/bar'})
    98 
   116 
    99 print('\n*** Test user matching\n')
   117 print('\n*** Test user matching\n')
   100 test({b'x.prefix': b'http://example.org/foo',
   118 test(
   101       b'x.username': None,
   119     {
   102       b'x.password': b'xpassword'},
   120         b'x.prefix': b'http://example.org/foo',
   103      urls=[b'http://y@example.org/foo'])
   121         b'x.username': None,
   104 test({b'x.prefix': b'http://example.org/foo',
   122         b'x.password': b'xpassword',
   105       b'x.username': None,
   123     },
   106       b'x.password': b'xpassword',
   124     urls=[b'http://y@example.org/foo'],
   107       b'y.prefix': b'http://example.org/foo',
   125 )
   108       b'y.username': b'y',
   126 test(
   109       b'y.password': b'ypassword'},
   127     {
   110      urls=[b'http://y@example.org/foo'])
   128         b'x.prefix': b'http://example.org/foo',
   111 test({b'x.prefix': b'http://example.org/foo/bar',
   129         b'x.username': None,
   112       b'x.username': None,
   130         b'x.password': b'xpassword',
   113       b'x.password': b'xpassword',
   131         b'y.prefix': b'http://example.org/foo',
   114       b'y.prefix': b'http://example.org/foo',
   132         b'y.username': b'y',
   115       b'y.username': b'y',
   133         b'y.password': b'ypassword',
   116       b'y.password': b'ypassword'},
   134     },
   117      urls=[b'http://y@example.org/foo/bar'])
   135     urls=[b'http://y@example.org/foo'],
       
   136 )
       
   137 test(
       
   138     {
       
   139         b'x.prefix': b'http://example.org/foo/bar',
       
   140         b'x.username': None,
       
   141         b'x.password': b'xpassword',
       
   142         b'y.prefix': b'http://example.org/foo',
       
   143         b'y.username': b'y',
       
   144         b'y.password': b'ypassword',
       
   145     },
       
   146     urls=[b'http://y@example.org/foo/bar'],
       
   147 )
   118 
   148 
   119 print('\n*** Test user matching with name in prefix\n')
   149 print('\n*** Test user matching with name in prefix\n')
   120 
   150 
   121 # prefix, username and URL have the same user
   151 # prefix, username and URL have the same user
   122 test({b'x.prefix': b'https://example.org/foo',
   152 test(
   123       b'x.username': None,
   153     {
   124       b'x.password': b'xpassword',
   154         b'x.prefix': b'https://example.org/foo',
   125       b'y.prefix': b'http://y@example.org/foo',
   155         b'x.username': None,
   126       b'y.username': b'y',
   156         b'x.password': b'xpassword',
   127       b'y.password': b'ypassword'},
   157         b'y.prefix': b'http://y@example.org/foo',
   128      urls=[b'http://y@example.org/foo'])
   158         b'y.username': b'y',
       
   159         b'y.password': b'ypassword',
       
   160     },
       
   161     urls=[b'http://y@example.org/foo'],
       
   162 )
   129 # Prefix has a different user from username and URL
   163 # Prefix has a different user from username and URL
   130 test({b'y.prefix': b'http://z@example.org/foo',
   164 test(
   131       b'y.username': b'y',
   165     {
   132       b'y.password': b'ypassword'},
   166         b'y.prefix': b'http://z@example.org/foo',
   133      urls=[b'http://y@example.org/foo'])
   167         b'y.username': b'y',
       
   168         b'y.password': b'ypassword',
       
   169     },
       
   170     urls=[b'http://y@example.org/foo'],
       
   171 )
   134 # Prefix has a different user from URL; no username
   172 # Prefix has a different user from URL; no username
   135 test({b'y.prefix': b'http://z@example.org/foo',
   173 test(
   136       b'y.password': b'ypassword'},
   174     {b'y.prefix': b'http://z@example.org/foo', b'y.password': b'ypassword'},
   137      urls=[b'http://y@example.org/foo'])
   175     urls=[b'http://y@example.org/foo'],
       
   176 )
   138 # Prefix and URL have same user, but doesn't match username
   177 # Prefix and URL have same user, but doesn't match username
   139 test({b'y.prefix': b'http://y@example.org/foo',
   178 test(
   140       b'y.username': b'z',
   179     {
   141       b'y.password': b'ypassword'},
   180         b'y.prefix': b'http://y@example.org/foo',
   142      urls=[b'http://y@example.org/foo'])
   181         b'y.username': b'z',
       
   182         b'y.password': b'ypassword',
       
   183     },
       
   184     urls=[b'http://y@example.org/foo'],
       
   185 )
   143 # Prefix and URL have the same user; no username
   186 # Prefix and URL have the same user; no username
   144 test({b'y.prefix': b'http://y@example.org/foo',
   187 test(
   145       b'y.password': b'ypassword'},
   188     {b'y.prefix': b'http://y@example.org/foo', b'y.password': b'ypassword'},
   146      urls=[b'http://y@example.org/foo'])
   189     urls=[b'http://y@example.org/foo'],
       
   190 )
   147 # Prefix user, but no URL user or username
   191 # Prefix user, but no URL user or username
   148 test({b'y.prefix': b'http://y@example.org/foo',
   192 test(
   149       b'y.password': b'ypassword'},
   193     {b'y.prefix': b'http://y@example.org/foo', b'y.password': b'ypassword'},
   150      urls=[b'http://example.org/foo'])
   194     urls=[b'http://example.org/foo'],
       
   195 )
       
   196 
   151 
   197 
   152 def testauthinfo(fullurl, authurl):
   198 def testauthinfo(fullurl, authurl):
   153     print('URIs:', fullurl, authurl)
   199     print('URIs:', fullurl, authurl)
   154     pm = urlreq.httppasswordmgrwithdefaultrealm()
   200     pm = urlreq.httppasswordmgrwithdefaultrealm()
   155     ai = _stringifyauthinfo(util.url(pycompat.bytesurl(fullurl)).authinfo()[1])
   201     ai = _stringifyauthinfo(util.url(pycompat.bytesurl(fullurl)).authinfo()[1])
   156     pm.add_password(*ai)
   202     pm.add_password(*ai)
   157     print(pm.find_user_password('test', authurl))
   203     print(pm.find_user_password('test', authurl))
   158 
   204 
       
   205 
   159 print('\n*** Test urllib2 and util.url\n')
   206 print('\n*** Test urllib2 and util.url\n')
   160 testauthinfo('http://user@example.com:8080/foo', 'http://example.com:8080/foo')
   207 testauthinfo('http://user@example.com:8080/foo', 'http://example.com:8080/foo')