-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.test.ts
More file actions
145 lines (122 loc) · 4.06 KB
/
auth.test.ts
File metadata and controls
145 lines (122 loc) · 4.06 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
133
134
135
136
137
138
139
140
141
142
143
144
145
import { authenticate, PRODUCTION_SERVICE, STAGING_SERVICE } from '../src/auth'
import { jest } from '@jest/globals'
describe('authenticate', () => {
const mockFetch = jest.spyOn(global, 'fetch')
beforeEach(() => {
process.env.GITHUB_TOKEN = 'ghp_default_token'
mockFetch.mockClear()
})
it('should use production GitHub App when installed', async () => {
mockFetch.mockResolvedValue({
ok: true,
status: 200,
json: async () => Promise.resolve({ token: 'ghs_prod_token' })
} as Response)
const result = await authenticate(
{ owner: 'dunder-mifflin', repo: 'website' },
'ghp_default_token'
)
expect(result).toBe('ghs_prod_token')
expect(mockFetch).toHaveBeenCalledTimes(1)
expect(mockFetch).toHaveBeenCalledWith(
`${PRODUCTION_SERVICE.url}/github/dunder-mifflin/website/installation-token`,
expect.objectContaining({
method: 'POST',
headers: { Authorization: `Bearer ${PRODUCTION_SERVICE.key}` }
})
)
})
it('should use staging GitHub App when production not installed', async () => {
mockFetch
.mockResolvedValueOnce({
ok: false,
status: 404,
json: async () => ({ error: 'Not installed' })
} as Response)
.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({ token: 'ghs_staging_token' })
} as Response)
const result = await authenticate(
{ owner: 'dunder-mifflin', repo: 'website' },
'ghp_default_token'
)
expect(result).toBe('ghs_staging_token')
expect(mockFetch).toHaveBeenCalledTimes(2)
expect(mockFetch).toHaveBeenCalledWith(
`${STAGING_SERVICE.url}/github/dunder-mifflin/website/installation-token`,
expect.objectContaining({
method: 'POST',
headers: { Authorization: `Bearer ${STAGING_SERVICE.key}` }
})
)
})
it('should fall back to standard authentication when app is not installed', async () => {
mockFetch.mockResolvedValue({
ok: false,
status: 404,
json: async () => Promise.resolve({ error: 'Not installed' })
} as Response)
const result = await authenticate(
{ owner: 'dunder-mifflin', repo: 'website' },
'ghp_default_token'
)
expect(result).toBe('ghp_default_token')
expect(mockFetch).toHaveBeenCalledTimes(2)
})
it('should fall back to standard authentication when service is unavailable', async () => {
mockFetch.mockRejectedValue(new Error('Network error'))
const result = await authenticate(
{ owner: 'dunder-mifflin', repo: 'website' },
'ghp_default_token'
)
expect(result).toBe('ghp_default_token')
})
it('should use user-provided PAT when different from GITHUB_TOKEN', async () => {
const customPAT = 'ghp_custom_pat'
const result = await authenticate(
{ owner: 'dunder-mifflin', repo: 'website' },
customPAT
)
expect(result).toBe(customPAT)
expect(mockFetch).not.toHaveBeenCalled()
})
it('should handle all services returning errors', async () => {
mockFetch
.mockResolvedValueOnce({
ok: false,
status: 401,
json: async () => ({ error: 'Unauthorized' })
} as Response)
.mockResolvedValueOnce({
ok: false,
status: 500,
json: async () => ({ error: 'Server error' })
} as Response)
const result = await authenticate(
{ owner: 'dunder-mifflin', repo: 'website' },
'ghp_default_token'
)
expect(result).toBe('ghp_default_token')
expect(mockFetch).toHaveBeenCalledTimes(2)
})
it('should handle unexpected status codes', async () => {
mockFetch
.mockResolvedValueOnce({
ok: false,
status: 403,
json: async () => ({ error: 'Forbidden' })
} as Response)
.mockResolvedValueOnce({
ok: false,
status: 403,
json: async () => ({ error: 'Forbidden' })
} as Response)
const result = await authenticate(
{ owner: 'dunder-mifflin', repo: 'website' },
'ghp_default_token'
)
expect(result).toBe('ghp_default_token')
})
})