import { errorAndExit } from '../errorAndExit'; import { getIamKey } from '../getIamKey'; import { handleAlksSessionsConsole } from './alks-sessions-console'; import { Key } from '../../model/keys'; import { checkForUpdate } from '../checkForUpdate'; import { getAlksAccount } from '../state/alksAccount'; import { getAlksRole } from '../state/alksRole'; import { tryToExtractRole } from '../tryToExtractRole'; jest.mock('../errorAndExit'); jest.mock('../getIamKey'); jest.mock('../checkForUpdate'); jest.mock('../log'); jest.mock('../getUserAgentString', () => ({ getUserAgentString: () => 'test-ua', })); jest.mock('../state/alksAccount'); jest.mock('../state/alksRole'); jest.mock('../tryToExtractRole'); jest.mock('open', () => ({ __esModule: true, default: jest.fn().mockResolvedValue(undefined), })); jest.mock('axios'); // Silence console output jest.spyOn(global.console, 'error').mockImplementation(() => {}); jest.spyOn(global.console, 'log').mockImplementation(() => {}); const mockKey: Key = { accessKey: 'AKIA_FAKE', secretKey: 'secret', sessionToken: 'token', expires: new Date(), alksAccount: '254681725729', alksRole: 'Admin', isIAM: true, }; const fakeErrorSymbol = Symbol('errorAndExit'); describe('handleAlksSessionsConsole', () => { beforeEach(() => { jest.clearAllMocks(); (errorAndExit as unknown as jest.Mock).mockImplementation(() => { throw fakeErrorSymbol; }); (getIamKey as jest.Mock).mockResolvedValue(mockKey); (tryToExtractRole as jest.Mock).mockReturnValue('Admin'); const open = require('open'); open.default.mockResolvedValue(undefined); // Mock axios for the AWS federation call inside generateConsoleUrl const axios = require('axios'); axios.get = jest.fn().mockResolvedValue({ status: 200, data: { SigninToken: 'fake-signin-token' }, }); }); describe('changeRequestOptions passthrough', () => { it('passes ciid/activityType/description to getIamKey when all three are provided', async () => { await handleAlksSessionsConsole({ account: '254681725729/ALKSAdmin - awscacclogi', role: 'Admin', ciid: 'CI2410930', activityType: 'Operational Task', description: 'reviewing secret values', url: true, // avoid browser open }); expect(getIamKey).toHaveBeenCalledWith( '254681725729/ALKSAdmin - awscacclogi', 'Admin', undefined, false, false, undefined, { ciid: 'CI2410930', activityType: 'Operational Task', description: 'reviewing secret values', } ); }); it('passes changeNumber to getIamKey when --chg-number is provided', async () => { await handleAlksSessionsConsole({ account: '254681725729/ALKSAdmin - awscacclogi', role: 'Admin', chgNumber: 'CHG1234567', url: true, }); expect(getIamKey).toHaveBeenCalledWith( '254681725729/ALKSAdmin - awscacclogi', 'Admin', undefined, false, false, undefined, { changeNumber: 'CHG1234567' } ); }); it('passes empty changeRequestOptions when no change ticket flags are provided', async () => { await handleAlksSessionsConsole({ account: '254681725729/ALKSAdmin - awscacclogi', role: 'Admin', url: true, }); expect(getIamKey).toHaveBeenCalledWith( '254681725729/ALKSAdmin - awscacclogi', 'Admin', undefined, false, false, undefined, { ciid: undefined, activityType: undefined, description: undefined } ); }); }); describe('flag validation', () => { it('calls errorAndExit when only --ciid is provided without --activity-type and --description', async () => { try { await handleAlksSessionsConsole({ account: '254681725729/ALKSAdmin - awscacclogi', role: 'Admin', ciid: 'CI2410930', }); } catch (e) { expect(e).toBe(fakeErrorSymbol); } expect(errorAndExit).toHaveBeenCalled(); expect(getIamKey).not.toHaveBeenCalled(); }); it('calls errorAndExit when the change request flags are whitespace-only', async () => { try { await handleAlksSessionsConsole({ account: '254681725729/ALKSAdmin - awscacclogi', role: 'Admin', ciid: ' ', activityType: ' ', description: ' ', }); } catch (e) { expect(e).toBe(fakeErrorSymbol); } expect(errorAndExit).toHaveBeenCalled(); expect(getIamKey).not.toHaveBeenCalled(); }); it('calls errorAndExit when a change request flag is an empty string', async () => { try { await handleAlksSessionsConsole({ account: '254681725729/ALKSAdmin - awscacclogi', role: 'Admin', ciid: '', activityType: 'Operational Task', description: 'reviewing secret values', }); } catch (e) { expect(e).toBe(fakeErrorSymbol); } expect(errorAndExit).toHaveBeenCalled(); expect(getIamKey).not.toHaveBeenCalled(); }); it('calls errorAndExit when the change request flags are not strings', async () => { try { await handleAlksSessionsConsole({ account: '254681725729/ALKSAdmin - awscacclogi', role: 'Admin', ciid: 2410930, activityType: true, description: {}, }); } catch (e) { expect(e).toBe(fakeErrorSymbol); } expect(errorAndExit).toHaveBeenCalled(); expect(getIamKey).not.toHaveBeenCalled(); }); it('calls errorAndExit when --chg-number and --ciid are both provided', async () => { try { await handleAlksSessionsConsole({ account: '254681725729/ALKSAdmin - awscacclogi', role: 'Admin', chgNumber: 'CHG1234567', ciid: 'CI2410930', activityType: 'Operational Task', description: 'reviewing secret values', }); } catch (e) { expect(e).toBe(fakeErrorSymbol); } expect(errorAndExit).toHaveBeenCalled(); expect(getIamKey).not.toHaveBeenCalled(); }); }); describe('browser open path', () => { beforeEach(() => { // Suppress the 5-second browser-open timeout (so the race doesn't fire it // before open() resolves via microtask) and fire the 3-second post-open // delay immediately so tests don't need real-timer waits. jest .spyOn(global, 'setTimeout') .mockImplementation((fn: any, delay?: number) => { if (delay === 3000 && typeof fn === 'function') fn(); return 0 as unknown as NodeJS.Timeout; }); }); afterEach(() => { jest.restoreAllMocks(); }); it('opens the browser and calls checkForUpdate when --url is not set', async () => { const open = require('open'); await handleAlksSessionsConsole({ account: '254681725729/ALKSAdmin - awscacclogi', role: 'Admin', }); expect(open.default).toHaveBeenCalled(); expect(checkForUpdate).toHaveBeenCalled(); }); it('passes app option to open when --open-with is specified', async () => { const open = require('open'); await handleAlksSessionsConsole({ account: '254681725729/ALKSAdmin - awscacclogi', role: 'Admin', openWith: 'firefox', }); expect(open.default).toHaveBeenCalledWith( expect.any(String), expect.objectContaining({ app: 'firefox', newInstance: true }) ); }); it('warns but still succeeds when the browser fails to open', async () => { const open = require('open'); open.default.mockRejectedValue(new Error('spawn error')); await handleAlksSessionsConsole({ account: '254681725729/ALKSAdmin - awscacclogi', role: 'Admin', }); // A failed browser launch is non-fatal: the console URL was already // generated, so the command warns and continues rather than exiting. expect(errorAndExit).not.toHaveBeenCalled(); expect(checkForUpdate).toHaveBeenCalled(); }); it('clears the browser-open timeout once the race settles', async () => { const open = require('open'); const clearTimeoutSpy = jest.spyOn(global, 'clearTimeout'); await handleAlksSessionsConsole({ account: '254681725729/ALKSAdmin - awscacclogi', role: 'Admin', }); expect(open.default).toHaveBeenCalled(); expect(clearTimeoutSpy).toHaveBeenCalled(); }); }); describe('default account', () => { it('loads account and role from defaults when --default flag is set', async () => { (getAlksAccount as jest.Mock).mockResolvedValue('default-account'); (getAlksRole as jest.Mock).mockResolvedValue('default-role'); await handleAlksSessionsConsole({ default: true, url: true }); expect(getIamKey).toHaveBeenCalledWith( 'default-account', 'default-role', undefined, false, false, undefined, expect.any(Object) ); }); it('calls errorAndExit when default account is not configured', async () => { (getAlksAccount as jest.Mock).mockResolvedValue(null); (getAlksRole as jest.Mock).mockResolvedValue(null); try { await handleAlksSessionsConsole({ default: true, url: true }); } catch (e) { expect(e).toBe(fakeErrorSymbol); } expect(errorAndExit).toHaveBeenCalledWith( 'Unable to load default account!' ); }); }); describe('getIamKey errors', () => { it('calls errorAndExit when getIamKey rejects', async () => { (getIamKey as jest.Mock).mockRejectedValue( new Error('ALKS server error') ); try { await handleAlksSessionsConsole({ account: '254681725729/ALKSAdmin - awscacclogi', role: 'Admin', url: true, }); } catch (e) { expect(e).toBe(fakeErrorSymbol); } expect(errorAndExit).toHaveBeenCalled(); }); }); describe('role extraction', () => { it('extracts role from account string when role is not provided', async () => { await handleAlksSessionsConsole({ account: '254681725729/ALKSAdmin - awscacclogi', url: true, }); expect(tryToExtractRole).toHaveBeenCalledWith( '254681725729/ALKSAdmin - awscacclogi' ); expect(getIamKey).toHaveBeenCalled(); }); }); describe('generateConsoleUrl error paths', () => { it('calls errorAndExit when AWS returns a non-200 response', async () => { const axios = require('axios'); axios.get.mockResolvedValue({ status: 403, data: 'Forbidden' }); try { await handleAlksSessionsConsole({ account: '254681725729/ALKSAdmin - awscacclogi', role: 'Admin', url: true, }); } catch (e) { expect(e).toBe(fakeErrorSymbol); } expect(errorAndExit).toHaveBeenCalled(); }); it('calls errorAndExit when AWS does not return a SigninToken', async () => { const axios = require('axios'); axios.get.mockResolvedValue({ status: 200, data: { SigninToken: '' } }); try { await handleAlksSessionsConsole({ account: '254681725729/ALKSAdmin - awscacclogi', role: 'Admin', url: true, }); } catch (e) { expect(e).toBe(fakeErrorSymbol); } expect(errorAndExit).toHaveBeenCalled(); }); it('calls errorAndExit when the axios request throws', async () => { const axios = require('axios'); axios.get.mockRejectedValue(new Error('network error')); try { await handleAlksSessionsConsole({ account: '254681725729/ALKSAdmin - awscacclogi', role: 'Admin', url: true, }); } catch (e) { expect(e).toBe(fakeErrorSymbol); } expect(errorAndExit).toHaveBeenCalled(); }); }); });