import commander from 'commander'; import { handleAlksDeveloperConfigure } from './alks-developer-configure'; import { promptForToken } from '../promptForToken'; import { REFRESH_TOKEN_AUTH_CHOICE } from '../promptForAuthType'; import { setServer } from '../state/server'; import { setToken } from '../state/token'; import { setCredentialProcess } from '../state/credentialProcess'; import { errorAndExit } from '../errorAndExit'; import { checkForUpdate } from '../checkForUpdate'; jest.mock('../promptForServer'); jest.mock('../promptForToken'); jest.mock('../promptForCredentialProcess'); jest.mock('../promptForAlksAccountAndRole'); jest.mock('../promptForOutputFormat'); jest.mock('../state/server'); jest.mock('../state/token'); jest.mock('../state/credentialProcess'); jest.mock('../state/alksAccount'); jest.mock('../state/alksRole'); jest.mock('../state/outputFormat'); jest.mock('../validateAlksAccount'); jest.mock('../errorAndExit'); jest.mock('../checkForUpdate'); jest.mock('tabtab'); jest.mock('../promptForAuthType', () => ({ __esModule: true, REFRESH_TOKEN_AUTH_CHOICE: 'refresh-token', CREDENTIAL_PROCESS_AUTH_CHOICE: 'credential-process', promptForAuthType: jest.fn(), })); // Silence console output jest.spyOn(global.console, 'error').mockImplementation(() => {}); jest.spyOn(global.console, 'log').mockImplementation(() => {}); describe('handleAlksDeveloperConfigure', () => { beforeEach(() => { (checkForUpdate as jest.Mock).mockResolvedValue(undefined); }); afterEach(() => { jest.resetAllMocks(); }); describe('refresh-token auth type', () => { beforeEach(async () => { (promptForToken as jest.Mock).mockResolvedValue('my-refresh-token'); await handleAlksDeveloperConfigure({ nonInteractive: true, authType: REFRESH_TOKEN_AUTH_CHOICE, } as commander.OptionValues); }); it('stores the refresh token', () => { expect(setToken).toHaveBeenCalledWith('my-refresh-token'); }); it('does not error', () => { expect(errorAndExit).not.toHaveBeenCalled(); }); }); describe('credential-process auth type', () => { beforeEach(async () => { await handleAlksDeveloperConfigure({ nonInteractive: true, credentialProcess: '/path/to/script', } as commander.OptionValues); }); it('stores the credential process', () => { expect(setCredentialProcess).toHaveBeenCalledWith('/path/to/script'); }); it('does not error', () => { expect(errorAndExit).not.toHaveBeenCalled(); }); }); describe('a removed auth type (password) supplied via --auth-type', () => { beforeEach(async () => { await handleAlksDeveloperConfigure({ nonInteractive: true, authType: 'password', } as commander.OptionValues); }); it('errors that the auth type is invalid', () => { expect(errorAndExit).toHaveBeenCalled(); const message = (errorAndExit as unknown as jest.Mock).mock .calls[0][0] as string; expect(message).toContain('Invalid auth type'); }); it('lists the remaining valid auth types', () => { const message = (errorAndExit as unknown as jest.Mock).mock .calls[0][0] as string; expect(message).toContain('refresh-token'); expect(message).toContain('credential-process'); }); it('does not store a token or credential process', () => { expect(setToken).not.toHaveBeenCalled(); expect(setCredentialProcess).not.toHaveBeenCalled(); }); }); it('does not reference removed server prompt when a server flag is passed', async () => { await handleAlksDeveloperConfigure({ nonInteractive: true, server: 'https://alks.example.com/rest', authType: REFRESH_TOKEN_AUTH_CHOICE, } as commander.OptionValues); expect(setServer).toHaveBeenCalledWith('https://alks.example.com/rest'); }); });