|
| 1 | +/* |
| 2 | + * Copyright Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/* eslint-disable @backstage/no-undeclared-imports -- deps in dcm-backend package.json */ |
| 18 | +import { mockServices } from '@backstage/backend-test-utils'; |
| 19 | +import express from 'express'; |
| 20 | +import request from 'supertest'; |
| 21 | + |
| 22 | +import { createDcmProxy } from './proxy'; |
| 23 | +import type { RouterOptions } from '../models/RouterOptions'; |
| 24 | + |
| 25 | +const TOKEN_RESPONSE = { |
| 26 | + ok: true, |
| 27 | + json: async () => ({ access_token: 'test-token', expires_in: 3600 }), |
| 28 | +} as Response; |
| 29 | + |
| 30 | +function makeApp(configData: Record<string, Record<string, string>> = {}) { |
| 31 | + const options: RouterOptions = { |
| 32 | + logger: mockServices.rootLogger(), |
| 33 | + config: mockServices.rootConfig({ data: configData }), |
| 34 | + httpAuth: mockServices.httpAuth.mock({ |
| 35 | + credentials: jest.fn().mockResolvedValue({ |
| 36 | + principal: { userEntityRef: 'user:default/test' }, |
| 37 | + }), |
| 38 | + }), |
| 39 | + permissions: mockServices.permissions.mock({ |
| 40 | + authorize: jest.fn().mockResolvedValue([{ result: 'ALLOW' }]), |
| 41 | + }), |
| 42 | + cache: mockServices.cache.mock(), |
| 43 | + }; |
| 44 | + const app = express(); |
| 45 | + app.use(express.json()); |
| 46 | + // Mount using a wildcard path matching the router convention |
| 47 | + app.all('/proxy/*', createDcmProxy(options)); |
| 48 | + return app; |
| 49 | +} |
| 50 | + |
| 51 | +describe('createDcmProxy', () => { |
| 52 | + let fetchSpy: jest.SpyInstance; |
| 53 | + |
| 54 | + afterEach(() => { |
| 55 | + fetchSpy?.mockRestore(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('returns 503 when dcm.apiGatewayUrl is not configured', async () => { |
| 59 | + const app = makeApp({ dcm: { clientId: 'id', clientSecret: 'secret' } }); |
| 60 | + |
| 61 | + const res = await request(app).get('/proxy/providers'); |
| 62 | + |
| 63 | + expect(res.status).toBe(503); |
| 64 | + expect(res.body).toMatchObject({ |
| 65 | + error: expect.stringContaining('not configured'), |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('returns 502 when token acquisition fails', async () => { |
| 70 | + fetchSpy = jest |
| 71 | + .spyOn(globalThis, 'fetch') |
| 72 | + .mockRejectedValueOnce(new Error('SSO unreachable')); |
| 73 | + |
| 74 | + const app = makeApp({ |
| 75 | + dcm: { |
| 76 | + apiGatewayUrl: 'https://gateway.example.com', |
| 77 | + clientId: 'id', |
| 78 | + clientSecret: 'secret', |
| 79 | + }, |
| 80 | + }); |
| 81 | + |
| 82 | + const res = await request(app).get('/proxy/providers'); |
| 83 | + |
| 84 | + expect(res.status).toBe(502); |
| 85 | + expect(res.body).toMatchObject({ |
| 86 | + error: expect.stringContaining('access token'), |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('returns 502 when the upstream fetch throws', async () => { |
| 91 | + fetchSpy = jest |
| 92 | + .spyOn(globalThis, 'fetch') |
| 93 | + // First call: token fetch succeeds |
| 94 | + .mockResolvedValueOnce(TOKEN_RESPONSE) |
| 95 | + // Second call: upstream fetch throws |
| 96 | + .mockRejectedValueOnce(new Error('Connection refused')); |
| 97 | + |
| 98 | + const app = makeApp({ |
| 99 | + dcm: { |
| 100 | + apiGatewayUrl: 'https://gateway.example.com', |
| 101 | + clientId: 'id', |
| 102 | + clientSecret: 'secret', |
| 103 | + }, |
| 104 | + }); |
| 105 | + |
| 106 | + const res = await request(app).get('/proxy/providers'); |
| 107 | + |
| 108 | + expect(res.status).toBe(502); |
| 109 | + expect(res.body).toMatchObject({ |
| 110 | + error: expect.stringContaining('DCM API gateway'), |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it('proxies a GET request and forwards the upstream response', async () => { |
| 115 | + const upstreamBody = JSON.stringify({ items: [] }); |
| 116 | + fetchSpy = jest |
| 117 | + .spyOn(globalThis, 'fetch') |
| 118 | + // Token fetch |
| 119 | + .mockResolvedValueOnce(TOKEN_RESPONSE) |
| 120 | + // Upstream GET |
| 121 | + .mockResolvedValueOnce({ |
| 122 | + status: 200, |
| 123 | + ok: true, |
| 124 | + headers: { |
| 125 | + get: (h: string) => |
| 126 | + h === 'content-type' ? 'application/json' : null, |
| 127 | + }, |
| 128 | + text: async () => upstreamBody, |
| 129 | + } as unknown as Response); |
| 130 | + |
| 131 | + const app = makeApp({ |
| 132 | + dcm: { |
| 133 | + apiGatewayUrl: 'https://gateway.example.com', |
| 134 | + clientId: 'id', |
| 135 | + clientSecret: 'secret', |
| 136 | + }, |
| 137 | + }); |
| 138 | + |
| 139 | + const res = await request(app).get('/proxy/providers?foo=bar'); |
| 140 | + |
| 141 | + expect(res.status).toBe(200); |
| 142 | + expect(res.text).toBe(upstreamBody); |
| 143 | + |
| 144 | + // Verify upstream URL contains path and query param |
| 145 | + const upstreamCall = fetchSpy.mock.calls[1]; |
| 146 | + expect(upstreamCall[0]).toContain('/api/v1alpha1/providers'); |
| 147 | + expect(upstreamCall[0]).toContain('foo=bar'); |
| 148 | + |
| 149 | + // Verify auth header was injected |
| 150 | + expect(upstreamCall[1].headers.Authorization).toBe('Bearer test-token'); |
| 151 | + }); |
| 152 | + |
| 153 | + it('proxies a POST request and forwards the request body', async () => { |
| 154 | + const requestBody = { name: 'my-provider' }; |
| 155 | + fetchSpy = jest |
| 156 | + .spyOn(globalThis, 'fetch') |
| 157 | + .mockResolvedValueOnce(TOKEN_RESPONSE) |
| 158 | + .mockResolvedValueOnce({ |
| 159 | + status: 201, |
| 160 | + ok: true, |
| 161 | + headers: { |
| 162 | + get: (h: string) => |
| 163 | + h === 'content-type' ? 'application/json' : null, |
| 164 | + }, |
| 165 | + text: async () => JSON.stringify(requestBody), |
| 166 | + } as unknown as Response); |
| 167 | + |
| 168 | + const app = makeApp({ |
| 169 | + dcm: { |
| 170 | + apiGatewayUrl: 'https://gateway.example.com', |
| 171 | + clientId: 'id', |
| 172 | + clientSecret: 'secret', |
| 173 | + }, |
| 174 | + }); |
| 175 | + |
| 176 | + const res = await request(app) |
| 177 | + .post('/proxy/providers') |
| 178 | + .send(requestBody) |
| 179 | + .set('Content-Type', 'application/json'); |
| 180 | + |
| 181 | + expect(res.status).toBe(201); |
| 182 | + |
| 183 | + const upstreamCall = fetchSpy.mock.calls[1]; |
| 184 | + expect(upstreamCall[1].method).toBe('POST'); |
| 185 | + expect(JSON.parse(upstreamCall[1].body)).toEqual(requestBody); |
| 186 | + }); |
| 187 | + |
| 188 | + it('handles 204 No Content upstream responses without a body', async () => { |
| 189 | + fetchSpy = jest |
| 190 | + .spyOn(globalThis, 'fetch') |
| 191 | + .mockResolvedValueOnce(TOKEN_RESPONSE) |
| 192 | + .mockResolvedValueOnce({ |
| 193 | + status: 204, |
| 194 | + ok: true, |
| 195 | + headers: { get: () => null }, |
| 196 | + text: async () => '', |
| 197 | + } as unknown as Response); |
| 198 | + |
| 199 | + const app = makeApp({ |
| 200 | + dcm: { |
| 201 | + apiGatewayUrl: 'https://gateway.example.com', |
| 202 | + clientId: 'id', |
| 203 | + clientSecret: 'secret', |
| 204 | + }, |
| 205 | + }); |
| 206 | + |
| 207 | + const res = await request(app).delete('/proxy/providers/test-id'); |
| 208 | + |
| 209 | + expect(res.status).toBe(204); |
| 210 | + expect(res.text).toBe(''); |
| 211 | + }); |
| 212 | +}); |
0 commit comments