Coverage for tests/test_webdavutils.py: 56%
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
25import requests
26import responses
27from lsst.resources.http import finalurl, isTokenAuth, isWebdavEndpoint
30class WebdavUtilsTestCase(unittest.TestCase):
31 """Test for the Webdav related utilities."""
33 session = requests.Session()
34 serverRoot = "www.lsstwithwebdav.orgx"
35 wrongRoot = "www.lsstwithoutwebdav.org"
36 existingfolderName = "testFolder"
37 notExistingfolderName = "testFolder_not_exist"
38 existingfileName = "testFileName"
39 notExistingfileName = "testFileName_not_exist"
41 def setUp(self):
42 # Used by isWebdavEndpoint()
43 responses.add(responses.OPTIONS, f"https://{self.serverRoot}", status=200, headers={"DAV": "1,2,3"})
44 responses.add(responses.OPTIONS, f"https://{self.wrongRoot}", status=200)
46 # Use by finalurl()
47 # Without redirection
48 responses.add(
49 responses.PUT,
50 f"https://{self.serverRoot}/{self.existingfolderName}/{self.existingfileName}",
51 status=200,
52 )
53 # With redirection
54 responses.add(
55 responses.PUT,
56 f"https://{self.wrongRoot}/{self.existingfolderName}/{self.existingfileName}",
57 headers={
58 "Location": f"https://{self.serverRoot}/{self.existingfolderName}/{self.existingfileName}"
59 },
60 status=307,
61 )
63 @responses.activate
64 def testIsWebdavEndpoint(self):
66 self.assertTrue(isWebdavEndpoint(f"https://{self.serverRoot}"))
67 self.assertFalse(isWebdavEndpoint(f"https://{self.wrongRoot}"))
69 def testIsTokenAuth(self):
70 with unittest.mock.patch.dict(os.environ, {"LSST_BUTLER_WEBDAV_AUTH": "TOKEN"}):
71 self.assertTrue(isTokenAuth())
72 with unittest.mock.patch.dict(os.environ, {"LSST_BUTLER_WEBDAV_AUTH": "X509"}):
73 self.assertFalse(isTokenAuth())
75 @responses.activate
76 def testFinalurl(self):
77 s = f"https://{self.serverRoot}/{self.existingfolderName}/{self.existingfileName}"
78 r = f"https://{self.wrongRoot}/{self.existingfolderName}/{self.existingfileName}"
80 resp_s = self.session.put(s)
81 resp_r = self.session.put(r)
83 self.assertEqual(finalurl(resp_s), s)
84 self.assertEqual(finalurl(resp_r), s)
87if __name__ == "__main__": 87 ↛ 88line 87 didn't jump to line 88, because the condition on line 87 was never true
88 unittest.main()