Debian Codegraph @

tests/test-hgweb-auth.py

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