-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathnode.spec.ts
More file actions
830 lines (726 loc) · 22.8 KB
/
node.spec.ts
File metadata and controls
830 lines (726 loc) · 22.8 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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
/**
* SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { Attribute, NodeData } from '../../lib/node/index.ts'
import { describe, expect, test } from 'vitest'
import { File, Folder, NodeStatus } from '../../lib/node/index.ts'
import { Permission } from '../../lib/permissions.ts'
describe('Node testing', () => {
test('Root null fallback', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
})
expect(file.root).toBeNull()
})
test('Remove source ending slash', () => {
const file = new Folder({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/',
owner: 'emma',
})
expect(file.source).toBe('https://cloud.domain.com/remote.php/dav/files/emma/Photos')
})
test('Invalid rename', () => {
const file = new Folder({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/',
owner: 'emma',
})
expect(() => file.rename('new/folder')).toThrowError('Invalid basename')
})
})
describe('FileId attribute', () => {
test('FileId undefined', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
})
expect(file.fileid).toBeUndefined()
})
test('FileId definition', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
id: 1234,
})
expect(file.fileid).toBe(1234)
})
// Mostly used when a node is unavailable
test('FileId negative', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
id: -1234,
})
expect(file.fileid).toBe(-1234)
})
test('FileId attributes no fallback', () => {
const file = new Folder({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/',
mime: 'image/jpeg',
owner: 'emma',
attributes: {
fileid: 1234,
},
})
expect(file.fileid).toBeUndefined()
})
})
describe('Mime attribute', () => {
test('Mime definition', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
})
expect(file.mime).toBe('image/jpeg')
})
test('Default mime', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
owner: 'emma',
})
expect(file.mime).toBe('application/octet-stream')
})
test('Changing mime', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
})
expect(file.mime).toBe('image/jpeg')
file.mime = 'image/png'
expect(file.mime).toBe('image/png')
})
test('Removing mime', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
})
expect(file.mime).toBe('image/jpeg')
file.mime = undefined
expect(file.mime).toBe('application/octet-stream')
})
})
describe('Mtime attribute', () => {
test('Mtime definition', () => {
const mtime = new Date()
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
mtime,
})
expect(file.mtime?.toISOString()).toBe(mtime.toISOString())
})
test('Mtime manual update', async () => {
const mtime = new Date()
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
mtime,
})
expect(file.mtime?.toISOString()).toBe(mtime.toISOString())
// Wait for 10ms to ensure mtime is updated
await new Promise(resolve => setTimeout(resolve, 10))
// Update mtime
file.mtime = new Date()
// Mtime is updated
expect(file.mtime?.toISOString()).not.toBe(mtime.toISOString())
})
test('Mtime method update', async () => {
const mtime = new Date()
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
mtime,
})
expect(file.mtime?.toISOString()).toBe(mtime.toISOString())
// Wait for 10ms to ensure mtime is updated
await new Promise(resolve => setTimeout(resolve, 10))
// Update mtime
file.updateMtime()
// Mtime is updated
expect(file.mtime?.toISOString()).not.toBe(mtime.toISOString())
})
})
describe('Size attribute', () => {
test('Size definition', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
size: 1234,
})
expect(file.size).toBe(1234)
})
test('Size update', async () => {
const mtime = new Date()
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
size: 1234,
mtime,
})
expect(file.size).toBe(1234)
expect(file.mtime?.toISOString()).toBe(mtime?.toISOString())
// Wait for 10ms to ensure mtime is updated
await new Promise(resolve => setTimeout(resolve, 10))
// Update size
file.size = 5678
// Size and mtime are updated
expect(file.size).toBe(5678)
expect(file.mtime?.toISOString()).not.toBe(mtime?.toISOString())
})
})
describe('Permissions attribute', () => {
test('Permissions definition', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
permissions: Permission.READ | Permission.UPDATE | Permission.CREATE | Permission.DELETE,
})
expect(file.permissions).toBe(15)
})
test('Permissions update', async () => {
const mtime = new Date()
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
permissions: Permission.READ,
mtime,
})
expect(file.permissions).toBe(1)
expect(file.mtime?.toISOString()).toBe(mtime?.toISOString())
// Wait for 10ms to ensure mtime is updated
await new Promise(resolve => setTimeout(resolve, 10))
// Update permissions
file.permissions = Permission.ALL
// Permissions and mtime are updated
expect(file.permissions).toBe(31)
expect(file.mtime?.toISOString()).not.toBe(mtime?.toISOString())
})
})
describe('Displayname attribute', () => {
test('Read displayname attribute', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
displayname: 'image.png',
})
expect(file.basename).toBe('picture.jpg')
expect(file.displayname).toBe('image.png')
})
test('Fallback displayname attribute', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
})
expect(file.basename).toBe('picture.jpg')
expect(file.displayname).toBe('picture.jpg')
})
test('Set displayname attribute', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
})
expect(file.basename).toBe('picture.jpg')
expect(file.displayname).toBe('picture.jpg')
file.displayname = 'image.png'
expect(file.displayname).toBe('image.png')
})
})
describe('Sanity checks', () => {
test('Invalid id', () => {
expect(() => new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
id: '1234' as unknown as number,
})).toThrowError('Invalid id type of value')
})
test('Invalid source', () => {
expect(() => new File({} as unknown as NodeData)).toThrowError('Missing mandatory source')
expect(() => new Folder({
source: 'cloud.domain.com/remote.php/dav/Photos',
owner: 'emma',
})).toThrowError('Invalid source')
expect(() => new File({
source: '/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
})).toThrowError('Invalid source format, source must be a valid URL')
expect(() => new File({
source: 'ftp://remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
})).toThrowError('Invalid source format, only http(s) is supported')
})
test('Invalid displayname', () => {
expect(() => new Folder({
source: 'https://cloud.domain.com/remote.php/dav/Photos',
displayname: true as unknown as string,
owner: 'emma',
})).toThrowError('Invalid displayname type')
const file = new Folder({
source: 'https://cloud.domain.com/remote.php/dav/Photos',
displayname: 'test',
owner: 'emma',
})
// @ts-expect-error wrong type error check
expect(() => { file.displayname = true }).toThrowError('Invalid displayname')
})
test('Invalid mtime', () => {
expect(() => new File({
source: 'https://cloud.domain.com/remote.php/dav/Photos/picture.jpg',
owner: 'emma',
mtime: 'invalid' as unknown as Date,
})).toThrowError('Invalid mtime type')
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/Photos/picture.jpg',
owner: 'emma',
})
// @ts-expect-error wrong type error check
expect(() => { file.mtime = 'invalid' }).toThrowError('Invalid mtime type')
})
test('Invalid crtime', () => {
expect(() => new File({
source: 'https://cloud.domain.com/remote.php/dav/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
crtime: 'invalid' as unknown as Date,
})).toThrowError('Invalid crtime type')
})
test('Invalid mime', () => {
expect(() => new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image',
owner: 'emma',
})).toThrowError('Missing or invalid mandatory mime')
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
})
// @ts-expect-error wrong type error check
expect(() => { file.mime = 1234 }).toThrowError('Missing or invalid mandatory mime')
expect(() => { file.mime = 'image' }).toThrowError('Missing or invalid mandatory mime')
})
test('Invalid attributes', () => {
expect(() => new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
attributes: 'test' as unknown as Attribute,
})).toThrowError('Invalid attributes type')
})
test('Invalid permissions', () => {
expect(() => new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
permissions: 324 as unknown as number,
})).toThrowError('Invalid permissions')
})
test('Invalid size', () => {
expect(() => new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
size: 'test' as unknown as number,
})).toThrowError('Invalid size type')
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
})
// @ts-expect-error wrong type error check
expect(() => { file.size = 'test' }).toThrowError('Invalid size type')
})
test('Invalid owner', () => {
expect(() => new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: true as unknown as string,
})).toThrowError('Invalid owner')
})
test('Invalid root', () => {
expect(() => new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
root: true as unknown as string,
})).toThrowError('Invalid root type')
expect(() => new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
root: 'https://cloud.domain.com/remote.php/dav/',
})).toThrowError('Root must start with a leading slash')
expect(() => new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
root: '/files/john',
})).toThrowError('Root must be part of the source')
expect(() => new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
root: '/remote.php/dav/files/emma',
})).toThrowError('The root must be relative to the service. e.g /files/emma')
})
test('Invalid status', () => {
expect(() => new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
status: 'invalid' as unknown as NodeStatus,
})).toThrowError('Status must be a valid NodeStatus')
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
status: NodeStatus.LOCKED,
})
// @ts-expect-error wrong type error check
expect(() => { file.status = 'invalid' }).toThrowError('Status must be a valid NodeStatus')
})
})
describe('Dav service detection', () => {
test('Known dav services', () => {
const file1 = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
})
expect(file1.isDavResource).toBe(true)
const file2 = new File({
source: 'https://cloud.domain.com/remote.php/webdav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
})
expect(file2.isDavResource).toBe(true)
const file3 = new File({
source: 'https://cloud.domain.com/public.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
})
expect(file3.isDavResource).toBe(true)
const file4 = new File({
source: 'https://cloud.domain.com/public.php/webdav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
})
expect(file4.isDavResource).toBe(true)
})
test('Custom dav service', () => {
const file1 = new File({
source: 'https://cloud.domain.com/test.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
}, /test\.php\/dav/)
expect(file1.isDavResource).toBe(true)
const file2 = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
}, /test\.php\/dav/)
expect(file2.isDavResource).toBe(false)
})
})
describe('Permissions handling', () => {
test('Default permissions', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
})
expect(file.permissions).toBe(0)
})
test('Custom permissions', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
permissions: Permission.READ | Permission.UPDATE | Permission.CREATE | Permission.DELETE | Permission.SHARE,
})
expect(file.permissions).toBe(31)
})
})
describe('Root and paths detection', () => {
test('Unknown root', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
})
expect(file.root).toBe('/files/emma/Photos')
expect(file.dirname).toBe('/')
})
test('Provided root dav service', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
root: '/files/emma',
})
expect(file.root).toBe('/files/emma')
expect(file.dirname).toBe('/Photos')
})
test('Root with public webdav endpoint', () => {
const file = new File({
source: 'https://cloud.domain.com/public.php/webdav/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
root: '/',
})
expect(file.root).toBe('/')
expect(file.dirname).toBe('/Photos')
expect(file.path).toBe('/Photos/picture.jpg')
})
test('Root with ending slash is removed', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
root: '/files/emma/',
})
expect(file.root).toBe('/files/emma')
expect(file.dirname).toBe('/Photos')
expect(file.path).toBe('/Photos/picture.jpg')
})
test('Root and source are the same', () => {
const folder = new Folder({
source: 'https://cloud.domain.com/remote.php/dav/files/emma',
owner: 'emma',
root: '/files/emma',
})
expect(folder.root).toBe('/files/emma')
expect(folder.dirname).toBe('/')
expect(folder.path).toBe('/')
})
test('Source contains a similar root path', () => {
const folder = new Folder({
source: 'https://domain.com/remote.php/dav/files/emma/files/emma',
owner: 'emma',
root: '/files/emma',
})
expect(folder.root).toBe('/files/emma')
expect(folder.dirname).toBe('/files')
expect(folder.path).toBe('/files/emma')
const file = new File({
source: 'https://domain.com/remote.php/dav/files/emma/files/emma.jpeg',
mime: 'image/jpeg',
owner: 'emma',
root: '/files/emma',
})
expect(file.root).toBe('/files/emma')
expect(file.dirname).toBe('/files')
expect(file.path).toBe('/files/emma.jpeg')
})
test('Non dav ressource with undefined root', () => {
const file = new File({
source: 'https://domain.com/files/images/emma.jpeg',
mime: 'image/jpeg',
owner: 'emma',
})
expect(file.isDavResource).toBe(false)
expect(file.root).toBe(null)
expect(file.dirname).toBe('/files/images')
expect(file.path).toBe('/files/images/emma.jpeg')
})
})
describe('Move and rename of a node', () => {
test('Move updates the basename', () => {
const file = new File({
source: 'https://cloud.example.com/dav/files/images/emma.jpeg',
mime: 'image/jpeg',
owner: 'emma',
root: '/dav',
})
expect(file.basename).toBe('emma.jpeg')
file.move('https://cloud.example.com/dav/files/images/jane.jpeg')
expect(file.basename).toBe('jane.jpeg')
})
test('Move updates the path', () => {
const file = new File({
source: 'https://cloud.example.com/dav/files/images/emma.jpeg',
mime: 'image/jpeg',
owner: 'emma',
root: '/dav',
})
expect(file.path).toBe('/files/images/emma.jpeg')
file.move('https://cloud.example.com/dav/files/pictures/emma.jpeg')
expect(file.path).toBe('/files/pictures/emma.jpeg')
})
test('Move updates the fallback displayname', () => {
const file = new File({
source: 'https://cloud.example.com/dav/files/images/emma.jpeg',
displayname: 'emma.jpeg',
mime: 'image/jpeg',
owner: 'emma',
root: '/dav',
})
expect(file.path).toBe('/files/images/emma.jpeg')
expect(file.displayname).toBe('emma.jpeg')
file.move('https://cloud.example.com/dav/files/pictures/jane.jpeg')
expect(file.path).toBe('/files/pictures/jane.jpeg')
expect(file.displayname).toBe('jane.jpeg')
})
test('Move does not updates custom displayname', () => {
const file = new File({
source: 'https://cloud.example.com/dav/files/images/emma.jpeg',
mime: 'image/jpeg',
owner: 'emma',
root: '/dav',
displayname: 'profile.jpeg',
})
expect(file.path).toBe('/files/images/emma.jpeg')
expect(file.displayname).toBe('profile.jpeg')
file.move('https://cloud.example.com/dav/files/pictures/jane.jpeg')
expect(file.path).toBe('/files/pictures/jane.jpeg')
expect(file.displayname).toBe('profile.jpeg')
})
})
describe('Undefined properties are allowed', () => {
test('File', () => {
expect(() => new File({
source: 'https://domain.com/files/images/emma.jpeg',
owner: 'emma',
id: undefined,
mtime: undefined,
crtime: undefined,
// Mime is optional for folders only
mime: 'image/jpeg',
size: undefined,
permissions: undefined,
attributes: undefined,
root: undefined,
})).not.toThrow()
})
test('Folder', () => {
expect(() => new Folder({
source: 'https://domain.com/files/images/',
owner: 'emma',
id: undefined,
mtime: undefined,
crtime: undefined,
mime: undefined,
size: undefined,
permissions: undefined,
attributes: undefined,
root: undefined,
})).not.toThrow()
})
})
describe('Encoded source is handled properly', () => {
test('File', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/em ma!/Photos~⛰️ shot of a $[big} mountain/realy #1\'s.md',
owner: 'em ma!',
id: 123456,
mime: 'image/jpeg',
root: '/files/em ma!',
})
expect(file.encodedSource).toBe('https://cloud.domain.com/remote.php/dav/files/em%20ma!/Photos~%E2%9B%B0%EF%B8%8F%20shot%20of%20a%20%24%5Bbig%7D%20mountain/realy%20%231\'s.md')
})
})
describe('Status handling', () => {
test('Update status', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
})
expect(file.status).toBeUndefined()
file.status = NodeStatus.LOCKED
expect(file.status).toBe(NodeStatus.LOCKED)
})
test('Clear status', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
status: NodeStatus.LOCKED,
})
expect(file.status).toBe(NodeStatus.LOCKED)
file.status = undefined
expect(file.status).toBeUndefined()
})
})
describe('Attributes update', () => {
test('Update attributes', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
attributes: {
etag: '1234',
'owner-display-name': 'Admin',
'owner-id': 'admin',
},
})
expect(file.attributes?.etag).toBe('1234')
expect(file.attributes?.['owner-display-name']).toBe('Admin')
expect(file.attributes?.['owner-id']).toBe('admin')
file.update({
etag: '5678',
'owner-display-name': undefined,
'owner-id': undefined,
})
expect(file.attributes?.etag).toBe('5678')
expect(file.attributes?.['owner-display-name']).toBeUndefined()
expect(file.attributes?.['owner-id']).toBeUndefined()
})
test('Update attributes with protected getters', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
size: 9999,
attributes: {
etag: '1234',
size: 9999,
},
})
file.update({
etag: '5678',
owner: 'admin',
fileid: 1234,
size: undefined,
})
expect(file.attributes?.etag).toBe('5678')
expect(file?.size).toBe(9999)
expect(file?.owner).toBe('emma')
expect(file?.fileid).toBeUndefined()
})
test('Changing a protected attributes is not possible', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
attributes: {
etag: '1234',
},
})
// We can not update the owner
// @ts-expect-error owner is a read-only property
expect(() => { file.owner = 'admin' }).toThrowError()
// The owner is still the original one
expect(file?.owner).toBe('emma')
})
})