|
| 1 | +/** |
| 2 | + * V48-Gate3-F33: getAuthFromConnection's GitHub App installation-token |
| 3 | + * regeneration reads `installation_token_expires_at` to decide whether the |
| 4 | + * stored token is stale, but its own updateTokens() call used to persist the |
| 5 | + * fresh expiry ONLY to `token_expires_at` — a different field, never read by |
| 6 | + * that same expiry check. So every regeneration "succeeded" yet the field |
| 7 | + * gate on kept reporting stale, forcing a full re-regeneration on literally |
| 8 | + * every subsequent call (Refresh, repo/branch/commit fetches, etc.) forever. |
| 9 | + * Pin that both fields move together. |
| 10 | + */ |
| 11 | + |
| 12 | +const mockGetById = jest.fn(); |
| 13 | +const mockUpdate = jest.fn(); |
| 14 | + |
| 15 | +jest.mock('@bitcode/orm', () => ({ |
| 16 | + UserConnectionsModel: jest.fn().mockImplementation(() => ({ |
| 17 | + getById: mockGetById, |
| 18 | + update: mockUpdate, |
| 19 | + getByUserAndProvider: jest.fn(), |
| 20 | + listByUserId: jest.fn(), |
| 21 | + getByProviderUserId: jest.fn(), |
| 22 | + getAuthFromConnectionByInstallationId: jest.fn(), |
| 23 | + })), |
| 24 | +})); |
| 25 | + |
| 26 | +const mockGenerateInstallationToken = jest.fn(); |
| 27 | + |
| 28 | +jest.mock('@bitcode/github', () => ({ |
| 29 | + GitHubAppAuth: jest.fn().mockImplementation(() => ({ |
| 30 | + generateInstallationToken: mockGenerateInstallationToken, |
| 31 | + })), |
| 32 | +})); |
| 33 | + |
| 34 | +import { VCSConnections } from '../connections'; |
| 35 | + |
| 36 | +const CONNECTION_ID = '11111111-1111-1111-1111-111111111111'; |
| 37 | +const FAR_FUTURE_ISO = new Date(Date.now() + 1000 * 60 * 60 * 24 * 365 * 5).toISOString(); |
| 38 | +const FAR_PAST_ISO = new Date(Date.now() - 1000 * 60 * 60 * 24 * 365 * 5).toISOString(); |
| 39 | + |
| 40 | +describe('VCSConnections.getAuthFromConnection — GitHub installation token regeneration', () => { |
| 41 | + beforeEach(() => { |
| 42 | + jest.clearAllMocks(); |
| 43 | + process.env.GITHUB_APP_ID = '12345'; |
| 44 | + process.env.GITHUB_PRIVATE_KEY = 'test-private-key'; |
| 45 | + }); |
| 46 | + |
| 47 | + afterEach(() => { |
| 48 | + delete process.env.GITHUB_APP_ID; |
| 49 | + delete process.env.GITHUB_PRIVATE_KEY; |
| 50 | + }); |
| 51 | + |
| 52 | + it('regenerates an expired installation token and persists both expiry fields in lockstep', async () => { |
| 53 | + mockGetById.mockResolvedValue({ |
| 54 | + id: CONNECTION_ID, |
| 55 | + provider: 'github', |
| 56 | + connection_data: { |
| 57 | + connectionId: '99999', |
| 58 | + access_token: 'stale-token', |
| 59 | + installation_token_expires_at: FAR_PAST_ISO, |
| 60 | + }, |
| 61 | + }); |
| 62 | + mockGenerateInstallationToken.mockResolvedValue({ |
| 63 | + token: 'fresh-token', |
| 64 | + expiresAt: new Date(FAR_FUTURE_ISO), |
| 65 | + }); |
| 66 | + |
| 67 | + const connections = new VCSConnections({} as never); |
| 68 | + const auth = await connections.getAuthFromConnection(CONNECTION_ID); |
| 69 | + |
| 70 | + expect(auth?.accessToken).toBe('fresh-token'); |
| 71 | + expect(mockUpdate).toHaveBeenCalledWith( |
| 72 | + CONNECTION_ID, |
| 73 | + expect.objectContaining({ |
| 74 | + connection_data: expect.objectContaining({ |
| 75 | + access_token: 'fresh-token', |
| 76 | + token_expires_at: FAR_FUTURE_ISO, |
| 77 | + installation_token_expires_at: FAR_FUTURE_ISO, |
| 78 | + }), |
| 79 | + }), |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + it('does not re-attempt regeneration once installation_token_expires_at reflects the fresh token', async () => { |
| 84 | + mockGetById.mockResolvedValue({ |
| 85 | + id: CONNECTION_ID, |
| 86 | + provider: 'github', |
| 87 | + connection_data: { |
| 88 | + connectionId: '99999', |
| 89 | + access_token: 'already-fresh-token', |
| 90 | + installation_token_expires_at: FAR_FUTURE_ISO, |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + const connections = new VCSConnections({} as never); |
| 95 | + const auth = await connections.getAuthFromConnection(CONNECTION_ID); |
| 96 | + |
| 97 | + expect(auth?.accessToken).toBe('already-fresh-token'); |
| 98 | + expect(mockGenerateInstallationToken).not.toHaveBeenCalled(); |
| 99 | + expect(mockUpdate).not.toHaveBeenCalled(); |
| 100 | + }); |
| 101 | +}); |
0 commit comments