Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions __mocks__/@nextcloud/auth.js

This file was deleted.

9 changes: 0 additions & 9 deletions __mocks__/@nextcloud/router.js

This file was deleted.

51 changes: 14 additions & 37 deletions __tests__/dav/dav.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import type { FileStat, WebDAVClient } from 'webdav'

import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { describe, expect, test, vi } from 'vitest'
import {
defaultRemoteURL,
defaultRootPath,
Expand All @@ -17,30 +17,26 @@ import { File, Folder, NodeStatus } from '../../lib/index.ts'
import FAVORITES_INNER_RESPONSE from '../fixtures/favorites-inner-response.json' with { type: 'json' }
import FAVORITES_RESPONSE from '../fixtures/favorites-response.json' with { type: 'json' }

const auth = vi.hoisted(() => ({
getCurrentUser: vi.fn(() => ({ uid: 'test', displayName: 'Test User', isAdmin: false })),
getRequestToken: vi.fn(() => 'test-token'),
onRequestTokenUpdate: vi.fn(),
}))
// The DAV root path and remote URL are computed on import from the current user and the webroot
vi.hoisted(() => {
document.head.dataset.user = 'test'
window._oc_webroot = ''
})

vi.mock('@nextcloud/auth', () => auth)
vi.mock('@nextcloud/router')
/** The remote URL of the server the tests run on */
const remoteURL = `${window.location.origin}/remote.php/dav`

describe('DAV functions', () => {
test('root path is correct', () => {
expect(defaultRootPath).toBe('/files/test')
})

test('remote url is correct', () => {
expect(defaultRemoteURL).toBe('https://localhost/dav')
expect(defaultRemoteURL).toBe(remoteURL)
})
})

describe('resultToNode', () => {
afterEach(() => {
vi.resetAllMocks()
})

/* Result of:
getClient().getDirectoryContents(`${defaultRootPath}${path}`, { details: true })
*/
Expand All @@ -67,7 +63,7 @@ describe('resultToNode', () => {
expect(node.basename).toBe(result.basename)
expect(node.displayname).toBe(result.props!.displayname)
expect(node.extension).toBe('.md')
expect(node.source).toBe('https://localhost/dav/files/test/New folder/Neue Textdatei.md')
expect(node.source).toBe(`${remoteURL}/files/test/New folder/Neue Textdatei.md`)
expect(node.root).toBe(defaultRootPath)
expect(node.path).toBe('/New folder/Neue Textdatei.md')
expect(node.dirname).toBe('/New folder')
Expand All @@ -82,7 +78,7 @@ describe('resultToNode', () => {
expect(node.basename).toBe(remoteResult.basename)
expect(node.extension).toBe('.md')
expect(node.root).toBe('/root')
expect(node.source).toBe('https://localhost/dav/root/New folder/Neue Textdatei.md')
expect(node.source).toBe(`${remoteURL}/root/New folder/Neue Textdatei.md`)
expect(node.path).toBe('/New folder/Neue Textdatei.md')
expect(node.dirname).toBe('/New folder')
})
Expand All @@ -104,10 +100,8 @@ describe('resultToNode', () => {
expect(node.displayname).toBe(remoteResult.props!.displayname)
})

// If owner-id is set, it will be used as owner
// If owner-id is set, it will be used as owner instead of the current user
test('has correct owner set', () => {
vi.mocked(auth).getCurrentUser.mockReturnValue({ uid: 'user1', displayName: 'User 1', isAdmin: false })

const remoteResult = { ...result, filename: '/root/New folder/Neue Textdatei.md' }
remoteResult.props = { ...remoteResult.props, ...{ 'owner-id': 'user1' } } as FileStat['props']
const node = resultToNode(remoteResult, '/root', 'http://example.com/remote.php/dav')
Expand All @@ -117,8 +111,6 @@ describe('resultToNode', () => {
})

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

const remoteResult = { ...result, filename: '/root/New folder/Neue Textdatei.md' }
remoteResult.props = { ...remoteResult.props, ...{ 'owner-id': 123456789 } } as FileStat['props']
const node = resultToNode(remoteResult, '/root', 'http://example.com/remote.php/dav')
Expand All @@ -128,36 +120,29 @@ describe('resultToNode', () => {
})

test('has correct owner set if not set on node', () => {
vi.mocked(auth).getCurrentUser.mockReturnValue({ uid: 'user1', displayName: 'User 1', isAdmin: false })

const remoteResult = { ...result, filename: '/root/New folder/Neue Textdatei.md' }
const node = resultToNode(remoteResult, '/root', 'http://example.com/remote.php/dav')

expect(node.isDavResource).toBe(true)
expect(node.owner).toBe('user1')
// falls back to the current user
expect(node.owner).toBe('test')
})

test('by default no status is set', () => {
vi.mocked(auth).getCurrentUser.mockReturnValue({ uid: 'user1', displayName: 'User 1', isAdmin: false })

const remoteResult = { ...result }
remoteResult.props!.fileid = 1
const node = resultToNode(remoteResult)
expect(node.status).toBeUndefined()
})

test('sets node status on invalid fileid', () => {
vi.mocked(auth).getCurrentUser.mockReturnValue({ uid: 'user1', displayName: 'User 1', isAdmin: false })

const remoteResult = { ...result }
remoteResult.props!.fileid = -1
const node = resultToNode(remoteResult)
expect(node.status).toBe(NodeStatus.FAILED)
})

test('Ignore invalid times', () => {
vi.mocked(auth).getCurrentUser.mockReturnValue({ uid: 'user1', displayName: 'User 1', isAdmin: false })

// Invalid dates
const remoteResult = { ...result }
remoteResult.lastmod = 'invalid'
Expand All @@ -176,14 +161,6 @@ describe('resultToNode', () => {
})

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

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

test('request all favorite files', async () => {
// Mock the WebDAV client
const client = {
Expand Down
83 changes: 17 additions & 66 deletions __tests__/dav/public-shares.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,33 @@
*/

import type { FileStat } from 'webdav'
import type { resultToNode as IResultToNode } from '../../lib/dav/dav.ts'

import { beforeEach, describe, expect, test, vi } from 'vitest'
import { beforeAll, describe, expect, test } from 'vitest'
import { getRemoteURL, getRootPath, resultToNode } from '../../lib/dav/dav.ts'
import { setPublicShare } from '../helpers.ts'

const getCurrentUser = vi.hoisted(() => (vi.fn()))
const router = vi.hoisted(() => ({ generateRemoteUrl: vi.fn() }))
const sharing = vi.hoisted(() => ({ isPublicShare: vi.fn(), getSharingToken: vi.fn() }))

vi.mock('@nextcloud/auth', async (original) => ({
...(await original()),
getCurrentUser,
}))

vi.mock('@nextcloud/router', () => router)
vi.mock('@nextcloud/sharing/public', () => sharing)

function restoreMocks() {
vi.resetAllMocks()
router.generateRemoteUrl.mockImplementation((service) => `https://example.com/remote.php/${service}`)
}

function mockPublicShare() {
getCurrentUser.mockImplementationOnce(() => null)
sharing.isPublicShare.mockImplementation(() => true)
sharing.getSharingToken.mockImplementation(() => 'token-1234')
}

describe('DAV path functions', () => {
beforeEach(() => {
vi.resetModules()
restoreMocks()
})

test('root path is correct on public shares', async () => {
mockPublicShare()
beforeAll(() => {
window._oc_webroot = ''
setPublicShare('token-1234')
})

const { getRootPath } = await import('../../lib/dav/dav.ts')
describe('DAV path functions on public shares', () => {
test('root path is correct', () => {
expect(getRootPath()).toBe('/files/token-1234')
})

test('remote URL is correct on public shares', async () => {
mockPublicShare()

const { getRemoteURL } = await import('../../lib/dav/dav.ts')
expect(getRemoteURL()).toBe('https://example.com/public.php/dav')
test('remote URL is correct', () => {
expect(getRemoteURL()).toBe(`${window.location.origin}/public.php/dav`)
})
})

describe('on public shares', () => {
beforeEach(() => {
vi.resetAllMocks()
vi.resetModules()
})

// Wrapper function as we can not static import the function to allow mocking the modules
const resultToNode = async (...rest: Parameters<typeof IResultToNode>) => {
const { resultToNode } = await import('../../lib/dav/dav.ts')
return resultToNode(...rest)
}

describe('resultToNode on public shares', () => {
/*
* Result of:
* davGetClient().getDirectoryContents(`${davRootPath}${path}`, { details: true })
*/
const result: FileStat = {
filename: '/files/test/New folder/Neue Textdatei.md',
filename: '/root/New folder/Neue Textdatei.md',
basename: 'Neue Textdatei.md',
lastmod: 'Tue, 25 Jul 2023 12:29:34 GMT',
size: 123,
Expand All @@ -86,20 +47,10 @@ describe('on public shares', () => {
},
}

describe('resultToNode', () => {
beforeEach(() => {
vi.resetModules()
restoreMocks()
})

test('has correct owner set on public shares', async () => {
mockPublicShare()

const remoteResult = { ...result, filename: '/root/New folder/Neue Textdatei.md' }
const node = await resultToNode(remoteResult, '/root', 'http://example.com/remote.php/dav')
test('has correct owner set', () => {
const node = resultToNode(result, '/root', 'http://example.com/remote.php/dav')

expect(node.isDavResource).toBe(true)
expect(node.owner).toBe('anonymous')
})
expect(node.isDavResource).toBe(true)
expect(node.owner).toBe('anonymous')
})
})
Loading
Loading