|
| 1 | +import { ErrorCode, REGION_IMAGE_LOGO_URL } from '@openops/shared'; |
| 2 | + |
| 3 | +import { resolveOptions } from '../../../../../src/app/benchmark/providers/azure/azure-option-resolver'; |
| 4 | + |
| 5 | +const mockListConnections = jest.fn(); |
| 6 | +const mockAuthenticateUserWithAzure = jest.fn(); |
| 7 | +const mockGetAzureSubscriptionsList = jest.fn(); |
| 8 | +const mockGetAzureRegionsList = jest.fn(); |
| 9 | +const mockGetAuthProviderLogoUrl = jest.fn(); |
| 10 | + |
| 11 | +jest.mock('../../../../../src/app/benchmark/common-resolvers', () => ({ |
| 12 | + ...jest.requireActual('../../../../../src/app/benchmark/common-resolvers'), |
| 13 | + listConnections: ( |
| 14 | + ...args: unknown[] |
| 15 | + ): ReturnType<typeof mockListConnections> => mockListConnections(...args), |
| 16 | + getAuthProviderLogoUrl: ( |
| 17 | + ...args: unknown[] |
| 18 | + ): ReturnType<typeof mockGetAuthProviderLogoUrl> => |
| 19 | + mockGetAuthProviderLogoUrl(...args), |
| 20 | +})); |
| 21 | + |
| 22 | +jest.mock('@openops/common', () => ({ |
| 23 | + ...jest.requireActual('@openops/common'), |
| 24 | + authenticateUserWithAzure: ( |
| 25 | + ...args: unknown[] |
| 26 | + ): ReturnType<typeof mockAuthenticateUserWithAzure> => |
| 27 | + mockAuthenticateUserWithAzure(...args), |
| 28 | + getAzureSubscriptionsList: ( |
| 29 | + ...args: unknown[] |
| 30 | + ): ReturnType<typeof mockGetAzureSubscriptionsList> => |
| 31 | + mockGetAzureSubscriptionsList(...args), |
| 32 | + getAzureRegionsList: ( |
| 33 | + ...args: unknown[] |
| 34 | + ): ReturnType<typeof mockGetAzureRegionsList> => |
| 35 | + mockGetAzureRegionsList(...args), |
| 36 | +})); |
| 37 | + |
| 38 | +const mockGetOneOrThrow = jest.fn(); |
| 39 | +jest.mock( |
| 40 | + '../../../../../src/app/app-connection/app-connection-service/app-connection-service', |
| 41 | + () => ({ |
| 42 | + appConnectionService: { |
| 43 | + getOneOrThrow: ( |
| 44 | + ...args: unknown[] |
| 45 | + ): ReturnType<typeof mockGetOneOrThrow> => mockGetOneOrThrow(...args), |
| 46 | + }, |
| 47 | + }), |
| 48 | +); |
| 49 | + |
| 50 | +describe('resolveOptions (Azure)', () => { |
| 51 | + const projectId = 'project-123'; |
| 52 | + const provider = 'azure'; |
| 53 | + |
| 54 | + beforeEach(() => { |
| 55 | + jest.clearAllMocks(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('delegates to listConnections and returns its result for listConnections', async () => { |
| 59 | + const options = [ |
| 60 | + { |
| 61 | + id: 'conn-1', |
| 62 | + displayName: 'Connection 1', |
| 63 | + metadata: { authProviderKey: 'Azure' }, |
| 64 | + }, |
| 65 | + ]; |
| 66 | + mockListConnections.mockResolvedValue(options); |
| 67 | + |
| 68 | + const result = await resolveOptions('listConnections', { |
| 69 | + projectId, |
| 70 | + provider, |
| 71 | + }); |
| 72 | + |
| 73 | + expect(mockListConnections).toHaveBeenCalledTimes(1); |
| 74 | + expect(mockListConnections).toHaveBeenCalledWith({ projectId, provider }); |
| 75 | + expect(result).toEqual(options); |
| 76 | + }); |
| 77 | + |
| 78 | + it('throws when getSubscriptionsList is called without a selected connection', async () => { |
| 79 | + await expect( |
| 80 | + resolveOptions('getSubscriptionsList', { |
| 81 | + projectId, |
| 82 | + provider, |
| 83 | + }), |
| 84 | + ).rejects.toThrow('Connection must be selected to list subscriptions'); |
| 85 | + expect(mockGetOneOrThrow).not.toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('returns subscriptions for getSubscriptionsList', async () => { |
| 89 | + mockGetOneOrThrow.mockResolvedValue({ |
| 90 | + authProviderKey: 'Azure', |
| 91 | + value: { |
| 92 | + type: 'CUSTOM_AUTH', |
| 93 | + props: { |
| 94 | + clientId: 'cid', |
| 95 | + clientSecret: 'secret', |
| 96 | + tenantId: 'tenant', |
| 97 | + }, |
| 98 | + }, |
| 99 | + }); |
| 100 | + mockAuthenticateUserWithAzure.mockResolvedValue({ |
| 101 | + access_token: 'token-1', |
| 102 | + }); |
| 103 | + mockGetAzureSubscriptionsList.mockResolvedValue([ |
| 104 | + { |
| 105 | + subscriptionId: 'sub-a', |
| 106 | + displayName: 'Sub A', |
| 107 | + }, |
| 108 | + { |
| 109 | + subscriptionId: 'sub-b', |
| 110 | + displayName: 'Sub B', |
| 111 | + }, |
| 112 | + ]); |
| 113 | + mockGetAuthProviderLogoUrl.mockResolvedValue('/blocks/azure.svg'); |
| 114 | + |
| 115 | + const result = await resolveOptions('getSubscriptionsList', { |
| 116 | + projectId, |
| 117 | + provider, |
| 118 | + benchmarkConfiguration: { connection: ['conn-123'] }, |
| 119 | + }); |
| 120 | + |
| 121 | + expect(mockGetOneOrThrow).toHaveBeenCalledWith({ |
| 122 | + id: 'conn-123', |
| 123 | + projectId, |
| 124 | + }); |
| 125 | + expect(mockAuthenticateUserWithAzure).toHaveBeenCalledWith({ |
| 126 | + clientId: 'cid', |
| 127 | + clientSecret: 'secret', |
| 128 | + tenantId: 'tenant', |
| 129 | + }); |
| 130 | + expect(mockGetAzureSubscriptionsList).toHaveBeenCalledWith('token-1'); |
| 131 | + expect(mockGetAuthProviderLogoUrl).toHaveBeenCalledWith('Azure', projectId); |
| 132 | + expect(result).toEqual([ |
| 133 | + { |
| 134 | + id: 'sub-a', |
| 135 | + displayName: 'Sub A', |
| 136 | + imageLogoUrl: '/blocks/azure.svg', |
| 137 | + }, |
| 138 | + { |
| 139 | + id: 'sub-b', |
| 140 | + displayName: 'Sub B', |
| 141 | + imageLogoUrl: '/blocks/azure.svg', |
| 142 | + }, |
| 143 | + ]); |
| 144 | + }); |
| 145 | + |
| 146 | + it('omits imageLogoUrl for getSubscriptionsList when getAuthProviderLogoUrl returns undefined', async () => { |
| 147 | + mockGetOneOrThrow.mockResolvedValue({ |
| 148 | + authProviderKey: 'Azure', |
| 149 | + value: { |
| 150 | + type: 'CUSTOM_AUTH', |
| 151 | + props: { |
| 152 | + clientId: 'cid', |
| 153 | + clientSecret: 'secret', |
| 154 | + tenantId: 'tenant', |
| 155 | + }, |
| 156 | + }, |
| 157 | + }); |
| 158 | + mockAuthenticateUserWithAzure.mockResolvedValue({ access_token: 't' }); |
| 159 | + mockGetAzureSubscriptionsList.mockResolvedValue([ |
| 160 | + { subscriptionId: 'sub-1', displayName: 'One' }, |
| 161 | + ]); |
| 162 | + mockGetAuthProviderLogoUrl.mockResolvedValue(undefined); |
| 163 | + |
| 164 | + const result = await resolveOptions('getSubscriptionsList', { |
| 165 | + projectId, |
| 166 | + provider, |
| 167 | + benchmarkConfiguration: { connection: ['c1'] }, |
| 168 | + }); |
| 169 | + |
| 170 | + expect(mockGetAuthProviderLogoUrl).toHaveBeenCalledWith('Azure', projectId); |
| 171 | + expect(result).toEqual([{ id: 'sub-1', displayName: 'One' }]); |
| 172 | + }); |
| 173 | + |
| 174 | + it('throws validation error when subscriptions list is empty', async () => { |
| 175 | + mockGetOneOrThrow.mockResolvedValue({ |
| 176 | + authProviderKey: 'Azure', |
| 177 | + value: { |
| 178 | + type: 'CUSTOM_AUTH', |
| 179 | + props: { |
| 180 | + clientId: 'client_id', |
| 181 | + clientSecret: 'client_secret', |
| 182 | + tenantId: 'tenant', |
| 183 | + }, |
| 184 | + }, |
| 185 | + }); |
| 186 | + mockAuthenticateUserWithAzure.mockResolvedValue({ access_token: 't' }); |
| 187 | + mockGetAzureSubscriptionsList.mockResolvedValue([]); |
| 188 | + |
| 189 | + const rejection = resolveOptions('getSubscriptionsList', { |
| 190 | + projectId, |
| 191 | + provider, |
| 192 | + benchmarkConfiguration: { connection: ['c1'] }, |
| 193 | + }); |
| 194 | + await expect(rejection).rejects.toMatchObject({ |
| 195 | + error: { code: ErrorCode.VALIDATION }, |
| 196 | + }); |
| 197 | + await expect(rejection).rejects.toThrow( |
| 198 | + /No Azure subscriptions were returned for this connection/, |
| 199 | + ); |
| 200 | + }); |
| 201 | + |
| 202 | + it('throws validation error when subscription listing fails', async () => { |
| 203 | + mockGetOneOrThrow.mockResolvedValue({ |
| 204 | + authProviderKey: 'Azure', |
| 205 | + value: { |
| 206 | + type: 'CUSTOM_AUTH', |
| 207 | + props: { |
| 208 | + clientId: 'client_id', |
| 209 | + clientSecret: 'client_secret', |
| 210 | + tenantId: 'tenant', |
| 211 | + }, |
| 212 | + }, |
| 213 | + }); |
| 214 | + mockAuthenticateUserWithAzure.mockRejectedValue(new Error('token failed')); |
| 215 | + |
| 216 | + const rejection = resolveOptions('getSubscriptionsList', { |
| 217 | + projectId, |
| 218 | + provider, |
| 219 | + benchmarkConfiguration: { connection: ['c1'] }, |
| 220 | + }); |
| 221 | + await expect(rejection).rejects.toMatchObject({ |
| 222 | + error: { code: ErrorCode.VALIDATION }, |
| 223 | + }); |
| 224 | + await expect(rejection).rejects.toThrow( |
| 225 | + /Unable to retrieve Azure subscriptions with the provided connection details/, |
| 226 | + ); |
| 227 | + }); |
| 228 | + |
| 229 | + it('delegates to getAzureRegionsList and returns options with imageLogoUrl for getRegionsList', async () => { |
| 230 | + const regionsList = [ |
| 231 | + { id: 'eastus', displayName: 'East US' }, |
| 232 | + { id: 'westus2', displayName: 'West US 2' }, |
| 233 | + ]; |
| 234 | + mockGetAzureRegionsList.mockReturnValue(regionsList); |
| 235 | + |
| 236 | + const result = await resolveOptions('getRegionsList', { |
| 237 | + projectId, |
| 238 | + provider, |
| 239 | + }); |
| 240 | + |
| 241 | + expect(mockGetAzureRegionsList).toHaveBeenCalledTimes(1); |
| 242 | + expect(mockListConnections).not.toHaveBeenCalled(); |
| 243 | + expect(result).toEqual([ |
| 244 | + { |
| 245 | + id: 'eastus', |
| 246 | + displayName: 'East US', |
| 247 | + imageLogoUrl: REGION_IMAGE_LOGO_URL, |
| 248 | + }, |
| 249 | + { |
| 250 | + id: 'westus2', |
| 251 | + displayName: 'West US 2', |
| 252 | + imageLogoUrl: REGION_IMAGE_LOGO_URL, |
| 253 | + }, |
| 254 | + ]); |
| 255 | + }); |
| 256 | + |
| 257 | + it('throws with method name in message for unknown method', async () => { |
| 258 | + await expect( |
| 259 | + resolveOptions('unknownMethod', { projectId, provider }), |
| 260 | + ).rejects.toThrow('Unknown Azure wizard option method: unknownMethod'); |
| 261 | + expect(mockListConnections).not.toHaveBeenCalled(); |
| 262 | + }); |
| 263 | +}); |
0 commit comments