Skip to content

Commit

Permalink
Merge pull request #683 from nextcloud/fix/allow-undefined
Browse files Browse the repository at this point in the history
fix: allow undefined properties in File and Folder
  • Loading branch information
skjnldsv authored Jul 25, 2023
2 parents 252eaa4 + 48a6ba7 commit 36f0f2f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
37 changes: 35 additions & 2 deletions __tests__/files/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('Sanity checks', () => {
mime: 'image/jpeg',
owner: 'emma',
attributes: 'test' as unknown as Attribute,
})).toThrowError('Invalid attributes format')
})).toThrowError('Invalid attributes type')
})

test('Invalid permissions', () => {
Expand Down Expand Up @@ -166,7 +166,7 @@ describe('Sanity checks', () => {
mime: 'image/jpeg',
owner: 'emma',
root: true as unknown as string,
})).toThrowError('Invalid root format')
})).toThrowError('Invalid root type')

expect(() => new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
Expand Down Expand Up @@ -338,3 +338,36 @@ describe('Root and paths detection', () => {
expect(file.path).toBe('/files/images/emma.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()
})
})
29 changes: 16 additions & 13 deletions lib/files/nodeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default interface NodeData {
/** Creation time */
crtime?: Date

/** The mime type */
/** The mime type Optional for folders only */
mime?: string

/** The node size type */
Expand Down Expand Up @@ -72,7 +72,7 @@ export const isDavRessource = function(source: string, davService: RegExp): bool
* Validate Node construct data
*/
export const validateData = (data: NodeData, davService: RegExp) => {
if ('id' in data && (typeof data.id !== 'number' || data.id < 0)) {
if (data.id && (typeof data.id !== 'number' || data.id < 0)) {
throw new Error('Invalid id type of value')
}

Expand All @@ -91,11 +91,11 @@ export const validateData = (data: NodeData, davService: RegExp) => {
throw new Error('Invalid source format, only http(s) is supported')
}

if ('mtime' in data && !(data.mtime instanceof Date)) {
if (data.mtime && !(data.mtime instanceof Date)) {
throw new Error('Invalid mtime type')
}

if ('crtime' in data && !(data.crtime instanceof Date)) {
if (data.crtime && !(data.crtime instanceof Date)) {
throw new Error('Invalid crtime type')
}

Expand All @@ -104,30 +104,33 @@ export const validateData = (data: NodeData, davService: RegExp) => {
throw new Error('Missing or invalid mandatory mime')
}

if ('size' in data && typeof data.size !== 'number') {
// Allow size to be 0
if ('size' in data && typeof data.size !== 'number' && data.size !== undefined) {
throw new Error('Invalid size type')
}

if ('permissions' in data && !(
typeof data.permissions === 'number'
// Allow permissions to be 0
if ('permissions' in data
&& data.permissions !== undefined
&& !(typeof data.permissions === 'number'
&& data.permissions >= Permission.NONE
&& data.permissions <= Permission.ALL
)) {
)) {
throw new Error('Invalid permissions')
}

if ('owner' in data
if (data.owner
&& data.owner !== null
&& typeof data.owner !== 'string') {
throw new Error('Invalid owner type')
}

if ('attributes' in data && typeof data.attributes !== 'object') {
throw new Error('Invalid attributes format')
if (data.attributes && typeof data.attributes !== 'object') {
throw new Error('Invalid attributes type')
}

if ('root' in data && typeof data.root !== 'string') {
throw new Error('Invalid root format')
if (data.root && typeof data.root !== 'string') {
throw new Error('Invalid root type')
}

if (data.root && !data.root.startsWith('/')) {
Expand Down

0 comments on commit 36f0f2f

Please sign in to comment.