Coverage for tests/test_webdavutils.py : 39%

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 unittest
23import requests
24import responses
25import os
27from lsst.daf.butler.core._butlerUri.http import (folderExists, webdavCheckFileExists,
28 isTokenAuth, _getFileURL,
29 webdavDeleteFile, isWebdavEndpoint,
30 finalurl)
31from lsst.daf.butler import Location, ButlerURI
34class WebdavUtilsTestCase(unittest.TestCase):
35 """Test for the Webdav related utilities.
36 """
37 session = requests.Session()
38 serverRoot = "www.lsstwithwebdav.orgx"
39 wrongRoot = "www.lsstwithoutwebdav.org"
40 existingfolderName = "testFolder"
41 notExistingfolderName = "testFolder_not_exist"
42 existingfileName = "testFileName"
43 notExistingfileName = "testFileName_not_exist"
45 def setUp(self):
46 # Used by folderExists()
47 responses.add(responses.HEAD, f"https://{self.serverRoot}/{self.existingfolderName}", status=200)
48 responses.add(responses.HEAD, f"https://{self.serverRoot}/{self.notExistingfolderName}", status=404)
50 # Used by webdavCheckFileExists()
51 responses.add(responses.HEAD,
52 f"https://{self.serverRoot}/{self.existingfolderName}/{self.existingfileName}",
53 status=200, headers={"Content-Length": "1024"})
54 responses.add(responses.HEAD,
55 f"https://{self.serverRoot}/{self.existingfolderName}/{self.notExistingfileName}",
56 status=404)
58 # Used by webdavDeleteFile()
59 responses.add(responses.DELETE,
60 f"https://{self.serverRoot}/{self.existingfolderName}/{self.existingfileName}",
61 status=200)
62 responses.add(responses.DELETE,
63 f"https://{self.serverRoot}/{self.existingfolderName}/{self.notExistingfileName}",
64 status=404)
66 # Used by isWebdavEndpoint()
67 responses.add(responses.OPTIONS, f"https://{self.serverRoot}", status=200, headers={"DAV": "1,2,3"})
68 responses.add(responses.OPTIONS, f"https://{self.wrongRoot}", status=200)
70 # Use by finalurl()
71 # Without redirection
72 responses.add(responses.PUT,
73 f"https://{self.serverRoot}/{self.existingfolderName}/{self.existingfileName}",
74 status=200)
75 # With redirection
76 responses.add(responses.PUT,
77 f"https://{self.wrongRoot}/{self.existingfolderName}/{self.existingfileName}",
78 headers={"Location":
79 f"https://{self.serverRoot}/{self.existingfolderName}/{self.existingfileName}"
80 },
81 status=307)
83 @responses.activate
84 def testFolderExists(self):
86 self.assertTrue(folderExists(f"https://{self.serverRoot}/{self.existingfolderName}",
87 session=self.session))
88 self.assertFalse(folderExists(f"https://{self.serverRoot}/{self.notExistingfolderName}",
89 session=self.session))
91 @responses.activate
92 def testWebdavCheckFileExists(self):
94 self.assertTrue(webdavCheckFileExists(
95 f"https://{self.serverRoot}/{self.existingfolderName}/{self.existingfileName}",
96 session=self.session)[0])
97 self.assertFalse(webdavCheckFileExists(
98 f"https://{self.serverRoot}/{self.existingfolderName}/{self.notExistingfileName}",
99 session=self.session)[0])
101 @responses.activate
102 def testWebdavDeleteFile(self):
104 self.assertIsNone(webdavDeleteFile(
105 f"https://{self.serverRoot}/{self.existingfolderName}/{self.existingfileName}",
106 session=self.session))
107 with self.assertRaises(FileNotFoundError):
108 webdavDeleteFile(
109 f"https://{self.serverRoot}/{self.existingfolderName}/{self.notExistingfileName}",
110 session=self.session)
112 @responses.activate
113 def testIsWebdavEndpoint(self):
115 self.assertTrue(isWebdavEndpoint(f"https://{self.serverRoot}"))
116 self.assertFalse(isWebdavEndpoint(f"https://{self.wrongRoot}"))
118 @responses.activate
119 def testGetFileURL(self):
121 s = f"https://{self.serverRoot}/{self.existingfolderName}/{self.existingfileName}"
122 buri = ButlerURI(f"https://{self.serverRoot}/{self.existingfolderName}/{self.existingfileName}")
123 loc = Location(f"https://{self.serverRoot}/", f"{self.existingfolderName}/{self.existingfileName}")
125 self.assertEqual(_getFileURL(s), s)
126 self.assertEqual(_getFileURL(buri), s)
127 self.assertEqual(_getFileURL(loc), s)
129 def testIsTokenAuth(self):
130 with unittest.mock.patch.dict(os.environ, {"LSST_BUTLER_WEBDAV_AUTH": "TOKEN"}):
131 self.assertTrue(isTokenAuth())
132 with unittest.mock.patch.dict(os.environ, {"LSST_BUTLER_WEBDAV_AUTH": "X509"}):
133 self.assertFalse(isTokenAuth())
135 @responses.activate
136 def testFinalurl(self):
137 s = f"https://{self.serverRoot}/{self.existingfolderName}/{self.existingfileName}"
138 r = f"https://{self.wrongRoot}/{self.existingfolderName}/{self.existingfileName}"
140 resp_s = self.session.put(s)
141 resp_r = self.session.put(r)
143 self.assertEqual(finalurl(resp_s), s)
144 self.assertEqual(finalurl(resp_r), s)
147if __name__ == "__main__": 147 ↛ 148line 147 didn't jump to line 148, because the condition on line 147 was never true
148 unittest.main()