|
1
|
|
|
import os |
|
2
|
|
|
from requests.utils import quote |
|
3
|
|
|
|
|
4
|
|
|
from .base import BaseTestCase, LocalNxcUserMixin |
|
5
|
|
|
from nextcloud.api_wrappers import WebDAV |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class TestWebDAV(LocalNxcUserMixin, BaseTestCase): |
|
9
|
|
|
|
|
10
|
|
|
CREATED_CODE = 201 |
|
11
|
|
|
NO_CONTENT_CODE = 204 |
|
12
|
|
|
MULTISTATUS_CODE = 207 |
|
13
|
|
|
ALREADY_EXISTS_CODE = 405 |
|
14
|
|
|
PRECONDITION_FAILED_CODE = 412 |
|
15
|
|
|
|
|
16
|
|
|
COLLECTION_TYPE = 'collection' |
|
17
|
|
|
|
|
18
|
|
|
def create_and_upload_file(self, file_name, file_content, timestamp=None): |
|
19
|
|
|
with open(file_name, "w") as f: |
|
20
|
|
|
f.write(file_content) |
|
21
|
|
|
file_local_path = os.path.abspath(file_name) |
|
22
|
|
|
return self.nxc_local.upload_file(self.user_username, file_local_path, file_name, timestamp) |
|
23
|
|
|
|
|
24
|
|
|
def test_list_folders(self): |
|
25
|
|
|
res = self.nxc_local.list_folders(self.user_username) |
|
26
|
|
|
assert res.is_ok |
|
27
|
|
|
assert isinstance(res.data, list) |
|
28
|
|
|
assert isinstance(res.data[0], dict) |
|
29
|
|
|
res = self.nxc_local.list_folders(self.user_username, all_properties=True) |
|
30
|
|
|
assert res.is_ok |
|
31
|
|
|
assert isinstance(res.data, list) |
|
32
|
|
|
assert isinstance(res.data[0], dict) |
|
33
|
|
|
|
|
34
|
|
|
def test_upload_download_file(self): |
|
35
|
|
|
file_name = "test_file" |
|
36
|
|
|
file_content = "test file content" |
|
37
|
|
|
file_local_path = os.path.join(os.getcwd(), file_name) |
|
38
|
|
|
res = self.create_and_upload_file(file_name, file_content) |
|
39
|
|
|
# check status code |
|
40
|
|
|
assert res.is_ok |
|
41
|
|
|
assert res.raw.status_code == self.CREATED_CODE |
|
42
|
|
|
|
|
43
|
|
|
# test uploaded file can be found with list_folders |
|
44
|
|
|
file_nextcloud_href = os.path.join(WebDAV.API_URL, self.user_username, file_name) |
|
45
|
|
|
folder_info = self.nxc_local.list_folders(self.user_username, path=file_name) |
|
46
|
|
|
assert folder_info.is_ok |
|
47
|
|
|
assert len(folder_info.data) == 1 |
|
48
|
|
|
assert isinstance(folder_info.data[0], dict) |
|
49
|
|
|
# check href |
|
50
|
|
|
assert folder_info.data[0]['href'] == file_nextcloud_href |
|
51
|
|
|
|
|
52
|
|
|
# remove file on local machine |
|
53
|
|
|
os.remove(file_local_path) |
|
54
|
|
|
self.nxc_local.download_file(self.user_username, file_name) |
|
55
|
|
|
# test file is downloaded to current dir |
|
56
|
|
|
assert file_name in os.listdir(".") |
|
57
|
|
|
with open(file_local_path, 'r') as f: |
|
58
|
|
|
downloaded_file_content = f.read() |
|
59
|
|
|
assert downloaded_file_content == file_content |
|
60
|
|
|
|
|
61
|
|
|
# delete file |
|
62
|
|
|
self.nxc_local.delete_path(self.user_username, file_name) |
|
63
|
|
|
os.remove(file_local_path) |
|
64
|
|
|
|
|
65
|
|
|
def test_upload_download_file_with_timestamp(self): |
|
66
|
|
|
file_name = "test_file" |
|
67
|
|
|
file_content = "test file content" |
|
68
|
|
|
file_local_path = os.path.join(os.getcwd(), file_name) |
|
69
|
|
|
|
|
70
|
|
|
# 2001-09-09T01:46:40 (UTC & GMT) |
|
71
|
|
|
timestamp = 1000000000 |
|
72
|
|
|
|
|
73
|
|
|
res = self.create_and_upload_file(file_name, file_content, timestamp) |
|
74
|
|
|
|
|
75
|
|
|
# check status code |
|
76
|
|
|
assert res.is_ok |
|
77
|
|
|
assert res.raw.status_code == self.CREATED_CODE |
|
78
|
|
|
|
|
79
|
|
|
# test uploaded file can be found with list_folders |
|
80
|
|
|
file_nextcloud_href = os.path.join(WebDAV.API_URL, self.user_username, file_name) |
|
81
|
|
|
folder_info = self.nxc_local.list_folders(self.user_username, path=file_name) |
|
82
|
|
|
|
|
83
|
|
|
assert folder_info.is_ok |
|
84
|
|
|
assert len(folder_info.data) == 1 |
|
85
|
|
|
assert isinstance(folder_info.data[0], dict) |
|
86
|
|
|
# check href |
|
87
|
|
|
assert folder_info.data[0]['href'] == file_nextcloud_href |
|
88
|
|
|
# test timestamp of uploaded file |
|
89
|
|
|
assert folder_info.data[0]["last_modified"] == "Sun, 09 Sep 2001 01:46:40 GMT" |
|
90
|
|
|
|
|
91
|
|
|
# remove file on local machine |
|
92
|
|
|
os.remove(file_local_path) |
|
93
|
|
|
self.nxc_local.download_file(self.user_username, file_name) |
|
94
|
|
|
|
|
95
|
|
|
# test file is downloaded to current dir |
|
96
|
|
|
assert file_name in os.listdir(".") |
|
97
|
|
|
with open(file_local_path, 'r') as f: |
|
98
|
|
|
downloaded_file_content = f.read() |
|
99
|
|
|
assert downloaded_file_content == file_content |
|
100
|
|
|
|
|
101
|
|
|
# test timestamp of downloaded file |
|
102
|
|
|
downloaded_file_timestamp = os.path.getmtime(file_local_path) |
|
103
|
|
|
assert downloaded_file_timestamp == timestamp |
|
104
|
|
|
|
|
105
|
|
|
# delete file |
|
106
|
|
|
self.nxc_local.delete_path(self.user_username, file_name) |
|
107
|
|
|
os.remove(file_local_path) |
|
108
|
|
|
|
|
109
|
|
|
def test_create_folder(self): |
|
110
|
|
|
folder_name = "test folder5" |
|
111
|
|
|
res = self.nxc_local.create_folder(self.user_username, folder_name) |
|
112
|
|
|
assert res.is_ok |
|
113
|
|
|
assert res.raw.status_code == self.CREATED_CODE |
|
114
|
|
|
|
|
115
|
|
|
# test uploaded file can be found with list_folders |
|
116
|
|
|
file_nextcloud_href = quote(os.path.join(WebDAV.API_URL, self.user_username, folder_name)) + "/" |
|
117
|
|
|
folder_info = self.nxc_local.list_folders(self.user_username, path=folder_name) |
|
118
|
|
|
assert folder_info.is_ok |
|
119
|
|
|
assert len(folder_info.data) == 1 |
|
120
|
|
|
assert isinstance(folder_info.data[0], dict) |
|
121
|
|
|
# check href |
|
122
|
|
|
assert folder_info.data[0]['href'] == file_nextcloud_href |
|
123
|
|
|
# check that created file type is a collection |
|
124
|
|
|
assert folder_info.data[0]['resource_type'] == self.COLLECTION_TYPE |
|
125
|
|
|
|
|
126
|
|
|
nested_folder_name = "test folder5/nested/folder" |
|
127
|
|
|
res = self.nxc_local.assure_tree_exists(self.user_username, nested_folder_name) |
|
128
|
|
|
folder_info = self.nxc_local.list_folders(self.user_username, path=nested_folder_name) |
|
129
|
|
|
assert folder_info.is_ok |
|
130
|
|
|
assert len(folder_info.data) == 1 |
|
131
|
|
|
|
|
132
|
|
|
# check 405 status code if location already exists |
|
133
|
|
|
res = self.nxc_local.create_folder(self.user_username, folder_name) |
|
134
|
|
|
assert not res.is_ok |
|
135
|
|
|
assert res.raw.status_code == self.ALREADY_EXISTS_CODE |
|
136
|
|
|
|
|
137
|
|
|
# delete folder |
|
138
|
|
|
res = self.nxc_local.delete_path(self.user_username, folder_name) |
|
139
|
|
|
assert res.is_ok |
|
140
|
|
|
assert res.raw.status_code == self.NO_CONTENT_CODE |
|
141
|
|
|
|
|
142
|
|
|
def test_delete_path(self): |
|
143
|
|
|
# test delete empty folder |
|
144
|
|
|
new_path_name = "path_to_delete" |
|
145
|
|
|
self.nxc_local.create_folder(self.user_username, new_path_name) |
|
146
|
|
|
res = self.nxc_local.delete_path(self.user_username, new_path_name) |
|
147
|
|
|
assert res.raw.status_code == self.NO_CONTENT_CODE |
|
148
|
|
|
assert res.is_ok |
|
149
|
|
|
res = self.nxc_local.list_folders(self.user_username, new_path_name) |
|
150
|
|
|
assert res.data is None |
|
151
|
|
|
|
|
152
|
|
|
# test delete file |
|
153
|
|
|
# create file at first |
|
154
|
|
|
file_name = "test_file" |
|
155
|
|
|
file_content = "test file content" |
|
156
|
|
|
with open(file_name, "w") as f: |
|
157
|
|
|
f.write(file_content) |
|
158
|
|
|
file_local_path = os.path.abspath(file_name) |
|
159
|
|
|
self.nxc_local.upload_file(self.user_username, file_local_path, file_name) |
|
160
|
|
|
# delete file |
|
161
|
|
|
res = self.nxc_local.delete_path(self.user_username, file_name) |
|
162
|
|
|
assert res.raw.status_code == self.NO_CONTENT_CODE |
|
163
|
|
|
assert res.is_ok |
|
164
|
|
|
res = self.nxc_local.list_folders(self.user_username, new_path_name) |
|
165
|
|
|
assert res.data is None |
|
166
|
|
|
|
|
167
|
|
|
# test delete nonexistent file |
|
168
|
|
|
res = self.nxc_local.delete_path(self.user_username, file_name) |
|
169
|
|
|
assert res.raw.status_code == self.NOT_FOUND_CODE |
|
170
|
|
|
assert not res.is_ok |
|
171
|
|
|
|
|
172
|
|
View Code Duplication |
def test_copy_path(self): |
|
|
|
|
|
|
173
|
|
|
# create a file to copy |
|
174
|
|
|
file_name = "test_file" |
|
175
|
|
|
file_content = "test file content" |
|
176
|
|
|
self.create_and_upload_file(file_name, file_content) |
|
177
|
|
|
|
|
178
|
|
|
# copy file |
|
179
|
|
|
destination_path = "new_test_file_location" |
|
180
|
|
|
res = self.nxc_local.copy_path(self.user_username, file_name, destination_path) |
|
181
|
|
|
assert res.raw.status_code == self.CREATED_CODE |
|
182
|
|
|
assert res.is_ok |
|
183
|
|
|
# check both file exist |
|
184
|
|
|
original_file_props = self.nxc_local.list_folders(self.user_username, file_name) |
|
185
|
|
|
copy_props = self.nxc_local.list_folders(self.user_username, destination_path) |
|
186
|
|
|
assert len(original_file_props.data) == 1 |
|
187
|
|
|
assert len(copy_props.data) == 1 |
|
188
|
|
|
|
|
189
|
|
|
# copy file to already exist location |
|
190
|
|
|
# create new file |
|
191
|
|
|
new_file_name = 'test_file_2' |
|
192
|
|
|
new_file_content = 'test_file_3' |
|
193
|
|
|
self.create_and_upload_file(new_file_name, new_file_content) |
|
194
|
|
|
res = self.nxc_local.copy_path(self.user_username, file_name, new_file_name) |
|
195
|
|
|
assert not res.is_ok |
|
196
|
|
|
assert res.raw.status_code == self.PRECONDITION_FAILED_CODE |
|
197
|
|
|
# copy with overriding |
|
198
|
|
|
res = self.nxc_local.copy_path(self.user_username, file_name, new_file_name, overwrite=True) |
|
199
|
|
|
assert res.is_ok |
|
200
|
|
|
assert res.raw.status_code == self.NO_CONTENT_CODE |
|
201
|
|
|
|
|
202
|
|
|
# download just copied file and check content |
|
203
|
|
|
os.remove(os.path.join(os.getcwd(), new_file_name)) # remove file locally to download it |
|
204
|
|
|
self.nxc_local.download_file(self.user_username, new_file_name) |
|
205
|
|
|
with open(new_file_name, 'r') as f: |
|
206
|
|
|
downloaded_file_content = f.read() |
|
207
|
|
|
assert downloaded_file_content == file_content |
|
208
|
|
|
assert downloaded_file_content != new_file_content |
|
209
|
|
|
|
|
210
|
|
View Code Duplication |
def test_move_path(self): |
|
|
|
|
|
|
211
|
|
|
# create a file to move |
|
212
|
|
|
file_name = "test_move_file 🇳🇴 😗 🇫🇴 🇦🇽" |
|
213
|
|
|
file_content = "test move file content 🇳🇴 😗 🇫🇴 🇦🇽" |
|
214
|
|
|
self.create_and_upload_file(file_name, file_content) |
|
215
|
|
|
|
|
216
|
|
|
# move file |
|
217
|
|
|
destination_path = "new_test_move_file_location 🇳🇴 😗 🇫🇴 🇦🇽" |
|
218
|
|
|
res = self.nxc_local.move_path(self.user_username, file_name, destination_path) |
|
219
|
|
|
assert res.is_ok |
|
220
|
|
|
assert res.raw.status_code == self.CREATED_CODE |
|
221
|
|
|
# check only new file exist |
|
222
|
|
|
original_file_props = self.nxc_local.list_folders(self.user_username, file_name) |
|
223
|
|
|
moved_file = self.nxc_local.list_folders(self.user_username, destination_path) |
|
224
|
|
|
assert original_file_props.data is None |
|
225
|
|
|
assert len(moved_file.data) == 1 |
|
226
|
|
|
|
|
227
|
|
|
# copy file to already exist location |
|
228
|
|
|
|
|
229
|
|
|
# create a file to move |
|
230
|
|
|
file_name = "test_move_file 🇳🇴 😗 🇫🇴 🇦🇽" |
|
231
|
|
|
file_content = "test move file content 🇳🇴 😗 🇫🇴 🇦🇽" |
|
232
|
|
|
self.create_and_upload_file(file_name, file_content) |
|
233
|
|
|
|
|
234
|
|
|
# create new file for conflict |
|
235
|
|
|
new_file_name = 'test_move_file_ 🇳🇴 😗 🇫🇴 🇦🇽' |
|
236
|
|
|
new_file_content = 'test_move_file_ 🇳🇴 😗 🇫🇴 🇦🇽' |
|
237
|
|
|
self.create_and_upload_file(new_file_name, new_file_content) |
|
238
|
|
|
|
|
239
|
|
|
# move file to the new file location |
|
240
|
|
|
res = self.nxc_local.move_path(self.user_username, file_name, new_file_name) |
|
241
|
|
|
assert not res.is_ok |
|
242
|
|
|
assert res.raw.status_code == self.PRECONDITION_FAILED_CODE |
|
243
|
|
|
# move with overriding |
|
244
|
|
|
res = self.nxc_local.move_path(self.user_username, file_name, new_file_name, overwrite=True) |
|
245
|
|
|
assert res.is_ok |
|
246
|
|
|
assert res.raw.status_code == self.NO_CONTENT_CODE |
|
247
|
|
|
|
|
248
|
|
|
# download just copied file and check content |
|
249
|
|
|
os.remove(os.path.join(os.getcwd(), new_file_name)) # remove file locally to download it |
|
250
|
|
|
self.nxc_local.download_file(self.user_username, new_file_name) |
|
251
|
|
|
with open(new_file_name, 'r') as f: |
|
252
|
|
|
downloaded_file_content = f.read() |
|
253
|
|
|
assert downloaded_file_content == file_content |
|
254
|
|
|
assert downloaded_file_content != new_file_content |
|
255
|
|
|
|
|
256
|
|
|
def test_set_list_favorites(self): |
|
257
|
|
|
# create new file to make favorite |
|
258
|
|
|
file_name = "test_favorite" |
|
259
|
|
|
file_content = "test favorite content" |
|
260
|
|
|
self.create_and_upload_file(file_name, file_content) |
|
261
|
|
|
file_nextcloud_href = os.path.join(WebDAV.API_URL, self.user_username, file_name) |
|
262
|
|
|
|
|
263
|
|
|
# get favorites |
|
264
|
|
|
res = self.nxc_local.list_favorites(self.user_username) |
|
265
|
|
|
assert len(res.data) == 0 |
|
266
|
|
|
|
|
267
|
|
|
# set file as favorite |
|
268
|
|
|
res = self.nxc_local.set_favorites(self.user_username, file_name) |
|
269
|
|
|
assert res.is_ok |
|
270
|
|
|
assert res.raw.status_code == self.MULTISTATUS_CODE |
|
271
|
|
|
|
|
272
|
|
|
# check file is in favorites |
|
273
|
|
|
res = self.nxc_local.list_favorites(self.user_username) |
|
274
|
|
|
assert res.is_ok |
|
275
|
|
|
assert len(res.data) == 1 |
|
276
|
|
|
assert res.data[0]['href'] == file_nextcloud_href |
|
277
|
|
|
|
|
278
|
|
|
|