UNPKG

@fye/netsuite-rest-api

Version:

NetSuite Rest API Client

155 lines (141 loc) 5.48 kB
const test = require('ava'); const nock = require('nock'); const api = require('../netsuite-rest-api'); test.beforeEach(t => { nock.disableNetConnect(); t.context = { errorMessagePre: 'Netsuite Rest API missing one or more keys: ', requestData: { method: 'GET', requestType: 'record', }, suiteql: { method: 'POST', requestType: 'suiteql', }, config: { netsuiteApiHost: 'test.api.net', consumerKey: 'consumer-key', consumerSecret: 'consumer-secret', netsuiteAccountId: 'test-account-12345', netsuiteTokenKey: 'token-key', netsuiteTokenSecret: 'token-secret', testEnv: true, }, successResponse: { statusCode: 200, body: { foo: 'bar' }, }, failResponse: { statusCode: 400, body: JSON.stringify({ 'o:errorCode': 'TEST_ERROR', title: 'Test Error' }), }, }; }); test.afterEach(() => { nock.restore(); }); test('throws error when config is not the first paramater', async t => { const { requestData } = t.context; await t.throwsAsync(() => api.makeRequest(requestData), { instanceOf: Error }); }); test('throws error when config is empty', async t => { const { requestData, errorMessagePre } = t.context; const errorMessage = `${errorMessagePre}netsuiteApiHost,consumerKey,consumerSecret,netsuiteAccountId,netsuiteTokenKey,netsuiteTokenSecret`; await t.throwsAsync(() => api.makeRequest({}, requestData), { instanceOf: Error, message: errorMessage }); }); test('throws error when config is missing at least one config value', async t => { const { requestData, errorMessagePre, config } = t.context; const errorMessage = `${errorMessagePre}netsuiteAccountId`; await t.throwsAsync(() => api.makeRequest({ ...config, netsuiteAccountId: undefined }, requestData), { instanceOf: Error, message: errorMessage, }); }); test('throws error when request type is missing from the request data', async t => { const { requestData, config } = t.context; await t.throwsAsync(() => api.makeRequest(config, { ...requestData, requestType: undefined }), { instanceOf: Error, message: 'Request type required', }); }); test('throws error when method is missing from the request data', async t => { const { requestData, config } = t.context; await t.throwsAsync(() => api.makeRequest(config, { ...requestData, method: undefined }), { instanceOf: Error, message: 'Request method required', }); }); test('throws error when request type is not suiteql, workbook or record', async t => { const { requestData, config } = t.context; await t.throwsAsync(() => api.makeRequest(config, { ...requestData, requestType: 'pto' }), { instanceOf: Error, message: 'Unrecognized request type', }); }); test('Complete request', async t => { const { requestData, config, successResponse } = t.context; const testPath = 'complete-request-path'; nock(`http://${config.netsuiteApiHost}`) .get(`/${testPath}`) .reply(200, successResponse.body); const response = await api.makeRequest(config, { ...requestData, path: testPath }); t.is(JSON.stringify(response.body), JSON.stringify(successResponse.body)); }); test('Complete request when query is defined', async t => { const { requestData, config, successResponse } = t.context; const testPath = 'complete-request-path'; const query = { field: 'updated' }; nock(`http://${config.netsuiteApiHost}`) .patch(`/${testPath}`, query) .reply(200, successResponse.body); const response = await api.makeRequest(config, { ...requestData, method: 'patch', path: testPath, query, }); t.is(JSON.stringify(response.body), JSON.stringify(successResponse.body)); }); test('Complete request when suiteql is the request type without nextURL', async t => { const { suiteql, config, successResponse } = t.context; const query = 'select * from test;'; nock(`http://${config.netsuiteApiHost}`) .post(`/${api.suitesqlPath}`, { q: query }) .query({ limit: 10, offset: 0 }) .reply(200, successResponse.body); const response = await api.makeRequest(config, { ...suiteql, query, }); t.is(JSON.stringify(response.body), JSON.stringify(successResponse.body)); }); test('Complete request when suiteql is the request type and a nextURL', async t => { const { suiteql, config, successResponse } = t.context; const query = 'select * from test;'; nock(`http://${config.netsuiteApiHost}`) .post(`/${api.suitesqlPath}`, { q: query }) .query({ limit: 10, offset: 10 }) .reply(200, successResponse.body); const response = await api.makeRequest(config, { ...suiteql, nextUrl: `http://test.api.net/${api.suitesqlPath}?limit=10&offset=10`, query, }); t.is(JSON.stringify(response.body), JSON.stringify(successResponse.body)); }); test('Complete request when workbook is the request type and a nextURL', async t => { const { suiteql, config, successResponse } = t.context; const query = 'workbook999'; nock(`http://${config.netsuiteApiHost}`) .post(`/${api.workbookPath}/${query}/result`) .query({ limit: 10, offset: 10 }) .reply(200, successResponse.body); const response = await api.makeRequest(config, { ...suiteql, requestType: 'workbook', nextUrl: `http://${config.netsuiteApiHost}/${api.workbookPath}/${query}/result?limit=10&offset=10`, query, }); t.is(JSON.stringify(response.body), JSON.stringify(successResponse.body)); });