|
| 1 | +const { runAction } = require('./helpers/create-sandbox'); |
| 2 | + |
| 3 | +function createCoreMock() { |
| 4 | + return { |
| 5 | + getIDToken: jest.fn().mockResolvedValue('mock-id-token'), |
| 6 | + setOutput: jest.fn(), |
| 7 | + setFailed: jest.fn(), |
| 8 | + }; |
| 9 | +} |
| 10 | + |
| 11 | +function okResponse(token) { |
| 12 | + return { ok: true, status: 200, statusText: 'OK', body: { token } }; |
| 13 | +} |
| 14 | + |
| 15 | +function errorResponse(status, statusText) { |
| 16 | + return { ok: false, status, statusText }; |
| 17 | +} |
| 18 | + |
| 19 | +function createFetchMock(responses) { |
| 20 | + let callIndex = 0; |
| 21 | + return jest.fn(async () => { |
| 22 | + if (callIndex >= responses.length) { |
| 23 | + throw new Error(`Unexpected fetch call #${callIndex + 1}`); |
| 24 | + } |
| 25 | + const resp = responses[callIndex++]; |
| 26 | + if (resp instanceof Error) throw resp; |
| 27 | + return { |
| 28 | + ok: resp.ok, |
| 29 | + status: resp.status, |
| 30 | + statusText: resp.statusText, |
| 31 | + json: async () => resp.body, |
| 32 | + }; |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +const DEFAULT_ENV = { |
| 37 | + API_BASE_URL: 'https://api.example.com/api/v1', |
| 38 | + PIPELINES_TOKEN_PATH: 'org/repo', |
| 39 | + FALLBACK_TOKEN: 'fallback-pat', |
| 40 | +}; |
| 41 | + |
| 42 | +const LOGIN_URL = 'https://api.example.com/api/v1/tokens/auth/login'; |
| 43 | +const TOKEN_URL = 'https://api.example.com/api/v1/tokens/pat/org/repo'; |
| 44 | + |
| 45 | +describe('pipelines-credentials action', () => { |
| 46 | + describe('happy path', () => { |
| 47 | + test('OIDC login and token fetch', async () => { |
| 48 | + const core = createCoreMock(); |
| 49 | + const fetch = createFetchMock([ |
| 50 | + okResponse('provider-token'), |
| 51 | + okResponse('pipelines-pat'), |
| 52 | + ]); |
| 53 | + |
| 54 | + await runAction({ coreMock: core, fetchMock: fetch, env: DEFAULT_ENV }); |
| 55 | + |
| 56 | + expect(fetch).toHaveBeenCalledTimes(2); |
| 57 | + expect(fetch.mock.calls[0][0]).toBe(LOGIN_URL); |
| 58 | + expect(fetch.mock.calls[1][0]).toBe(TOKEN_URL); |
| 59 | + expect(core.setOutput).toHaveBeenCalledWith('PIPELINES_TOKEN', 'pipelines-pat'); |
| 60 | + expect(core.setFailed).not.toHaveBeenCalled(); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('fallback behavior', () => { |
| 65 | + test('uses FALLBACK_TOKEN when OIDC fails', async () => { |
| 66 | + const core = createCoreMock(); |
| 67 | + core.getIDToken.mockRejectedValue(new Error('OIDC unavailable')); |
| 68 | + |
| 69 | + await runAction({ coreMock: core, fetchMock: createFetchMock([]), env: DEFAULT_ENV }); |
| 70 | + |
| 71 | + expect(core.setOutput).toHaveBeenCalledWith('PIPELINES_TOKEN', 'fallback-pat'); |
| 72 | + expect(core.setFailed).not.toHaveBeenCalled(); |
| 73 | + }); |
| 74 | + |
| 75 | + test('trims whitespace from FALLBACK_TOKEN', async () => { |
| 76 | + const core = createCoreMock(); |
| 77 | + core.getIDToken.mockRejectedValue(new Error('OIDC unavailable')); |
| 78 | + |
| 79 | + await runAction({ |
| 80 | + coreMock: core, |
| 81 | + fetchMock: createFetchMock([]), |
| 82 | + env: { ...DEFAULT_ENV, FALLBACK_TOKEN: ' padded-token ' }, |
| 83 | + }); |
| 84 | + |
| 85 | + expect(core.setOutput).toHaveBeenCalledWith('PIPELINES_TOKEN', 'padded-token'); |
| 86 | + }); |
| 87 | + |
| 88 | + test('calls setFailed when API fails and no FALLBACK_TOKEN', async () => { |
| 89 | + const core = createCoreMock(); |
| 90 | + core.getIDToken.mockRejectedValue(new Error('OIDC unavailable')); |
| 91 | + |
| 92 | + await runAction({ |
| 93 | + coreMock: core, |
| 94 | + fetchMock: createFetchMock([]), |
| 95 | + env: { ...DEFAULT_ENV, FALLBACK_TOKEN: '' }, |
| 96 | + }); |
| 97 | + |
| 98 | + expect(core.setFailed).toHaveBeenCalled(); |
| 99 | + }); |
| 100 | + |
| 101 | + test('uses FALLBACK_TOKEN when login returns non-retryable error', async () => { |
| 102 | + const core = createCoreMock(); |
| 103 | + const fetch = createFetchMock([ |
| 104 | + errorResponse(403, 'Forbidden'), |
| 105 | + ]); |
| 106 | + |
| 107 | + await runAction({ coreMock: core, fetchMock: fetch, env: DEFAULT_ENV }); |
| 108 | + |
| 109 | + expect(core.setOutput).toHaveBeenCalledWith('PIPELINES_TOKEN', 'fallback-pat'); |
| 110 | + expect(fetch).toHaveBeenCalledTimes(1); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('retry behavior', () => { |
| 115 | + test.each([ |
| 116 | + { |
| 117 | + name: 'HTTP 500', |
| 118 | + failResponse: errorResponse(500, 'Internal Server Error'), |
| 119 | + }, |
| 120 | + { |
| 121 | + name: 'HTTP 502', |
| 122 | + failResponse: errorResponse(502, 'Bad Gateway'), |
| 123 | + }, |
| 124 | + { |
| 125 | + name: 'HTTP 429 rate limit', |
| 126 | + failResponse: errorResponse(429, 'Too Many Requests'), |
| 127 | + }, |
| 128 | + { |
| 129 | + name: 'ECONNREFUSED network error', |
| 130 | + failResponse: new Error('ECONNREFUSED'), |
| 131 | + }, |
| 132 | + { |
| 133 | + name: 'ETIMEDOUT network error', |
| 134 | + failResponse: new Error('ETIMEDOUT'), |
| 135 | + }, |
| 136 | + { |
| 137 | + name: 'TypeError fetch failed', |
| 138 | + failResponse: new TypeError('fetch failed'), |
| 139 | + }, |
| 140 | + ])('retries on $name then succeeds', async ({ failResponse }) => { |
| 141 | + const core = createCoreMock(); |
| 142 | + const fetch = createFetchMock([ |
| 143 | + failResponse, |
| 144 | + okResponse('provider-token'), |
| 145 | + okResponse('pipelines-pat'), |
| 146 | + ]); |
| 147 | + |
| 148 | + await runAction({ coreMock: core, fetchMock: fetch, env: DEFAULT_ENV }); |
| 149 | + |
| 150 | + expect(core.setOutput).toHaveBeenCalledWith('PIPELINES_TOKEN', 'pipelines-pat'); |
| 151 | + expect(fetch).toHaveBeenCalledTimes(3); |
| 152 | + }); |
| 153 | + |
| 154 | + test('retries multiple times before succeeding', async () => { |
| 155 | + const core = createCoreMock(); |
| 156 | + const fetch = createFetchMock([ |
| 157 | + errorResponse(500, 'Internal Server Error'), |
| 158 | + errorResponse(500, 'Internal Server Error'), |
| 159 | + okResponse('provider-token'), |
| 160 | + okResponse('pipelines-pat'), |
| 161 | + ]); |
| 162 | + |
| 163 | + await runAction({ coreMock: core, fetchMock: fetch, env: DEFAULT_ENV }); |
| 164 | + |
| 165 | + expect(core.setOutput).toHaveBeenCalledWith('PIPELINES_TOKEN', 'pipelines-pat'); |
| 166 | + expect(fetch).toHaveBeenCalledTimes(4); |
| 167 | + }); |
| 168 | + }); |
| 169 | +}); |
0 commit comments