forked from cloud-py-api/nc_py_api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles_sharing_test.py
More file actions
435 lines (373 loc) · 19.1 KB
/
files_sharing_test.py
File metadata and controls
435 lines (373 loc) · 19.1 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import datetime
from os import environ
import pytest
from nc_py_api import (
FilePermissions,
FsNode,
Nextcloud,
NextcloudException,
NextcloudExceptionNotFound,
ShareType,
)
from nc_py_api.files.sharing import Share
def test_available(nc_any):
assert nc_any.files.sharing.available
@pytest.mark.asyncio(scope="session")
async def test_available_async(anc_any):
assert await anc_any.files.sharing.available
def test_create_delete(nc_any):
new_share = nc_any.files.sharing.create("test_12345_text.txt", ShareType.TYPE_LINK)
nc_any.files.sharing.delete(new_share)
with pytest.raises(NextcloudExceptionNotFound):
nc_any.files.sharing.delete(new_share)
@pytest.mark.asyncio(scope="session")
async def test_create_delete_async(anc_any):
new_share = await anc_any.files.sharing.create("test_12345_text.txt", ShareType.TYPE_LINK)
await anc_any.files.sharing.delete(new_share)
with pytest.raises(NextcloudExceptionNotFound):
await anc_any.files.sharing.delete(new_share)
def _test_share_fields(new_share: Share, get_by_id: Share, shared_file: FsNode):
assert new_share.share_type == ShareType.TYPE_LINK
assert not new_share.label
assert not new_share.note
assert new_share.mimetype.find("text") != -1
assert new_share.permissions & FilePermissions.PERMISSION_READ
assert new_share.url
assert new_share.path == shared_file.user_path
assert new_share.token
assert get_by_id.token == new_share.token
assert get_by_id.share_id == new_share.share_id
assert get_by_id.path == new_share.path
assert get_by_id.mimetype == new_share.mimetype
assert get_by_id.share_type == new_share.share_type
assert get_by_id.file_owner == new_share.file_owner
assert get_by_id.share_owner == new_share.share_owner
assert not get_by_id.share_with
assert str(get_by_id) == str(new_share)
assert get_by_id.file_source_id == shared_file.info.fileid
assert get_by_id.can_delete is True
assert get_by_id.can_edit is True
def test_share_fields(nc_any):
shared_file = nc_any.files.by_path("test_12345_text.txt")
new_share = nc_any.files.sharing.create(shared_file, ShareType.TYPE_LINK, FilePermissions.PERMISSION_READ)
try:
get_by_id = nc_any.files.sharing.get_by_id(new_share.share_id)
_test_share_fields(new_share, get_by_id, shared_file)
finally:
nc_any.files.sharing.delete(new_share)
@pytest.mark.asyncio(scope="session")
async def test_share_fields_async(anc_any):
shared_file = await anc_any.files.by_path("test_12345_text.txt")
new_share = await anc_any.files.sharing.create(shared_file, ShareType.TYPE_LINK, FilePermissions.PERMISSION_READ)
try:
get_by_id = await anc_any.files.sharing.get_by_id(new_share.share_id)
_test_share_fields(new_share, get_by_id, shared_file)
finally:
await anc_any.files.sharing.delete(new_share)
def test_create_permissions(nc_any):
new_share = nc_any.files.sharing.create("test_empty_dir", ShareType.TYPE_LINK, FilePermissions.PERMISSION_CREATE)
nc_any.files.sharing.delete(new_share)
assert (new_share.permissions & FilePermissions.PERMISSION_CREATE) == FilePermissions.PERMISSION_CREATE
new_share = nc_any.files.sharing.create(
"test_empty_dir",
ShareType.TYPE_LINK,
FilePermissions.PERMISSION_CREATE + FilePermissions.PERMISSION_READ + FilePermissions.PERMISSION_DELETE,
)
nc_any.files.sharing.delete(new_share)
assert (new_share.permissions & FilePermissions.PERMISSION_DELETE) == FilePermissions.PERMISSION_DELETE
new_share = nc_any.files.sharing.create(
"test_empty_dir",
ShareType.TYPE_LINK,
FilePermissions.PERMISSION_CREATE + FilePermissions.PERMISSION_READ + FilePermissions.PERMISSION_UPDATE,
)
nc_any.files.sharing.delete(new_share)
assert (new_share.permissions & FilePermissions.PERMISSION_UPDATE) == FilePermissions.PERMISSION_UPDATE
@pytest.mark.asyncio(scope="session")
async def test_create_permissions_async(anc_any):
new_share = await anc_any.files.sharing.create(
"test_empty_dir", ShareType.TYPE_LINK, FilePermissions.PERMISSION_CREATE
)
await anc_any.files.sharing.delete(new_share)
assert (new_share.permissions & FilePermissions.PERMISSION_CREATE) == FilePermissions.PERMISSION_CREATE
new_share = await anc_any.files.sharing.create(
"test_empty_dir",
ShareType.TYPE_LINK,
FilePermissions.PERMISSION_CREATE + FilePermissions.PERMISSION_READ + FilePermissions.PERMISSION_DELETE,
)
await anc_any.files.sharing.delete(new_share)
assert (new_share.permissions & FilePermissions.PERMISSION_DELETE) == FilePermissions.PERMISSION_DELETE
new_share = await anc_any.files.sharing.create(
"test_empty_dir",
ShareType.TYPE_LINK,
FilePermissions.PERMISSION_CREATE + FilePermissions.PERMISSION_READ + FilePermissions.PERMISSION_UPDATE,
)
await anc_any.files.sharing.delete(new_share)
assert (new_share.permissions & FilePermissions.PERMISSION_UPDATE) == FilePermissions.PERMISSION_UPDATE
def test_create_public_upload(nc_any):
new_share = nc_any.files.sharing.create("test_empty_dir", ShareType.TYPE_LINK, public_upload=True)
nc_any.files.sharing.delete(new_share)
assert (
new_share.permissions
== FilePermissions.PERMISSION_READ
| FilePermissions.PERMISSION_UPDATE
| FilePermissions.PERMISSION_SHARE
| FilePermissions.PERMISSION_DELETE
| FilePermissions.PERMISSION_CREATE
)
@pytest.mark.asyncio(scope="session")
async def test_create_public_upload_async(anc_any):
new_share = await anc_any.files.sharing.create("test_empty_dir", ShareType.TYPE_LINK, public_upload=True)
await anc_any.files.sharing.delete(new_share)
assert (
new_share.permissions
== FilePermissions.PERMISSION_READ
| FilePermissions.PERMISSION_UPDATE
| FilePermissions.PERMISSION_SHARE
| FilePermissions.PERMISSION_DELETE
| FilePermissions.PERMISSION_CREATE
)
def test_create_password(nc):
if nc.check_capabilities("spreed"):
pytest.skip(reason="Talk is not installed.")
new_share = nc.files.sharing.create("test_generated_image.png", ShareType.TYPE_LINK, password="s2dDS_z44ad1")
nc.files.sharing.delete(new_share)
assert new_share.password
assert new_share.send_password_by_talk is False
new_share = nc.files.sharing.create(
"test_generated_image.png", ShareType.TYPE_LINK, password="s2dDS_z44ad1", send_password_by_talk=True
)
nc.files.sharing.delete(new_share)
assert new_share.password
assert new_share.send_password_by_talk is True
@pytest.mark.asyncio(scope="session")
async def test_create_password_async(anc):
if await anc.check_capabilities("spreed"):
pytest.skip(reason="Talk is not installed.")
new_share = await anc.files.sharing.create("test_generated_image.png", ShareType.TYPE_LINK, password="s2dDS_z44ad1")
await anc.files.sharing.delete(new_share)
assert new_share.password
assert new_share.send_password_by_talk is False
new_share = await anc.files.sharing.create(
"test_generated_image.png", ShareType.TYPE_LINK, password="s2dDS_z44ad1", send_password_by_talk=True
)
await anc.files.sharing.delete(new_share)
assert new_share.password
assert new_share.send_password_by_talk is True
def test_create_note_label(nc_any):
new_share = nc_any.files.sharing.create(
"test_empty_text.txt", ShareType.TYPE_LINK, note="This is note", label="label"
)
nc_any.files.sharing.delete(new_share)
assert new_share.note == "This is note"
assert new_share.label == "label"
@pytest.mark.asyncio(scope="session")
async def test_create_note_label_async(anc_any):
new_share = await anc_any.files.sharing.create(
"test_empty_text.txt", ShareType.TYPE_LINK, note="This is note", label="label"
)
await anc_any.files.sharing.delete(new_share)
assert new_share.note == "This is note"
assert new_share.label == "label"
def test_create_expire_time(nc):
expire_time = datetime.datetime.now() + datetime.timedelta(days=1)
expire_time = expire_time.replace(hour=0, minute=0, second=0, microsecond=0)
new_share = nc.files.sharing.create("test_12345_text.txt", ShareType.TYPE_LINK, expire_date=expire_time)
nc.files.sharing.delete(new_share)
assert new_share.expire_date.date() == expire_time.date()
with pytest.raises(NextcloudException):
nc.files.sharing.create(
"test_12345_text.txt", ShareType.TYPE_LINK, expire_date=datetime.datetime.now() - datetime.timedelta(days=1)
)
new_share.raw_data["expiration"] = "invalid time"
new_share2 = Share(new_share.raw_data)
assert new_share2.expire_date == datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)
@pytest.mark.asyncio(scope="session")
async def test_create_expire_time_async(anc):
expire_time = datetime.datetime.now() + datetime.timedelta(days=1)
expire_time = expire_time.replace(hour=0, minute=0, second=0, microsecond=0)
new_share = await anc.files.sharing.create("test_12345_text.txt", ShareType.TYPE_LINK, expire_date=expire_time)
await anc.files.sharing.delete(new_share)
assert new_share.expire_date.date() == expire_time.date()
with pytest.raises(NextcloudException):
await anc.files.sharing.create(
"test_12345_text.txt", ShareType.TYPE_LINK, expire_date=datetime.datetime.now() - datetime.timedelta(days=1)
)
new_share.raw_data["expiration"] = "invalid time"
new_share2 = Share(new_share.raw_data)
assert new_share2.expire_date == datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)
def _test_get_list(share_by_id: Share, shares_list: list[Share]):
assert share_by_id.share_owner == shares_list[-1].share_owner
assert share_by_id.mimetype == shares_list[-1].mimetype
assert share_by_id.password == shares_list[-1].password
assert share_by_id.permissions == shares_list[-1].permissions
assert share_by_id.url == shares_list[-1].url
def test_get_list(nc):
shared_file = nc.files.by_path("test_12345_text.txt")
result = nc.files.sharing.get_list()
assert isinstance(result, list)
n_shares = len(result)
new_share = nc.files.sharing.create(shared_file, ShareType.TYPE_LINK)
assert isinstance(new_share, Share)
shares_list = nc.files.sharing.get_list()
assert n_shares + 1 == len(shares_list)
share_by_id = nc.files.sharing.get_by_id(shares_list[-1].share_id)
nc.files.sharing.delete(new_share)
assert n_shares == len(nc.files.sharing.get_list())
_test_get_list(share_by_id, shares_list)
@pytest.mark.asyncio(scope="session")
async def test_get_list_async(anc):
shared_file = await anc.files.by_path("test_12345_text.txt")
result = await anc.files.sharing.get_list()
assert isinstance(result, list)
n_shares = len(result)
new_share = await anc.files.sharing.create(shared_file, ShareType.TYPE_LINK)
assert isinstance(new_share, Share)
shares_list = await anc.files.sharing.get_list()
assert n_shares + 1 == len(shares_list)
share_by_id = await anc.files.sharing.get_by_id(shares_list[-1].share_id)
await anc.files.sharing.delete(new_share)
assert n_shares == len(await anc.files.sharing.get_list())
_test_get_list(share_by_id, shares_list)
def test_create_update(nc):
if nc.check_capabilities("spreed"):
pytest.skip(reason="Talk is not installed.")
new_share = nc.files.sharing.create(
"test_empty_dir",
ShareType.TYPE_LINK,
permissions=FilePermissions.PERMISSION_READ
+ FilePermissions.PERMISSION_SHARE
+ FilePermissions.PERMISSION_UPDATE,
)
update_share = nc.files.sharing.update(new_share, password="s2dDS_z44ad1")
assert update_share.password
assert update_share.permissions != FilePermissions.PERMISSION_READ + FilePermissions.PERMISSION_SHARE
update_share = nc.files.sharing.update(
new_share, permissions=FilePermissions.PERMISSION_READ + FilePermissions.PERMISSION_SHARE
)
assert update_share.password
assert update_share.permissions == FilePermissions.PERMISSION_READ + FilePermissions.PERMISSION_SHARE
assert update_share.send_password_by_talk is False
update_share = nc.files.sharing.update(new_share, send_password_by_talk=True, public_upload=True)
assert update_share.password
assert update_share.send_password_by_talk is True
expire_time = datetime.datetime.now() + datetime.timedelta(days=1)
expire_time = expire_time.replace(hour=0, minute=0, second=0, microsecond=0)
update_share = nc.files.sharing.update(new_share, expire_date=expire_time)
assert update_share.expire_date.date() == expire_time.date()
update_share = nc.files.sharing.update(new_share, note="note", label="label")
assert update_share.note == "note"
assert update_share.label == "label"
nc.files.sharing.delete(new_share)
@pytest.mark.asyncio(scope="session")
async def test_create_update_async(anc):
if await anc.check_capabilities("spreed"):
pytest.skip(reason="Talk is not installed.")
new_share = await anc.files.sharing.create(
"test_empty_dir",
ShareType.TYPE_LINK,
permissions=FilePermissions.PERMISSION_READ
+ FilePermissions.PERMISSION_SHARE
+ FilePermissions.PERMISSION_UPDATE,
)
update_share = await anc.files.sharing.update(new_share, password="s2dDS_z44ad1")
assert update_share.password
assert update_share.permissions != FilePermissions.PERMISSION_READ + FilePermissions.PERMISSION_SHARE
update_share = await anc.files.sharing.update(
new_share, permissions=FilePermissions.PERMISSION_READ + FilePermissions.PERMISSION_SHARE
)
assert update_share.password
assert update_share.permissions == FilePermissions.PERMISSION_READ + FilePermissions.PERMISSION_SHARE
assert update_share.send_password_by_talk is False
update_share = await anc.files.sharing.update(new_share, send_password_by_talk=True, public_upload=True)
assert update_share.password
assert update_share.send_password_by_talk is True
expire_time = datetime.datetime.now() + datetime.timedelta(days=1)
expire_time = expire_time.replace(hour=0, minute=0, second=0, microsecond=0)
update_share = await anc.files.sharing.update(new_share, expire_date=expire_time)
assert update_share.expire_date.date() == expire_time.date()
update_share = await anc.files.sharing.update(new_share, note="note", label="label")
assert update_share.note == "note"
assert update_share.label == "label"
await anc.files.sharing.delete(new_share)
def test_get_inherited(nc_any):
new_share = nc_any.files.sharing.create("test_dir/subdir", ShareType.TYPE_LINK)
assert not nc_any.files.sharing.get_inherited("test_dir")
assert not nc_any.files.sharing.get_inherited("test_dir/subdir")
new_share2 = nc_any.files.sharing.get_inherited("test_dir/subdir/test_12345_text.txt")[0]
assert new_share.share_id == new_share2.share_id
assert new_share.share_owner == new_share2.share_owner
assert new_share.file_owner == new_share2.file_owner
assert new_share.url == new_share2.url
nc_any.files.sharing.delete(new_share)
@pytest.mark.asyncio(scope="session")
async def test_get_inherited_async(anc_any):
new_share = await anc_any.files.sharing.create("test_dir/subdir", ShareType.TYPE_LINK)
assert not await anc_any.files.sharing.get_inherited("test_dir")
assert not await anc_any.files.sharing.get_inherited("test_dir/subdir")
new_share2 = (await anc_any.files.sharing.get_inherited("test_dir/subdir/test_12345_text.txt"))[0]
assert new_share.share_id == new_share2.share_id
assert new_share.share_owner == new_share2.share_owner
assert new_share.file_owner == new_share2.file_owner
assert new_share.url == new_share2.url
await anc_any.files.sharing.delete(new_share)
def test_share_with(nc, nc_client):
nc_second_user = Nextcloud(nc_auth_user=environ["TEST_USER_ID"], nc_auth_pass=environ["TEST_USER_PASS"])
assert not nc_second_user.files.sharing.get_list()
shared_file = nc.files.by_path("test_empty_text.txt")
folder_share = nc.files.sharing.create(
"test_empty_dir_in_dir", ShareType.TYPE_USER, share_with=environ["TEST_USER_ID"]
)
file_share = nc.files.sharing.create(shared_file, ShareType.TYPE_USER, share_with=environ["TEST_USER_ID"])
shares_list1 = nc.files.sharing.get_list(path="test_empty_dir_in_dir/")
shares_list2 = nc.files.sharing.get_list(path="test_empty_text.txt")
second_user_shares_list = nc_second_user.files.sharing.get_list()
second_user_shares_list_with_me = nc_second_user.files.sharing.get_list(shared_with_me=True)
nc.files.sharing.delete(folder_share)
nc.files.sharing.delete(file_share)
assert not second_user_shares_list
assert len(second_user_shares_list_with_me) == 2
assert len(shares_list1) == 1
assert len(shares_list2) == 1
assert not nc_second_user.files.sharing.get_list()
@pytest.mark.asyncio(scope="session")
async def test_share_with_async(anc, anc_client):
nc_second_user = Nextcloud(nc_auth_user=environ["TEST_USER_ID"], nc_auth_pass=environ["TEST_USER_PASS"])
assert not nc_second_user.files.sharing.get_list()
shared_file = await anc.files.by_path("test_empty_text.txt")
folder_share = await anc.files.sharing.create(
"test_empty_dir_in_dir", ShareType.TYPE_USER, share_with=environ["TEST_USER_ID"]
)
file_share = await anc.files.sharing.create(shared_file, ShareType.TYPE_USER, share_with=environ["TEST_USER_ID"])
shares_list1 = await anc.files.sharing.get_list(path="test_empty_dir_in_dir/")
shares_list2 = await anc.files.sharing.get_list(path="test_empty_text.txt")
second_user_shares_list = nc_second_user.files.sharing.get_list()
second_user_shares_list_with_me = nc_second_user.files.sharing.get_list(shared_with_me=True)
await anc.files.sharing.delete(folder_share)
await anc.files.sharing.delete(file_share)
assert not second_user_shares_list
assert len(second_user_shares_list_with_me) == 2
assert len(shares_list1) == 1
assert len(shares_list2) == 1
assert not nc_second_user.files.sharing.get_list()
def test_pending(nc_any):
assert isinstance(nc_any.files.sharing.get_pending(), list)
with pytest.raises(NextcloudExceptionNotFound):
nc_any.files.sharing.accept_share(99999999)
with pytest.raises(NextcloudExceptionNotFound):
nc_any.files.sharing.decline_share(99999999)
@pytest.mark.asyncio(scope="session")
async def test_pending_async(anc_any):
assert isinstance(await anc_any.files.sharing.get_pending(), list)
with pytest.raises(NextcloudExceptionNotFound):
await anc_any.files.sharing.accept_share(99999999)
with pytest.raises(NextcloudExceptionNotFound):
await anc_any.files.sharing.decline_share(99999999)
def test_deleted(nc_any):
assert isinstance(nc_any.files.sharing.get_deleted(), list)
with pytest.raises(NextcloudExceptionNotFound):
nc_any.files.sharing.undelete(99999999)
@pytest.mark.asyncio(scope="session")
async def test_deleted_async(anc_any):
assert isinstance(await anc_any.files.sharing.get_deleted(), list)
with pytest.raises(NextcloudExceptionNotFound):
await anc_any.files.sharing.undelete(99999999)