@stylusapparel/opv3-merchant-api-nodejs
Version:
This is the official NodeJs wrapper for connecting to the StylusOP API V3
97 lines (91 loc) • 2.8 kB
JavaScript
const axios = require('axios');
const __mockWrapper = require('../index');
const { __defaults } = require('../constants/url');
const { _errorConst } = require('../constants/const');
const { OAUTH_TOKEN_RESPONSE, MOCK_SECRET_TOKEN, MOCK_MERCHANT_ID, MOCK_MERCHANT_NAME, IS_TOKEN__VALID_SUCCESS_RESPONSE, IS_TOKEN__VALID_ERROR_RESPONSE, IS_TOKEN__VALID_EXPIRE_ERROR_RESPONSE } = require('./__mocks__/auth.json');
// Set the API URL to the local API for testing in development
process.env.STYLUSOP_API_URL = 'http://localhost:3600/api/';
const _mockClient = __mockWrapper.createClient(MOCK_MERCHANT_ID, MOCK_SECRET_TOKEN,
{ apiVersion: __defaults.LATEST_VERSION, sandbox: true, merchantName: MOCK_MERCHANT_NAME, maxRetries: 0 }
);
afterEach(() => {
jest.clearAllMocks();
});
beforeEach(async () => {
axios.post.mockImplementationOnce(() =>
Promise.resolve({
data: { ...OAUTH_TOKEN_RESPONSE },
status: 200,
}),
);
await _mockClient.oauthToken();
});
describe('AUTH MODULE - Validating success scenarios', () => {
it(' => stylusClient.isTokenValid() ', async () => {
axios.post.mockImplementationOnce(() =>
Promise.resolve({
data: { ...IS_TOKEN__VALID_SUCCESS_RESPONSE },
status: 200,
}),
);
const gen = await _mockClient.isTokenValid();
expect(axios.post).toHaveBeenCalledTimes(2);
expect(gen.merchantId).toEqual(IS_TOKEN__VALID_SUCCESS_RESPONSE.merchantId);
});
});
describe('AUTH MODULE - Validating failure scenario', () => {
it(' => stylusClient.isTokenValid() - Token Invalid', async () => {
axios.post.mockImplementationOnce(() =>
Promise.reject({
response: {
status: 401,
data: {
...IS_TOKEN__VALID_ERROR_RESPONSE,
},
},
}),
);
try {
await _mockClient.isTokenValid();
expect(true).toEqual(false);
} catch (err) {
expect(axios.post).toHaveBeenCalledTimes(2);
expect(err.errorCode).toEqual(_errorConst.TOKEN_INVALID);
}
});
it(' => stylusClient.isTokenValid() - Token Expire', async () => {
axios.post.mockImplementationOnce(() =>
Promise.reject({
response: {
status: 401,
data: {
...IS_TOKEN__VALID_EXPIRE_ERROR_RESPONSE,
},
},
}),
);
try {
await _mockClient.isTokenValid();
expect(true).toEqual(false);
} catch (err) {
expect(axios.post).toHaveBeenCalledTimes(2);
expect(err.errorCode).toEqual(_errorConst.TOKEN_EXPIRE);
}
});
it(' => stylusClient.isTokenValid() - Unknown error', async () => {
axios.post.mockImplementationOnce(() =>
Promise.reject({
response: {
status: 500,
},
}),
);
try {
await _mockClient.isTokenValid();
expect(true).toEqual(false);
} catch (err) {
expect(axios.post).toHaveBeenCalledTimes(2);
expect(err.errorCode).toEqual(_errorConst.UNKNOWN_ERROR);
}
});
});