Skip to content

Commit

Permalink
fix(systemtags): sanity checks for bulk tagging action
Browse files Browse the repository at this point in the history
Signed-off-by: skjnldsv <[email protected]>
  • Loading branch information
skjnldsv committed Nov 8, 2024
1 parent cb5da97 commit 4985626
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
75 changes: 75 additions & 0 deletions apps/systemtags/src/files_actions/bulkSystemTagsAction.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { File, Folder, Node, Permission, View, FileAction } from '@nextcloud/files'

Check failure on line 5 in apps/systemtags/src/files_actions/bulkSystemTagsAction.spec.ts

View workflow job for this annotation

GitHub Actions / NPM lint

'Node' is defined but never used
import { describe, expect, test, vi } from 'vitest'

Check failure on line 6 in apps/systemtags/src/files_actions/bulkSystemTagsAction.spec.ts

View workflow job for this annotation

GitHub Actions / NPM lint

'vi' is defined but never used
import { action } from './bulkSystemTagsAction'

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


Check failure on line 14 in apps/systemtags/src/files_actions/bulkSystemTagsAction.spec.ts

View workflow job for this annotation

GitHub Actions / NPM lint

More than 1 blank line not allowed
describe('Manage tags action conditions tests', () => {
test('Default values', () => {
expect(action).toBeInstanceOf(FileAction)
expect(action.id).toBe('systemtags:bulk')
expect(action.displayName([], view)).toBe('Manage tags')
expect(action.iconSvgInline([], view)).toMatch(/<svg.+<\/svg>/)
expect(action.default).toBeUndefined()
expect(action.order).toBe(undefined)
expect(action.enabled).toBeDefined()
})
})

describe('Manage tags action enabled tests', () => {
test('Disabled without permissions', () => {
const file1 = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
permissions: Permission.NONE,
})

const file2 = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
permissions: Permission.UPDATE,
})

expect(action.enabled).toBeDefined()
expect(action.enabled!([file1, file2], view)).toBe(false)
expect(action.enabled!([file1], view)).toBe(false)
expect(action.enabled!([file2], view)).toBe(true)
})

test('Disabled for non-dav ressources', () => {
const file = new File({
id: 1,
source: 'https://domain.com/foobar.txt',
owner: 'admin',
mime: 'text/plain',
permissions: Permission.ALL,
})

expect(action.enabled).toBeDefined()
expect(action.enabled!([file], view)).toBe(false)
})

test('Disabled for files outside the user root folder', () => {
const file = new Folder({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/trashbin/admin/trash/image.jpg.d1731053878',
owner: 'admin',
permissions: Permission.ALL,
})

expect(action.enabled).toBeDefined()
expect(action.enabled!([file], view)).toBe(false)
})
})
16 changes: 13 additions & 3 deletions apps/systemtags/src/files_actions/bulkSystemTagsAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { type Node } from '@nextcloud/files'
import { Permission, type Node } from '@nextcloud/files'

import { defineAsyncComponent } from 'vue'
import { FileAction } from '@nextcloud/files'
Expand Down Expand Up @@ -38,8 +38,18 @@ export const action = new FileAction({
return false
}

// If the user is not logged in, the action is not available
return true
// Disabled for non dav resources
if (nodes.some((node) => !node.isDavRessource)) {
return false
}

// Disabled if node outside user root folder
if (nodes.some((node) => !node.root?.startsWith('/files'))) {
return false
}

// We need to have the update permission on all nodes
return !nodes.some((node) => (node.permissions & Permission.UPDATE) === 0)
},

async exec(node: Node) {
Expand Down

0 comments on commit 4985626

Please sign in to comment.