UNPKG

mojio-js-sdk

Version:

Mojio javascript REST client.

295 lines (286 loc) 11 kB
// Generated by CoffeeScript 1.10.0 (function() { var MojioRestSDK, MojioSDK, async, nock, should; MojioSDK = require('../../src/nodejs/sdk/MojioSDK'); MojioRestSDK = require('../../src/nodejs/sdk/MojioRestSDK'); should = require('should'); async = require('async'); nock = require('nock'); describe('Node Mojio Fluent Rest SDK POST calls', function() { var authorization, call, callback_url, client_id, client_secret, mojio, options, sdk, testAccountsURL, testApiURL, timeout, user, vehicle; user = null; mojio = null; vehicle = null; client_id = 'cacc0d94-b6b4-4da7-9983-3991de197038'; client_secret = '427d5794-5021-4274-a6e8-a38d5d83bf99'; call = null; timeout = 50; callback_url = "http://localhost:3000/callback"; authorization = { client_id: client_id, client_secret: client_secret, redirect_uri: 'http://localhost:3000/callback', username: 'testing', password: 'Test123!', scope: 'full', grant_type: 'password' }; options = { sdk: MojioRestSDK, environment: 'staging', accountsURL: 'accounts.moj.io', apiURL: 'api.moj.io', pushURL: 'push.moj.io', client_id: authorization.client_id, client_secret: authorization.client_secret }; if (options.environment !== '') { testAccountsURL = 'https://' + options.environment + '-' + options.accountsURL; testApiURL = 'https://' + options.environment + '-' + options.apiURL; } else { testAccountsURL = 'https://' + options.accountsURL; testApiURL = 'https://' + options.apiURL; } beforeEach(function() { user = null; mojio = null; return vehicle = null; }); sdk = new MojioSDK(options); return it('can POST and PUT entities and get a resource or secondary resource by ids or lists', function(done) { var docall, entities, entity, execute, i, len, results1, secondaries, secondaryEntity, setupAuthorizeNock, setupNock, setupPostPutNock, setupTokenNock, testErrorResult; setupTokenNock = function() { if (process.env.FUNCTIONAL_TESTS === 'true') { timeout = 3000; return { done: function() {} }; } else { call = nock(testAccountsURL).post("/oauth2/token", authorization).reply(function(uri, requestBody, cb) { cb(null, [ 200, { id: 4, access_token: "token" } ]); }); return call; } }; setupAuthorizeNock = function() { if (process.env.FUNCTIONAL_TESTS === 'true') { timeout = 3000; return { done: function() {} }; } else { call = nock(testAccountsURL).post("/oauth2/token", authorization).reply(function(uri, requestBody, cb) { cb(null, [ 200, { id: 2, access_token: "token" } ]); }); return call; } }; setupNock = function(api, verb, version, primary_, pid_, secondary_, sid_, _tertiary, tid_) { var pathFilter, test; if (process.env.FUNCTIONAL_TESTS === 'true') { timeout = 3000; return { done: function() {} }; } else { test = { pid: pid_, sid: sid_, tid: tid_, secondary: secondary_, primary: primary_, tertiary: _tertiary }; pathFilter = function(path) { var idRegex, parts, valid, versionValid; parts = path.split('/'); idRegex = /\b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}\b/; parts = parts.slice(1, parts.length); versionValid = parts[0] === version; parts = parts.slice(1, parts.length); if (parts.length === 1) { valid = parts[0] === test.primary; } else if (parts.length === 2) { valid = parts[0] === test.primary && (parts[1].match(idRegex) || parts[1] === "" + test.pid); } else if (parts.length === 3) { valid = parts[0] === test.primary && (parts[1].match(idRegex) || parts[1] === "" + test.pid) && parts[2] === test.secondary; } else if (parts.length === 4 && (test.sid != null)) { valid = parts[0] === test.primary && (parts[1].match(idRegex) || parts[1] === "" + test.pid) && parts[2] === test.secondary && (parts[3].match(idRegex) || parts[3] === "" + test.sid); } else if (parts.length === 4 && (test.sid == null)) { valid = parts[0] === test.primary && (parts[1].match(idRegex) || parts[1] === "" + test.pid) && parts[2] === test.secondary && parts[3] === test.tertiary; } else if (parts.length === 5) { valid = parts[0] === test.primary && (parts[1].match(idRegex) || parts[1] === "" + test.pid) && parts[2] === test.secondary && (parts[3].match(idRegex) || parts[3] === "" + test.sid) && parts[4] === test.tertiary; } else if (parts.length === 6) { valid = parts[0] === test.primary && (parts[1].match(idRegex) || parts[1] === "" + test.pid) && parts[2] === test.secondary && (parts[3].match(idRegex) || parts[3] === "" + test.sid) && parts[4] === test.tertiary && (parts[5].match(idRegex) || parts[5] === "" + test.tid); } if (valid && versionValid) { return '/true'; } else { return '/false'; } }; nock.removeInterceptor({ hostname: api }); call = nock(api).filteringPath(pathFilter)[verb]('/true').reply(function(uri, requestBody, cb) { cb(null, [ 200, { id: 3, message: "hi" } ]); }); return call; } }; testErrorResult = function(error, result) { if (error != null) { console.log("ERROR: " + error.statusMessage + " content:" + error.content); } (error === null).should.be["true"]; return (result !== null).should.be["true"]; }; execute = function(test, doneOne) { return async.series([ function(cb) { setupAuthorizeNock(); return sdk.token(authorization.redirect_uri).credentials(authorization.username, authorization.password).scope(['full']).callback(function(error, result) { if (error) { console.log('Access Token Error' + " url:" + sdk.url()); return cb(error, result); } else { setupTokenNock(); return sdk.token().parse(result).callback(function(error, result) { var token; token = result; return cb(error, result); }); } }); }, function(cb) { return test(cb); } ], function(error, results) { if (error != null) { console.log(error); } (error === null).should.be["true"]; (results !== null).should.be["true"]; doneOne(); }); }; setupPostPutNock = function(api, verb, version, primary_, pid_, secondary_, data_) { var check; if (process.env.FUNCTIONAL_TESTS === 'true') { timeout = 3000; return { done: function() {} }; } else { check = { pid: pid_, data: data_, secondary: secondary_, primary: primary_ }; call = nock(api).filteringPath(function(path) { var idRegex, parts, valid, versionValid; parts = path.split('/'); idRegex = /\b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}\b/; parts = parts.slice(1, parts.length); versionValid = parts[0] === version; parts = parts.slice(1, parts.length); if (parts.length === 1) { valid = parts[0] === check.primary; } else if (parts.length === 3) { valid = parts[0] === check.primary && (parts[1].match(idRegex) || parts[1] === "" + check.pid) && parts[2] === check.secondary; } if (valid && versionValid) { return '/true'; } else { return '/false'; } })[verb]('/true', check.data).reply(function(uri, requestBody, cb) { return cb(null, [ 200, { id: 1, name: "name" } ]); }); return null; } }; docall = function(verb, entity, pid, secondary, data) { var finish; if (pid == null) { pid = null; } if (secondary == null) { secondary = null; } if (data == null) { data = null; } if (entity === 'Trips' && secondary === 'Permissions' && verb === 'put') { finish = done; } else { finish = (function() {}); } return execute(function(cb) { if ((secondary != null)) { call = setupPostPutNock(testApiURL, verb, 'v2', entity, pid, secondary, data); return sdk[verb]()[entity](pid)[secondary](data).callback(function(error, result) { testErrorResult(error, result); return cb(null, result); }); } else { call = setupPostPutNock(testApiURL, verb, 'v2', entity, null, null, data); return sdk[verb]()[entity](data).callback(function(error, result) { testErrorResult(error, result); return cb(null, result); }); } }, finish); }; entities = ['Mojios', 'Vehicles', 'Users', 'Apps', 'Groups', 'Trips']; secondaries = ['Tags', 'Permissions', 'Images', 'Details']; results1 = []; for (i = 0, len = entities.length; i < len; i++) { entity = entities[i]; docall('post', entity, null, null, { name: 'name' + entity }); docall('put', entity, null, null, { name: 'name' + entity }); results1.push((function() { var j, len1, results2; results2 = []; for (j = 0, len1 = secondaries.length; j < len1; j++) { secondaryEntity = secondaries[j]; docall('post', entity, 1, secondaryEntity, { name: 'name/' + entity + '/1/' + secondaryEntity }); results2.push(docall('put', entity, 1, secondaryEntity, { name: 'name/' + entity + '/1/' + secondaryEntity })); } return results2; })()); } return results1; }); }); }).call(this); //# sourceMappingURL=NodeRestSDK_POST_test.js.map