projex
Version:
A command line to manage the workflow
60 lines (59 loc) • 2.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("../index");
const util_1 = require("./util");
const _shared_1 = require("../../../../shared/index");
jest.mock('@shared');
jest.mock('./util');
describe('login', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should save the credentials to the config file', async () => {
const account = 'my-account';
const email = 'my-email';
const workspace = 'my-workspace';
const apiKey = 'my-api-key';
const apiToken = 'my-api-token';
const authToken = 'my-auth-token';
const serviceGetAuthMock = util_1.serviceGetAuth;
serviceGetAuthMock.mockResolvedValueOnce({
data: { token: authToken },
status: 200,
statusText: 'OK',
config: {
headers: {},
url: 'https://api.vtex.com',
},
headers: {},
});
const result = await (0, index_1.login)(account, email, workspace, apiKey, apiToken, false);
expect(result).toBe(true);
expect(serviceGetAuthMock).toHaveBeenCalledWith(account, apiKey, apiToken);
expect(util_1.saveVtexConfig).toHaveBeenCalledWith({
account,
token: authToken,
workspace,
login: email,
env: 'prod',
});
expect(_shared_1.log.info).toHaveBeenCalledWith('saving the authentication token in the VTEX config file...');
});
it('should throw an error if no token information is found', async () => {
const account = 'my-account';
const email = 'my-email';
const workspace = 'my-workspace';
const apiKey = 'my-api-key';
const apiToken = 'my-api-token';
const serviceGetAuthMock = util_1.serviceGetAuth;
serviceGetAuthMock.mockResolvedValueOnce(undefined);
await expect((0, index_1.login)(account, email, workspace, apiKey, apiToken, false))
.rejects.toThrow('Failed to obtain authentication token');
expect(_shared_1.log.error).toHaveBeenCalledWith('error while obtaining authentication token');
});
it('should throw an error if any of the required parameters are missing', async () => {
await expect((0, index_1.login)(undefined, 'email', 'workspace', 'apiKey', 'apiToken', false))
.rejects.toThrow('Missing required parameters');
expect(_shared_1.log.error).toHaveBeenCalledWith(expect.stringContaining('please provide all the required parameters to log in.'));
});
});