|
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): |
|
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) |
|
23
|
|
|
|
|
24
|
|
|
def test_list_folders(self): |
|
25
|
|
|
res = self.nxc_local.list_folders(self.user_username) |
|
26
|
|
|
assert isinstance(res, list) |
|
27
|
|
|
assert isinstance(res[0], dict) |
|
28
|
|
|
res = self.nxc_local.list_folders(self.user_username, all_properties=True) |
|
29
|
|
|
assert isinstance(res, list) |
|
30
|
|
|
assert isinstance(res[0], dict) |
|
31
|
|
|
|
|
32
|
|
|
def test_upload_download_file(self): |
|
33
|
|
|
file_name = "test_file" |
|
34
|
|
|
file_content = "test file content" |
|
35
|
|
|
file_local_path = os.path.join(os.getcwd(), file_name) |
|
36
|
|
|
res = self.create_and_upload_file(file_name, file_content) |
|
37
|
|
|
# check status code |
|
38
|
|
|
assert res.raw.status_code == self.CREATED_CODE |
|
39
|
|
|
|
|
40
|
|
|
# test uploaded file can be found with list_folders |
|
41
|
|
|
file_nextcloud_href = os.path.join(WebDAV.API_URL, self.user_username, file_name) |
|
42
|
|
|
folder_info = self.nxc_local.list_folders(self.user_username, path=file_name) |
|
43
|
|
|
assert len(folder_info) == 1 |
|
44
|
|
|
assert isinstance(folder_info[0], dict) |
|
45
|
|
|
# check href |
|
46
|
|
|
assert folder_info[0]['href'] == file_nextcloud_href |
|
47
|
|
|
|
|
48
|
|
|
# remove file on local machine |
|
49
|
|
|
os.remove(file_local_path) |
|
50
|
|
|
self.nxc_local.download_file(self.user_username, file_name) |
|
51
|
|
|
# test file is downloaded to current dir |
|
52
|
|
|
assert file_name in os.listdir(".") |
|
53
|
|
|
with open(file_local_path, 'r') as f: |
|
54
|
|
|
downloaded_file_content = f.read() |
|
55
|
|
|
assert downloaded_file_content == file_content |
|
56
|
|
|
|
|
57
|
|
|
# delete file |
|
58
|
|
|
self.nxc_local.delete_path(self.user_username, file_name) |
|
59
|
|
|
os.remove(file_local_path) |
|
60
|
|
|
|
|
61
|
|
|
def test_create_folder(self): |
|
62
|
|
|
folder_name = "test folder5" |
|
63
|
|
|
res = self.nxc_local.create_folder(self.user_username, folder_name) |
|
64
|
|
|
assert res.raw.status_code == self.CREATED_CODE |
|
65
|
|
|
|
|
66
|
|
|
# test uploaded file can be found with list_folders |
|
67
|
|
|
file_nextcloud_href = quote(os.path.join(WebDAV.API_URL, self.user_username, folder_name)) + "/" |
|
68
|
|
|
folder_info = self.nxc_local.list_folders(self.user_username, path=folder_name) |
|
69
|
|
|
assert len(folder_info) == 1 |
|
70
|
|
|
assert isinstance(folder_info[0], dict) |
|
71
|
|
|
# check href |
|
72
|
|
|
assert folder_info[0]['href'] == file_nextcloud_href |
|
73
|
|
|
# check that created file type is a collection |
|
74
|
|
|
assert folder_info[0]['resource_type'] == self.COLLECTION_TYPE |
|
75
|
|
|
|
|
76
|
|
|
# check 405 status code if location already exists |
|
77
|
|
|
res = self.nxc_local.create_folder(self.user_username, folder_name) |
|
78
|
|
|
assert res.raw.status_code == self.ALREADY_EXISTS_CODE |
|
79
|
|
|
|
|
80
|
|
|
# delete folder |
|
81
|
|
|
res = self.nxc_local.delete_path(self.user_username, folder_name) |
|
82
|
|
|
assert res.raw.status_code == self.NO_CONTENT_CODE |
|
83
|
|
|
|
|
84
|
|
|
def test_delete_path(self): |
|
85
|
|
|
# test delete empty folder |
|
86
|
|
|
new_path_name = "path_to_delete" |
|
87
|
|
|
self.nxc_local.create_folder(self.user_username, new_path_name) |
|
88
|
|
|
res = self.nxc_local.delete_path(self.user_username, new_path_name) |
|
89
|
|
|
assert res.raw.status_code == self.NO_CONTENT_CODE |
|
90
|
|
|
res = self.nxc_local.list_folders(self.user_username, new_path_name) |
|
91
|
|
|
assert len(res) == 0 |
|
92
|
|
|
|
|
93
|
|
|
# test delete file |
|
94
|
|
|
# create file at first |
|
95
|
|
|
file_name = "test_file" |
|
96
|
|
|
file_content = "test file content" |
|
97
|
|
|
with open(file_name, "w") as f: |
|
98
|
|
|
f.write(file_content) |
|
99
|
|
|
file_local_path = os.path.abspath(file_name) |
|
100
|
|
|
self.nxc_local.upload_file(self.user_username, file_local_path, file_name) |
|
101
|
|
|
# delete file |
|
102
|
|
|
res = self.nxc_local.delete_path(self.user_username, file_name) |
|
103
|
|
|
assert res.raw.status_code == self.NO_CONTENT_CODE |
|
104
|
|
|
res = self.nxc_local.list_folders(self.user_username, new_path_name) |
|
105
|
|
|
assert len(res) == 0 |
|
106
|
|
|
|
|
107
|
|
|
# test delete nonexistent file |
|
108
|
|
|
res = self.nxc_local.delete_path(self.user_username, file_name) |
|
109
|
|
|
assert res.raw.status_code == 404 |
|
110
|
|
|
|
|
111
|
|
View Code Duplication |
def test_copy_path(self): |
|
|
|
|
|
|
112
|
|
|
# create a file to copy |
|
113
|
|
|
file_name = "test_file" |
|
114
|
|
|
file_content = "test file content" |
|
115
|
|
|
self.create_and_upload_file(file_name, file_content) |
|
116
|
|
|
|
|
117
|
|
|
# copy file |
|
118
|
|
|
destination_path = "new_test_file_location" |
|
119
|
|
|
res = self.nxc_local.copy_path(self.user_username, file_name, destination_path) |
|
120
|
|
|
assert res.raw.status_code == self.CREATED_CODE |
|
121
|
|
|
# check both file exist |
|
122
|
|
|
original_file_props = self.nxc_local.list_folders(self.user_username, file_name) |
|
123
|
|
|
copy_props = self.nxc_local.list_folders(self.user_username, destination_path) |
|
124
|
|
|
assert len(original_file_props) == 1 and len(copy_props) == 1 |
|
125
|
|
|
|
|
126
|
|
|
# copy file to already exist location |
|
127
|
|
|
# create new file |
|
128
|
|
|
new_file_name = 'test_file_2' |
|
129
|
|
|
new_file_content = 'test_file_3' |
|
130
|
|
|
self.create_and_upload_file(new_file_name, new_file_content) |
|
131
|
|
|
res = self.nxc_local.copy_path(self.user_username, file_name, new_file_name) |
|
132
|
|
|
assert res.raw.status_code == self.PRECONDITION_FAILED_CODE |
|
133
|
|
|
# copy with overriding |
|
134
|
|
|
res = self.nxc_local.copy_path(self.user_username, file_name, new_file_name, overwrite=True) |
|
135
|
|
|
assert res.raw.status_code == self.NO_CONTENT_CODE |
|
136
|
|
|
|
|
137
|
|
|
# download just copied file and check content |
|
138
|
|
|
os.remove(os.path.join(os.getcwd(), new_file_name)) # remove file locally to download it |
|
139
|
|
|
self.nxc_local.download_file(self.user_username, new_file_name) |
|
140
|
|
|
with open(new_file_name, 'r') as f: |
|
141
|
|
|
downloaded_file_content = f.read() |
|
142
|
|
|
assert downloaded_file_content == file_content |
|
143
|
|
|
assert downloaded_file_content != new_file_content |
|
144
|
|
|
|
|
145
|
|
View Code Duplication |
def test_move_path(self): |
|
|
|
|
|
|
146
|
|
|
# create a file to move |
|
147
|
|
|
file_name = "test_move_file" |
|
148
|
|
|
file_content = "test move file content" |
|
149
|
|
|
self.create_and_upload_file(file_name, file_content) |
|
150
|
|
|
|
|
151
|
|
|
# move file |
|
152
|
|
|
destination_path = "new_test_move_file_location" |
|
153
|
|
|
res = self.nxc_local.move_path(self.user_username, file_name, destination_path) |
|
154
|
|
|
assert res.raw.status_code == self.CREATED_CODE |
|
155
|
|
|
# check only new file exist |
|
156
|
|
|
original_file_props = self.nxc_local.list_folders(self.user_username, file_name) |
|
157
|
|
|
moved_file = self.nxc_local.list_folders(self.user_username, destination_path) |
|
158
|
|
|
assert len(original_file_props) == 0 and len(moved_file) == 1 |
|
159
|
|
|
|
|
160
|
|
|
# copy file to already exist location |
|
161
|
|
|
|
|
162
|
|
|
# create a file to move |
|
163
|
|
|
file_name = "test_move_file" |
|
164
|
|
|
file_content = "test move file content" |
|
165
|
|
|
self.create_and_upload_file(file_name, file_content) |
|
166
|
|
|
|
|
167
|
|
|
# create new file for conflict |
|
168
|
|
|
new_file_name = 'test_move_file_' |
|
169
|
|
|
new_file_content = 'test_move_file_' |
|
170
|
|
|
self.create_and_upload_file(new_file_name, new_file_content) |
|
171
|
|
|
|
|
172
|
|
|
# move file to the new file location |
|
173
|
|
|
res = self.nxc_local.move_path(self.user_username, file_name, new_file_name) |
|
174
|
|
|
assert res.raw.status_code == self.PRECONDITION_FAILED_CODE |
|
175
|
|
|
# move with overriding |
|
176
|
|
|
res = self.nxc_local.move_path(self.user_username, file_name, new_file_name, overwrite=True) |
|
177
|
|
|
assert res.raw.status_code == self.NO_CONTENT_CODE |
|
178
|
|
|
|
|
179
|
|
|
# download just copied file and check content |
|
180
|
|
|
os.remove(os.path.join(os.getcwd(), new_file_name)) # remove file locally to download it |
|
181
|
|
|
self.nxc_local.download_file(self.user_username, new_file_name) |
|
182
|
|
|
with open(new_file_name, 'r') as f: |
|
183
|
|
|
downloaded_file_content = f.read() |
|
184
|
|
|
assert downloaded_file_content == file_content |
|
185
|
|
|
assert downloaded_file_content != new_file_content |
|
186
|
|
|
|
|
187
|
|
|
def test_set_list_favorites(self): |
|
188
|
|
|
# create new file to make favorite |
|
189
|
|
|
file_name = "test_favorite" |
|
190
|
|
|
file_content = "test favorite content" |
|
191
|
|
|
self.create_and_upload_file(file_name, file_content) |
|
192
|
|
|
file_nextcloud_href = os.path.join(WebDAV.API_URL, self.user_username, file_name) |
|
193
|
|
|
|
|
194
|
|
|
# get favorites |
|
195
|
|
|
res = self.nxc_local.list_favorites(self.user_username) |
|
196
|
|
|
assert len(res) == 0 |
|
197
|
|
|
|
|
198
|
|
|
# set file as favorite |
|
199
|
|
|
res = self.nxc_local.set_favorites(self.user_username, file_name) |
|
200
|
|
|
assert res.raw.status_code == self.MULTISTATUS_CODE |
|
201
|
|
|
|
|
202
|
|
|
# check file is in favorites |
|
203
|
|
|
res = self.nxc_local.list_favorites(self.user_username) |
|
204
|
|
|
assert len(res) == 1 |
|
205
|
|
|
assert res[0]['href'] == file_nextcloud_href |
|
206
|
|
|
|
|
207
|
|
|
|