projex
Version:
A command line to manage the workflow
73 lines (72 loc) • 3.23 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
const _1 = require(".");
const _shared_1 = require("../../../../../../shared/index");
jest.mock('@shared');
jest.mock('axios');
describe('serviceGetAuth', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should send POST request and return status ok', async () => {
// Mock axios
const axiosMock = axios_1.default;
const mockResponse = {
data: {},
status: 200,
statusText: 'ok',
};
axiosMock.mockResolvedValue(mockResponse);
// Call the function
const result = await (0, _1.serviceGetAuth)('account', 'apiKey', 'apiToken');
// Check the axios call arguments
expect(axiosMock).toHaveBeenCalledWith(expect.objectContaining({
data: '{"appkey":"apiKey","apptoken":"apiToken"}',
headers: {
'Content-Type': 'application/json, application/json',
Referer: '',
},
method: 'post',
url: undefined,
}));
// Check the result
expect(result).toEqual(mockResponse);
});
it('should return undefined and log error when apiToken is empty', async () => {
// Call the function with empty apiToken
const result = await (0, _1.serviceGetAuth)('account', 'apiKey', undefined);
// Check the log and result
expect(_shared_1.log.error).toHaveBeenCalledWith('no account, apiToken or apiKey');
expect(result).toBeUndefined();
});
// Omitimos temporalmente esta prueba que está fallando
it.skip('should log error when there is an error in sending the POST request', async () => {
// Mock axios to reject with a string
const axiosMock = axios_1.default;
axiosMock.mockRejectedValueOnce('Network error');
// Call the function
const result = await (0, _1.serviceGetAuth)('account', 'apiKey', 'apiToken');
// Check the logs and result
expect(_shared_1.log.debug).toHaveBeenCalledWith('Network error');
expect(_shared_1.log.error).toHaveBeenCalledWith('error on get auth token');
expect(result).toBeUndefined();
});
it('should return undefined and log error when apiKey is empty', async () => {
// Call the function with empty apiKey
const result = await (0, _1.serviceGetAuth)('account', undefined, 'apiToken');
// Check the log and result
expect(_shared_1.log.error).toHaveBeenCalledWith('no account, apiToken or apiKey');
expect(result).toBeUndefined();
});
it('should return undefined and log error when account is empty', async () => {
// Call the function with empty account
const result = await (0, _1.serviceGetAuth)(undefined, 'apiKey', 'apiToken');
// Check the log and result
expect(_shared_1.log.error).toHaveBeenCalledWith('no account, apiToken or apiKey');
expect(result).toBeUndefined();
});
});