@twec/node-suite
Version:
Generic functionality for connecting to NetSuite Web Services from Node
70 lines (61 loc) • 2.47 kB
JavaScript
const test = require('ava');
const NetSuiteAuth = require('../lib/auth');
test.beforeEach('instantiate NetSuiteAuth into context, and add sample data', (t) => {
const { context } = t;
const authOptions = {
consumer: {}, // required by oAuth
realm: 'TEST-REALM',
};
context.auth = new NetSuiteAuth(authOptions);
context.sampleTextBeforeEncoding = 'HI! <$>';
context.sampleTextAfterEncoding = 'HI%21%20%3C%24%3E';
context.sampleOAuthData = {
oauth_consumer_key: 'A',
oauth_token: 'B',
oauth_nonce: 'C',
oauth_timestamp: ':)',
};
});
test('NetSuiteAuth is a function (class)', (t) => {
t.is(typeof NetSuiteAuth, 'function');
});
test('percentEncode encodes text as expected', (t) => {
const { auth, sampleTextBeforeEncoding, sampleTextAfterEncoding } = t.context;
const encoded = auth.percentEncode(sampleTextBeforeEncoding);
t.is(encoded, sampleTextAfterEncoding);
});
test('percentEncodeArrayValues encodes the values of an array', (t) => {
const { auth, sampleTextBeforeEncoding, sampleTextAfterEncoding } = t.context;
const arr = ['HELLO', ':)', sampleTextBeforeEncoding];
const returnedArray = auth.percentEncodeArrayValues(arr);
t.log(t);
t.deepEqual(returnedArray, arr);
t.is(arr[0], 'HELLO');
t.is(arr[1], '%3A%29');
t.is(arr[2], sampleTextAfterEncoding);
});
test('toBaseStringArray returns the expected array', (t) => {
const { auth, sampleOAuthData } = t.context;
const arr = auth.toBaseStringArray(sampleOAuthData);
t.is(arr[0], 'TEST-REALM');
t.is(arr[1], sampleOAuthData.oauth_consumer_key);
t.is(arr[2], sampleOAuthData.oauth_token);
t.is(arr[3], sampleOAuthData.oauth_nonce);
t.is(arr[4], sampleOAuthData.oauth_timestamp);
});
test('getBaseString returns the expected string', (t) => {
const { auth, sampleOAuthData } = t.context;
const str = auth.getBaseString(null, sampleOAuthData);
t.is(str, 'TEST-REALM&A&B&C&%3A%29');
});
test('toNode returns an object with expected first couple of children', (t) => {
const { auth, sampleOAuthData } = t.context;
const node = auth.toNode(sampleOAuthData);
t.is(typeof node, 'object');
t.is(node.name, 'platformMsgs:tokenPassport');
t.is(node.children[0].name, 'platformMsgs:account');
t.is(node.children[0].value, 'TEST-REALM');
t.is(node.children[1].name, 'platformMsgs:consumerKey');
t.is(node.children[1].value, sampleOAuthData.oauth_consumer_key);
});
// TODO: Test more of the object that toNode returns