-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathheaders.test.ts
More file actions
132 lines (114 loc) · 3.89 KB
/
Copy pathheaders.test.ts
File metadata and controls
132 lines (114 loc) · 3.89 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import {buildHeaders, sanitizedHeadersOutput, GraphQLClientError} from './headers.js'
import {CLI_KIT_VERSION} from '../../../public/common/version.js'
import {randomUUID} from '../../../public/node/crypto.js'
import {firstPartyDev, isUnitTest} from '../../../public/node/context/local.js'
import {test, vi, expect, describe, beforeEach} from 'vitest'
vi.mock('../../../public/node/crypto.js')
vi.mock('../../../public/node/context/local.js')
vi.mock('../version')
beforeEach(() => {
vi.mocked(isUnitTest).mockReturnValue(true)
})
describe('common API methods', () => {
test('headers are built correctly when firstPartyDev yields true', () => {
// Given
vi.mocked(randomUUID).mockReturnValue('random-uuid')
vi.mocked(firstPartyDev).mockReturnValue(true)
// When
const headers = buildHeaders('my-token')
// Then
const version = CLI_KIT_VERSION
expect(headers).toEqual({
'Content-Type': 'application/json',
'Keep-Alive': 'timeout=30',
'X-Shopify-Access-Token': 'Bearer my-token',
'User-Agent': `Shopify CLI; v=${version}`,
authorization: 'Bearer my-token',
'Sec-CH-UA-PLATFORM': process.platform,
'X-Shopify-Cli-Employee': '1',
})
})
test('when user is not employee, do not include header', () => {
// Given
vi.mocked(randomUUID).mockReturnValue('random-uuid')
vi.mocked(firstPartyDev).mockReturnValue(false)
// When
const headers = buildHeaders('my-token')
// Then
const version = CLI_KIT_VERSION
expect(headers).toEqual({
'Content-Type': 'application/json',
'Keep-Alive': 'timeout=30',
'X-Shopify-Access-Token': 'Bearer my-token',
'User-Agent': `Shopify CLI; v=${version}`,
authorization: 'Bearer my-token',
'Sec-CH-UA-PLATFORM': process.platform,
})
})
test.each(['shpat', 'shpua', 'shpca', 'shptka'])(
`when custom app token starts with %s, do not prepend 'Bearer'`,
(prefix) => {
// Given
vi.mocked(randomUUID).mockReturnValue('random-uuid')
vi.mocked(firstPartyDev).mockReturnValue(false)
const token = `${prefix}_my_token`
// When
const headers = buildHeaders(token)
// Then
const version = CLI_KIT_VERSION
expect(headers).toEqual({
'Content-Type': 'application/json',
'Keep-Alive': 'timeout=30',
'X-Shopify-Access-Token': token,
'User-Agent': `Shopify CLI; v=${version}`,
authorization: token,
'Sec-CH-UA-PLATFORM': process.platform,
})
},
)
test('sanitizedHeadersOutput removes sensitive headers', () => {
// Given
const headers = {
'User-Agent': 'useragent',
'Keep-Alive': 'timeout=30',
Authorization: 'token',
authorization: 'token',
'Content-Type': 'application/json',
'X-Shopify-Access-Token': 'token',
Cookie: 'session=123',
'Set-Cookie': 'session=456',
}
// When
const got = sanitizedHeadersOutput(headers)
// Then
expect(got).toMatchInlineSnapshot(`
" - User-Agent: useragent
- Keep-Alive: timeout=30
- Content-Type: application/json"
`)
})
})
describe('GraphQLClientError', () => {
test('sets custom tryMessage for 403 status code', () => {
// Given
const message = 'Forbidden access'
const statusCode = 403
// When
const error = new GraphQLClientError(message, statusCode)
// Then
expect(error.message).toBe(message)
expect(error.statusCode).toBe(403)
expect(error.tryMessage).toBe('Ensure you are using the correct account. You can switch with `shopify auth login`')
})
test('does not set tryMessage for non-403 status codes', () => {
// Given
const message = 'Server error'
const statusCode = 500
// When
const error = new GraphQLClientError(message, statusCode)
// Then
expect(error.message).toBe(message)
expect(error.statusCode).toBe(500)
expect(error.tryMessage).toBeNull()
})
})