Skip to content
Open
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
20 changes: 14 additions & 6 deletions apps/files/src/actions/renameAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@
*/
import { action } from './renameAction'
import { expect } from '@jest/globals'
import { File, Permission, View, FileAction } from '@nextcloud/files'
import { File, Folder, Permission, View, FileAction } from '@nextcloud/files'
import eventBus from '@nextcloud/event-bus'
import { useFilesStore } from '../store/files'
import { pinia } from '../store/index.ts'

const view = {
id: 'files',
name: 'Files',
} as View

beforeEach(() => {
const root = new Folder({ owner: 'test', source: 'https://cloud.domain.com/remote.php/dav/files/admin/', id: 1, permissions: Permission.CREATE })
const files = useFilesStore(pinia)
files.setRoot({ service: 'files', root })
})

describe('Rename action conditions tests', () => {
test('Default values', () => {
expect(action).toBeInstanceOf(FileAction)
Expand All @@ -26,7 +34,7 @@ describe('Rename action conditions tests', () => {
describe('Rename action enabled tests', () => {
test('Enabled for node with UPDATE permission', () => {
const file = new File({
id: 1,
id: 2,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
Expand All @@ -39,7 +47,7 @@ describe('Rename action enabled tests', () => {

test('Disabled for node without DELETE permission', () => {
const file = new File({
id: 1,
id: 2,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
Expand All @@ -54,13 +62,13 @@ describe('Rename action enabled tests', () => {
window.OCA = { Files: { Sidebar: {} } }

const file1 = new File({
id: 1,
id: 2,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foo.txt',
owner: 'admin',
mime: 'text/plain',
})
const file2 = new File({
id: 1,
id: 2,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/bar.txt',
owner: 'admin',
mime: 'text/plain',
Expand All @@ -76,7 +84,7 @@ describe('Rename action exec tests', () => {
jest.spyOn(eventBus, 'emit')

const file = new File({
id: 1,
id: 2,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
Expand Down
21 changes: 16 additions & 5 deletions apps/files/src/actions/renameAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { emit } from '@nextcloud/event-bus'
import { Permission, type Node, FileAction } from '@nextcloud/files'
import { Permission, type Node, FileAction, View } from '@nextcloud/files'
import { translate as t } from '@nextcloud/l10n'
import PencilSvg from '@mdi/svg/svg/pencil.svg?raw'
import { pinia } from '../store'
import { useFilesStore } from '../store/files'
import { dirname } from 'path'

export const ACTION_DETAILS = 'details'

Expand All @@ -14,10 +17,18 @@ export const action = new FileAction({
displayName: () => t('files', 'Rename'),
iconSvgInline: () => PencilSvg,

enabled: (nodes: Node[]) => {
return nodes.length > 0 && nodes
.map(node => node.permissions)
.every(permission => Boolean(permission & Permission.DELETE))
enabled: (nodes: Node[], view: View) => {
const node = nodes[0]
const filesStore = useFilesStore(pinia)
const parentNode = node.dirname === '/'
? filesStore.getRoot(view.id)
: filesStore.getNode(dirname(node.source))
const parentPermissions = parentNode?.permissions || Permission.NONE

// Only enable if the node have the delete permission
// and if the parent folder allows creating files
return Boolean(node.permissions & Permission.DELETE)
&& Boolean(parentPermissions & Permission.CREATE)
},

async exec(node: Node) {
Expand Down
4 changes: 2 additions & 2 deletions dist/files-init.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/files-init.js.map

Large diffs are not rendered by default.

Loading