|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { createMockRequest, hybridAuthMockFns } from '@sim/testing' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const { mockSecureFetch, mockValidateUrl, MOCK_MAX_JSON_BYTES } = vi.hoisted(() => ({ |
| 8 | + mockSecureFetch: vi.fn(), |
| 9 | + mockValidateUrl: vi.fn(), |
| 10 | + MOCK_MAX_JSON_BYTES: 10 * 1024 * 1024, |
| 11 | +})) |
| 12 | + |
| 13 | +vi.mock('@/lib/core/security/input-validation.server', () => ({ |
| 14 | + secureFetchWithPinnedIP: mockSecureFetch, |
| 15 | + validateUrlWithDNS: mockValidateUrl, |
| 16 | + MAX_JSON_API_RESPONSE_BYTES: MOCK_MAX_JSON_BYTES, |
| 17 | +})) |
| 18 | + |
| 19 | +import { POST } from '@/app/api/tools/grafana/check_data_source_health/route' |
| 20 | + |
| 21 | +const baseBody = { |
| 22 | + apiKey: 'glsa_token', |
| 23 | + baseUrl: 'https://grafana.example.com', |
| 24 | + dataSourceUid: 'P1234AB5678', |
| 25 | +} |
| 26 | + |
| 27 | +function grafanaResponse(body: unknown, status: number) { |
| 28 | + return { |
| 29 | + ok: status >= 200 && status < 300, |
| 30 | + status, |
| 31 | + statusText: '', |
| 32 | + headers: new Headers(), |
| 33 | + text: async () => (typeof body === 'string' ? body : JSON.stringify(body)), |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +function post(body: Record<string, unknown> = baseBody) { |
| 38 | + return POST(createMockRequest('POST', body) as never, undefined as never) |
| 39 | +} |
| 40 | + |
| 41 | +describe('POST /api/tools/grafana/check_data_source_health', () => { |
| 42 | + beforeEach(() => { |
| 43 | + vi.clearAllMocks() |
| 44 | + hybridAuthMockFns.mockCheckInternalAuth.mockResolvedValue({ success: true, userId: 'user-1' }) |
| 45 | + mockValidateUrl.mockResolvedValue({ isValid: true, resolvedIP: '203.0.113.10' }) |
| 46 | + }) |
| 47 | + |
| 48 | + it('reports a healthy data source', async () => { |
| 49 | + mockSecureFetch.mockResolvedValue( |
| 50 | + grafanaResponse({ status: 'OK', message: 'Data source is working' }, 200) |
| 51 | + ) |
| 52 | + |
| 53 | + const response = await post() |
| 54 | + const data = await response.json() |
| 55 | + |
| 56 | + expect(data.success).toBe(true) |
| 57 | + expect(data.output).toEqual({ status: 'OK', message: 'Data source is working' }) |
| 58 | + }) |
| 59 | + |
| 60 | + it('reports an UNHEALTHY data source, which Grafana answers with HTTP 400', async () => { |
| 61 | + mockSecureFetch.mockResolvedValue( |
| 62 | + grafanaResponse({ status: 'ERROR', message: 'dial tcp: connection refused' }, 400) |
| 63 | + ) |
| 64 | + |
| 65 | + const response = await post() |
| 66 | + const data = await response.json() |
| 67 | + |
| 68 | + expect(data.success).toBe(true) |
| 69 | + expect(data.output.status).toBe('ERROR') |
| 70 | + expect(data.output.message).toBe('dial tcp: connection refused') |
| 71 | + }) |
| 72 | + |
| 73 | + it('surfaces the plugin details when Grafana supplies them', async () => { |
| 74 | + mockSecureFetch.mockResolvedValue( |
| 75 | + grafanaResponse( |
| 76 | + { status: 'ERROR', message: 'bad query', details: { verboseMessage: 'x' } }, |
| 77 | + 400 |
| 78 | + ) |
| 79 | + ) |
| 80 | + |
| 81 | + const response = await post() |
| 82 | + const data = await response.json() |
| 83 | + |
| 84 | + expect(data.output.details).toEqual({ verboseMessage: 'x' }) |
| 85 | + }) |
| 86 | + |
| 87 | + it('treats a failure with no health verdict as a real request failure', async () => { |
| 88 | + mockSecureFetch.mockResolvedValue(grafanaResponse({ message: 'Data source not found' }, 404)) |
| 89 | + |
| 90 | + const response = await post() |
| 91 | + const data = await response.json() |
| 92 | + |
| 93 | + expect(data.success).toBe(false) |
| 94 | + expect(data.error).toContain('404') |
| 95 | + }) |
| 96 | + |
| 97 | + it('bounds and protects the outbound call', async () => { |
| 98 | + mockSecureFetch.mockResolvedValue(grafanaResponse({ status: 'OK', message: 'ok' }, 200)) |
| 99 | + |
| 100 | + await post() |
| 101 | + |
| 102 | + const [url, resolvedIP, options] = mockSecureFetch.mock.calls[0] |
| 103 | + expect(resolvedIP).toBe('203.0.113.10') |
| 104 | + expect(url).toBe('https://grafana.example.com/api/datasources/uid/P1234AB5678/health') |
| 105 | + expect(options.maxResponseBytes).toBe(MOCK_MAX_JSON_BYTES) |
| 106 | + expect(options.timeout).toBeGreaterThan(0) |
| 107 | + expect(options.stripAuthOnRedirect).toBe(true) |
| 108 | + expect(options.headers.Authorization).toBe('Bearer glsa_token') |
| 109 | + }) |
| 110 | + |
| 111 | + it('encodes the UID so it cannot re-target the request path', async () => { |
| 112 | + mockSecureFetch.mockResolvedValue(grafanaResponse({ status: 'OK', message: 'ok' }, 200)) |
| 113 | + |
| 114 | + await post({ ...baseBody, dataSourceUid: 'a/../../admin' }) |
| 115 | + |
| 116 | + const [url] = mockSecureFetch.mock.calls[0] |
| 117 | + expect(url).toBe('https://grafana.example.com/api/datasources/uid/a%2F..%2F..%2Fadmin/health') |
| 118 | + }) |
| 119 | + |
| 120 | + it('rejects an unauthenticated request before reaching Grafana', async () => { |
| 121 | + hybridAuthMockFns.mockCheckInternalAuth.mockResolvedValue({ |
| 122 | + success: false, |
| 123 | + error: 'Authentication required', |
| 124 | + }) |
| 125 | + |
| 126 | + const response = await post() |
| 127 | + |
| 128 | + expect(response.status).toBe(401) |
| 129 | + expect(mockSecureFetch).not.toHaveBeenCalled() |
| 130 | + }) |
| 131 | +}) |
0 commit comments