Coverage for cc_modules/tests/cc_export_tests.py: 30%
27 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-08 23:14 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-08 23:14 +0000
1#!/usr/bin/env python
3"""
4camcops_server/cc_modules/tests/cc_export_tests.py
6===============================================================================
8 Copyright (C) 2012, University of Cambridge, Department of Psychiatry.
9 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
11 This file is part of CamCOPS.
13 CamCOPS is free software: you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
18 CamCOPS is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with CamCOPS. If not, see <https://www.gnu.org/licenses/>.
26===============================================================================
28"""
30from os.path import join
31from pathlib import Path
32import tempfile
33import unittest
35from camcops_server.cc_modules.cc_export import UserDownloadFile
38# =============================================================================
39# Unit tests
40# =============================================================================
43class ExportTests(unittest.TestCase):
44 """
45 Test aspects of the export infrastructure.
46 """
48 def test_directory_safety(self) -> None:
49 """
50 Here we ensure that passing a dodgy path to
51 :class:`camcops_server.cc_modules.cc_export.UserDownloadFile` fails.
52 """
53 with tempfile.TemporaryDirectory() as tmpdirname:
54 topdir = Path(tmpdirname)
55 safe_dirname = "safe_dir"
56 safe_dir = topdir / safe_dirname
57 safe_dir.mkdir()
58 danger_dirname = "danger_dir"
59 danger_dir = topdir / danger_dirname
60 danger_dir.mkdir()
61 safe_filename = "safe_file.txt"
62 safe_file = safe_dir / safe_filename
63 safe_file.touch()
64 danger_filename = "danger_file.txt"
65 danger_file = danger_dir / danger_filename
66 danger_file.touch()
68 # log.debug(f"Top directory for test: {tmpdirname}")
70 ok = UserDownloadFile(safe_filename, str(safe_dir))
71 self.assertEqual(ok.exists, True)
73 danger_path = join("..", danger_dir, danger_filename)
74 bad = UserDownloadFile(danger_path, str(safe_dir))
75 self.assertEqual(bad.exists, False)