|
| 1 | +/********************************************************************** |
| 2 | + * Copyright (C) 2025 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 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + ***********************************************************************/ |
| 18 | + |
| 19 | +import { beforeAll, beforeEach, expect, test, vi } from 'vitest'; |
| 20 | +import { Container } from 'inversify'; |
| 21 | + |
| 22 | +import { AccountSelectHelper } from './account-select-helper'; |
| 23 | +import type { IAMSession } from '../api/iam-session'; |
| 24 | +import type { CloudAccountType } from '../api/cloud-account'; |
| 25 | + |
| 26 | +let accountSelectHelper: AccountSelectHelper; |
| 27 | + |
| 28 | +beforeAll(() => { |
| 29 | + // Mock the fetch function globally |
| 30 | + Object.defineProperty(globalThis, 'fetch', { |
| 31 | + writable: true, |
| 32 | + value: vi.fn<(input: string | URL | globalThis.Request, init?: RequestInit) => Promise<Response>>(), |
| 33 | + }); |
| 34 | +}); |
| 35 | + |
| 36 | +beforeEach(async () => { |
| 37 | + vi.restoreAllMocks(); |
| 38 | + vi.resetAllMocks(); |
| 39 | + |
| 40 | + // Create fresh instance each time |
| 41 | + const container = new Container(); |
| 42 | + container.bind(AccountSelectHelper).toSelf().inSingletonScope(); |
| 43 | + |
| 44 | + accountSelectHelper = await container.getAsync(AccountSelectHelper); |
| 45 | +}); |
| 46 | + |
| 47 | +test('should fetch and return valid CloudAccount list', async () => { |
| 48 | + expect.assertions(2); |
| 49 | + |
| 50 | + const session: IAMSession = { |
| 51 | + access_token: 'mock-access-token', |
| 52 | + } as unknown as IAMSession; |
| 53 | + |
| 54 | + const validAccountResponse: CloudAccountType = { |
| 55 | + next_url: '', |
| 56 | + resources: [ |
| 57 | + { |
| 58 | + metadata: { guid: 'account-1' }, |
| 59 | + entity: { |
| 60 | + name: 'Account One', |
| 61 | + primary_owner: { ibmid: '[email protected]' }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + metadata: { guid: 'account-2' }, |
| 66 | + entity: { |
| 67 | + name: 'Account Two', |
| 68 | + primary_owner: { ibmid: '[email protected]' }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + ], |
| 72 | + }; |
| 73 | + |
| 74 | + vi.mocked(fetch).mockResolvedValue({ |
| 75 | + json: async () => validAccountResponse, |
| 76 | + } as Response); |
| 77 | + |
| 78 | + const result = await accountSelectHelper.getAccounts(session); |
| 79 | + |
| 80 | + expect(fetch).toHaveBeenCalledWith(new URL(AccountSelectHelper.ACCOUNT_URL), { |
| 81 | + method: 'GET', |
| 82 | + headers: { |
| 83 | + Accept: 'application/json', |
| 84 | + Authorization: `Bearer ${session.access_token}`, |
| 85 | + }, |
| 86 | + }); |
| 87 | + |
| 88 | + expect(result).toStrictEqual([ |
| 89 | + { guid: 'account-1', name: 'Account One', ibmid: '[email protected]' }, |
| 90 | + { guid: 'account-2', name: 'Account Two', ibmid: '[email protected]' }, |
| 91 | + ]); |
| 92 | +}); |
| 93 | + |
| 94 | +test('should throw error on invalid account response', async () => { |
| 95 | + expect.assertions(2); |
| 96 | + |
| 97 | + const session: IAMSession = { |
| 98 | + access_token: 'mock-access-token', |
| 99 | + } as unknown as IAMSession; |
| 100 | + |
| 101 | + const invalidResponse = { foo: 'bar' }; |
| 102 | + |
| 103 | + vi.mocked(fetch).mockResolvedValue({ |
| 104 | + json: async () => invalidResponse, |
| 105 | + } as Response); |
| 106 | + |
| 107 | + const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 108 | + |
| 109 | + await expect(accountSelectHelper.getAccounts(session)).rejects.toThrow('Invalid account'); |
| 110 | + |
| 111 | + expect(errorSpy).toHaveBeenCalledWith( |
| 112 | + 'Invalid account', |
| 113 | + expect.anything(), // Zod error details |
| 114 | + ); |
| 115 | + |
| 116 | + errorSpy.mockRestore(); |
| 117 | +}); |
0 commit comments