Skip to content

Commit

Permalink
Add File and Folder API
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Oct 1, 2022
1 parent 0aa39fd commit cac806a
Show file tree
Hide file tree
Showing 10 changed files with 699 additions and 2 deletions.
95 changes: 95 additions & 0 deletions __tests__/files/file.spec.ts
@@ -0,0 +1,95 @@
import { File } from '../../lib/files/file'
import { FileType } from '../../lib/files/node'
import { Permission } from '../../lib/permissions'

describe('File creation', () => {
test('Valid dav file', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma'
})

expect(file).toBeInstanceOf(File)
expect(file.type).toBe(FileType.File)

// various data
expect(file.mime).toBe('image/jpeg')
expect(file.owner).toBe('emma')
expect(file.size).toBeUndefined()
expect(file.attributes).toStrictEqual({})

// path checks
expect(file.basename).toBe('picture.jpg')
expect(file.extension).toBe('.jpg')
expect(file.dirname).toBe('https://cloud.domain.com/remote.php/dav/files/emma/Photos')
expect(file.root).toBe('/files/emma/Photos')
expect(file.isDavRessource).toBe(true)
expect(file.permissions).toBe(Permission.READ)
})

test('Valid remote file', () => {
const file = new File({
source: 'https://domain.com/Photos/picture.jpg',
mime: 'image/jpeg',
owner: null
})

expect(file).toBeInstanceOf(File)
expect(file.type).toBe(FileType.File)

// various data
expect(file.mime).toBe('image/jpeg')
expect(file.owner).toBeNull()
expect(file.size).toBeUndefined()
expect(file.attributes).toStrictEqual({})

// path checks
expect(file.basename).toBe('picture.jpg')
expect(file.extension).toBe('.jpg')
expect(file.dirname).toBe('https://domain.com/Photos')
expect(file.root).toBeNull()
expect(file.isDavRessource).toBe(false)
expect(file.permissions).toBe(Permission.READ)
})
})

describe('File data change', () => {
test('Rename a file', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma'
})

expect(file.basename).toBe('picture.jpg')
expect(file.dirname).toBe('https://cloud.domain.com/remote.php/dav/files/emma/Photos')
expect(file.root).toBe('/files/emma/Photos')

file.rename('picture-old.jpg')

expect(file.basename).toBe('picture-old.jpg')
expect(file.dirname).toBe('https://cloud.domain.com/remote.php/dav/files/emma/Photos')
expect(file.source).toBe('https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture-old.jpg')
expect(file.root).toBe('/files/emma/Photos')
})

test('Changing source', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma'
})

expect(file.basename).toBe('picture.jpg')
expect(file.dirname).toBe('https://cloud.domain.com/remote.php/dav/files/emma/Photos')
expect(file.root).toBe('/files/emma/Photos')

file.move('https://cloud.domain.com/remote.php/dav/files/emma/Pictures/picture-old.jpg')

expect(file.basename).toBe('picture-old.jpg')
expect(file.dirname).toBe('https://cloud.domain.com/remote.php/dav/files/emma/Pictures')
expect(file.source).toBe('https://cloud.domain.com/remote.php/dav/files/emma/Pictures/picture-old.jpg')
expect(file.root).toBe('/files/emma/Pictures')
})
})
91 changes: 91 additions & 0 deletions __tests__/files/folder.spec.ts
@@ -0,0 +1,91 @@
import { Folder } from '../../lib/files/folder'
import { FileType } from '../../lib/files/node'
import { Permission } from '../../lib/permissions'

describe('File creation', () => {
test('Valid dav folder', () => {
const folder = new Folder({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/',
owner: 'emma'
})

expect(folder).toBeInstanceOf(Folder)
expect(folder.type).toBe(FileType.Folder)

// various data
expect(folder.mime).toBe('httpd/unix-directory')
expect(folder.owner).toBe('emma')
expect(folder.size).toBeUndefined()
expect(folder.attributes).toStrictEqual({})

// path checks
expect(folder.basename).toBe('Photos')
expect(folder.extension).toBeNull()
expect(folder.dirname).toBe('https://cloud.domain.com/remote.php/dav/files/emma')
expect(folder.root).toBe('/files/emma')
expect(folder.isDavRessource).toBe(true)
expect(folder.permissions).toBe(Permission.READ)
})

test('Valid remote folder', () => {
const folder = new Folder({
source: 'https://domain.com/Photos/',
owner: null
})

expect(folder).toBeInstanceOf(Folder)
expect(folder.type).toBe(FileType.Folder)

// various data
expect(folder.mime).toBe('httpd/unix-directory')
expect(folder.owner).toBeNull()
expect(folder.size).toBeUndefined()
expect(folder.attributes).toStrictEqual({})

// path checks
expect(folder.basename).toBe('Photos')
expect(folder.extension).toBeNull()
expect(folder.dirname).toBe('https://domain.com')
expect(folder.root).toBeNull()
expect(folder.isDavRessource).toBe(false)
expect(folder.permissions).toBe(Permission.READ)
})
})

describe('Folder data change', () => {
test('Rename a folder', () => {
const folder = new Folder({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos',
owner: 'emma'
})

expect(folder.basename).toBe('Photos')
expect(folder.dirname).toBe('https://cloud.domain.com/remote.php/dav/files/emma')
expect(folder.root).toBe('/files/emma')

folder.rename('Pictures')

expect(folder.basename).toBe('Pictures')
expect(folder.dirname).toBe('https://cloud.domain.com/remote.php/dav/files/emma')
expect(folder.source).toBe('https://cloud.domain.com/remote.php/dav/files/emma/Pictures')
expect(folder.root).toBe('/files/emma')
})

test('Changing source', () => {
const folder = new Folder({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/',
owner: 'emma'
})

expect(folder.basename).toBe('Photos')
expect(folder.dirname).toBe('https://cloud.domain.com/remote.php/dav/files/emma')
expect(folder.root).toBe('/files/emma')

folder.move('https://cloud.domain.com/remote.php/dav/files/emma/Pictures/')

expect(folder.basename).toBe('Pictures')
expect(folder.dirname).toBe('https://cloud.domain.com/remote.php/dav/files/emma')
expect(folder.source).toBe('https://cloud.domain.com/remote.php/dav/files/emma/Pictures')
expect(folder.root).toBe('/files/emma')
})
})
131 changes: 131 additions & 0 deletions __tests__/files/node.spec.ts
@@ -0,0 +1,131 @@
import { File } from '../../lib/files/file'
import { Folder } from '../../lib/files/folder'
import NodeData, { Attribute } from '../../lib/files/nodeData'

describe('Node testing', () => {
test('Source without origin', () => {
const file = new File({
source: '/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma'
})
expect(file.source).toBe('http://localhost/remote.php/dav/files/emma/Photos/picture.jpg')
})

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('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')

expect(() => new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
id: -1234,
})).toThrowError('Invalid id type of value')
})

test('Invalid source', () => {
expect(() => new File({} as unknown as NodeData)).toThrowError('Missing mandatory source')
})

test('Invalid source', () => {
expect(() => new File({
source: 'cloud.domain.com/remote.php/dav/Photos',
mime: 'image/jpeg',
owner: 'emma'
})).toThrowError('Invalid source')
})

test('Invalid mtime', () => {
expect(() => new File({
source: 'https://cloud.domain.com/remote.php/dav/Photos',
mime: 'image',
owner: 'emma',
mtime: 'invalid' as unknown as Date
})).toThrowError('Invalid mtime type')
})


test('Invalid crtime', () => {
expect(() => new File({
source: 'https://cloud.domain.com/remote.php/dav/Photos',
mime: 'image',
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')
})

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 format')
})

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,
})).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')
})

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')
})
})
2 changes: 0 additions & 2 deletions __tests__/humanFileSize.spec.ts
Expand Up @@ -6,8 +6,6 @@ declare global {
}
}

jest.mock

describe('humanFileSize', () => {
describe('formatFileSize', () => {
it('renders file sizes with the correct unit', function() {
Expand Down
28 changes: 28 additions & 0 deletions lib/files/file.ts
@@ -0,0 +1,28 @@
/**
* @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import Node, {FileType} from './node'

export class File extends Node {
get type(): FileType {
return FileType.File
}
}

0 comments on commit cac806a

Please sign in to comment.