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