|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { jsonResponse } from '@sim/testing/helpers/http' |
| 5 | +import { |
| 6 | + inputValidationMock, |
| 7 | + inputValidationMockFns, |
| 8 | +} from '@sim/testing/mocks/input-validation.mock' |
| 9 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 10 | + |
| 11 | +vi.mock('@/lib/core/security/input-validation.server', () => inputValidationMock) |
| 12 | + |
| 13 | +import { validateGitLabServiceAccount } from '@/lib/credentials/token-service-accounts/validators/gitlab' |
| 14 | + |
| 15 | +const activeToken = { name: 'sim-workflows', active: true, revoked: false, scopes: ['api'] } |
| 16 | + |
| 17 | +describe('GitLab saved token verification', () => { |
| 18 | + const fetch = inputValidationMockFns.mockSecureFetchWithValidation |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + fetch.mockReset() |
| 22 | + }) |
| 23 | + |
| 24 | + it('verifies the token at the host it was typed with and stores that host', async () => { |
| 25 | + fetch.mockResolvedValue(jsonResponse(activeToken)) |
| 26 | + |
| 27 | + await expect( |
| 28 | + validateGitLabServiceAccount({ apiToken: 'glpat-x', domain: 'https://gitlab.example.com/' }) |
| 29 | + ).resolves.toMatchObject({ normalizedDomain: 'gitlab.example.com' }) |
| 30 | + expect(fetch).toHaveBeenCalledWith( |
| 31 | + 'https://gitlab.example.com/api/v4/personal_access_tokens/self', |
| 32 | + expect.objectContaining({ maxRedirects: 0, profile: 'configuredEndpoint' }) |
| 33 | + ) |
| 34 | + }) |
| 35 | + |
| 36 | + it('refuses a host that could send the token somewhere else, without calling it', async () => { |
| 37 | + await expect( |
| 38 | + validateGitLabServiceAccount({ apiToken: 'glpat-x', domain: 'evil.example.com@gitlab.com' }) |
| 39 | + ).rejects.toMatchObject({ code: 'invalid_credentials' }) |
| 40 | + expect(fetch).not.toHaveBeenCalled() |
| 41 | + }) |
| 42 | + |
| 43 | + it.each([ |
| 44 | + ['a revoked token', { ...activeToken, active: false, revoked: true }], |
| 45 | + ['a token without an API scope', { ...activeToken, scopes: ['read_repository'] }], |
| 46 | + ])('rejects %s', async (_, token) => { |
| 47 | + fetch.mockResolvedValue(jsonResponse(token)) |
| 48 | + |
| 49 | + await expect( |
| 50 | + validateGitLabServiceAccount({ apiToken: 'glpat-x', domain: 'gitlab.com' }) |
| 51 | + ).rejects.toMatchObject({ code: 'invalid_credentials' }) |
| 52 | + }) |
| 53 | + |
| 54 | + it('reports an unreachable GitLab instance as unavailable, not as a bad token', async () => { |
| 55 | + fetch.mockResolvedValue(jsonResponse({ message: 'Bad Gateway' }, 502)) |
| 56 | + |
| 57 | + await expect( |
| 58 | + validateGitLabServiceAccount({ apiToken: 'glpat-x', domain: 'gitlab.com' }) |
| 59 | + ).rejects.toMatchObject({ code: 'provider_unavailable' }) |
| 60 | + }) |
| 61 | +}) |
0 commit comments