Skip to content

Commit

Permalink
Merge pull request #882 from nextcloud-libraries/fix/typing
Browse files Browse the repository at this point in the history
  • Loading branch information
skjnldsv committed Feb 3, 2024
2 parents 4c2e5ba + 1241439 commit c4fe83c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
27 changes: 26 additions & 1 deletion __tests__/dav/dav.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterAll, afterEach, describe, expect, test, vi } from 'vitest'
import { afterAll, afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { readFile } from 'node:fs/promises'

import { File, Folder, davRemoteURL, davGetFavoritesReport, davRootPath, getFavoriteNodes, davResultToNode } from '../../lib'
Expand Down Expand Up @@ -96,9 +96,28 @@ describe('davResultToNode', () => {
expect(node.isDavRessource).toBe(true)
expect(node.owner).toBe('user1')
})

test('has correct owner set if number', () => {
vi.spyOn(auth, 'getCurrentUser').mockReturnValue({ uid: 'admin', displayName: 'admin', isAdmin: true })

result.props = { ...result.props, ...{ 'owner-id': 123456789 } } as FileStat['props']
const remoteResult = { ...result, filename: '/root/New folder/Neue Textdatei.md' }
const node = davResultToNode(remoteResult, '/root', 'http://example.com/remote.php/dav')

expect(node.isDavRessource).toBe(true)
expect(node.owner).toBe('123456789')
})
})

describe('DAV requests', () => {
beforeEach(() => {
vi.spyOn(auth, 'getCurrentUser').mockReturnValue({ uid: 'user1', displayName: 'User 1', isAdmin: false })
})

afterEach(() => {
vi.resetAllMocks()
})

test('request all favorite files', async () => {
const favoritesResponseJSON = JSON.parse((await readFile(new FileURL('../fixtures/favorites-response.json', import.meta.url))).toString())

Expand All @@ -115,12 +134,15 @@ describe('DAV requests', () => {
}),
}

// Get the favorite nodes
const nodes = await getFavoriteNodes(client as never)

// Check client was called correctly
expect(client.getDirectoryContents).toBeCalled()
expect(client.getDirectoryContents.mock.lastCall?.at(0)).toBe(`${davRootPath}/`)
expect(client.getDirectoryContents.mock.lastCall?.at(1)?.data).toBe(davGetFavoritesReport())
expect(client.getDirectoryContents.mock.lastCall?.at(1)?.headers?.method).toBe('REPORT')

// Check for correct output
expect(nodes.length).toBe(2)
expect(nodes[0] instanceof Folder).toBe(true)
Expand All @@ -145,12 +167,15 @@ describe('DAV requests', () => {
}),
}

// Get the favorite nodes
const nodes = await getFavoriteNodes(client as never, '/Neuer Ordner')

// Check client was called correctly
expect(client.getDirectoryContents).toBeCalled()
expect(client.getDirectoryContents.mock.lastCall?.at(0)).toBe(`${davRootPath}/Neuer Ordner`)
expect(client.getDirectoryContents.mock.lastCall?.at(1)?.data).toBe(davGetFavoritesReport())
expect(client.getDirectoryContents.mock.lastCall?.at(1)?.headers?.method).toBe('REPORT')

// There are no inner nodes
expect(nodes.length).toBe(0)
})
Expand Down
13 changes: 10 additions & 3 deletions lib/dav/dav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ import { createClient, getPatcher } from 'webdav'
*/
interface ResponseProps extends DAVResultResponseProps {
permissions: string
mime: string
fileid: number
size: number
'owner-id': string | number
}

/**
Expand Down Expand Up @@ -141,15 +143,20 @@ export const getFavoriteNodes = async (davClient: WebDAVClient, path = '/', davR
* @param remoteURL The DAV server remote URL (same as on `davGetClient`)
*/
export const davResultToNode = function(node: FileStat, filesRoot = davRootPath, remoteURL = davRemoteURL): Node {
const userId = getCurrentUser()?.uid
if (!userId) {
throw new Error('No user id found')
}

const props = node.props as ResponseProps
const permissions = davParsePermissions(props?.permissions)
const owner = props?.['owner-id'] as string || getCurrentUser()?.uid as string
const owner = (props?.['owner-id'] || userId).toString()

const nodeData: NodeData = {
id: (props?.fileid as number) || 0,
id: props?.fileid || 0,
source: `${remoteURL}${node.filename}`,
mtime: new Date(Date.parse(node.lastmod)),
mime: node.mime as string,
mime: node.mime || 'application/octet-stream',
size: props?.size || Number.parseInt(props.getcontentlength || '0'),
permissions,
owner,
Expand Down

0 comments on commit c4fe83c

Please sign in to comment.