|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2026 LibreSign contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { mount } from '@vue/test-utils' |
| 7 | +import { describe, expect, it, vi } from 'vitest' |
| 8 | + |
| 9 | +import { createL10nMock } from '../../../testHelpers/l10n.js' |
| 10 | + |
| 11 | +vi.mock('@nextcloud/l10n', () => createL10nMock()) |
| 12 | + |
| 13 | +const { currentUserState, initialConfigState, axiosGet } = vi.hoisted(() => ({ |
| 14 | + currentUserState: { |
| 15 | + isAdmin: true, |
| 16 | + }, |
| 17 | + initialConfigState: { |
| 18 | + manageable_policy_group_ids: [] as string[], |
| 19 | + }, |
| 20 | + axiosGet: vi.fn(), |
| 21 | +})) |
| 22 | + |
| 23 | +vi.mock('@nextcloud/auth', () => ({ |
| 24 | + getCurrentUser: vi.fn(() => currentUserState), |
| 25 | +})) |
| 26 | + |
| 27 | +vi.mock('@nextcloud/initial-state', () => ({ |
| 28 | + loadState: vi.fn((_app: string, key: string, defaultValue: unknown) => { |
| 29 | + if (key === 'config') { |
| 30 | + return initialConfigState |
| 31 | + } |
| 32 | + |
| 33 | + return defaultValue |
| 34 | + }), |
| 35 | +})) |
| 36 | + |
| 37 | +vi.mock('@nextcloud/axios', () => ({ |
| 38 | + default: { |
| 39 | + get: axiosGet, |
| 40 | + }, |
| 41 | +})) |
| 42 | + |
| 43 | +vi.mock('@nextcloud/router', () => ({ |
| 44 | + generateOcsUrl: vi.fn((path: string) => path), |
| 45 | +})) |
| 46 | + |
| 47 | +import ApprovalGroupsRuleEditor from '../../../../views/Settings/PolicyWorkbench/settings/approval-groups/ApprovalGroupsRuleEditor.vue' |
| 48 | + |
| 49 | +function mountEditor(modelValue = '["finance"]') { |
| 50 | + return mount(ApprovalGroupsRuleEditor, { |
| 51 | + props: { |
| 52 | + modelValue, |
| 53 | + }, |
| 54 | + global: { |
| 55 | + stubs: { |
| 56 | + NcSelect: { |
| 57 | + props: ['options'], |
| 58 | + template: '<div class="nc-select-stub">{{ JSON.stringify(options) }}</div>', |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +describe('ApprovalGroupsRuleEditor.vue', () => { |
| 66 | + it('loads groups from cloud/groups/details for instance admin', async () => { |
| 67 | + currentUserState.isAdmin = true |
| 68 | + initialConfigState.manageable_policy_group_ids = [] |
| 69 | + axiosGet.mockReset() |
| 70 | + axiosGet.mockResolvedValue({ |
| 71 | + data: { |
| 72 | + ocs: { |
| 73 | + data: { |
| 74 | + groups: [ |
| 75 | + { id: 'finance', displayname: 'Finance' }, |
| 76 | + { id: 'legal', displayname: 'Legal' }, |
| 77 | + ], |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }) |
| 82 | + |
| 83 | + const wrapper = mountEditor() |
| 84 | + await Promise.resolve() |
| 85 | + await Promise.resolve() |
| 86 | + |
| 87 | + expect(axiosGet).toHaveBeenCalledWith('cloud/groups/details', { |
| 88 | + params: { |
| 89 | + search: '', |
| 90 | + limit: 40, |
| 91 | + offset: 0, |
| 92 | + }, |
| 93 | + }) |
| 94 | + expect(wrapper.text()).toContain('Finance') |
| 95 | + expect(wrapper.text()).toContain('Legal') |
| 96 | + }) |
| 97 | + |
| 98 | + it('filters available groups to manageable scope for group admins', async () => { |
| 99 | + currentUserState.isAdmin = false |
| 100 | + initialConfigState.manageable_policy_group_ids = ['finance'] |
| 101 | + axiosGet.mockReset() |
| 102 | + axiosGet.mockResolvedValue({ |
| 103 | + data: { |
| 104 | + ocs: { |
| 105 | + data: { |
| 106 | + groups: ['finance', 'legal'], |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }) |
| 111 | + |
| 112 | + const wrapper = mountEditor('["finance","legal"]') |
| 113 | + await Promise.resolve() |
| 114 | + await Promise.resolve() |
| 115 | + |
| 116 | + expect(axiosGet).toHaveBeenCalledWith('cloud/groups', { |
| 117 | + params: { |
| 118 | + search: '', |
| 119 | + limit: 40, |
| 120 | + offset: 0, |
| 121 | + }, |
| 122 | + }) |
| 123 | + expect(wrapper.text()).toContain('finance') |
| 124 | + expect(wrapper.text()).not.toContain('legal') |
| 125 | + }) |
| 126 | +}) |
0 commit comments