|
| 1 | +// SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 2 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 3 | + |
| 4 | +import { describe, expect, it, vi } from 'vitest' |
| 5 | +import { flushPromises, mount } from '@vue/test-utils' |
| 6 | +import { defineComponent } from 'vue' |
| 7 | +import LoginSetup from '../../views/LoginSetup.vue' |
| 8 | + |
| 9 | +Object.defineProperty(window, 'matchMedia', { |
| 10 | + writable: true, |
| 11 | + value: vi.fn().mockImplementation(() => ({ |
| 12 | + matches: false, |
| 13 | + addEventListener: vi.fn(), |
| 14 | + removeEventListener: vi.fn(), |
| 15 | + })), |
| 16 | +}) |
| 17 | + |
| 18 | +vi.mock('@nextcloud/l10n', () => ({ |
| 19 | + t: (_app: string, text: string, parameters?: Record<string, string | number>) => { |
| 20 | + if (parameters === undefined) { |
| 21 | + return `tr:${text}` |
| 22 | + } |
| 23 | + return Object.entries(parameters).reduce( |
| 24 | + (translated, [key, value]) => translated.replace(`{${key}}`, String(value)), |
| 25 | + `tr:${text}`, |
| 26 | + ) |
| 27 | + }, |
| 28 | +})) |
| 29 | + |
| 30 | +vi.mock('@nextcloud/axios', () => ({ |
| 31 | + default: { |
| 32 | + get: vi.fn(), |
| 33 | + post: vi.fn(), |
| 34 | + }, |
| 35 | +})) |
| 36 | + |
| 37 | +vi.mock('@nextcloud/router', () => ({ |
| 38 | + generateOcsUrl: (url: string, params: Record<string, string> = {}) => Object.entries(params).reduce( |
| 39 | + (acc, [key, value]) => acc.replace(`{${key}}`, value), |
| 40 | + url, |
| 41 | + ), |
| 42 | +})) |
| 43 | + |
| 44 | +vi.mock('dompurify', () => ({ |
| 45 | + default: { sanitize: (value: string) => value }, |
| 46 | +})) |
| 47 | + |
| 48 | +vi.mock('@nextcloud/vue/components/NcButton', () => ({ |
| 49 | + default: defineComponent({ |
| 50 | + emits: ['click'], |
| 51 | + template: '<button type="button" @click="$emit(\'click\', $event)"><slot /></button>', |
| 52 | + }), |
| 53 | +})) |
| 54 | + |
| 55 | +vi.mock('@nextcloud/vue/components/NcLoadingIcon', () => ({ |
| 56 | + default: defineComponent({ template: '<div class="nc-loading-icon" />' }), |
| 57 | +})) |
| 58 | + |
| 59 | +vi.mock('@nextcloud/vue/components/NcTextField', () => ({ |
| 60 | + default: defineComponent({ |
| 61 | + props: ['modelValue', 'error', 'helperText'], |
| 62 | + emits: ['update:modelValue'], |
| 63 | + template: '<input type="text" :value="modelValue" @input="$emit(\'update:modelValue\', $event.target.value)">', |
| 64 | + }), |
| 65 | +})) |
| 66 | + |
| 67 | +const makeProps = (overrides: Record<string, unknown> = {}) => ({ |
| 68 | + gatewayName: 'signal', |
| 69 | + displayName: 'Signal', |
| 70 | + instructions: 'Install Signal first', |
| 71 | + isComplete: true, |
| 72 | + ...overrides, |
| 73 | +}) |
| 74 | + |
| 75 | +describe('LoginSetup', () => { |
| 76 | + it('shows the not-available message when the gateway is not configured', async () => { |
| 77 | + const wrapper = mount(LoginSetup, { props: makeProps({ isComplete: false }) }) |
| 78 | + await flushPromises() |
| 79 | + |
| 80 | + expect(wrapper.text()).toContain('tr:Signal is not available. Please ask your administrator to finish setting it up.') |
| 81 | + expect(wrapper.find('.nc-loading-icon').exists()).toBe(false) |
| 82 | + }) |
| 83 | + |
| 84 | + it('starts at the identifier step when the user has no in-flight verification', async () => { |
| 85 | + const axios = (await import('@nextcloud/axios')).default |
| 86 | + vi.mocked(axios.get).mockResolvedValueOnce({ data: { state: 0, phoneNumber: null } }) |
| 87 | + |
| 88 | + const wrapper = mount(LoginSetup, { props: makeProps() }) |
| 89 | + await flushPromises() |
| 90 | + |
| 91 | + expect((wrapper.vm as unknown as { state: number }).state).toBe(1) |
| 92 | + expect(wrapper.text()).toContain('tr:Enter your identification (e.g. phone number to start the verification):') |
| 93 | + }) |
| 94 | + |
| 95 | + it('resumes at the confirmation step when the server reports state 2', async () => { |
| 96 | + const axios = (await import('@nextcloud/axios')).default |
| 97 | + vi.mocked(axios.get).mockResolvedValueOnce({ data: { state: 2, phoneNumber: '+33 6 ** ** ** 12' } }) |
| 98 | + |
| 99 | + const wrapper = mount(LoginSetup, { props: makeProps() }) |
| 100 | + await flushPromises() |
| 101 | + |
| 102 | + expect((wrapper.vm as unknown as { state: number }).state).toBe(2) |
| 103 | + expect(wrapper.text()).toContain('+33 6 ** ** ** 12') |
| 104 | + }) |
| 105 | + |
| 106 | + it('moves to the confirmation step after a successful verify call', async () => { |
| 107 | + const axios = (await import('@nextcloud/axios')).default |
| 108 | + vi.mocked(axios.get).mockResolvedValueOnce({ data: { state: 0, phoneNumber: null } }) |
| 109 | + vi.mocked(axios.post).mockResolvedValueOnce({ data: { phoneNumber: '+33 6 ** ** ** 12' } }) |
| 110 | + |
| 111 | + const wrapper = mount(LoginSetup, { props: makeProps() }) |
| 112 | + await flushPromises() |
| 113 | + |
| 114 | + const vm = wrapper.vm as unknown as { identifier: string; verify: () => Promise<void>; state: number; phoneNumber: string } |
| 115 | + vm.identifier = '+33612345612' |
| 116 | + await vm.verify() |
| 117 | + await flushPromises() |
| 118 | + |
| 119 | + expect(vm.state).toBe(2) |
| 120 | + expect(vm.phoneNumber).toBe('+33 6 ** ** ** 12') |
| 121 | + }) |
| 122 | + |
| 123 | + it('surfaces a verification error when the confirm call fails', async () => { |
| 124 | + const axios = (await import('@nextcloud/axios')).default |
| 125 | + vi.mocked(axios.get).mockResolvedValueOnce({ data: { state: 2, phoneNumber: '+33' } }) |
| 126 | + vi.mocked(axios.post).mockRejectedValueOnce({ response: { data: { ocs: { data: { message: 'Wrong code' } } } } }) |
| 127 | + |
| 128 | + const wrapper = mount(LoginSetup, { props: makeProps() }) |
| 129 | + await flushPromises() |
| 130 | + |
| 131 | + const vm = wrapper.vm as unknown as { confirmationCode: string; confirm: () => Promise<void>; state: number; verificationError: string } |
| 132 | + vm.confirmationCode = '000000' |
| 133 | + await vm.confirm() |
| 134 | + await flushPromises() |
| 135 | + |
| 136 | + expect(vm.state).toBe(1) |
| 137 | + expect(vm.verificationError).toBe('Wrong code') |
| 138 | + }) |
| 139 | + |
| 140 | + it('submits the redirect form after a successful confirmation', async () => { |
| 141 | + const axios = (await import('@nextcloud/axios')).default |
| 142 | + vi.mocked(axios.get).mockResolvedValueOnce({ data: { state: 2, phoneNumber: '+33' } }) |
| 143 | + vi.mocked(axios.post).mockResolvedValueOnce({ data: {} }) |
| 144 | + |
| 145 | + const wrapper = mount(LoginSetup, { props: makeProps() }) |
| 146 | + await flushPromises() |
| 147 | + |
| 148 | + const submit = vi.fn() |
| 149 | + const vm = wrapper.vm as unknown as { |
| 150 | + confirmationCode: string |
| 151 | + confirm: () => Promise<void> |
| 152 | + state: number |
| 153 | + $refs: { redirectForm: HTMLFormElement } |
| 154 | + } |
| 155 | + vm.$refs.redirectForm.submit = submit |
| 156 | + vm.confirmationCode = '123456' |
| 157 | + await vm.confirm() |
| 158 | + await flushPromises() |
| 159 | + |
| 160 | + expect(vm.state).toBe(3) |
| 161 | + expect(submit).toHaveBeenCalledTimes(1) |
| 162 | + }) |
| 163 | +}) |
0 commit comments