comparison tests/test-hgweb-auth.py @ 41471:30dd20a56f3e

tests: port test-hgweb-auth.py to Python 3 Differential Revision: https://phab.mercurial-scm.org/D5736
author Augie Fackler <augie@google.com>
date Tue, 29 Jan 2019 13:26:18 -0500
parents c53f0ead5781
children 2372284d9457
comparison
equal deleted inserted replaced
41470:d437d1e2a711 41471:30dd20a56f3e
22 origui = myui.load() 22 origui = myui.load()
23 23
24 def writeauth(items): 24 def writeauth(items):
25 ui = origui.copy() 25 ui = origui.copy()
26 for name, value in items.items(): 26 for name, value in items.items():
27 ui.setconfig('auth', name, value) 27 ui.setconfig(b'auth', name, value)
28 return ui 28 return ui
29
30 def _stringifyauthinfo(ai):
31 if ai is None:
32 return ai
33 realm, authuris, user, passwd = ai
34 return (pycompat.strurl(realm),
35 [pycompat.strurl(u) for u in authuris],
36 pycompat.strurl(user),
37 pycompat.strurl(passwd),
38 )
29 39
30 def test(auth, urls=None): 40 def test(auth, urls=None):
31 print('CFG:', pycompat.sysstr(stringutil.pprint(auth, bprefix=True))) 41 print('CFG:', pycompat.sysstr(stringutil.pprint(auth, bprefix=True)))
32 prefixes = set() 42 prefixes = set()
33 for k in auth: 43 for k in auth:
34 prefixes.add(k.split('.', 1)[0]) 44 prefixes.add(k.split(b'.', 1)[0])
35 for p in prefixes: 45 for p in prefixes:
36 for name in ('.username', '.password'): 46 for name in (b'.username', b'.password'):
37 if (p + name) not in auth: 47 if (p + name) not in auth:
38 auth[p + name] = p 48 auth[p + name] = p
39 auth = dict((k, v) for k, v in auth.items() if v is not None) 49 auth = dict((k, v) for k, v in auth.items() if v is not None)
40 50
41 ui = writeauth(auth) 51 ui = writeauth(auth)
42 52
43 def _test(uri): 53 def _test(uri):
44 print('URI:', uri) 54 print('URI:', pycompat.strurl(uri))
45 try: 55 try:
46 pm = url.passwordmgr(ui, urlreq.httppasswordmgrwithdefaultrealm()) 56 pm = url.passwordmgr(ui, urlreq.httppasswordmgrwithdefaultrealm())
47 u, authinfo = util.url(uri).authinfo() 57 u, authinfo = util.url(uri).authinfo()
48 if authinfo is not None: 58 if authinfo is not None:
49 pm.add_password(*authinfo) 59 pm.add_password(*_stringifyauthinfo(authinfo))
50 print(' ', pm.find_user_password('test', u)) 60 print(' ', tuple(pycompat.strurl(a) for a in
61 pm.find_user_password('test',
62 pycompat.strurl(u))))
51 except error.Abort: 63 except error.Abort:
52 print(' ','abort') 64 print(' ','abort')
53 65
54 if not urls: 66 if not urls:
55 urls = [ 67 urls = [
56 'http://example.org/foo', 68 b'http://example.org/foo',
57 'http://example.org/foo/bar', 69 b'http://example.org/foo/bar',
58 'http://example.org/bar', 70 b'http://example.org/bar',
59 'https://example.org/foo', 71 b'https://example.org/foo',
60 'https://example.org/foo/bar', 72 b'https://example.org/foo/bar',
61 'https://example.org/bar', 73 b'https://example.org/bar',
62 'https://x@example.org/bar', 74 b'https://x@example.org/bar',
63 'https://y@example.org/bar', 75 b'https://y@example.org/bar',
64 ] 76 ]
65 for u in urls: 77 for u in urls:
66 _test(u) 78 _test(u)
67 79
68 80
69 print('\n*** Test in-uri schemes\n') 81 print('\n*** Test in-uri schemes\n')
70 test({'x.prefix': 'http://example.org'}) 82 test({b'x.prefix': b'http://example.org'})
71 test({'x.prefix': 'https://example.org'}) 83 test({b'x.prefix': b'https://example.org'})
72 test({'x.prefix': 'http://example.org', 'x.schemes': 'https'}) 84 test({b'x.prefix': b'http://example.org', b'x.schemes': b'https'})
73 test({'x.prefix': 'https://example.org', 'x.schemes': 'http'}) 85 test({b'x.prefix': b'https://example.org', b'x.schemes': b'http'})
74 86
75 print('\n*** Test separately configured schemes\n') 87 print('\n*** Test separately configured schemes\n')
76 test({'x.prefix': 'example.org', 'x.schemes': 'http'}) 88 test({b'x.prefix': b'example.org', b'x.schemes': b'http'})
77 test({'x.prefix': 'example.org', 'x.schemes': 'https'}) 89 test({b'x.prefix': b'example.org', b'x.schemes': b'https'})
78 test({'x.prefix': 'example.org', 'x.schemes': 'http https'}) 90 test({b'x.prefix': b'example.org', b'x.schemes': b'http https'})
79 91
80 print('\n*** Test prefix matching\n') 92 print('\n*** Test prefix matching\n')
81 test({'x.prefix': 'http://example.org/foo', 93 test({b'x.prefix': b'http://example.org/foo',
82 'y.prefix': 'http://example.org/bar'}) 94 b'y.prefix': b'http://example.org/bar'})
83 test({'x.prefix': 'http://example.org/foo', 95 test({b'x.prefix': b'http://example.org/foo',
84 'y.prefix': 'http://example.org/foo/bar'}) 96 b'y.prefix': b'http://example.org/foo/bar'})
85 test({'x.prefix': '*', 'y.prefix': 'https://example.org/bar'}) 97 test({b'x.prefix': b'*', b'y.prefix': b'https://example.org/bar'})
86 98
87 print('\n*** Test user matching\n') 99 print('\n*** Test user matching\n')
88 test({'x.prefix': 'http://example.org/foo', 100 test({b'x.prefix': b'http://example.org/foo',
89 'x.username': None, 101 b'x.username': None,
90 'x.password': 'xpassword'}, 102 b'x.password': b'xpassword'},
91 urls=['http://y@example.org/foo']) 103 urls=[b'http://y@example.org/foo'])
92 test({'x.prefix': 'http://example.org/foo', 104 test({b'x.prefix': b'http://example.org/foo',
93 'x.username': None, 105 b'x.username': None,
94 'x.password': 'xpassword', 106 b'x.password': b'xpassword',
95 'y.prefix': 'http://example.org/foo', 107 b'y.prefix': b'http://example.org/foo',
96 'y.username': 'y', 108 b'y.username': b'y',
97 'y.password': 'ypassword'}, 109 b'y.password': b'ypassword'},
98 urls=['http://y@example.org/foo']) 110 urls=[b'http://y@example.org/foo'])
99 test({'x.prefix': 'http://example.org/foo/bar', 111 test({b'x.prefix': b'http://example.org/foo/bar',
100 'x.username': None, 112 b'x.username': None,
101 'x.password': 'xpassword', 113 b'x.password': b'xpassword',
102 'y.prefix': 'http://example.org/foo', 114 b'y.prefix': b'http://example.org/foo',
103 'y.username': 'y', 115 b'y.username': b'y',
104 'y.password': 'ypassword'}, 116 b'y.password': b'ypassword'},
105 urls=['http://y@example.org/foo/bar']) 117 urls=[b'http://y@example.org/foo/bar'])
106 118
107 print('\n*** Test user matching with name in prefix\n') 119 print('\n*** Test user matching with name in prefix\n')
108 120
109 # prefix, username and URL have the same user 121 # prefix, username and URL have the same user
110 test({'x.prefix': 'https://example.org/foo', 122 test({b'x.prefix': b'https://example.org/foo',
111 'x.username': None, 123 b'x.username': None,
112 'x.password': 'xpassword', 124 b'x.password': b'xpassword',
113 'y.prefix': 'http://y@example.org/foo', 125 b'y.prefix': b'http://y@example.org/foo',
114 'y.username': 'y', 126 b'y.username': b'y',
115 'y.password': 'ypassword'}, 127 b'y.password': b'ypassword'},
116 urls=['http://y@example.org/foo']) 128 urls=[b'http://y@example.org/foo'])
117 # Prefix has a different user from username and URL 129 # Prefix has a different user from username and URL
118 test({'y.prefix': 'http://z@example.org/foo', 130 test({b'y.prefix': b'http://z@example.org/foo',
119 'y.username': 'y', 131 b'y.username': b'y',
120 'y.password': 'ypassword'}, 132 b'y.password': b'ypassword'},
121 urls=['http://y@example.org/foo']) 133 urls=[b'http://y@example.org/foo'])
122 # Prefix has a different user from URL; no username 134 # Prefix has a different user from URL; no username
123 test({'y.prefix': 'http://z@example.org/foo', 135 test({b'y.prefix': b'http://z@example.org/foo',
124 'y.password': 'ypassword'}, 136 b'y.password': b'ypassword'},
125 urls=['http://y@example.org/foo']) 137 urls=[b'http://y@example.org/foo'])
126 # Prefix and URL have same user, but doesn't match username 138 # Prefix and URL have same user, but doesn't match username
127 test({'y.prefix': 'http://y@example.org/foo', 139 test({b'y.prefix': b'http://y@example.org/foo',
128 'y.username': 'z', 140 b'y.username': b'z',
129 'y.password': 'ypassword'}, 141 b'y.password': b'ypassword'},
130 urls=['http://y@example.org/foo']) 142 urls=[b'http://y@example.org/foo'])
131 # Prefix and URL have the same user; no username 143 # Prefix and URL have the same user; no username
132 test({'y.prefix': 'http://y@example.org/foo', 144 test({b'y.prefix': b'http://y@example.org/foo',
133 'y.password': 'ypassword'}, 145 b'y.password': b'ypassword'},
134 urls=['http://y@example.org/foo']) 146 urls=[b'http://y@example.org/foo'])
135 # Prefix user, but no URL user or username 147 # Prefix user, but no URL user or username
136 test({'y.prefix': 'http://y@example.org/foo', 148 test({b'y.prefix': b'http://y@example.org/foo',
137 'y.password': 'ypassword'}, 149 b'y.password': b'ypassword'},
138 urls=['http://example.org/foo']) 150 urls=[b'http://example.org/foo'])
139 151
140 def testauthinfo(fullurl, authurl): 152 def testauthinfo(fullurl, authurl):
141 print('URIs:', fullurl, authurl) 153 print('URIs:', fullurl, authurl)
142 pm = urlreq.httppasswordmgrwithdefaultrealm() 154 pm = urlreq.httppasswordmgrwithdefaultrealm()
143 pm.add_password(*util.url(fullurl).authinfo()[1]) 155 ai = _stringifyauthinfo(util.url(pycompat.bytesurl(fullurl)).authinfo()[1])
156 pm.add_password(*ai)
144 print(pm.find_user_password('test', authurl)) 157 print(pm.find_user_password('test', authurl))
145 158
146 print('\n*** Test urllib2 and util.url\n') 159 print('\n*** Test urllib2 and util.url\n')
147 testauthinfo('http://user@example.com:8080/foo', 'http://example.com:8080/foo') 160 testauthinfo('http://user@example.com:8080/foo', 'http://example.com:8080/foo')