Repository navigation
Expand file tree
/
Copy pathcli-auth.test.ts
More file actions
63 lines (52 loc) · 2.45 KB
/
Copy pathcli-auth.test.ts
File metadata and controls
63 lines (52 loc) · 2.45 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
import { describe, expect, it, vi } from 'vitest'
import { buildApprovalUrl, pollOnce } from './cli-auth'
const ORIGIN = 'https://www.sim.test/prefix'
const REQUEST = 'request'
const VERIFIER = 'verifier'
function jsonResponse(status: number, body: unknown, headers?: Record<string, string>): Response {
return new Response(JSON.stringify(body), {
status,
headers: { 'content-type': 'application/json', ...headers },
})
}
describe('buildApprovalUrl', () => {
it('preserves the origin prefix and never includes the poll secret', () => {
const url = buildApprovalUrl(ORIGIN, REQUEST, 'challenge', 'ABCD-2345')
expect(url).toMatch(/^https:\/\/www\.sim\.test\/prefix\/cli\/auth\?/)
expect(url).toContain('challenge=challenge')
expect(url).not.toContain(VERIFIER)
})
})
describe('pollOnce', () => {
it('keeps pending, rate-limited, server, and transport failures retryable', async () => {
const fetchMock = vi.spyOn(globalThis, 'fetch')
fetchMock.mockResolvedValueOnce(jsonResponse(200, { status: 'pending' }))
await expect(pollOnce(ORIGIN, REQUEST, VERIFIER)).resolves.toEqual({ status: 'pending' })
fetchMock.mockResolvedValueOnce(
jsonResponse(429, { error: 'slow down' }, { 'retry-after': '4' })
)
await expect(pollOnce(ORIGIN, REQUEST, VERIFIER)).resolves.toEqual({
status: 'pending',
retryAfterMs: 4000,
})
fetchMock.mockResolvedValueOnce(jsonResponse(503, { error: 'deploying' }))
await expect(pollOnce(ORIGIN, REQUEST, VERIFIER)).resolves.toEqual({ status: 'pending' })
fetchMock.mockResolvedValueOnce(jsonResponse(409, { error: 'name collision' }))
await expect(pollOnce(ORIGIN, REQUEST, VERIFIER)).resolves.toEqual({ status: 'pending' })
fetchMock.mockRejectedValueOnce(new TypeError('fetch failed'))
await expect(pollOnce(ORIGIN, REQUEST, VERIFIER)).resolves.toEqual({ status: 'pending' })
fetchMock.mockRejectedValueOnce(new DOMException('timed out', 'TimeoutError'))
await expect(pollOnce(ORIGIN, REQUEST, VERIFIER)).resolves.toEqual({ status: 'pending' })
})
it('refuses redirects instead of forwarding the poll secret', async () => {
vi.spyOn(globalThis, 'fetch').mockResolvedValue(
new Response(null, {
status: 301,
headers: { location: 'https://other.sim.test/api/cli/auth/poll' },
})
)
await expect(pollOnce(ORIGIN, REQUEST, VERIFIER)).rejects.toThrow(
'will not forward the poll secret across a redirect'
)
})
})