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

37 statements  

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/>. 

21 

22import unittest 

23import requests 

24import responses 

25import os 

26 

27from lsst.daf.butler.core._butlerUri.http import (isTokenAuth, 

28 isWebdavEndpoint, finalurl) 

29 

30 

31class WebdavUtilsTestCase(unittest.TestCase): 

32 """Test for the Webdav related utilities. 

33 """ 

34 session = requests.Session() 

35 serverRoot = "www.lsstwithwebdav.orgx" 

36 wrongRoot = "www.lsstwithoutwebdav.org" 

37 existingfolderName = "testFolder" 

38 notExistingfolderName = "testFolder_not_exist" 

39 existingfileName = "testFileName" 

40 notExistingfileName = "testFileName_not_exist" 

41 

42 def setUp(self): 

43 # Used by isWebdavEndpoint() 

44 responses.add(responses.OPTIONS, f"https://{self.serverRoot}", status=200, headers={"DAV": "1,2,3"}) 

45 responses.add(responses.OPTIONS, f"https://{self.wrongRoot}", status=200) 

46 

47 # Use by finalurl() 

48 # Without redirection 

49 responses.add(responses.PUT, 

50 f"https://{self.serverRoot}/{self.existingfolderName}/{self.existingfileName}", 

51 status=200) 

52 # With redirection 

53 responses.add(responses.PUT, 

54 f"https://{self.wrongRoot}/{self.existingfolderName}/{self.existingfileName}", 

55 headers={"Location": 

56 f"https://{self.serverRoot}/{self.existingfolderName}/{self.existingfileName}" 

57 }, 

58 status=307) 

59 

60 @responses.activate 

61 def testIsWebdavEndpoint(self): 

62 

63 self.assertTrue(isWebdavEndpoint(f"https://{self.serverRoot}")) 

64 self.assertFalse(isWebdavEndpoint(f"https://{self.wrongRoot}")) 

65 

66 def testIsTokenAuth(self): 

67 with unittest.mock.patch.dict(os.environ, {"LSST_BUTLER_WEBDAV_AUTH": "TOKEN"}): 

68 self.assertTrue(isTokenAuth()) 

69 with unittest.mock.patch.dict(os.environ, {"LSST_BUTLER_WEBDAV_AUTH": "X509"}): 

70 self.assertFalse(isTokenAuth()) 

71 

72 @responses.activate 

73 def testFinalurl(self): 

74 s = f"https://{self.serverRoot}/{self.existingfolderName}/{self.existingfileName}" 

75 r = f"https://{self.wrongRoot}/{self.existingfolderName}/{self.existingfileName}" 

76 

77 resp_s = self.session.put(s) 

78 resp_r = self.session.put(r) 

79 

80 self.assertEqual(finalurl(resp_s), s) 

81 self.assertEqual(finalurl(resp_r), s) 

82 

83 

84if __name__ == "__main__": 84 ↛ 85line 84 didn't jump to line 85, because the condition on line 84 was never true

85 unittest.main()