##// END OF EJS Templates
test-hgweb-auth: stop direct symbol import of mercurial.error.Abort
Yuya Nishihara -
r28808:10c2ce44 default
parent child Browse files
Show More
@@ -1,114 +1,112 b''
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; demandimport.enable()
4 import urllib2
4 import urllib2
5 from mercurial import (
5 from mercurial import (
6 error,
6 ui as uimod,
7 ui as uimod,
7 url,
8 url,
8 util,
9 util,
9 )
10 )
10 from mercurial.error import (
11 Abort,
12 )
13
11
14 class myui(uimod.ui):
12 class myui(uimod.ui):
15 def interactive(self):
13 def interactive(self):
16 return False
14 return False
17
15
18 origui = myui()
16 origui = myui()
19
17
20 def writeauth(items):
18 def writeauth(items):
21 ui = origui.copy()
19 ui = origui.copy()
22 for name, value in items.iteritems():
20 for name, value in items.iteritems():
23 ui.setconfig('auth', name, value)
21 ui.setconfig('auth', name, value)
24 return ui
22 return ui
25
23
26 def dumpdict(dict):
24 def dumpdict(dict):
27 return '{' + ', '.join(['%s: %s' % (k, dict[k])
25 return '{' + ', '.join(['%s: %s' % (k, dict[k])
28 for k in sorted(dict.iterkeys())]) + '}'
26 for k in sorted(dict.iterkeys())]) + '}'
29
27
30 def test(auth, urls=None):
28 def test(auth, urls=None):
31 print('CFG:', dumpdict(auth))
29 print('CFG:', dumpdict(auth))
32 prefixes = set()
30 prefixes = set()
33 for k in auth:
31 for k in auth:
34 prefixes.add(k.split('.', 1)[0])
32 prefixes.add(k.split('.', 1)[0])
35 for p in prefixes:
33 for p in prefixes:
36 for name in ('.username', '.password'):
34 for name in ('.username', '.password'):
37 if (p + name) not in auth:
35 if (p + name) not in auth:
38 auth[p + name] = p
36 auth[p + name] = p
39 auth = dict((k, v) for k, v in auth.iteritems() if v is not None)
37 auth = dict((k, v) for k, v in auth.iteritems() if v is not None)
40
38
41 ui = writeauth(auth)
39 ui = writeauth(auth)
42
40
43 def _test(uri):
41 def _test(uri):
44 print('URI:', uri)
42 print('URI:', uri)
45 try:
43 try:
46 pm = url.passwordmgr(ui)
44 pm = url.passwordmgr(ui)
47 u, authinfo = util.url(uri).authinfo()
45 u, authinfo = util.url(uri).authinfo()
48 if authinfo is not None:
46 if authinfo is not None:
49 pm.add_password(*authinfo)
47 pm.add_password(*authinfo)
50 print(' ', pm.find_user_password('test', u))
48 print(' ', pm.find_user_password('test', u))
51 except Abort:
49 except error.Abort:
52 print(' ','abort')
50 print(' ','abort')
53
51
54 if not urls:
52 if not urls:
55 urls = [
53 urls = [
56 'http://example.org/foo',
54 'http://example.org/foo',
57 'http://example.org/foo/bar',
55 'http://example.org/foo/bar',
58 'http://example.org/bar',
56 'http://example.org/bar',
59 'https://example.org/foo',
57 'https://example.org/foo',
60 'https://example.org/foo/bar',
58 'https://example.org/foo/bar',
61 'https://example.org/bar',
59 'https://example.org/bar',
62 'https://x@example.org/bar',
60 'https://x@example.org/bar',
63 'https://y@example.org/bar',
61 'https://y@example.org/bar',
64 ]
62 ]
65 for u in urls:
63 for u in urls:
66 _test(u)
64 _test(u)
67
65
68
66
69 print('\n*** Test in-uri schemes\n')
67 print('\n*** Test in-uri schemes\n')
70 test({'x.prefix': 'http://example.org'})
68 test({'x.prefix': 'http://example.org'})
71 test({'x.prefix': 'https://example.org'})
69 test({'x.prefix': 'https://example.org'})
72 test({'x.prefix': 'http://example.org', 'x.schemes': 'https'})
70 test({'x.prefix': 'http://example.org', 'x.schemes': 'https'})
73 test({'x.prefix': 'https://example.org', 'x.schemes': 'http'})
71 test({'x.prefix': 'https://example.org', 'x.schemes': 'http'})
74
72
75 print('\n*** Test separately configured schemes\n')
73 print('\n*** Test separately configured schemes\n')
76 test({'x.prefix': 'example.org', 'x.schemes': 'http'})
74 test({'x.prefix': 'example.org', 'x.schemes': 'http'})
77 test({'x.prefix': 'example.org', 'x.schemes': 'https'})
75 test({'x.prefix': 'example.org', 'x.schemes': 'https'})
78 test({'x.prefix': 'example.org', 'x.schemes': 'http https'})
76 test({'x.prefix': 'example.org', 'x.schemes': 'http https'})
79
77
80 print('\n*** Test prefix matching\n')
78 print('\n*** Test prefix matching\n')
81 test({'x.prefix': 'http://example.org/foo',
79 test({'x.prefix': 'http://example.org/foo',
82 'y.prefix': 'http://example.org/bar'})
80 'y.prefix': 'http://example.org/bar'})
83 test({'x.prefix': 'http://example.org/foo',
81 test({'x.prefix': 'http://example.org/foo',
84 'y.prefix': 'http://example.org/foo/bar'})
82 'y.prefix': 'http://example.org/foo/bar'})
85 test({'x.prefix': '*', 'y.prefix': 'https://example.org/bar'})
83 test({'x.prefix': '*', 'y.prefix': 'https://example.org/bar'})
86
84
87 print('\n*** Test user matching\n')
85 print('\n*** Test user matching\n')
88 test({'x.prefix': 'http://example.org/foo',
86 test({'x.prefix': 'http://example.org/foo',
89 'x.username': None,
87 'x.username': None,
90 'x.password': 'xpassword'},
88 'x.password': 'xpassword'},
91 urls=['http://y@example.org/foo'])
89 urls=['http://y@example.org/foo'])
92 test({'x.prefix': 'http://example.org/foo',
90 test({'x.prefix': 'http://example.org/foo',
93 'x.username': None,
91 'x.username': None,
94 'x.password': 'xpassword',
92 'x.password': 'xpassword',
95 'y.prefix': 'http://example.org/foo',
93 'y.prefix': 'http://example.org/foo',
96 'y.username': 'y',
94 'y.username': 'y',
97 'y.password': 'ypassword'},
95 'y.password': 'ypassword'},
98 urls=['http://y@example.org/foo'])
96 urls=['http://y@example.org/foo'])
99 test({'x.prefix': 'http://example.org/foo/bar',
97 test({'x.prefix': 'http://example.org/foo/bar',
100 'x.username': None,
98 'x.username': None,
101 'x.password': 'xpassword',
99 'x.password': 'xpassword',
102 'y.prefix': 'http://example.org/foo',
100 'y.prefix': 'http://example.org/foo',
103 'y.username': 'y',
101 'y.username': 'y',
104 'y.password': 'ypassword'},
102 'y.password': 'ypassword'},
105 urls=['http://y@example.org/foo/bar'])
103 urls=['http://y@example.org/foo/bar'])
106
104
107 def testauthinfo(fullurl, authurl):
105 def testauthinfo(fullurl, authurl):
108 print('URIs:', fullurl, authurl)
106 print('URIs:', fullurl, authurl)
109 pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
107 pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
110 pm.add_password(*util.url(fullurl).authinfo()[1])
108 pm.add_password(*util.url(fullurl).authinfo()[1])
111 print(pm.find_user_password('test', authurl))
109 print(pm.find_user_password('test', authurl))
112
110
113 print('\n*** Test urllib2 and util.url\n')
111 print('\n*** Test urllib2 and util.url\n')
114 testauthinfo('http://user@example.com:8080/foo', 'http://example.com:8080/foo')
112 testauthinfo('http://user@example.com:8080/foo', 'http://example.com:8080/foo')
General Comments 0
You need to be logged in to leave comments. Login now