|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2026 LibreSign contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | +import { flushPromises, mount } from '@vue/test-utils' |
| 8 | + |
| 9 | +import axios from '@nextcloud/axios' |
| 10 | +import CertificatePolicy from '../../../views/Settings/CertificatePolicy.vue' |
| 11 | +import * as viewer from '../../../utils/viewer.js' |
| 12 | + |
| 13 | +const loadStateMock = vi.fn() |
| 14 | + |
| 15 | +vi.mock('@nextcloud/axios', () => ({ |
| 16 | + default: { |
| 17 | + post: vi.fn(), |
| 18 | + delete: vi.fn(), |
| 19 | + }, |
| 20 | +})) |
| 21 | + |
| 22 | +vi.mock('@nextcloud/initial-state', () => ({ |
| 23 | + loadState: (...args: unknown[]) => loadStateMock(...args), |
| 24 | +})) |
| 25 | + |
| 26 | +vi.mock('@nextcloud/l10n', () => ({ |
| 27 | + t: vi.fn((_app: string, text: string) => text), |
| 28 | + translate: vi.fn((_app: string, text: string) => text), |
| 29 | + translatePlural: vi.fn((_app: string, singular: string, plural: string, count: number) => (count === 1 ? singular : plural)), |
| 30 | + n: vi.fn((_app: string, singular: string, plural: string, count: number) => (count === 1 ? singular : plural)), |
| 31 | + getLanguage: vi.fn(() => 'en'), |
| 32 | + getLocale: vi.fn(() => 'en'), |
| 33 | + isRTL: vi.fn(() => false), |
| 34 | +})) |
| 35 | + |
| 36 | +vi.mock('@nextcloud/router', () => ({ |
| 37 | + generateOcsUrl: vi.fn((path: string) => path), |
| 38 | +})) |
| 39 | + |
| 40 | +vi.mock('../../../utils/viewer.js', () => ({ |
| 41 | + openDocument: vi.fn(), |
| 42 | +})) |
| 43 | + |
| 44 | +describe('CertificatePolicy.vue', () => { |
| 45 | + beforeEach(() => { |
| 46 | + vi.clearAllMocks() |
| 47 | + loadStateMock.mockImplementation((_app: string, key: string) => { |
| 48 | + if (key === 'certificate_policies_oid') { |
| 49 | + return '1.2.3' |
| 50 | + } |
| 51 | + |
| 52 | + if (key === 'certificate_policies_cps') { |
| 53 | + return '/apps/files/cps.pdf' |
| 54 | + } |
| 55 | + |
| 56 | + return '' |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + function createWrapper() { |
| 61 | + return mount(CertificatePolicy, { |
| 62 | + global: { |
| 63 | + stubs: { |
| 64 | + NcButton: { template: '<button><slot /><slot name="icon" /></button>' }, |
| 65 | + NcNoteCard: { template: '<div><slot /></div>' }, |
| 66 | + NcTextField: true, |
| 67 | + NcIconSvgWrapper: true, |
| 68 | + NcLoadingIcon: true, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }) |
| 72 | + } |
| 73 | + |
| 74 | + it('emits the initial validity on mount', () => { |
| 75 | + const wrapper = createWrapper() |
| 76 | + |
| 77 | + expect(wrapper.emitted('certificate-policy-valid')).toEqual([['/apps/files/cps.pdf']]) |
| 78 | + }) |
| 79 | + |
| 80 | + it('opens the current CPS document in the viewer', () => { |
| 81 | + const wrapper = createWrapper() |
| 82 | + |
| 83 | + wrapper.vm.view() |
| 84 | + |
| 85 | + expect(viewer.openDocument).toHaveBeenCalledWith({ |
| 86 | + fileUrl: '/apps/files/cps.pdf', |
| 87 | + filename: 'Certificate Policy', |
| 88 | + nodeId: null, |
| 89 | + }) |
| 90 | + }) |
| 91 | + |
| 92 | + it('persists the OID and toggles the success flag', async () => { |
| 93 | + vi.useFakeTimers() |
| 94 | + vi.mocked(axios.post).mockResolvedValue({ data: {} }) |
| 95 | + const wrapper = createWrapper() |
| 96 | + |
| 97 | + wrapper.vm.OID = '2.5.4.3' |
| 98 | + await wrapper.vm._saveOID() |
| 99 | + |
| 100 | + expect(axios.post).toHaveBeenCalledWith('/apps/libresign/api/v1/admin/certificate-policy/oid', { |
| 101 | + oid: '2.5.4.3', |
| 102 | + }) |
| 103 | + expect(wrapper.vm.dislaySuccessOID).toBe(true) |
| 104 | + |
| 105 | + await vi.advanceTimersByTimeAsync(2000) |
| 106 | + expect(wrapper.vm.dislaySuccessOID).toBe(false) |
| 107 | + |
| 108 | + vi.useRealTimers() |
| 109 | + }) |
| 110 | + |
| 111 | + it('removes the CPS and emits the updated validity', async () => { |
| 112 | + vi.mocked(axios.delete).mockResolvedValue({ data: {} }) |
| 113 | + const wrapper = createWrapper() |
| 114 | + |
| 115 | + await wrapper.vm.removeCps() |
| 116 | + await flushPromises() |
| 117 | + |
| 118 | + expect(axios.delete).toHaveBeenCalledWith('/apps/libresign/api/v1/admin/certificate-policy') |
| 119 | + expect(wrapper.vm.CPS).toBe('') |
| 120 | + expect(wrapper.emitted('certificate-policy-valid')?.at(-1)).toEqual(['']) |
| 121 | + }) |
| 122 | +}) |
0 commit comments