Coverage for cc_modules/tests/cc_export_tests.py: 27%

26 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-30 13:48 +0000

1""" 

2camcops_server/cc_modules/tests/cc_export_tests.py 

3 

4=============================================================================== 

5 

6 Copyright (C) 2012, University of Cambridge, Department of Psychiatry. 

7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk). 

8 

9 This file is part of CamCOPS. 

10 

11 CamCOPS is free software: you can redistribute it and/or modify 

12 it under the terms of the GNU General Public License as published by 

13 the Free Software Foundation, either version 3 of the License, or 

14 (at your option) any later version. 

15 

16 CamCOPS is distributed in the hope that it will be useful, 

17 but WITHOUT ANY WARRANTY; without even the implied warranty of 

18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

19 GNU General Public License for more details. 

20 

21 You should have received a copy of the GNU General Public License 

22 along with CamCOPS. If not, see <https://www.gnu.org/licenses/>. 

23 

24=============================================================================== 

25 

26""" 

27 

28from os.path import join 

29from pathlib import Path 

30import tempfile 

31import unittest 

32 

33from camcops_server.cc_modules.cc_export import UserDownloadFile 

34 

35 

36# ============================================================================= 

37# Unit tests 

38# ============================================================================= 

39 

40 

41class ExportTests(unittest.TestCase): 

42 """ 

43 Test aspects of the export infrastructure. 

44 """ 

45 

46 def test_directory_safety(self) -> None: 

47 """ 

48 Here we ensure that passing a dodgy path to 

49 :class:`camcops_server.cc_modules.cc_export.UserDownloadFile` fails. 

50 """ 

51 with tempfile.TemporaryDirectory() as tmpdirname: 

52 topdir = Path(tmpdirname) 

53 safe_dirname = "safe_dir" 

54 safe_dir = topdir / safe_dirname 

55 safe_dir.mkdir() 

56 danger_dirname = "danger_dir" 

57 danger_dir = topdir / danger_dirname 

58 danger_dir.mkdir() 

59 safe_filename = "safe_file.txt" 

60 safe_file = safe_dir / safe_filename 

61 safe_file.touch() 

62 danger_filename = "danger_file.txt" 

63 danger_file = danger_dir / danger_filename 

64 danger_file.touch() 

65 

66 # log.debug(f"Top directory for test: {tmpdirname}") 

67 

68 ok = UserDownloadFile(safe_filename, str(safe_dir)) 

69 self.assertEqual(ok.exists, True) 

70 

71 danger_path = join("..", danger_dir, danger_filename) 

72 bad = UserDownloadFile(danger_path, str(safe_dir)) 

73 self.assertEqual(bad.exists, False)