Coverage for tests/test_dbAuth.py : 12%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of daf_butler
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22import os
23import unittest
24import lsst.utils.tests
26from lsst.daf.butler.registry import DbAuth, DbAuthError
28TESTDIR = os.path.join(
29 os.path.abspath(os.path.dirname(__file__)), "config", "dbAuth")
32class DbAuthTestCase(unittest.TestCase):
33 """Test DbAuth class."""
35 def test_patterns(self):
36 """Test various patterns against a fixed connection URL."""
37 for matchPattern in [
38 "postgresql://*",
39 "postgresql://*.example.com",
40 "postgresql://*.example.com/my_*",
41 "postgresql://host.example.com/my_database",
42 "postgresql://host.example.com:5432/my_database",
43 "postgresql://user@host.example.com/my_database"]:
44 auth = DbAuth(authList=[
45 dict(url=matchPattern, username="foo",
46 password="bar")])
47 user, pwd = auth.getAuth(
48 "postgresql", "user", "host.example.com", "5432",
49 "my_database")
50 self.assertEqual(user, "user")
51 self.assertEqual(pwd, "bar")
53 def test_connStrings(self):
54 """Test various connection URLs against a fixed pattern."""
55 auth = DbAuth(authList=[
56 dict(url="postgresql://host.example.com/my_database",
57 username="foo", password="bar")])
58 for connComponents in [
59 ("postgresql", "user", "host.example.com", 5432,
60 "my_database"),
61 ("postgresql", "user", "host.example.com", None,
62 "my_database")]:
63 user, pwd = auth.getAuth(*connComponents)
64 self.assertEqual(user, "user")
65 self.assertEqual(pwd, "bar")
66 for connComponents in [
67 ("postgresql", None, "host.example.com", None,
68 "my_database"),
69 ("postgresql", None, "host.example.com", "5432",
70 "my_database")]:
71 user, pwd = auth.getAuth(*connComponents)
72 self.assertEqual(user, "foo")
73 self.assertEqual(pwd, "bar")
75 def test_load(self):
76 """Test loading from a YAML file and matching in order."""
77 filePath = os.path.join(TESTDIR, "db-auth.yaml")
78 os.chmod(filePath, 0o600)
79 auth = DbAuth(filePath)
80 self.assertEqual(
81 auth.getAuth("postgresql", "user", "host.example.com",
82 "5432", "my_database"),
83 ("user", "test1"))
84 self.assertEqual(
85 auth.getAuth("postgresql", "user", "host.example.com",
86 "3360", "my_database"),
87 ("user", "test2"))
88 self.assertEqual(
89 auth.getAuth("postgresql", "", "host.example.com",
90 "5432", "my_database"),
91 ("user", "test3"))
92 self.assertEqual(
93 auth.getAuth("postgresql", None, "host.example.com",
94 "", "my_database"),
95 ("user", "test4"))
96 self.assertEqual(
97 auth.getAuth("postgresql", "", "host2.example.com",
98 None, "my_other"),
99 ("user", "test5"))
100 self.assertEqual(
101 auth.getAuth("postgresql", None, "host3.example.com",
102 3360, "some_database"),
103 ("user", "test6"))
104 self.assertEqual(
105 auth.getAuth("postgresql", "user", "host4.other.com",
106 None, "other_database"),
107 ("user", "test8"))
109 def test_ipv6(self):
110 """Test IPv6 addresses as host names."""
111 auth = DbAuth(authList=[
112 dict(url="postgresql://user@[fe80::1ff:fe23:4567:890a]:5432/db",
113 password="pwd"),
114 dict(url="postgresql://[fe80::1ff:fe23:4567:890a]:5432/db",
115 password="pwd2"),
116 dict(url="postgresql://user3@[fe80::1ff:fe23:4567:890a]/db",
117 password="pwd3"),
118 dict(url="postgresql://[fe80::1ff:fe23:4567:890a]/db",
119 password="pwd4")])
120 self.assertEqual(
121 auth.getAuth("postgresql", "user",
122 "fe80::1ff:fe23:4567:890a", "5432", "db"),
123 ("user", "pwd"))
124 self.assertEqual(
125 auth.getAuth("postgresql", "user2",
126 "fe80::1ff:fe23:4567:890a", "5432", "db"),
127 ("user2", "pwd2"))
128 self.assertEqual(
129 auth.getAuth("postgresql", "user3",
130 "fe80::1ff:fe23:4567:890a", "3360", "db"),
131 ("user3", "pwd3"))
132 self.assertEqual(
133 auth.getAuth("postgresql", "user4",
134 "fe80::1ff:fe23:4567:890a", "3360", "db"),
135 ("user4", "pwd4"))
137 def test_search(self):
138 """Test ordered searching."""
139 auth = DbAuth(authList=[
140 dict(url="postgresql://user1@host2.example.com:3360/database3",
141 password="first"),
142 dict(url="postgresql://host2.example.com:3360/database3",
143 username="second_u", password="second"),
144 dict(url="postgresql://host2.example.com:5432/database3",
145 username="wrong", password="port"),
146 dict(url="postgresql://host3.example.com/",
147 username="third_u", password="third"),
148 dict(url="oracle://oracle.example.com/other_database",
149 username="scott", password="tiger"),
150 dict(url="postgresql://*.example.com/database3",
151 username="fourth_u", password="fourth"),
152 dict(url="postgresql://*.example.com",
153 username="fifth_u", password="fifth")])
154 self.assertEqual(
155 auth.getAuth("postgresql", "user1", "host2.example.com",
156 "3360", "database3"),
157 ("user1", "first"))
158 self.assertEqual(
159 auth.getAuth("postgresql", "user2", "host2.example.com",
160 "3360", "database3"),
161 ("user2", "second"))
162 self.assertEqual(
163 auth.getAuth("postgresql", "", "host2.example.com",
164 "3360", "database3"),
165 ("second_u", "second"))
166 self.assertEqual(
167 auth.getAuth("postgresql", None, "host2.example.com",
168 "3360", "database3"),
169 ("second_u", "second"))
170 self.assertEqual(
171 auth.getAuth("postgresql", None, "host3.example.com",
172 "3360", "database3"),
173 ("third_u", "third"))
174 self.assertEqual(
175 auth.getAuth("postgresql", None, "host3.example.com",
176 "3360", "database4"),
177 ("third_u", "third"))
178 self.assertEqual(
179 auth.getAuth("postgresql", None, "host4.example.com",
180 "3360", "database3"),
181 ("fourth_u", "fourth"))
182 self.assertEqual(
183 auth.getAuth("postgresql", None, "host4.example.com",
184 "3360", "database4"),
185 ("fifth_u", "fifth"))
187 def test_errors(self):
188 """Test for error exceptions."""
189 with self.assertRaisesRegex(
190 DbAuthError,
191 r"^No default path provided to DbAuth configuration file$"):
192 auth = DbAuth()
193 with self.assertRaisesRegex(
194 DbAuthError,
195 r"^No DbAuth configuration file: .*this_does_not_exist$"):
196 auth = DbAuth(os.path.join(TESTDIR, "this_does_not_exist"))
197 with self.assertRaisesRegex(
198 DbAuthError,
199 r"^No DbAuth configuration file: .*this_does_not_exist$"):
200 auth = DbAuth(os.path.join(TESTDIR, "this_does_not_exist"),
201 envVar="LSST_NONEXISTENT")
202 with self.assertRaisesRegex(
203 DbAuthError,
204 r"^DbAuth configuration file .*badDbAuth1.yaml has incorrect "
205 r"permissions: \d*644$"):
206 filePath = os.path.join(TESTDIR, "badDbAuth1.yaml")
207 os.chmod(filePath, 0o644)
208 auth = DbAuth(filePath)
209 with self.assertRaisesRegex(
210 DbAuthError,
211 r"^Unable to load DbAuth configuration file: "
212 r".*badDbAuth2.yaml"):
213 filePath = os.path.join(TESTDIR, "badDbAuth2.yaml")
214 os.chmod(filePath, 0o600)
215 os.environ["LSST_DB_AUTH_TEST"] = filePath
216 auth = DbAuth(envVar="LSST_DB_AUTH_TEST")
218 auth = DbAuth(authList=[])
219 with self.assertRaisesRegex(
220 DbAuthError,
221 r"^Missing dialectname parameter$"):
222 auth.getAuth(None, None, None, None, None)
223 with self.assertRaisesRegex(
224 DbAuthError,
225 r"^Missing dialectname parameter$"):
226 auth.getAuth("", None, None, None, None)
227 with self.assertRaisesRegex(
228 DbAuthError,
229 r"^Missing host parameter$"):
230 auth.getAuth("postgresql", None, None, None, None)
231 with self.assertRaisesRegex(
232 DbAuthError,
233 r"^Missing host parameter$"):
234 auth.getAuth("postgresql", None, "", None, None)
235 with self.assertRaisesRegex(
236 DbAuthError,
237 r"^Missing database parameter$"):
238 auth.getAuth("postgresql", None, "example.com", None, None)
239 with self.assertRaisesRegex(
240 DbAuthError,
241 r"^Missing database parameter$"):
242 auth.getAuth("postgresql", None, "example.com", None, "")
243 with self.assertRaisesRegex(
244 DbAuthError,
245 r"^No matching DbAuth configuration for: "
246 r"\(postgresql, None, example\.com, None, foo\)$"):
247 auth.getAuth("postgresql", None, "example.com", None, "foo")
249 auth = DbAuth(authList=[dict(password="testing")])
250 with self.assertRaisesRegex(
251 DbAuthError,
252 r"^Missing URL in DbAuth configuration$"):
253 auth.getAuth("postgresql", None, "example.com", None, "foo")
255 auth = DbAuth(authList=[dict(url="testing", password="testing")])
256 with self.assertRaisesRegex(
257 DbAuthError,
258 r"^Missing database dialect in URL: testing$"):
259 auth.getAuth("postgresql", None, "example.com", None, "foo")
261 auth = DbAuth(authList=[
262 dict(url="postgresql:///foo", password="testing")])
263 with self.assertRaisesRegex(
264 DbAuthError,
265 r"^Missing host in URL: postgresql:///foo$"):
266 auth.getAuth("postgresql", None, "example.com", None, "foo")
268 def test_getUrl(self):
269 """Repeat relevant tests using getUrl interface."""
270 for matchPattern in [
271 "postgresql://*",
272 "postgresql://*.example.com",
273 "postgresql://*.example.com/my_*",
274 "postgresql://host.example.com/my_database",
275 "postgresql://host.example.com:5432/my_database",
276 "postgresql://user@host.example.com/my_database"]:
277 auth = DbAuth(authList=[
278 dict(url=matchPattern, username="foo",
279 password="bar")])
280 self.assertEqual(
281 auth.getUrl(
282 "postgresql://user@host.example.com:5432/my_database"),
283 "postgresql://user:bar@host.example.com:5432/my_database")
285 auth = DbAuth(authList=[
286 dict(url="postgresql://host.example.com/my_database",
287 username="foo", password="bar")])
288 self.assertEqual(
289 auth.getUrl("postgresql://user@host.example.com:5432/my_database"),
290 "postgresql://user:bar@host.example.com:5432/my_database"),
291 self.assertEqual(
292 auth.getUrl("postgresql://user@host.example.com/my_database"),
293 "postgresql://user:bar@host.example.com/my_database"),
294 self.assertEqual(
295 auth.getUrl("postgresql://host.example.com/my_database"),
296 "postgresql://foo:bar@host.example.com/my_database"),
297 self.assertEqual(
298 auth.getUrl("postgresql://host.example.com:5432/my_database"),
299 "postgresql://foo:bar@host.example.com:5432/my_database"),
301 filePath = os.path.join(TESTDIR, "db-auth.yaml")
302 os.chmod(filePath, 0o600)
303 auth = DbAuth(filePath)
304 self.assertEqual(
305 auth.getUrl("postgresql://user@host.example.com:5432/my_database"),
306 "postgresql://user:test1@host.example.com:5432/my_database")
307 self.assertEqual(
308 auth.getUrl("postgresql://user@host.example.com:3360/my_database"),
309 "postgresql://user:test2@host.example.com:3360/my_database")
310 self.assertEqual(
311 auth.getUrl("postgresql://host.example.com:5432/my_database"),
312 "postgresql://user:test3@host.example.com:5432/my_database")
313 self.assertEqual(
314 auth.getUrl("postgresql://host.example.com/my_database"),
315 "postgresql://user:test4@host.example.com/my_database")
316 self.assertEqual(
317 auth.getUrl("postgresql://host2.example.com/my_other"),
318 "postgresql://user:test5@host2.example.com/my_other")
319 self.assertEqual(
320 auth.getUrl("postgresql://host3.example.com:3360/some_database"),
321 "postgresql://user:test6@host3.example.com:3360/some_database")
322 self.assertEqual(
323 auth.getUrl("postgresql://user@host4.other.com/other_database"),
324 "postgresql://user:test8@host4.other.com/other_database")
326 auth = DbAuth(authList=[
327 dict(url="postgresql://user@[fe80::1ff:fe23:4567:890a]:5432/db",
328 password="pwd"),
329 dict(url="postgresql://[fe80::1ff:fe23:4567:890a]:5432/db",
330 password="pwd2"),
331 dict(url="postgresql://user3@[fe80::1ff:fe23:4567:890a]/db",
332 password="pwd3"),
333 dict(url="postgresql://[fe80::1ff:fe23:4567:890a]/db",
334 password="pwd4")])
335 self.assertEqual(
336 auth.getUrl(
337 "postgresql://user@[fe80::1ff:fe23:4567:890a]:5432/db"),
338 "postgresql://user:pwd@[fe80::1ff:fe23:4567:890a]:5432/db")
339 self.assertEqual(
340 auth.getUrl(
341 "postgresql://user2@[fe80::1ff:fe23:4567:890a]:5432/db"),
342 "postgresql://user2:pwd2@[fe80::1ff:fe23:4567:890a]:5432/db")
343 self.assertEqual(
344 auth.getUrl(
345 "postgresql://user3@[fe80::1ff:fe23:4567:890a]:3360/db"),
346 "postgresql://user3:pwd3@[fe80::1ff:fe23:4567:890a]:3360/db")
347 self.assertEqual(
348 auth.getUrl(
349 "postgresql://user4@[fe80::1ff:fe23:4567:890a]:3360/db"),
350 "postgresql://user4:pwd4@[fe80::1ff:fe23:4567:890a]:3360/db")
352 auth = DbAuth(authList=[
353 dict(url="postgresql://user1@host2.example.com:3360/database3",
354 password="first"),
355 dict(url="postgresql://host2.example.com:3360/database3",
356 username="second_u", password="second"),
357 dict(url="postgresql://host2.example.com:5432/database3",
358 username="wrong", password="port"),
359 dict(url="postgresql://host3.example.com/",
360 username="third_u", password="third"),
361 dict(url="oracle://oracle.example.com/other_database",
362 username="scott", password="tiger"),
363 dict(url="postgresql://*.example.com/database3",
364 username="fourth_u", password="fourth"),
365 dict(url="postgresql://*.example.com",
366 username="fifth_u", password="fifth")])
367 self.assertEqual(
368 auth.getUrl("postgresql://user1@host2.example.com:3360/database3"),
369 "postgresql://user1:first@host2.example.com:3360/database3")
370 self.assertEqual(
371 auth.getUrl("postgresql://user2@host2.example.com:3360/database3"),
372 "postgresql://user2:second@host2.example.com:3360/database3")
373 self.assertEqual(
374 auth.getUrl("postgresql://host2.example.com:3360/database3"),
375 "postgresql://second_u:second@host2.example.com:3360/database3")
376 self.assertEqual(
377 auth.getUrl("postgresql://host3.example.com:3360/database3"),
378 "postgresql://third_u:third@host3.example.com:3360/database3")
379 self.assertEqual(
380 auth.getUrl("postgresql://host3.example.com:3360/database4"),
381 "postgresql://third_u:third@host3.example.com:3360/database4")
382 self.assertEqual(
383 auth.getUrl("postgresql://host4.example.com:3360/database3"),
384 "postgresql://fourth_u:fourth@host4.example.com:3360/database3")
385 self.assertEqual(
386 auth.getUrl("postgresql://host4.example.com:3360/database4"),
387 "postgresql://fifth_u:fifth@host4.example.com:3360/database4")
389 auth = DbAuth(authList=[])
390 with self.assertRaisesRegex(
391 DbAuthError,
392 r"^Missing dialectname parameter$"):
393 auth.getUrl("/")
394 with self.assertRaisesRegex(
395 DbAuthError,
396 r"^Missing host parameter$"):
397 auth.getUrl("postgresql:///")
398 with self.assertRaisesRegex(
399 DbAuthError,
400 r"^Missing database parameter$"):
401 auth.getUrl("postgresql://example.com")
402 with self.assertRaisesRegex(
403 DbAuthError,
404 r"^Missing database parameter$"):
405 auth.getUrl("postgresql://example.com/")
406 with self.assertRaisesRegex(
407 DbAuthError,
408 r"^No matching DbAuth configuration for: "
409 r"\(postgresql, None, example\.com, None, foo\)$"):
410 auth.getUrl("postgresql://example.com/foo")
412 def test_urlEncoding(self):
413 """Test URL encoding of username and password."""
414 auth = DbAuth(authList=[
415 dict(url="postgresql://host.example.com/my_database",
416 username="foo:étude", password="bar,.@%&/*[]")])
417 self.assertEqual(
418 auth.getUrl("postgresql://host.example.com:5432/my_database"),
419 "postgresql://foo%3A%C3%A9tude:bar%2C.%40%25%26%2F"
420 "%2A%5B%5D@host.example.com:5432/my_database")
423class MemoryTester(lsst.utils.tests.MemoryTestCase):
424 pass
427def setup_module(module):
428 lsst.utils.tests.init()
431if __name__ == "__main__": 431 ↛ 432line 431 didn't jump to line 432, because the condition on line 431 was never true
432 lsst.utils.tests.init()
433 unittest.main()