Coverage for tests/test_dbAuth.py: 10%
Shortcuts 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
Shortcuts 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
25from lsst.daf.butler.registry import DbAuth, DbAuthError
27TESTDIR = os.path.join(
28 os.path.abspath(os.path.dirname(__file__)), "config", "dbAuth")
31class DbAuthTestCase(unittest.TestCase):
32 """Test DbAuth class."""
34 def test_patterns(self):
35 """Test various patterns against a fixed connection URL."""
36 for matchPattern in [
37 "postgresql://*",
38 "postgresql://*.example.com",
39 "postgresql://*.example.com/my_*",
40 "postgresql://host.example.com/my_database",
41 "postgresql://host.example.com:5432/my_database",
42 "postgresql://user@host.example.com/my_database"]:
43 auth = DbAuth(authList=[
44 dict(url=matchPattern, username="foo",
45 password="bar")])
46 user, pwd = auth.getAuth(
47 "postgresql", "user", "host.example.com", "5432",
48 "my_database")
49 self.assertEqual(user, "user")
50 self.assertEqual(pwd, "bar")
52 def test_connStrings(self):
53 """Test various connection URLs against a fixed pattern."""
54 auth = DbAuth(authList=[
55 dict(url="postgresql://host.example.com/my_database",
56 username="foo", password="bar")])
57 for connComponents in [
58 ("postgresql", "user", "host.example.com", 5432,
59 "my_database"),
60 ("postgresql", "user", "host.example.com", None,
61 "my_database")]:
62 user, pwd = auth.getAuth(*connComponents)
63 self.assertEqual(user, "user")
64 self.assertEqual(pwd, "bar")
65 for connComponents in [
66 ("postgresql", None, "host.example.com", None,
67 "my_database"),
68 ("postgresql", None, "host.example.com", "5432",
69 "my_database")]:
70 user, pwd = auth.getAuth(*connComponents)
71 self.assertEqual(user, "foo")
72 self.assertEqual(pwd, "bar")
74 def test_load(self):
75 """Test loading from a YAML file and matching in order."""
76 filePath = os.path.join(TESTDIR, "db-auth.yaml")
77 os.chmod(filePath, 0o600)
78 auth = DbAuth(filePath)
79 self.assertEqual(
80 auth.getAuth("postgresql", "user", "host.example.com",
81 "5432", "my_database"),
82 ("user", "test1"))
83 self.assertEqual(
84 auth.getAuth("postgresql", "user", "host.example.com",
85 "3360", "my_database"),
86 ("user", "test2"))
87 self.assertEqual(
88 auth.getAuth("postgresql", "", "host.example.com",
89 "5432", "my_database"),
90 ("user", "test3"))
91 self.assertEqual(
92 auth.getAuth("postgresql", None, "host.example.com",
93 "", "my_database"),
94 ("user", "test4"))
95 self.assertEqual(
96 auth.getAuth("postgresql", "", "host2.example.com",
97 None, "my_other"),
98 ("user", "test5"))
99 self.assertEqual(
100 auth.getAuth("postgresql", None, "host3.example.com",
101 3360, "some_database"),
102 ("user", "test6"))
103 self.assertEqual(
104 auth.getAuth("postgresql", "user", "host4.other.com",
105 None, "other_database"),
106 ("user", "test8"))
108 def test_ipv6(self):
109 """Test IPv6 addresses as host names."""
110 auth = DbAuth(authList=[
111 dict(url="postgresql://user@[fe80::1ff:fe23:4567:890a]:5432/db",
112 password="pwd"),
113 dict(url="postgresql://[fe80::1ff:fe23:4567:890a]:5432/db",
114 password="pwd2"),
115 dict(url="postgresql://user3@[fe80::1ff:fe23:4567:890a]/db",
116 password="pwd3"),
117 dict(url="postgresql://[fe80::1ff:fe23:4567:890a]/db",
118 password="pwd4")])
119 self.assertEqual(
120 auth.getAuth("postgresql", "user",
121 "fe80::1ff:fe23:4567:890a", "5432", "db"),
122 ("user", "pwd"))
123 self.assertEqual(
124 auth.getAuth("postgresql", "user2",
125 "fe80::1ff:fe23:4567:890a", "5432", "db"),
126 ("user2", "pwd2"))
127 self.assertEqual(
128 auth.getAuth("postgresql", "user3",
129 "fe80::1ff:fe23:4567:890a", "3360", "db"),
130 ("user3", "pwd3"))
131 self.assertEqual(
132 auth.getAuth("postgresql", "user4",
133 "fe80::1ff:fe23:4567:890a", "3360", "db"),
134 ("user4", "pwd4"))
136 def test_search(self):
137 """Test ordered searching."""
138 auth = DbAuth(authList=[
139 dict(url="postgresql://user1@host2.example.com:3360/database3",
140 password="first"),
141 dict(url="postgresql://host2.example.com:3360/database3",
142 username="second_u", password="second"),
143 dict(url="postgresql://host2.example.com:5432/database3",
144 username="wrong", password="port"),
145 dict(url="postgresql://host3.example.com/",
146 username="third_u", password="third"),
147 dict(url="oracle://oracle.example.com/other_database",
148 username="scott", password="tiger"),
149 dict(url="postgresql://*.example.com/database3",
150 username="fourth_u", password="fourth"),
151 dict(url="postgresql://*.example.com",
152 username="fifth_u", password="fifth")])
153 self.assertEqual(
154 auth.getAuth("postgresql", "user1", "host2.example.com",
155 "3360", "database3"),
156 ("user1", "first"))
157 self.assertEqual(
158 auth.getAuth("postgresql", "user2", "host2.example.com",
159 "3360", "database3"),
160 ("user2", "second"))
161 self.assertEqual(
162 auth.getAuth("postgresql", "", "host2.example.com",
163 "3360", "database3"),
164 ("second_u", "second"))
165 self.assertEqual(
166 auth.getAuth("postgresql", None, "host2.example.com",
167 "3360", "database3"),
168 ("second_u", "second"))
169 self.assertEqual(
170 auth.getAuth("postgresql", None, "host3.example.com",
171 "3360", "database3"),
172 ("third_u", "third"))
173 self.assertEqual(
174 auth.getAuth("postgresql", None, "host3.example.com",
175 "3360", "database4"),
176 ("third_u", "third"))
177 self.assertEqual(
178 auth.getAuth("postgresql", None, "host4.example.com",
179 "3360", "database3"),
180 ("fourth_u", "fourth"))
181 self.assertEqual(
182 auth.getAuth("postgresql", None, "host4.example.com",
183 "3360", "database4"),
184 ("fifth_u", "fifth"))
186 def test_errors(self):
187 """Test for error exceptions."""
188 with self.assertRaisesRegex(
189 DbAuthError,
190 r"^No default path provided to DbAuth configuration file$"):
191 auth = DbAuth()
192 with self.assertRaisesRegex(
193 DbAuthError,
194 r"^No DbAuth configuration file: .*this_does_not_exist$"):
195 auth = DbAuth(os.path.join(TESTDIR, "this_does_not_exist"))
196 with self.assertRaisesRegex(
197 DbAuthError,
198 r"^No DbAuth configuration file: .*this_does_not_exist$"):
199 auth = DbAuth(os.path.join(TESTDIR, "this_does_not_exist"),
200 envVar="LSST_NONEXISTENT")
201 with self.assertRaisesRegex(
202 DbAuthError,
203 r"^DbAuth configuration file .*badDbAuth1.yaml has incorrect "
204 r"permissions: \d*644$"):
205 filePath = os.path.join(TESTDIR, "badDbAuth1.yaml")
206 os.chmod(filePath, 0o644)
207 auth = DbAuth(filePath)
208 with self.assertRaisesRegex(
209 DbAuthError,
210 r"^Unable to load DbAuth configuration file: "
211 r".*badDbAuth2.yaml"):
212 filePath = os.path.join(TESTDIR, "badDbAuth2.yaml")
213 os.chmod(filePath, 0o600)
214 os.environ["LSST_DB_AUTH_TEST"] = filePath
215 auth = DbAuth(envVar="LSST_DB_AUTH_TEST")
217 auth = DbAuth(authList=[])
218 with self.assertRaisesRegex(
219 DbAuthError,
220 r"^Missing dialectname parameter$"):
221 auth.getAuth(None, None, None, None, None)
222 with self.assertRaisesRegex(
223 DbAuthError,
224 r"^Missing dialectname parameter$"):
225 auth.getAuth("", None, None, None, None)
226 with self.assertRaisesRegex(
227 DbAuthError,
228 r"^Missing host parameter$"):
229 auth.getAuth("postgresql", None, None, None, None)
230 with self.assertRaisesRegex(
231 DbAuthError,
232 r"^Missing host parameter$"):
233 auth.getAuth("postgresql", None, "", None, None)
234 with self.assertRaisesRegex(
235 DbAuthError,
236 r"^Missing database parameter$"):
237 auth.getAuth("postgresql", None, "example.com", None, None)
238 with self.assertRaisesRegex(
239 DbAuthError,
240 r"^Missing database parameter$"):
241 auth.getAuth("postgresql", None, "example.com", None, "")
242 with self.assertRaisesRegex(
243 DbAuthError,
244 r"^No matching DbAuth configuration for: "
245 r"\(postgresql, None, example\.com, None, foo\)$"):
246 auth.getAuth("postgresql", None, "example.com", None, "foo")
248 auth = DbAuth(authList=[dict(password="testing")])
249 with self.assertRaisesRegex(
250 DbAuthError,
251 r"^Missing URL in DbAuth configuration$"):
252 auth.getAuth("postgresql", None, "example.com", None, "foo")
254 auth = DbAuth(authList=[dict(url="testing", password="testing")])
255 with self.assertRaisesRegex(
256 DbAuthError,
257 r"^Missing database dialect in URL: testing$"):
258 auth.getAuth("postgresql", None, "example.com", None, "foo")
260 auth = DbAuth(authList=[
261 dict(url="postgresql:///foo", password="testing")])
262 with self.assertRaisesRegex(
263 DbAuthError,
264 r"^Missing host in URL: postgresql:///foo$"):
265 auth.getAuth("postgresql", None, "example.com", None, "foo")
267 def test_getUrl(self):
268 """Repeat relevant tests using getUrl interface."""
269 for matchPattern in [
270 "postgresql://*",
271 "postgresql://*.example.com",
272 "postgresql://*.example.com/my_*",
273 "postgresql://host.example.com/my_database",
274 "postgresql://host.example.com:5432/my_database",
275 "postgresql://user@host.example.com/my_database"]:
276 auth = DbAuth(authList=[
277 dict(url=matchPattern, username="foo",
278 password="bar")])
279 self.assertEqual(
280 auth.getUrl(
281 "postgresql://user@host.example.com:5432/my_database"),
282 "postgresql://user:bar@host.example.com:5432/my_database")
284 auth = DbAuth(authList=[
285 dict(url="postgresql://host.example.com/my_database",
286 username="foo", password="bar")])
287 self.assertEqual(
288 auth.getUrl("postgresql://user@host.example.com:5432/my_database"),
289 "postgresql://user:bar@host.example.com:5432/my_database"),
290 self.assertEqual(
291 auth.getUrl("postgresql://user@host.example.com/my_database"),
292 "postgresql://user:bar@host.example.com/my_database"),
293 self.assertEqual(
294 auth.getUrl("postgresql://host.example.com/my_database"),
295 "postgresql://foo:bar@host.example.com/my_database"),
296 self.assertEqual(
297 auth.getUrl("postgresql://host.example.com:5432/my_database"),
298 "postgresql://foo:bar@host.example.com:5432/my_database"),
300 filePath = os.path.join(TESTDIR, "db-auth.yaml")
301 os.chmod(filePath, 0o600)
302 auth = DbAuth(filePath)
303 self.assertEqual(
304 auth.getUrl("postgresql://user@host.example.com:5432/my_database"),
305 "postgresql://user:test1@host.example.com:5432/my_database")
306 self.assertEqual(
307 auth.getUrl("postgresql://user@host.example.com:3360/my_database"),
308 "postgresql://user:test2@host.example.com:3360/my_database")
309 self.assertEqual(
310 auth.getUrl("postgresql://host.example.com:5432/my_database"),
311 "postgresql://user:test3@host.example.com:5432/my_database")
312 self.assertEqual(
313 auth.getUrl("postgresql://host.example.com/my_database"),
314 "postgresql://user:test4@host.example.com/my_database")
315 self.assertEqual(
316 auth.getUrl("postgresql://host2.example.com/my_other"),
317 "postgresql://user:test5@host2.example.com/my_other")
318 self.assertEqual(
319 auth.getUrl("postgresql://host3.example.com:3360/some_database"),
320 "postgresql://user:test6@host3.example.com:3360/some_database")
321 self.assertEqual(
322 auth.getUrl("postgresql://user@host4.other.com/other_database"),
323 "postgresql://user:test8@host4.other.com/other_database")
325 auth = DbAuth(authList=[
326 dict(url="postgresql://user@[fe80::1ff:fe23:4567:890a]:5432/db",
327 password="pwd"),
328 dict(url="postgresql://[fe80::1ff:fe23:4567:890a]:5432/db",
329 password="pwd2"),
330 dict(url="postgresql://user3@[fe80::1ff:fe23:4567:890a]/db",
331 password="pwd3"),
332 dict(url="postgresql://[fe80::1ff:fe23:4567:890a]/db",
333 password="pwd4")])
334 self.assertEqual(
335 auth.getUrl(
336 "postgresql://user@[fe80::1ff:fe23:4567:890a]:5432/db"),
337 "postgresql://user:pwd@[fe80::1ff:fe23:4567:890a]:5432/db")
338 self.assertEqual(
339 auth.getUrl(
340 "postgresql://user2@[fe80::1ff:fe23:4567:890a]:5432/db"),
341 "postgresql://user2:pwd2@[fe80::1ff:fe23:4567:890a]:5432/db")
342 self.assertEqual(
343 auth.getUrl(
344 "postgresql://user3@[fe80::1ff:fe23:4567:890a]:3360/db"),
345 "postgresql://user3:pwd3@[fe80::1ff:fe23:4567:890a]:3360/db")
346 self.assertEqual(
347 auth.getUrl(
348 "postgresql://user4@[fe80::1ff:fe23:4567:890a]:3360/db"),
349 "postgresql://user4:pwd4@[fe80::1ff:fe23:4567:890a]:3360/db")
351 auth = DbAuth(authList=[
352 dict(url="postgresql://user1@host2.example.com:3360/database3",
353 password="first"),
354 dict(url="postgresql://host2.example.com:3360/database3",
355 username="second_u", password="second"),
356 dict(url="postgresql://host2.example.com:5432/database3",
357 username="wrong", password="port"),
358 dict(url="postgresql://host3.example.com/",
359 username="third_u", password="third"),
360 dict(url="oracle://oracle.example.com/other_database",
361 username="scott", password="tiger"),
362 dict(url="postgresql://*.example.com/database3",
363 username="fourth_u", password="fourth"),
364 dict(url="postgresql://*.example.com",
365 username="fifth_u", password="fifth")])
366 self.assertEqual(
367 auth.getUrl("postgresql://user1@host2.example.com:3360/database3"),
368 "postgresql://user1:first@host2.example.com:3360/database3")
369 self.assertEqual(
370 auth.getUrl("postgresql://user2@host2.example.com:3360/database3"),
371 "postgresql://user2:second@host2.example.com:3360/database3")
372 self.assertEqual(
373 auth.getUrl("postgresql://host2.example.com:3360/database3"),
374 "postgresql://second_u:second@host2.example.com:3360/database3")
375 self.assertEqual(
376 auth.getUrl("postgresql://host3.example.com:3360/database3"),
377 "postgresql://third_u:third@host3.example.com:3360/database3")
378 self.assertEqual(
379 auth.getUrl("postgresql://host3.example.com:3360/database4"),
380 "postgresql://third_u:third@host3.example.com:3360/database4")
381 self.assertEqual(
382 auth.getUrl("postgresql://host4.example.com:3360/database3"),
383 "postgresql://fourth_u:fourth@host4.example.com:3360/database3")
384 self.assertEqual(
385 auth.getUrl("postgresql://host4.example.com:3360/database4"),
386 "postgresql://fifth_u:fifth@host4.example.com:3360/database4")
388 auth = DbAuth(authList=[])
389 with self.assertRaisesRegex(
390 DbAuthError,
391 r"^Missing dialectname parameter$"):
392 auth.getUrl("/")
393 with self.assertRaisesRegex(
394 DbAuthError,
395 r"^Missing host parameter$"):
396 auth.getUrl("postgresql:///")
397 with self.assertRaisesRegex(
398 DbAuthError,
399 r"^Missing database parameter$"):
400 auth.getUrl("postgresql://example.com")
401 with self.assertRaisesRegex(
402 DbAuthError,
403 r"^Missing database parameter$"):
404 auth.getUrl("postgresql://example.com/")
405 with self.assertRaisesRegex(
406 DbAuthError,
407 r"^No matching DbAuth configuration for: "
408 r"\(postgresql, None, example\.com, None, foo\)$"):
409 auth.getUrl("postgresql://example.com/foo")
411 def test_urlEncoding(self):
412 """Test URL encoding of username and password."""
413 auth = DbAuth(authList=[
414 dict(url="postgresql://host.example.com/my_database",
415 username="foo:étude", password="bar,.@%&/*[]")])
416 self.assertEqual(
417 auth.getUrl("postgresql://host.example.com:5432/my_database"),
418 "postgresql://foo%3A%C3%A9tude:bar%2C.%40%25%26%2F"
419 "%2A%5B%5D@host.example.com:5432/my_database")
422if __name__ == "__main__": 422 ↛ 423line 422 didn't jump to line 423, because the condition on line 422 was never true
423 unittest.main()