diff --git a/.gitignore b/.gitignore index 52a8e13..3f78c8d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +**.orig # Logs logs *.log diff --git a/.jscsrc b/.jscsrc new file mode 100644 index 0000000..1ecc9a3 --- /dev/null +++ b/.jscsrc @@ -0,0 +1,5 @@ +{ + "preset": "google", + "validateIndentation": 4, + "maximumLineLength": 100 +} \ No newline at end of file diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..8e990ee --- /dev/null +++ b/.jshintrc @@ -0,0 +1,32 @@ +{ + "indent" : 4, // {int} Number of spaces to use for indentation + "latedef" : false, // true: Require variables/functions to be defined before being used + "newcap" : true, // true: Require capitalization of all constructor functions e.g. `new F()` + "noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee` + "noempty" : true, // true: Prohibit use of empty blocks + "nonbsp" : true, // true: Prohibit "non-breaking whitespace" characters. + "nonew" : false, // true: Prohibit use of constructors for side-effects (without assignment) + "plusplus" : false, // true: Prohibit use of `++` & `--` + "quotmark" : "single", // Quotation mark consistency: + // false : do nothing (default) + // true : ensure whatever is used is consistent + // "single" : require single quotes + // "double" : require double quotes + "undef" : true, // true: Require all non-global variables to be declared (prevents global leaks) + "unused" : true, // Unused variables: + // true : all variables, last function parameter + // "vars" : all variables only + // "strict" : all variables, all function parameters + "strict" : true, // true: Requires all functions run in ES5 Strict Mode + "maxparams" : false, // {int} Max number of formal params allowed per function + "maxdepth" : false, // {int} Max depth of nested blocks (within functions) + "maxstatements" : false, // {int} Max number statements per function + "maxcomplexity" : false, // {int} Max cyclomatic complexity per function + "maxlen" : false, // {int} Max number of characters per line + + "node" : true, // Node.js + "mocha" : true, // Mocha testing framework + + // Custom Globals + "globals" : {} // additional predefined global variables +} \ No newline at end of file diff --git a/lib/config/config.json b/lib/config/config.json index 9b39ac6..d5310e7 100644 --- a/lib/config/config.json +++ b/lib/config/config.json @@ -1,3 +1,3 @@ { - "apiUrl": "https://api.packet.net" -} \ No newline at end of file + "apiUrl": "https://api.packet.net" +} diff --git a/lib/packet.js b/lib/packet.js index a4fb8d6..3a81122 100644 --- a/lib/packet.js +++ b/lib/packet.js @@ -1,7 +1,7 @@ +'use strict'; + var request = require('request'); -var querystring = require('querystring'); var config = require('./config/config.json'); - var Packet = function(apiKey) { this.apiKey = apiKey; this.currentEnvironment = config.apiUrl; @@ -254,7 +254,7 @@ Packet.prototype.removeEmail = function(id, callback) { }); }; -Packet.prototype._get = function(url, parameters, callback){ +Packet.prototype._get = function(url, parameters, callback) { var self = this; request( { @@ -264,12 +264,12 @@ Packet.prototype._get = function(url, parameters, callback){ headers: self.getAuthHeaders(), json:true }, function(err, res, body) { - callback(handleRequestError(err,body), body || {}); + callback(handleRequestError(err, body), body || {}); } ); }; -Packet.prototype._post = function(url, postData, callback){ +Packet.prototype._post = function(url, postData, callback) { var self = this; request( { @@ -279,12 +279,12 @@ Packet.prototype._post = function(url, postData, callback){ headers: self.getAuthHeaders(), json:true }, function(err, res, body) { - callback(handleRequestError(err,body), body || {}); + callback(handleRequestError(err, body), body || {}); } ); }; -Packet.prototype._patch = function(url, postData, callback){ +Packet.prototype._patch = function(url, postData, callback) { var self = this; request( { @@ -294,12 +294,12 @@ Packet.prototype._patch = function(url, postData, callback){ headers: self.getAuthHeaders(), json:true }, function(err, res, body) { - callback(handleRequestError(err,body), body || {}); + callback(handleRequestError(err, body), body || {}); } ); }; -Packet.prototype._delete = function(url, callback){ +Packet.prototype._delete = function(url, callback) { var self = this; request( { @@ -308,13 +308,13 @@ Packet.prototype._delete = function(url, callback){ headers: self.getAuthHeaders(), json:true }, function(err, res, body) { - callback(handleRequestError(err,body), body || {}); + callback(handleRequestError(err, body), body || {}); } ); }; Packet.prototype.getAuthHeaders = function() { - return { + return { 'X-Auth-Token': this.apiKey, 'Content-Type': 'application/json', 'Accept': 'application/json' @@ -347,7 +347,7 @@ function getProjectsUrl(id, action) { function getDevicesUrl(projectId, id, action) { if (id) { - return '/devices/' + (id + '/' || '') + (action || ''); + return '/devices/' + (id + '/' || '') + (action || ''); } if (projectId) { return '/projects/' + projectId + '/devices/'; @@ -402,7 +402,3 @@ function getNotificationsUrl(id) { function getEmailsUrl(id) { return '/emails/' + (id || ''); } - -function getApiKeysUrl(id) { - return '/api-keys/' + (id || ''); -} \ No newline at end of file diff --git a/package.json b/package.json index 7c07e37..a9caa9e 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,12 @@ "author": "Lucas Perez ", "description": "Packet API wrapper", "main": "./lib/packet.js", + "scripts": { + "cs": "jscs lib test", + "lint": "jshint lib test", + "test": "mocha --reporter spec test", + "ci": "npm run cs && npm run lint && npm test" + }, "dependencies": { "request": "2.x" }, @@ -15,9 +21,9 @@ ], "devDependencies": { "chai": "^3.0.0", - "grunt": "^0.4.5", - "grunt-contrib-jshint": "^0.11.2", - "grunt-mocha-test": "^0.12.7", + "jscs": "^1.11.0", + "jshint": "^2.8.0", + "mocha": "^2.2.5", "nock": "^2.3.0", "node-uuid": "^1.4.3" } diff --git a/shippable.yaml b/shippable.yaml index 91ff68a..bbb02e3 100644 --- a/shippable.yaml +++ b/shippable.yaml @@ -6,11 +6,12 @@ node_js: cache: true before_install: - - npm install -g grunt-cli - npm install script: - - grunt + - npm run cs + - npm run lint + - npm test notifications: - email: true \ No newline at end of file + email: true diff --git a/test/devicesSpec.js b/test/devicesSpec.js index 0427eed..6739660 100644 --- a/test/devicesSpec.js +++ b/test/devicesSpec.js @@ -1,11 +1,40 @@ +'use strict'; + var nock = require('nock'); var Packet = new require('../lib/packet'); var uuid = require('node-uuid'); +var apiConfig = require('../lib/config/config.json'); var expect = require('chai').expect; var projectId; var deviceId; var api = new Packet(''); describe('Client Devices Methods', function() { beforeEach(function() { + projectId = uuid.v4(); + deviceId = uuid.v4(); }); -}); \ No newline at end of file + describe('getDevices', function() { + it('should get a list of devices', function(done) { + var mock = nock(apiConfig.apiUrl) + .intercept('/projects/' + projectId + '/devices/', 'GET') + .reply(200, {devices:[{name:'device 1'}]}); + api.getDevices(projectId, false, false, function(err, data) { + expect(err).to.equal(null); + expect(data.devices).to.deep.equal([{name:'device 1'}]); + done(); + mock.done(); + }); + }); + it('should get a single device', function(done) { + var mock = nock(apiConfig.apiUrl) + .intercept('/devices/' + deviceId + '/', 'GET') + .reply(200, {name:'device 1'}); + api.getDevices(false, deviceId, false, function(err, data) { + expect(err).to.equal(null); + expect(data).to.deep.equal({name:'device 1'}); + done(); + mock.done(); + }); + }); + }); +}); diff --git a/test/emailsSpec.js b/test/emailsSpec.js index 795895c..0612ea6 100644 --- a/test/emailsSpec.js +++ b/test/emailsSpec.js @@ -1,3 +1,5 @@ +'use strict'; +// jshint ignore: start var nock = require('nock'); var Packet = new require('../lib/packet'); var uuid = require('node-uuid'); @@ -7,4 +9,4 @@ var api = new Packet(''); describe('Client Emails Methods', function() { beforeEach(function() { }); -}); \ No newline at end of file +}); diff --git a/test/eventsSpec.js b/test/eventsSpec.js index 80cd0aa..51f7c37 100644 --- a/test/eventsSpec.js +++ b/test/eventsSpec.js @@ -1,3 +1,5 @@ +'use strict'; + var nock = require('nock'); var Packet = new require('../lib/packet'); var uuid = require('node-uuid'); @@ -11,20 +13,28 @@ describe('Client Events Methods', function() { }); describe('getEvents', function() { it('should get a list of events', function(done) { - var randomId_1 = uuid.v4(); - var randomId_2 = uuid.v4(); + var randomId1 = uuid.v4(); + var randomId2 = uuid.v4(); var mock = nock(apiUrl) .intercept('/events/', 'GET') .reply(200, { events:[ - {href: apiUrl + '/events/'+ randomId_1}, - {href: apiUrl + '/events/'+ randomId_2} - ]}); + {href: apiUrl + '/events/' + randomId1}, + {href: apiUrl + '/events/' + randomId2} + ] + }); api.getEvents(false, false, function(err, data) { - expect(data).to.deep.equal({events:[{href: apiUrl + '/events/'+ randomId_1}, {href: apiUrl + '/events/'+ randomId_2}]}); + expect(err).to.equal(null); + expect(data).to.deep.equal({events:[ + { + href: apiUrl + '/events/' + randomId1 + }, { + href: apiUrl + '/events/' + randomId2 + } + ]}); done(); mock.done(); }); }); }); -}); \ No newline at end of file +}); diff --git a/test/facilitiesSpec.js b/test/facilitiesSpec.js index 15a80d8..94b443e 100644 --- a/test/facilitiesSpec.js +++ b/test/facilitiesSpec.js @@ -1,14 +1,15 @@ +'use strict'; + var nock = require('nock'); var Packet = new require('../lib/packet'); -var uuid = require('node-uuid'); var apiConfig = require('../lib/config/config.json'); var expect = require('chai').expect; var api = new Packet(''); describe('Client Facilities Methods', function() { - beforeEach(function() { - }); - describe('getLocations', function() { - it('should get a list of facilities', function(done) { + beforeEach(function() { + }); + describe('getLocations', function() { + it('should get a list of facilities', function(done) { var mock = nock(apiConfig.apiUrl) .intercept('/facilities/', 'GET') .reply(200, { @@ -17,10 +18,11 @@ describe('Client Facilities Methods', function() { {name:'facility 2'} ]}); api.getLocations(function(err, data) { + expect(err).to.equal(null); expect(data).to.deep.equal({facilities:[{name:'facility 1'}, {name:'facility 2'}]}); done(); mock.done(); }); }); }); -}); \ No newline at end of file +}); diff --git a/test/invitationsSpec.js b/test/invitationsSpec.js index 8fd49c3..ff6bd22 100644 --- a/test/invitationsSpec.js +++ b/test/invitationsSpec.js @@ -1,3 +1,5 @@ +'use strict'; +// jshint ignore: start var nock = require('nock'); var Packet = new require('../lib/packet'); var uuid = require('node-uuid'); @@ -8,4 +10,4 @@ var api = new Packet(''); describe('Client Invitations Methods', function() { beforeEach(function() { }); -}); \ No newline at end of file +}); diff --git a/test/membershipsSpec.js b/test/membershipsSpec.js index 5e82dc5..148b752 100644 --- a/test/membershipsSpec.js +++ b/test/membershipsSpec.js @@ -1,3 +1,5 @@ +'use strict'; +// jshint ignore: start var nock = require('nock'); var Packet = new require('../lib/packet'); var uuid = require('node-uuid'); @@ -8,4 +10,4 @@ var api = new Packet(''); describe('Client Membership Methods', function() { beforeEach(function() { }); -}); \ No newline at end of file +}); diff --git a/test/notificationsSpec.js b/test/notificationsSpec.js index 038b70f..ef5d4e9 100644 --- a/test/notificationsSpec.js +++ b/test/notificationsSpec.js @@ -1,27 +1,32 @@ +'use strict'; +// jshint ignore: start var nock = require('nock'); var Packet = new require('../lib/packet'); var uuid = require('node-uuid'); var apiConfig = require('../lib/config/config.json'); var expect = require('chai').expect; -var notificationId; var api = new Packet(''); describe('Client Notification Methods', function() { - describe('getNotifications', function() { + describe('getNotifications', function() { it('should get a list of notifications', function(done) { - var randomId_1 = uuid.v4(); - var randomId_2 = uuid.v4(); + var randomId1 = uuid.v4(); + var randomId2 = uuid.v4(); var mock = nock(apiConfig.apiUrl) .intercept('/notifications/', 'GET') .reply(200, { notifications:[ - {id:randomId_1}, - {id:randomId_2} - ]}); + {id:randomId1}, + {id:randomId2} + ]}); api.getNotifications(false, false, function(err, data) { - expect(data).to.deep.equal({notifications:[{id:randomId_1}, {id:randomId_2}]}); + expect(err).to.equal(null); + expect(data).to.deep.equal({notifications:[ + {id:randomId1}, + {id:randomId2} + ]}); mock.done(); done(); }); }); }); -}); \ No newline at end of file +}); diff --git a/test/osSpec.js b/test/osSpec.js index fd8d0fc..a67f760 100644 --- a/test/osSpec.js +++ b/test/osSpec.js @@ -1,8 +1,8 @@ +'use strict'; + var nock = require('nock'); var Packet = new require('../lib/packet'); -var uuid = require('node-uuid'); var apiConfig = require('../lib/config/config.json'); -var currentEnv = 'dev'; var expect = require('chai').expect; var api = new Packet(''); describe('Client Operating Systems Methods', function() { @@ -16,10 +16,11 @@ describe('Client Operating Systems Methods', function() { {slug:'coreos_stable'} ]}); api.getOperatingSystems(function(err, data) { + expect(err).to.equal(null); expect(data).to.deep.equal({oses:[{slug:'ubuntu_14_04'}, {slug:'coreos_stable'}]}); done(); mock.done(); }); }); }); -}); \ No newline at end of file +}); diff --git a/test/plansSpec.js b/test/plansSpec.js index d7ef14b..028a446 100644 --- a/test/plansSpec.js +++ b/test/plansSpec.js @@ -1,6 +1,7 @@ +'use strict'; + var nock = require('nock'); var Packet = new require('../lib/packet'); -var uuid = require('node-uuid'); var apiConfig = require('../lib/config/config.json'); var expect = require('chai').expect; var api = new Packet(''); @@ -15,10 +16,11 @@ describe('Client Plans Methods', function() { {slug:'baremetal_2'} ]}); api.getPlans(function(err, data) { + expect(err).to.equal(null); expect(data).to.deep.equal({plans:[{slug:'baremetal_1'}, {slug:'baremetal_2'}]}); done(); mock.done(); }); }); }); -}); \ No newline at end of file +}); diff --git a/test/projectsSpec.js b/test/projectsSpec.js index dd82da5..bbc4407 100644 --- a/test/projectsSpec.js +++ b/test/projectsSpec.js @@ -1,3 +1,5 @@ +'use strict'; + var nock = require('nock'); var Packet = new require('../lib/packet'); var uuid = require('node-uuid'); @@ -15,6 +17,7 @@ describe('Client Projects Methods', function() { .intercept('/projects/', 'GET') .reply(200, {projects:[{name:'project 1'}]}); api.getProjects(false, false, function(err, data) { + expect(err).to.equal(null); expect(data.projects).to.deep.equal([{name:'project 1'}]); done(); mock.done(); @@ -25,10 +28,11 @@ describe('Client Projects Methods', function() { .intercept('/projects/' + projectId + '/', 'GET') .reply(200, {name:'project 1'}); api.getProjects(projectId, false, function(err, data) { + expect(err).to.equal(null); expect(data).to.deep.equal({name:'project 1'}); done(); mock.done(); }); }); }); -}); \ No newline at end of file +}); diff --git a/test/sshkeysSpec.js b/test/sshkeysSpec.js index 8c47648..ef747a7 100644 --- a/test/sshkeysSpec.js +++ b/test/sshkeysSpec.js @@ -1,3 +1,5 @@ +'use strict'; +// jshint ignore: start var nock = require('nock'); var Packet = new require('../lib/packet'); var uuid = require('node-uuid'); @@ -6,4 +8,4 @@ var expect = require('chai').expect; var sshkeyId; var api = new Packet(''); describe('Client SSH Keys Methods', function() { -}); \ No newline at end of file +}); diff --git a/test/transfersSpec.js b/test/transfersSpec.js index 26be7c4..dac8242 100644 --- a/test/transfersSpec.js +++ b/test/transfersSpec.js @@ -1,3 +1,5 @@ +'use strict'; +// jshint ignore: start var nock = require('nock'); var Packet = new require('../lib/packet'); var uuid = require('node-uuid'); @@ -6,6 +8,6 @@ var expect = require('chai').expect; var transferId; var api = new Packet(''); describe('Client Transfers Methods', function() { - beforeEach(function() { - }); -}); \ No newline at end of file + beforeEach(function() { + }); +}); diff --git a/test/usersSpec.js b/test/usersSpec.js index b32971a..3470e7e 100644 --- a/test/usersSpec.js +++ b/test/usersSpec.js @@ -1,3 +1,5 @@ +'use strict'; +// jshint ignore: start var nock = require('nock'); var Packet = new require('../lib/packet'); var uuid = require('node-uuid'); @@ -6,4 +8,4 @@ var expect = require('chai').expect; var userId; var api = new Packet(''); describe('Client Users Methods', function() { -}); \ No newline at end of file +});