|
| 1 | +/*! |
| 2 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { beforeEach, describe, expect, test, vi } from 'vitest' |
| 7 | + |
| 8 | +const isAxiosErrorMock = vi.fn() |
| 9 | +const loggerDebugMock = vi.fn() |
| 10 | + |
| 11 | +vi.mock('@nextcloud/axios', () => ({ |
| 12 | + isAxiosError: isAxiosErrorMock, |
| 13 | +})) |
| 14 | + |
| 15 | +vi.mock('./utils/logger.ts', () => ({ |
| 16 | + logger: { |
| 17 | + debug: loggerDebugMock, |
| 18 | + }, |
| 19 | +})) |
| 20 | + |
| 21 | +describe('isConfirmationError', () => { |
| 22 | + beforeEach(() => { |
| 23 | + vi.resetModules() |
| 24 | + vi.clearAllMocks() |
| 25 | + window._oc_config = undefined |
| 26 | + }) |
| 27 | + |
| 28 | + async function importSubject(version?: string) { |
| 29 | + window._oc_config = version ? { version } : undefined |
| 30 | + return import('./apiError.ts') |
| 31 | + } |
| 32 | + |
| 33 | + test('returns false for non axios errors', async () => { |
| 34 | + const { isConfirmationError } = await importSubject('33.0.0') |
| 35 | + isAxiosErrorMock.mockReturnValue(false) |
| 36 | + |
| 37 | + expect(isConfirmationError(new Error('nope'))).toBe(false) |
| 38 | + expect(loggerDebugMock).not.toBeCalled() |
| 39 | + }) |
| 40 | + |
| 41 | + test('returns false when axios error has no response', async () => { |
| 42 | + const { isConfirmationError } = await importSubject('33.0.0') |
| 43 | + isAxiosErrorMock.mockReturnValue(true) |
| 44 | + |
| 45 | + expect(isConfirmationError({ response: undefined })).toBe(false) |
| 46 | + expect(loggerDebugMock).not.toBeCalled() |
| 47 | + }) |
| 48 | + |
| 49 | + test('uses header based detection on Nextcloud 32', async () => { |
| 50 | + const { isConfirmationError } = await importSubject('32.0.7') |
| 51 | + isAxiosErrorMock.mockReturnValue(true) |
| 52 | + |
| 53 | + const error = { |
| 54 | + response: { |
| 55 | + headers: { |
| 56 | + 'x-nextcloud-password-confirmation': 'true', |
| 57 | + }, |
| 58 | + status: 403, |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + expect(isConfirmationError(error)).toBe(true) |
| 63 | + expect(loggerDebugMock).toBeCalledWith('Handle modern confirmation error based on header', { hasConfirmationHeader: true }) |
| 64 | + }) |
| 65 | + |
| 66 | + test('returns false if header is not present on Nextcloud 32', async () => { |
| 67 | + const { isConfirmationError } = await importSubject('32.0.7') |
| 68 | + isAxiosErrorMock.mockReturnValue(true) |
| 69 | + |
| 70 | + const error = { |
| 71 | + response: { |
| 72 | + status: 403, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + expect(isConfirmationError(error)).toBe(false) |
| 77 | + expect(loggerDebugMock).toBeCalledWith('Handle modern confirmation error based on header', { hasConfirmationHeader: false }) |
| 78 | + }) |
| 79 | + |
| 80 | + test('uses status based detection on Nextcloud 31', async () => { |
| 81 | + const { isConfirmationError } = await importSubject('31.0.8') |
| 82 | + isAxiosErrorMock.mockReturnValue(true) |
| 83 | + |
| 84 | + const error = { |
| 85 | + response: { |
| 86 | + status: 403, |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + expect(isConfirmationError(error)).toBe(true) |
| 91 | + expect(loggerDebugMock).toBeCalledWith('Handle legacy confirmation error based on status code', { status: 403 }) |
| 92 | + }) |
| 93 | +}) |
0 commit comments