-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.test.ts
More file actions
85 lines (74 loc) · 3.29 KB
/
Copy patherrors.test.ts
File metadata and controls
85 lines (74 loc) · 3.29 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
import { CommsRequestError } from '@doist/comms-sdk'
import { describe, expect, it } from 'vitest'
import { isForbidden, isInsufficientScope } from './errors.js'
describe('isInsufficientScope', () => {
it('returns true for a 403 with "Insufficient scope" error_string', () => {
const error = new CommsRequestError('Request failed with status 403', 403, {
error_code: 109,
error_string: 'Insufficient scope provided: user:write',
})
expect(isInsufficientScope(error)).toBe(true)
})
it('returns false for a 403 without "Insufficient scope"', () => {
const error = new CommsRequestError('Request failed with status 403', 403, {
error_code: 100,
error_string: 'Access denied',
})
expect(isInsufficientScope(error)).toBe(false)
})
it('returns false for non-403 errors', () => {
const error = new CommsRequestError('Request failed with status 401', 401, {
error_code: 100,
error_string: 'Insufficient scope provided: user:write',
})
expect(isInsufficientScope(error)).toBe(false)
})
it('returns false for plain errors', () => {
expect(isInsufficientScope(new Error('something'))).toBe(false)
})
it('returns false for non-error values', () => {
expect(isInsufficientScope(null)).toBe(false)
expect(isInsufficientScope(undefined)).toBe(false)
expect(isInsufficientScope('string')).toBe(false)
})
})
describe('isForbidden', () => {
it('returns true for a 403 with undefined responseData', () => {
const error = new CommsRequestError('Request failed with status 403', 403, undefined)
expect(isForbidden(error)).toBe(true)
})
it('returns true for a 403 with an arbitrary error_string', () => {
const error = new CommsRequestError('Request failed with status 403', 403, {
error_code: 100,
error_string: 'Access denied',
})
expect(isForbidden(error)).toBe(true)
})
it('returns false for non-403 status codes', () => {
expect(isForbidden(new CommsRequestError('Request failed with status 401', 401, {}))).toBe(
false,
)
expect(isForbidden(new CommsRequestError('Request failed with status 404', 404, {}))).toBe(
false,
)
expect(isForbidden(new CommsRequestError('Request failed with status 500', 500, {}))).toBe(
false,
)
})
it('returns false for plain errors and non-object values', () => {
expect(isForbidden(new Error('something'))).toBe(false)
expect(isForbidden(null)).toBe(false)
expect(isForbidden(undefined)).toBe(false)
expect(isForbidden('string')).toBe(false)
})
// Exclusive with isInsufficientScope: a scope 403 must NOT also classify as
// a plain FORBIDDEN, so callers can check the two predicates in any order.
it('returns false for an "Insufficient scope" 403 (exclusive with isInsufficientScope)', () => {
const error = new CommsRequestError('Request failed with status 403', 403, {
error_code: 109,
error_string: 'Insufficient scope provided: user:write',
})
expect(isInsufficientScope(error)).toBe(true)
expect(isForbidden(error)).toBe(false)
})
})