Skip to content

Commit

Permalink
Merge pull request #19 from packethost/improve-tests
Browse files Browse the repository at this point in the history
Replace grunt ci tasks with npm scripts
  • Loading branch information
lucasmpb committed Jun 16, 2015
2 parents 53c61ce + f39fbbf commit e19a1bd
Show file tree
Hide file tree
Showing 20 changed files with 167 additions and 61 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**.orig
# Logs
logs
*.log
Expand Down
5 changes: 5 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"preset": "google",
"validateIndentation": 4,
"maximumLineLength": 100
}
32 changes: 32 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions lib/config/config.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"apiUrl": "https://api.packet.net"
}
"apiUrl": "https://api.packet.net"
}
28 changes: 12 additions & 16 deletions lib/packet.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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(
{
Expand All @@ -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(
{
Expand All @@ -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(
{
Expand All @@ -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(
{
Expand All @@ -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'
Expand Down Expand Up @@ -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/';
Expand Down Expand Up @@ -402,7 +402,3 @@ function getNotificationsUrl(id) {
function getEmailsUrl(id) {
return '/emails/' + (id || '');
}

function getApiKeysUrl(id) {
return '/api-keys/' + (id || '');
}
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
"author": "Lucas Perez <lucas@packet.net>",
"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"
},
Expand All @@ -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"
}
Expand Down
7 changes: 4 additions & 3 deletions shippable.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
email: true
31 changes: 30 additions & 1 deletion test/devicesSpec.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
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();
});
});
});
});
4 changes: 3 additions & 1 deletion test/emailsSpec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';
// jshint ignore: start
var nock = require('nock');
var Packet = new require('../lib/packet');
var uuid = require('node-uuid');
Expand All @@ -7,4 +9,4 @@ var api = new Packet('');
describe('Client Emails Methods', function() {
beforeEach(function() {
});
});
});
24 changes: 17 additions & 7 deletions test/eventsSpec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var nock = require('nock');
var Packet = new require('../lib/packet');
var uuid = require('node-uuid');
Expand All @@ -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();
});
});
});
});
});
14 changes: 8 additions & 6 deletions test/facilitiesSpec.js
Original file line number Diff line number Diff line change
@@ -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, {
Expand All @@ -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();
});
});
});
});
});
4 changes: 3 additions & 1 deletion test/invitationsSpec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';
// jshint ignore: start
var nock = require('nock');
var Packet = new require('../lib/packet');
var uuid = require('node-uuid');
Expand All @@ -8,4 +10,4 @@ var api = new Packet('');
describe('Client Invitations Methods', function() {
beforeEach(function() {
});
});
});
4 changes: 3 additions & 1 deletion test/membershipsSpec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';
// jshint ignore: start
var nock = require('nock');
var Packet = new require('../lib/packet');
var uuid = require('node-uuid');
Expand All @@ -8,4 +10,4 @@ var api = new Packet('');
describe('Client Membership Methods', function() {
beforeEach(function() {
});
});
});
23 changes: 14 additions & 9 deletions test/notificationsSpec.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
});
});
Loading

0 comments on commit e19a1bd

Please sign in to comment.