forked from EnterpriseyIntranet/nextcloud-API
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_webdav.py
227 lines (193 loc) Β· 9.57 KB
/
test_webdav.py
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
import os
from requests.utils import quote
from .base import BaseTestCase, LocalNxcUserMixin
from nextcloud.api_wrappers import WebDAV
class TestWebDAV(LocalNxcUserMixin, BaseTestCase):
CREATED_CODE = 201
NO_CONTENT_CODE = 204
MULTISTATUS_CODE = 207
ALREADY_EXISTS_CODE = 405
PRECONDITION_FAILED_CODE = 412
COLLECTION_TYPE = 'collection'
def create_and_upload_file(self, file_name, file_content):
with open(file_name, "w") as f:
f.write(file_content)
file_local_path = os.path.abspath(file_name)
return self.nxc_local.upload_file(self.user_username, file_local_path, file_name)
def test_list_folders(self):
res = self.nxc_local.list_folders(self.user_username)
assert res.is_ok
assert isinstance(res.data, list)
assert isinstance(res.data[0], dict)
res = self.nxc_local.list_folders(self.user_username, all_properties=True)
assert res.is_ok
assert isinstance(res.data, list)
assert isinstance(res.data[0], dict)
def test_upload_download_file(self):
file_name = "test_file"
file_content = "test file content"
file_local_path = os.path.join(os.getcwd(), file_name)
res = self.create_and_upload_file(file_name, file_content)
# check status code
assert res.is_ok
assert res.raw.status_code == self.CREATED_CODE
# test uploaded file can be found with list_folders
file_nextcloud_href = os.path.join(WebDAV.API_URL, self.user_username, file_name)
folder_info = self.nxc_local.list_folders(self.user_username, path=file_name)
assert folder_info.is_ok
assert len(folder_info.data) == 1
assert isinstance(folder_info.data[0], dict)
# check href
assert folder_info.data[0]['href'] == file_nextcloud_href
# remove file on local machine
os.remove(file_local_path)
self.nxc_local.download_file(self.user_username, file_name)
# test file is downloaded to current dir
assert file_name in os.listdir(".")
with open(file_local_path, 'r') as f:
downloaded_file_content = f.read()
assert downloaded_file_content == file_content
# delete file
self.nxc_local.delete_path(self.user_username, file_name)
os.remove(file_local_path)
def test_create_folder(self):
folder_name = "test folder5"
res = self.nxc_local.create_folder(self.user_username, folder_name)
assert res.is_ok
assert res.raw.status_code == self.CREATED_CODE
# test uploaded file can be found with list_folders
file_nextcloud_href = quote(os.path.join(WebDAV.API_URL, self.user_username, folder_name)) + "/"
folder_info = self.nxc_local.list_folders(self.user_username, path=folder_name)
assert folder_info.is_ok
assert len(folder_info.data) == 1
assert isinstance(folder_info.data[0], dict)
# check href
assert folder_info.data[0]['href'] == file_nextcloud_href
# check that created file type is a collection
assert folder_info.data[0]['resource_type'] == self.COLLECTION_TYPE
# check 405 status code if location already exists
res = self.nxc_local.create_folder(self.user_username, folder_name)
assert not res.is_ok
assert res.raw.status_code == self.ALREADY_EXISTS_CODE
# delete folder
res = self.nxc_local.delete_path(self.user_username, folder_name)
assert res.is_ok
assert res.raw.status_code == self.NO_CONTENT_CODE
def test_delete_path(self):
# test delete empty folder
new_path_name = "path_to_delete"
self.nxc_local.create_folder(self.user_username, new_path_name)
res = self.nxc_local.delete_path(self.user_username, new_path_name)
assert res.raw.status_code == self.NO_CONTENT_CODE
assert res.is_ok
res = self.nxc_local.list_folders(self.user_username, new_path_name)
assert res.data is None
# test delete file
# create file at first
file_name = "test_file"
file_content = "test file content"
with open(file_name, "w") as f:
f.write(file_content)
file_local_path = os.path.abspath(file_name)
self.nxc_local.upload_file(self.user_username, file_local_path, file_name)
# delete file
res = self.nxc_local.delete_path(self.user_username, file_name)
assert res.raw.status_code == self.NO_CONTENT_CODE
assert res.is_ok
res = self.nxc_local.list_folders(self.user_username, new_path_name)
assert res.data is None
# test delete nonexistent file
res = self.nxc_local.delete_path(self.user_username, file_name)
assert res.raw.status_code == self.NOT_FOUND_CODE
assert not res.is_ok
def test_copy_path(self):
# create a file to copy
file_name = "test_file"
file_content = "test file content"
self.create_and_upload_file(file_name, file_content)
# copy file
destination_path = "new_test_file_location"
res = self.nxc_local.copy_path(self.user_username, file_name, destination_path)
assert res.raw.status_code == self.CREATED_CODE
assert res.is_ok
# check both file exist
original_file_props = self.nxc_local.list_folders(self.user_username, file_name)
copy_props = self.nxc_local.list_folders(self.user_username, destination_path)
assert len(original_file_props.data) == 1
assert len(copy_props.data) == 1
# copy file to already exist location
# create new file
new_file_name = 'test_file_2'
new_file_content = 'test_file_3'
self.create_and_upload_file(new_file_name, new_file_content)
res = self.nxc_local.copy_path(self.user_username, file_name, new_file_name)
assert not res.is_ok
assert res.raw.status_code == self.PRECONDITION_FAILED_CODE
# copy with overriding
res = self.nxc_local.copy_path(self.user_username, file_name, new_file_name, overwrite=True)
assert res.is_ok
assert res.raw.status_code == self.NO_CONTENT_CODE
# download just copied file and check content
os.remove(os.path.join(os.getcwd(), new_file_name)) # remove file locally to download it
self.nxc_local.download_file(self.user_username, new_file_name)
with open(new_file_name, 'r') as f:
downloaded_file_content = f.read()
assert downloaded_file_content == file_content
assert downloaded_file_content != new_file_content
def test_move_path(self):
# create a file to move
file_name = "test_move_file π³π΄ π π«π΄ π¦π½"
file_content = "test move file content π³π΄ π π«π΄ π¦π½"
self.create_and_upload_file(file_name, file_content)
# move file
destination_path = "new_test_move_file_location π³π΄ π π«π΄ π¦π½"
res = self.nxc_local.move_path(self.user_username, file_name, destination_path)
assert res.is_ok
assert res.raw.status_code == self.CREATED_CODE
# check only new file exist
original_file_props = self.nxc_local.list_folders(self.user_username, file_name)
moved_file = self.nxc_local.list_folders(self.user_username, destination_path)
assert original_file_props.data is None
assert len(moved_file.data) == 1
# copy file to already exist location
# create a file to move
file_name = "test_move_file π³π΄ π π«π΄ π¦π½"
file_content = "test move file content π³π΄ π π«π΄ π¦π½"
self.create_and_upload_file(file_name, file_content)
# create new file for conflict
new_file_name = 'test_move_file_ π³π΄ π π«π΄ π¦π½'
new_file_content = 'test_move_file_ π³π΄ π π«π΄ π¦π½'
self.create_and_upload_file(new_file_name, new_file_content)
# move file to the new file location
res = self.nxc_local.move_path(self.user_username, file_name, new_file_name)
assert not res.is_ok
assert res.raw.status_code == self.PRECONDITION_FAILED_CODE
# move with overriding
res = self.nxc_local.move_path(self.user_username, file_name, new_file_name, overwrite=True)
assert res.is_ok
assert res.raw.status_code == self.NO_CONTENT_CODE
# download just copied file and check content
os.remove(os.path.join(os.getcwd(), new_file_name)) # remove file locally to download it
self.nxc_local.download_file(self.user_username, new_file_name)
with open(new_file_name, 'r') as f:
downloaded_file_content = f.read()
assert downloaded_file_content == file_content
assert downloaded_file_content != new_file_content
def test_set_list_favorites(self):
# create new file to make favorite
file_name = "test_favorite"
file_content = "test favorite content"
self.create_and_upload_file(file_name, file_content)
file_nextcloud_href = os.path.join(WebDAV.API_URL, self.user_username, file_name)
# get favorites
res = self.nxc_local.list_favorites(self.user_username)
assert len(res.data) == 0
# set file as favorite
res = self.nxc_local.set_favorites(self.user_username, file_name)
assert res.is_ok
assert res.raw.status_code == self.MULTISTATUS_CODE
# check file is in favorites
res = self.nxc_local.list_favorites(self.user_username)
assert res.is_ok
assert len(res.data) == 1
assert res.data[0]['href'] == file_nextcloud_href