Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(files): always ask for confirmation if trashbin app is disabled #46786

Merged
merged 1 commit into from
Jul 27, 2024
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
22 changes: 22 additions & 0 deletions __mocks__/@nextcloud/capabilities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { Capabilities } from '../../apps/files/src/types'

export const getCapabilities = (): Capabilities => {
return {
files: {
bigfilechunking: true,
blacklisted_files: [],
forbidden_filename_basenames: [],
forbidden_filename_characters: [],
forbidden_filename_extensions: [],
forbidden_filenames: [],
undelete: true,
version_deletion: true,
version_labeling: true,
versioning: true,
},
}
}
162 changes: 162 additions & 0 deletions apps/files/src/actions/deleteAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { expect } from '@jest/globals'
import { File, Folder, Permission, View, FileAction } from '@nextcloud/files'
import axios from '@nextcloud/axios'
import * as capabilities from '@nextcloud/capabilities'
import eventBus from '@nextcloud/event-bus'

import logger from '../logger'
Expand Down Expand Up @@ -94,6 +95,16 @@
expect(action.displayName([file], trashbinView)).toBe('Delete permanently')
})

test('Trashbin disabled displayName', () => {
jest.spyOn(capabilities, 'getCapabilities').mockImplementation(() => {
return {
files: {},
}
})
expect(action.displayName([file], view)).toBe('Delete permanently')
expect(capabilities.getCapabilities).toBeCalledTimes(1)
})

test('Shared root node displayName', () => {
expect(action.displayName([file2], view)).toBe('Leave this share')
expect(action.displayName([folder2], view)).toBe('Leave this share')
Expand Down Expand Up @@ -164,6 +175,9 @@
})

describe('Delete action execute tests', () => {
afterEach(() => {
jest.restoreAllMocks()
})
test('Delete action', async () => {
jest.spyOn(axios, 'delete')
jest.spyOn(eventBus, 'emit')
Expand All @@ -182,8 +196,8 @@
expect(axios.delete).toBeCalledTimes(1)
expect(axios.delete).toBeCalledWith('https://cloud.domain.com/remote.php/dav/files/test/foobar.txt')

expect(eventBus.emit).toBeCalledTimes(1)

Check warning on line 199 in apps/files/src/actions/deleteAction.spec.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Caution: `eventBus` also has a named export `emit`. Check if you meant to write `import {emit} from '@nextcloud/event-bus'` instead
expect(eventBus.emit).toBeCalledWith('files:node:deleted', file)

Check warning on line 200 in apps/files/src/actions/deleteAction.spec.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Caution: `eventBus` also has a named export `emit`. Check if you meant to write `import {emit} from '@nextcloud/event-bus'` instead
})

test('Delete action batch', async () => {
Expand Down Expand Up @@ -225,9 +239,125 @@
expect(eventBus.emit).toHaveBeenNthCalledWith(2, 'files:node:deleted', file2)
})

test('Delete action batch large set', async () => {
jest.spyOn(axios, 'delete')
jest.spyOn(eventBus, 'emit')

// Emulate the confirmation dialog to always confirm
const confirmMock = jest.fn().mockImplementation((a, b, c, resolve) => resolve(true))
// @ts-expect-error We only mock what needed
window.OC = { dialogs: { confirmDestructive: confirmMock } }

const file1 = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/test/foo.txt',
owner: 'test',
mime: 'text/plain',
permissions: Permission.READ | Permission.UPDATE | Permission.DELETE,
})

const file2 = new File({
id: 2,
source: 'https://cloud.domain.com/remote.php/dav/files/test/bar.txt',
owner: 'test',
mime: 'text/plain',
permissions: Permission.READ | Permission.UPDATE | Permission.DELETE,
})

const file3 = new File({
id: 3,
source: 'https://cloud.domain.com/remote.php/dav/files/test/baz.txt',
owner: 'test',
mime: 'text/plain',
permissions: Permission.READ | Permission.UPDATE | Permission.DELETE,
})

const file4 = new File({
id: 4,
source: 'https://cloud.domain.com/remote.php/dav/files/test/qux.txt',
owner: 'test',
mime: 'text/plain',
permissions: Permission.READ | Permission.UPDATE | Permission.DELETE,
})

const file5 = new File({
id: 5,
source: 'https://cloud.domain.com/remote.php/dav/files/test/quux.txt',
owner: 'test',
mime: 'text/plain',
permissions: Permission.READ | Permission.UPDATE | Permission.DELETE,
})

const exec = await action.execBatch!([file1, file2, file3, file4, file5], view, '/')

// Enough nodes to trigger a confirmation dialog
expect(confirmMock).toBeCalledTimes(1)

expect(exec).toStrictEqual([true, true, true, true, true])
expect(axios.delete).toBeCalledTimes(5)
expect(axios.delete).toHaveBeenNthCalledWith(1, 'https://cloud.domain.com/remote.php/dav/files/test/foo.txt')
expect(axios.delete).toHaveBeenNthCalledWith(2, 'https://cloud.domain.com/remote.php/dav/files/test/bar.txt')
expect(axios.delete).toHaveBeenNthCalledWith(3, 'https://cloud.domain.com/remote.php/dav/files/test/baz.txt')
expect(axios.delete).toHaveBeenNthCalledWith(4, 'https://cloud.domain.com/remote.php/dav/files/test/qux.txt')
expect(axios.delete).toHaveBeenNthCalledWith(5, 'https://cloud.domain.com/remote.php/dav/files/test/quux.txt')

expect(eventBus.emit).toBeCalledTimes(5)
expect(eventBus.emit).toHaveBeenNthCalledWith(1, 'files:node:deleted', file1)
expect(eventBus.emit).toHaveBeenNthCalledWith(2, 'files:node:deleted', file2)
expect(eventBus.emit).toHaveBeenNthCalledWith(3, 'files:node:deleted', file3)
expect(eventBus.emit).toHaveBeenNthCalledWith(4, 'files:node:deleted', file4)
expect(eventBus.emit).toHaveBeenNthCalledWith(5, 'files:node:deleted', file5)
})

test('Delete action batch trashbin disabled', async () => {
jest.spyOn(axios, 'delete')
jest.spyOn(eventBus, 'emit')
jest.spyOn(capabilities, 'getCapabilities').mockImplementation(() => {
return {
files: {},
}
})

// Emulate the confirmation dialog to always confirm
const confirmMock = jest.fn().mockImplementation((a, b, c, resolve) => resolve(true))
// @ts-expect-error We only mock what needed
window.OC = { dialogs: { confirmDestructive: confirmMock } }

const file1 = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/test/foo.txt',
owner: 'test',
mime: 'text/plain',
permissions: Permission.READ | Permission.UPDATE | Permission.DELETE,
})

const file2 = new File({
id: 2,
source: 'https://cloud.domain.com/remote.php/dav/files/test/bar.txt',
owner: 'test',
mime: 'text/plain',
permissions: Permission.READ | Permission.UPDATE | Permission.DELETE,
})

const exec = await action.execBatch!([file1, file2], view, '/')

// Will trigger a confirmation dialog because trashbin app is disabled
expect(confirmMock).toBeCalledTimes(1)

expect(exec).toStrictEqual([true, true])
expect(axios.delete).toBeCalledTimes(2)
expect(axios.delete).toHaveBeenNthCalledWith(1, 'https://cloud.domain.com/remote.php/dav/files/test/foo.txt')
expect(axios.delete).toHaveBeenNthCalledWith(2, 'https://cloud.domain.com/remote.php/dav/files/test/bar.txt')

expect(eventBus.emit).toBeCalledTimes(2)
expect(eventBus.emit).toHaveBeenNthCalledWith(1, 'files:node:deleted', file1)
expect(eventBus.emit).toHaveBeenNthCalledWith(2, 'files:node:deleted', file2)
})

test('Delete fails', async () => {
jest.spyOn(axios, 'delete').mockImplementation(() => { throw new Error('Mock error') })
jest.spyOn(logger, 'error').mockImplementation(() => jest.fn())
jest.spyOn(eventBus, 'emit')

const file = new File({
id: 1,
Expand All @@ -246,4 +376,36 @@
expect(eventBus.emit).toBeCalledTimes(0)
expect(logger.error).toBeCalledTimes(1)
})

test('Delete is cancelled', async () => {
jest.spyOn(axios, 'delete')
jest.spyOn(eventBus, 'emit')
jest.spyOn(capabilities, 'getCapabilities').mockImplementation(() => {
return {
files: {},
}
})

// Emulate the confirmation dialog to always confirm
const confirmMock = jest.fn().mockImplementation((a, b, c, resolve) => resolve(false))
// @ts-expect-error We only mock what needed
window.OC = { dialogs: { confirmDestructive: confirmMock } }

const file1 = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/test/foo.txt',
owner: 'test',
mime: 'text/plain',
permissions: Permission.READ | Permission.UPDATE | Permission.DELETE,
})

const exec = await action.execBatch!([file1], view, '/')

expect(confirmMock).toBeCalledTimes(1)

expect(exec).toStrictEqual([null])
expect(axios.delete).toBeCalledTimes(0)

expect(eventBus.emit).toBeCalledTimes(0)
})
})
Loading
Loading