Skip to content

Commit 1bc9a87

Browse files
committed
fix: also validate Node data on setter
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
1 parent edb80ff commit 1bc9a87

2 files changed

Lines changed: 52 additions & 8 deletions

File tree

__tests__/files/node.spec.ts

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,8 @@ describe('Sanity checks', () => {
314314

315315
test('Invalid source', () => {
316316
expect(() => new File({} as unknown as NodeData)).toThrowError('Missing mandatory source')
317-
expect(() => new File({
317+
expect(() => new Folder({
318318
source: 'cloud.domain.com/remote.php/dav/Photos',
319-
mime: 'image/jpeg',
320319
owner: 'emma',
321320
})).toThrowError('Invalid source')
322321
expect(() => new File({
@@ -332,27 +331,41 @@ describe('Sanity checks', () => {
332331
})
333332

334333
test('Invalid displayname', () => {
335-
expect(() => new File({
334+
expect(() => new Folder({
336335
source: 'https://cloud.domain.com/remote.php/dav/Photos',
337-
mime: 'image',
338336
displayname: true as unknown as string,
339337
owner: 'emma',
340338
})).toThrowError('Invalid displayname type')
339+
340+
const file = new Folder({
341+
source: 'https://cloud.domain.com/remote.php/dav/Photos',
342+
displayname: 'test',
343+
owner: 'emma',
344+
})
345+
// @ts-expect-error wrong type error check
346+
expect(() => file.displayname = true).toThrowError('Invalid displayname')
341347
})
348+
342349

343350
test('Invalid mtime', () => {
344351
expect(() => new File({
345-
source: 'https://cloud.domain.com/remote.php/dav/Photos',
346-
mime: 'image',
352+
source: 'https://cloud.domain.com/remote.php/dav/Photos/picture.jpg',
347353
owner: 'emma',
348354
mtime: 'invalid' as unknown as Date,
349355
})).toThrowError('Invalid mtime type')
356+
357+
const file = new File({
358+
source: 'https://cloud.domain.com/remote.php/dav/Photos/picture.jpg',
359+
owner: 'emma',
360+
})
361+
// @ts-expect-error wrong type error check
362+
expect(() => file.mtime = 'invalid').toThrowError('Invalid mtime type')
350363
})
351364

352365
test('Invalid crtime', () => {
353366
expect(() => new File({
354-
source: 'https://cloud.domain.com/remote.php/dav/Photos',
355-
mime: 'image',
367+
source: 'https://cloud.domain.com/remote.php/dav/Photos/picture.jpg',
368+
mime: 'image/jpeg',
356369
owner: 'emma',
357370
crtime: 'invalid' as unknown as Date,
358371
})).toThrowError('Invalid crtime type')
@@ -364,6 +377,15 @@ describe('Sanity checks', () => {
364377
mime: 'image',
365378
owner: 'emma',
366379
})).toThrowError('Missing or invalid mandatory mime')
380+
381+
const file = new File({
382+
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
383+
mime: 'image/jpeg',
384+
owner: 'emma',
385+
})
386+
// @ts-expect-error wrong type error check
387+
expect(() => file.mime = 1234).toThrowError('Missing or invalid mandatory mime')
388+
expect(() => file.mime = 'image').toThrowError('Missing or invalid mandatory mime')
367389
})
368390

369391
test('Invalid attributes', () => {
@@ -391,6 +413,14 @@ describe('Sanity checks', () => {
391413
owner: 'emma',
392414
size: 'test' as unknown as number,
393415
})).toThrowError('Invalid size type')
416+
417+
const file = new File({
418+
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
419+
mime: 'image/jpeg',
420+
owner: 'emma',
421+
})
422+
// @ts-expect-error wrong type error check
423+
expect(() => file.size = 'test').toThrowError('Invalid size type')
394424
})
395425

396426
test('Invalid owner', () => {
@@ -438,6 +468,15 @@ describe('Sanity checks', () => {
438468
owner: 'emma',
439469
status: 'invalid' as unknown as NodeStatus,
440470
})).toThrowError('Status must be a valid NodeStatus')
471+
472+
const file = new File({
473+
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
474+
mime: 'image/jpeg',
475+
owner: 'emma',
476+
status: NodeStatus.LOCKED,
477+
})
478+
// @ts-expect-error wrong type error check
479+
expect(() => file.status = 'invalid').toThrowError('Status must be a valid NodeStatus')
441480
})
442481
})
443482

lib/files/node.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export abstract class Node {
125125
* Set the displayname
126126
*/
127127
set displayname(displayname: string) {
128+
validateData({ ...this._data, displayname }, this._knownDavService)
128129
this._data.displayname = displayname
129130
}
130131

@@ -201,6 +202,7 @@ export abstract class Node {
201202
* Set the file modification time
202203
*/
203204
set mtime(mtime: Date|undefined) {
205+
validateData({ ...this._data, mtime }, this._knownDavService)
204206
this._data.mtime = mtime
205207
}
206208

@@ -223,6 +225,7 @@ export abstract class Node {
223225
* Set the file size
224226
*/
225227
set size(size: number|undefined) {
228+
validateData({ ...this._data, size }, this._knownDavService)
226229
this.updateMtime()
227230
this._data.size = size
228231
}
@@ -254,6 +257,7 @@ export abstract class Node {
254257
* Set the file permissions
255258
*/
256259
set permissions(permissions: Permission) {
260+
validateData({ ...this._data, permissions }, this._knownDavService)
257261
this.updateMtime()
258262
this._data.permissions = permissions
259263
}
@@ -341,6 +345,7 @@ export abstract class Node {
341345
* Set the node status.
342346
*/
343347
set status(status: NodeStatus|undefined) {
348+
validateData({ ...this._data, status }, this._knownDavService)
344349
this._data.status = status
345350
}
346351

0 commit comments

Comments
 (0)