File: test_apache.py

package info (click to toggle)
xandikos 0.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,552 kB
  • sloc: python: 17,496; xml: 583; sh: 343; makefile: 64
file content (140 lines) | stat: -rw-r--r-- 4,346 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Xandikos
# Copyright (C) 2025 Jelmer Vernooij <jelmer@jelmer.uk>, et al.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 3
# of the License or (at your option) any later version of
# the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA  02110-1301, USA.

"""Tests for xandikos.apache."""

import unittest
from unittest.mock import Mock
from xml.etree import ElementTree as ET
import asyncio

from xandikos import apache


class ExecutablePropertyTests(unittest.TestCase):
    """Tests for ExecutableProperty."""

    def test_property_attributes(self):
        """Test ExecutableProperty attributes."""
        prop = apache.ExecutableProperty()
        self.assertEqual(prop.name, "{http://apache.org/dav/props/}executable")
        self.assertIsNone(prop.resource_type)
        self.assertFalse(prop.live)

    def test_get_value_true(self):
        """Test get_value when resource is executable."""

        async def run_test():
            prop = apache.ExecutableProperty()
            resource = Mock()
            resource.get_is_executable.return_value = True

            el = ET.Element("test")
            await prop.get_value("/test.sh", resource, el, {})

            self.assertEqual(el.text, "T")
            resource.get_is_executable.assert_called_once()

        asyncio.run(run_test())

    def test_get_value_false(self):
        """Test get_value when resource is not executable."""

        async def run_test():
            prop = apache.ExecutableProperty()
            resource = Mock()
            resource.get_is_executable.return_value = False

            el = ET.Element("test")
            await prop.get_value("/test.txt", resource, el, {})

            self.assertEqual(el.text, "F")
            resource.get_is_executable.assert_called_once()

        asyncio.run(run_test())

    def test_set_value_true(self):
        """Test set_value with 'T' (true)."""

        async def run_test():
            prop = apache.ExecutableProperty()
            resource = Mock()

            el = ET.Element("test")
            el.text = "T"
            await prop.set_value("/test.sh", resource, el)

            resource.set_is_executable.assert_called_once_with(True)

        asyncio.run(run_test())

    def test_set_value_false(self):
        """Test set_value with 'F' (false)."""

        async def run_test():
            prop = apache.ExecutableProperty()
            resource = Mock()

            el = ET.Element("test")
            el.text = "F"
            await prop.set_value("/test.txt", resource, el)

            resource.set_is_executable.assert_called_once_with(False)

        asyncio.run(run_test())

    def test_set_value_invalid(self):
        """Test set_value with invalid value."""

        async def run_test():
            prop = apache.ExecutableProperty()
            resource = Mock()

            el = ET.Element("test")
            el.text = "X"  # Invalid value

            with self.assertRaises(ValueError) as cm:
                await prop.set_value("/test", resource, el)

            self.assertIn("invalid executable setting 'X'", str(cm.exception))
            resource.set_is_executable.assert_not_called()

        asyncio.run(run_test())

    def test_set_value_empty(self):
        """Test set_value with empty/None value."""

        async def run_test():
            prop = apache.ExecutableProperty()
            resource = Mock()

            el = ET.Element("test")
            el.text = None

            with self.assertRaises(ValueError) as cm:
                await prop.set_value("/test", resource, el)

            self.assertIn("invalid executable setting None", str(cm.exception))
            resource.set_is_executable.assert_not_called()

        asyncio.run(run_test())


if __name__ == "__main__":
    unittest.main()