-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.test.ts
More file actions
75 lines (61 loc) · 2.01 KB
/
Copy pathauth.test.ts
File metadata and controls
75 lines (61 loc) · 2.01 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
import {
authenticate,
GITHUB_AUTH_API_KEY,
GITHUB_AUTH_SERVICE_URL
} 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 GitHub App authentication when app is installed', async () => {
mockFetch.mockResolvedValue({
ok: true,
status: 200,
json: async () => Promise.resolve({ token: 'ghs_app_123' })
} as Response)
const result = await authenticate(
{ owner: 'dunder-mifflin', repo: 'website' },
'ghp_default_token'
)
expect(result).toBe('ghs_app_123')
expect(mockFetch).toHaveBeenCalledWith(
`${GITHUB_AUTH_SERVICE_URL}/github/dunder-mifflin/website/installation-token`,
expect.objectContaining({
method: 'POST',
headers: { Authorization: `Bearer ${GITHUB_AUTH_API_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')
})
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: 'owner', repo: 'repo' },
customPAT
)
expect(result).toBe(customPAT)
expect(mockFetch).not.toHaveBeenCalled()
})
})