File: test_client_unit.py

package info (click to toggle)
python-webdavclient 3.14.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 644 kB
  • sloc: python: 2,241; xml: 535; makefile: 5; sh: 3
file content (254 lines) | stat: -rw-r--r-- 12,090 bytes parent folder | download | duplicates (2)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# coding=utf-8
import unittest
from unittest import TestCase
from unittest.mock import patch, Mock, PropertyMock

from lxml.etree import ElementTree, Element

from webdav3.client import WebDavXmlUtils as Utils, listdir, MethodNotSupported, RemoteResourceNotFound, Client
from webdav3.exceptions import ResponseErrorCode, NotEnoughSpace


def read_file_content(file_name):
    with open(file_name, encoding='utf-8') as f:
        return f.read().encode('utf-8')


class ClientTestCase(TestCase):
    def test_parse_get_list_response(self):
        content = read_file_content('./tests/responses/get_list.xml')
        result = Utils.parse_get_list_response(content)
        self.assertEqual(result.__len__(), 2)
        self.assertTrue(result[0].is_dir(), '{} should be marked as directory'.format(result[0].path()))
        self.assertFalse(result[1].is_dir(), '{} should be marked as file'.format(result[1].path()))
        self.assertEqual(result[1].__str__(), '/test_dir/test.txt')

    def test_parse_get_list_info_response(self):
        content = read_file_content('./tests/responses/get_list_info.xml')
        result = Utils.parse_get_list_info_response(content)
        print(result)
        self.assertEqual(result[0]['created'], '2017-10-18T15:16:04Z')
        self.assertEqual(result[0]['name'], 'test.txt')
        self.assertEqual(result[0]['modified'], 'Wed, 18 Oct 2017 15:16:04 GMT')
        self.assertEqual(result[0]['size'], '41')
        self.assertEqual(result[0]['etag'], 'ab0b4b7973803c03639b848682b5f38c')
        self.assertEqual(result[0]['isdir'], False)
        self.assertEqual(result[0]['path'], '/test_dir/test.txt')
        self.assertEqual(result[0]['content_type'], 'text/plain')

    def test_parse_get_list_response_empty(self):
        content = read_file_content('./tests/responses/get_list_empty.xml')
        result = Utils.parse_get_list_response(content)
        self.assertEqual(result.__len__(), 0)

    def test_parse_get_list_response_incorrect(self):
        content = read_file_content('./tests/responses/get_list_incorrect.xml')
        result = Utils.parse_get_list_response(content)
        self.assertEqual(result.__len__(), 0)

    def test_parse_get_list_response_dirs(self):
        content = read_file_content('./tests/responses/get_list_directories.xml')
        result = Utils.parse_get_list_response(content)
        self.assertEqual(result.__len__(), 5)
        for d in result:
            self.assertTrue(d.is_dir(), '{} should be marked as directory'.format(d.path()))

    def test_create_free_space_request_content(self):
        result = Utils.create_free_space_request_content()
        self.assertEqual(result, b'<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<propfind xmlns="DAV:"><prop>'
                                 b'<quota-available-bytes/><quota-used-bytes/></prop></propfind>')

    def test_parse_free_space_response(self):
        content = read_file_content('./tests/responses/free_space.xml')
        result = Utils.parse_free_space_response(content, 'localhost')
        self.assertEqual(result, 10737417543)

    def test_parse_free_space_response_not_supported(self):
        content = read_file_content('./tests/responses/free_space_not_supported.xml')
        self.assertRaises(MethodNotSupported, Utils.parse_free_space_response, content, 'localhost')

    def test_parse_free_space_response_incorrect(self):
        content = read_file_content('./tests/responses/free_space_incorrect.xml')
        result = Utils.parse_free_space_response(content, 'localhost')
        self.assertEqual(result, '')

    def test_parse_info_response(self):
        content = read_file_content('./tests/responses/get_info.xml')
        result = Utils.parse_info_response(content, '/test_dir/test.txt', 'localhost')
        self.assertEqual(result['created'], '2017-10-18T15:16:04Z')
        self.assertEqual(result['name'], 'test.txt')
        self.assertEqual(result['modified'], 'Wed, 18 Oct 2017 15:16:04 GMT')
        self.assertEqual(result['size'], '41')
        self.assertEqual(result['content_type'], 'text/plain')
    
    def test_get_info_from_response(self):
        content = read_file_content('./tests/responses/get_info.xml')
        response = Utils.extract_response_for_path(content, '/test_dir/test.txt', 'localhost')
        result = Utils.get_info_from_response(response)
        self.assertEqual(result['created'], '2017-10-18T15:16:04Z')
        self.assertEqual(result['name'], 'test.txt')
        self.assertEqual(result['modified'], 'Wed, 18 Oct 2017 15:16:04 GMT')
        self.assertEqual(result['size'], '41')
        self.assertEqual(result['content_type'], 'text/plain')

    def test_create_get_property_request_content(self):
        option = {
            'namespace': 'test',
            'name': 'aProperty'
        }
        result = Utils.create_get_property_request_content(option=option, )
        self.assertEqual(result, b'<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<propfind xmlns="DAV:"><prop>'
                                 b'<aProperty xmlns="test"/></prop></propfind>')

    def test_create_get_property_request_content_name_only(self):
        option = {
            'name': 'aProperty'
        }
        result = Utils.create_get_property_request_content(option=option)
        self.assertEqual(result, b'<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<propfind xmlns="DAV:"><prop>'
                                 b'<aProperty xmlns=""/></prop></propfind>')

    def test_parse_get_property_response(self):
        content = read_file_content('./tests/responses/get_property.xml')
        result = Utils.parse_get_property_response(content=content, name='aProperty')
        self.assertEqual(result, 'aValue')

    def test_create_set_one_property_request_content(self):
        option = {
            'namespace': 'test',
            'name': 'aProperty',
            'value': 'aValue'
        }
        result = Utils.create_set_property_batch_request_content(options=[option])
        self.assertEqual(result, b'<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<propertyupdate xmlns="DAV:"><set><prop>'
                                 b'<aProperty xmlns="test">aValue</aProperty></prop></set></propertyupdate>')

    def test_create_set_one_property_request_content_name_only(self):
        option = {
            'name': 'aProperty'
        }
        result = Utils.create_set_property_batch_request_content(options=[option])
        self.assertEqual(result, b'<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<propertyupdate xmlns="DAV:"><set><prop>'
                                 b'<aProperty xmlns=""></aProperty></prop></set></propertyupdate>')

    def test_create_set_property_batch_request_content(self):
        options = [
            {
                'namespace': 'test',
                'name': 'aProperty',
                'value': 'aValue'
            },
            {
                'namespace': 'test2',
                'name': 'aProperty2',
                'value': 'aValue2'
            }
        ]
        result = Utils.create_set_property_batch_request_content(options=options)
        self.assertEqual(result, b'<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<propertyupdate xmlns="DAV:"><set><prop>'
                                 b'<aProperty xmlns="test">aValue</aProperty><aProperty2 xmlns="test2">aValue2'
                                 b'</aProperty2></prop></set></propertyupdate>')

    def test_create_set_property_batch_request_content_name_only(self):
        options = [
            {
                'name': 'aProperty'
            },
            {
                'name': 'aProperty2'
            }
        ]
        result = Utils.create_set_property_batch_request_content(options=options)
        self.assertEqual(result, b'<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<propertyupdate xmlns="DAV:"><set><prop>'
                                 b'<aProperty xmlns=""></aProperty><aProperty2 xmlns=""></aProperty2></prop></set>'
                                 b'</propertyupdate>')

    def test_etree_to_string(self):
        tree = ElementTree(Element('test'))
        result = Utils.etree_to_string(tree)
        self.assertEqual(result, b'<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<test/>')

    def test_parse_is_dir_response_not_supported(self):
        content = read_file_content('./tests/responses/is_dir_directory_not_supported.xml')
        path = '/test_dir'
        hostname = 'https://webdav.yandex.ru'
        self.assertRaises(MethodNotSupported, Utils.parse_is_dir_response, content, path, hostname)

    def test_parse_is_dir_response_directory(self):
        content = read_file_content('./tests/responses/is_dir_directory.xml')
        path = '/test_dir'
        hostname = 'https://webdav.yandex.ru'
        result = Utils.parse_is_dir_response(content, path, hostname)
        self.assertTrue(result, 'It should be directory')

    def test_parse_is_dir_response_file(self):
        content = read_file_content('./tests/responses/is_dir_file.xml')
        path = '/test_dir/test.txt'
        hostname = 'https://webdav.yandex.ru'
        result = Utils.parse_is_dir_response(content, path, hostname)
        self.assertFalse(result, 'It should be file')

    def test_listdir_inner_dir(self):
        file_names = listdir('.')
        self.assertGreater(len(file_names), 0)
        self.assertTrue('README.md' in file_names)

    def test_extract_response_for_path_not_supported(self):
        self.assertRaises(MethodNotSupported, Utils.extract_response_for_path, 'WrongXML', 'test', 'https://webdav.ru')

    def test_extract_response_for_path_not_found(self):
        content = read_file_content('./tests/responses/is_dir_directory.xml')
        hostname = 'https://webdav.yandex.ru'
        self.assertRaises(RemoteResourceNotFound, Utils.extract_response_for_path, content, 'wrong_name', hostname)

    def test_parse_is_dir_response_file_with_prefix(self):
        content = read_file_content('./tests/responses/is_dir_file.xml')
        path = '/test.txt'
        hostname = 'https://webdav.yandex.ru/test_dir/'
        result = Utils.parse_is_dir_response(content, path, hostname)
        self.assertFalse(result, 'It should be file')

    def test_utils_created(self):
        self.assertIsNotNone(Utils())

    options = {
        'webdav_hostname': 'http://localhost:8585',
        'webdav_login': 'alice',
        'webdav_password': 'secret1234'
    }

    @patch('requests.Session')
    def test_auth_invoked(self, mock_session):
        client = Client(self.options)
        client.session.auth.return_value = True
        client.session.request.return_value.status_code = 200
        client.execute_request(action='list', path='')
        client.session.request.assert_any_call(auth=None, cert=None, data=None, headers={'Accept': '*/*', 'Depth': '1'}, method='PROPFIND', stream=True, timeout=30, url='http://localhost:8585', verify=True)

    @patch('requests.Session')
    def test_response_error_code(self, mock_session):
        client = Client(self.options)
        client.session.request.return_value.status_code = 400
        self.assertRaises(ResponseErrorCode, client.execute_request, action='list', path='')

    @patch('requests.Session')
    def test_method_not_supported(self, mock_session):
        client = Client(self.options)
        client.session.request.return_value.status_code = 405
        self.assertRaises(MethodNotSupported, client.execute_request, action='list', path='')

    @patch('requests.Session')
    def test_not_found(self, mock_session):
        client = Client(self.options)
        client.session.request.return_value.status_code = 404
        self.assertRaises(RemoteResourceNotFound, client.execute_request, action='list', path='')

    @patch('requests.Session')
    def test_not_enough_space(self, mock_session):
        client = Client(self.options)
        client.session.request.return_value.status_code = 507
        self.assertRaises(NotEnoughSpace, client.execute_request, action='list', path='')


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