tests/test-hgweb-auth.py
author Yuya Nishihara <yuya@tcha.org>
Tue, 05 Apr 2016 23:21:17 +0900
changeset 28808 10c2ce44c35d
parent 28807 736f64b23a24
child 28883 032c4c2f802a
permissions -rw-r--r--
test-hgweb-auth: stop direct symbol import of mercurial.error.Abort
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28748
c2ba5a810fa1 py3: use print_function in test-hgweb-auth.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28747
diff changeset
     1
from __future__ import absolute_import, print_function
28747
779addce6910 py3: use absolute_import in test-hgweb-auth.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 19378
diff changeset
     2
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
     3
from mercurial import demandimport; demandimport.enable()
15024
0f1311e829c9 http: strip credentials from urllib2 manager URIs (issue2885)
Patrick Mezard <pmezard@gmail.com>
parents: 15005
diff changeset
     4
import urllib2
28747
779addce6910 py3: use absolute_import in test-hgweb-auth.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 19378
diff changeset
     5
from mercurial import (
28808
10c2ce44c35d test-hgweb-auth: stop direct symbol import of mercurial.error.Abort
Yuya Nishihara <yuya@tcha.org>
parents: 28807
diff changeset
     6
    error,
28807
736f64b23a24 test-hgweb-auth: alias ui as uimod
Yuya Nishihara <yuya@tcha.org>
parents: 28748
diff changeset
     7
    ui as uimod,
28747
779addce6910 py3: use absolute_import in test-hgweb-auth.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 19378
diff changeset
     8
    url,
779addce6910 py3: use absolute_import in test-hgweb-auth.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 19378
diff changeset
     9
    util,
779addce6910 py3: use absolute_import in test-hgweb-auth.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 19378
diff changeset
    10
)
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    11
28807
736f64b23a24 test-hgweb-auth: alias ui as uimod
Yuya Nishihara <yuya@tcha.org>
parents: 28748
diff changeset
    12
class myui(uimod.ui):
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    13
    def interactive(self):
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    14
        return False
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    15
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    16
origui = myui()
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    17
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    18
def writeauth(items):
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    19
    ui = origui.copy()
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    20
    for name, value in items.iteritems():
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    21
        ui.setconfig('auth', name, value)
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    22
    return ui
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    23
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    24
def dumpdict(dict):
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 8333
diff changeset
    25
    return '{' + ', '.join(['%s: %s' % (k, dict[k])
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 8333
diff changeset
    26
                            for k in sorted(dict.iterkeys())]) + '}'
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    27
15005
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    28
def test(auth, urls=None):
28748
c2ba5a810fa1 py3: use print_function in test-hgweb-auth.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28747
diff changeset
    29
    print('CFG:', dumpdict(auth))
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    30
    prefixes = set()
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    31
    for k in auth:
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    32
        prefixes.add(k.split('.', 1)[0])
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    33
    for p in prefixes:
15005
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    34
        for name in ('.username', '.password'):
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    35
            if (p + name) not in auth:
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    36
                auth[p + name] = p
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    37
    auth = dict((k, v) for k, v in auth.iteritems() if v is not None)
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    38
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    39
    ui = writeauth(auth)
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    40
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    41
    def _test(uri):
28748
c2ba5a810fa1 py3: use print_function in test-hgweb-auth.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28747
diff changeset
    42
        print('URI:', uri)
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    43
        try:
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    44
            pm = url.passwordmgr(ui)
15025
0593e8f81c71 http: pass user to readauthforuri() (fix 4a43e23b8c55)
Patrick Mezard <pmezard@gmail.com>
parents: 15024
diff changeset
    45
            u, authinfo = util.url(uri).authinfo()
15005
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    46
            if authinfo is not None:
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    47
                pm.add_password(*authinfo)
28748
c2ba5a810fa1 py3: use print_function in test-hgweb-auth.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28747
diff changeset
    48
            print('    ', pm.find_user_password('test', u))
28808
10c2ce44c35d test-hgweb-auth: stop direct symbol import of mercurial.error.Abort
Yuya Nishihara <yuya@tcha.org>
parents: 28807
diff changeset
    49
        except error.Abort:
28748
c2ba5a810fa1 py3: use print_function in test-hgweb-auth.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28747
diff changeset
    50
            print('    ','abort')
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    51
15005
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    52
    if not urls:
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    53
        urls = [
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    54
            'http://example.org/foo',
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    55
            'http://example.org/foo/bar',
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    56
            'http://example.org/bar',
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    57
            'https://example.org/foo',
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    58
            'https://example.org/foo/bar',
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    59
            'https://example.org/bar',
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    60
            'https://x@example.org/bar',
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    61
            'https://y@example.org/bar',
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    62
            ]
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    63
    for u in urls:
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    64
        _test(u)
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    65
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    66
28748
c2ba5a810fa1 py3: use print_function in test-hgweb-auth.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28747
diff changeset
    67
print('\n*** Test in-uri schemes\n')
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    68
test({'x.prefix': 'http://example.org'})
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    69
test({'x.prefix': 'https://example.org'})
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    70
test({'x.prefix': 'http://example.org', 'x.schemes': 'https'})
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    71
test({'x.prefix': 'https://example.org', 'x.schemes': 'http'})
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    72
28748
c2ba5a810fa1 py3: use print_function in test-hgweb-auth.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28747
diff changeset
    73
print('\n*** Test separately configured schemes\n')
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    74
test({'x.prefix': 'example.org', 'x.schemes': 'http'})
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    75
test({'x.prefix': 'example.org', 'x.schemes': 'https'})
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    76
test({'x.prefix': 'example.org', 'x.schemes': 'http https'})
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    77
28748
c2ba5a810fa1 py3: use print_function in test-hgweb-auth.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28747
diff changeset
    78
print('\n*** Test prefix matching\n')
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 8333
diff changeset
    79
test({'x.prefix': 'http://example.org/foo',
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 8333
diff changeset
    80
      'y.prefix': 'http://example.org/bar'})
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 8333
diff changeset
    81
test({'x.prefix': 'http://example.org/foo',
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 8333
diff changeset
    82
      'y.prefix': 'http://example.org/foo/bar'})
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    83
test({'x.prefix': '*', 'y.prefix': 'https://example.org/bar'})
15005
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    84
28748
c2ba5a810fa1 py3: use print_function in test-hgweb-auth.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28747
diff changeset
    85
print('\n*** Test user matching\n')
15005
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    86
test({'x.prefix': 'http://example.org/foo',
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    87
      'x.username': None,
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    88
      'x.password': 'xpassword'},
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    89
     urls=['http://y@example.org/foo'])
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    90
test({'x.prefix': 'http://example.org/foo',
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    91
      'x.username': None,
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    92
      'x.password': 'xpassword',
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    93
      'y.prefix': 'http://example.org/foo',
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    94
      'y.username': 'y',
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    95
      'y.password': 'ypassword'},
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    96
     urls=['http://y@example.org/foo'])
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    97
test({'x.prefix': 'http://example.org/foo/bar',
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    98
      'x.username': None,
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
    99
      'x.password': 'xpassword',
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
   100
      'y.prefix': 'http://example.org/foo',
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
   101
      'y.username': 'y',
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
   102
      'y.password': 'ypassword'},
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
   103
     urls=['http://y@example.org/foo/bar'])
15024
0f1311e829c9 http: strip credentials from urllib2 manager URIs (issue2885)
Patrick Mezard <pmezard@gmail.com>
parents: 15005
diff changeset
   104
0f1311e829c9 http: strip credentials from urllib2 manager URIs (issue2885)
Patrick Mezard <pmezard@gmail.com>
parents: 15005
diff changeset
   105
def testauthinfo(fullurl, authurl):
28748
c2ba5a810fa1 py3: use print_function in test-hgweb-auth.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28747
diff changeset
   106
    print('URIs:', fullurl, authurl)
15024
0f1311e829c9 http: strip credentials from urllib2 manager URIs (issue2885)
Patrick Mezard <pmezard@gmail.com>
parents: 15005
diff changeset
   107
    pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
0f1311e829c9 http: strip credentials from urllib2 manager URIs (issue2885)
Patrick Mezard <pmezard@gmail.com>
parents: 15005
diff changeset
   108
    pm.add_password(*util.url(fullurl).authinfo()[1])
28748
c2ba5a810fa1 py3: use print_function in test-hgweb-auth.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28747
diff changeset
   109
    print(pm.find_user_password('test', authurl))
15024
0f1311e829c9 http: strip credentials from urllib2 manager URIs (issue2885)
Patrick Mezard <pmezard@gmail.com>
parents: 15005
diff changeset
   110
28748
c2ba5a810fa1 py3: use print_function in test-hgweb-auth.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28747
diff changeset
   111
print('\n*** Test urllib2 and util.url\n')
15024
0f1311e829c9 http: strip credentials from urllib2 manager URIs (issue2885)
Patrick Mezard <pmezard@gmail.com>
parents: 15005
diff changeset
   112
testauthinfo('http://user@example.com:8080/foo', 'http://example.com:8080/foo')