From 7669faf42b8d3d799650e0acc4a7e4256b06bc3c Mon Sep 17 00:00:00 2001 From: parel Date: Fri, 4 Dec 2026 03:14:10 +0700 Subject: [PATCH] fix(systemtags): Read tag creation restriction from systemtags state SystemTags.vue read restrictSystemTagsCreationToAdmin from the settings app initial state, which only exists on the admin settings page, so it was always false in the Files sidebar and a failed tag creation always showed the generic error. Read it from the systemtags app initial state instead, like SystemTagPicker does, and reset the loading state when creation fails instead of leaving the component stuck loading. Fixes #67606 Signed-off-by: parel Assisted-by: Muse:Muse Spark 1.3 --- .../src/components/SystemTags.spec.ts | 88 +++++++++++++++++++ apps/systemtags/src/components/SystemTags.vue | 6 +- 2 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 apps/systemtags/src/components/SystemTags.spec.ts diff --git a/apps/systemtags/src/components/SystemTags.spec.ts b/apps/systemtags/src/components/SystemTags.spec.ts new file mode 100644 index 0000000000000..e79f60ae60e74 --- /dev/null +++ b/apps/systemtags/src/components/SystemTags.spec.ts @@ -0,0 +1,88 @@ +/** + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import type { VueWrapper } from '@vue/test-utils' +import type { Tag } from '../types.ts' + +import { showError } from '@nextcloud/dialogs' +import { loadState } from '@nextcloud/initial-state' +import { flushPromises, mount } from '@vue/test-utils' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { createTagForFile } from '../services/files.ts' +import { defaultBaseTag } from '../utils.ts' +import SystemTags from './SystemTags.vue' + +vi.mock('@nextcloud/dialogs', () => ({ + showError: vi.fn(), +})) + +vi.mock('@nextcloud/initial-state', () => ({ + loadState: vi.fn(), +})) + +vi.mock('@nextcloud/files', () => ({ + getSidebar: vi.fn(() => ({})), +})) + +vi.mock('../services/api.ts', () => ({ + fetchTags: vi.fn(async () => []), + fetchLastUsedTagIds: vi.fn(async () => []), +})) + +vi.mock('../services/files.ts', () => ({ + createTagForFile: vi.fn(), + deleteTagForFile: vi.fn(async () => {}), + fetchTagsForFile: vi.fn(async () => []), + setTagForFile: vi.fn(async () => {}), +})) + +vi.mock('../services/davClient.ts', () => ({ + fetchNode: vi.fn(async () => null), +})) + +const newTag: Tag = { + ...defaultBaseTag, + displayName: 'new tag', +} + +describe('SystemTags tag creation', () => { + let component: VueWrapper + + beforeEach(() => { + vi.clearAllMocks() + vi.mocked(loadState).mockReturnValue(false) + }) + + afterEach(() => { + component?.unmount() + }) + + async function mountAndFailToCreate() { + vi.mocked(createTagForFile).mockRejectedValue(new Error('Failed to create tag')) + component = mount(SystemTags, { props: { fileId: 1 } }) + await flushPromises() + component.findComponent({ name: 'NcSelectTags' }).vm.$emit('option:created', newTag) + await flushPromises() + return component.findComponent({ name: 'NcSelectTags' }) + } + + it('shows the admin restriction message when tag creation is restricted', async () => { + vi.mocked(loadState).mockReturnValue(true) + + const select = await mountAndFailToCreate() + + expect(loadState).toHaveBeenCalledWith('systemtags', 'restrictSystemTagsCreationToAdmin', false) + expect(showError).toHaveBeenCalledWith('System admin disabled tag creation. You can only use existing ones.') + expect(select.props('loading')).toBe(false) + }) + + it('shows the generic message and resets loading when tag creation is not restricted', async () => { + const select = await mountAndFailToCreate() + + expect(loadState).toHaveBeenCalledWith('systemtags', 'restrictSystemTagsCreationToAdmin', false) + expect(showError).toHaveBeenCalledWith('Failed to create tag') + expect(select.props('loading')).toBe(false) + }) +}) diff --git a/apps/systemtags/src/components/SystemTags.vue b/apps/systemtags/src/components/SystemTags.vue index a5f98671f5658..2292ecb248182 100644 --- a/apps/systemtags/src/components/SystemTags.vue +++ b/apps/systemtags/src/components/SystemTags.vue @@ -161,13 +161,13 @@ async function handleCreate(tag: Tag) { sortedTags.value.unshift(createdTag) selectedTags.value.push(createdTag) } catch (error) { - const systemTagsCreationRestrictedToAdmin = loadState('settings', 'restrictSystemTagsCreationToAdmin', false) === true + const systemTagsCreationRestrictedToAdmin = loadState('systemtags', 'restrictSystemTagsCreationToAdmin', false) === true logger.error('Failed to create tag', { error }) if (systemTagsCreationRestrictedToAdmin) { showError(t('systemtags', 'System admin disabled tag creation. You can only use existing ones.')) - return + } else { + showError(t('systemtags', 'Failed to create tag')) } - showError(t('systemtags', 'Failed to create tag')) } loading.value = false