From 25ae4e9c132d6e68ddb389808b8f22c8f43b17cd Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Mon, 14 Dec 2015 22:23:28 -0500 Subject: Begin rewrite for new OAuth API --- tests/active911.js | 128 +++++++++++---------------------------------- tests/replies/devices.json | 0 2 files changed, 31 insertions(+), 97 deletions(-) delete mode 100644 tests/replies/devices.json (limited to 'tests') diff --git a/tests/active911.js b/tests/active911.js index 309aafb..c0a9e1b 100644 --- a/tests/active911.js +++ b/tests/active911.js @@ -1,105 +1,39 @@ -require('should'); -require('nock'); -Active911 = require('../lib/active911.js'); +var should = require('should'); +var nock = require('nock'); +var Active911 = require('../lib/active911.js'); var a911; - -var testDevicesResponse = require('replies/devices.json'); -var testAgencyResponse = require('replies/agency.json'); -var testDeviceResponse = require('replies/device.json'); -var testErrorResponse = require('replies/error.json'); +var testDeviceResponse = require('./replies/device.json'); +var testAgencyResponse = require('./replies/agency.json'); +var testErrorResponse = require('./replies/error.json'); describe('Active911 API', function() { - - beforeEach(function(done) { - a911 = new Active911(); - }); - - describe('#getAgency', function() { - - it('Should return correct agency data', function() { - - nock('https://access.active911.com') - .get('/interface/open_api/api/') - .replyWithFile(200, __dirname + 'replies/agency.json'}); - - a911.getAgency(function(err, agency) { - should.not.exist(err); - agency.should.equal(testAgencyResponse.message.agency); - }); - }); - - it('Should give an error if the API gives an error', function() { - nock('https://access.active911.com') - .get('/interface/open_api/api/') - .replyWithFile(400, __dirname + 'replies/error.json'); - - a911.getAgency(function(err, agency) { - err.should.equal(testErrorResponse.message); - should.not.exist(agency); - }); - }); - - }); - - describe('#getDevices', function() { - it('Should return correct device data', function() { - - nock('https://access.active911.com') - .get('/interface/open_api/api/') - .replyWithFile(400, __dirname + 'replies/agency.json'); - - a911.getDevices(function(err, devices) { - should.not.exist(err); - devices.should.equal(testDevicesResponse.message.devices); - }); - }); - - it('Should give an error if the API gives an error', function() { - nock('https://access.active911.com') - .get('/interface/open_api/api/') - .replyWithFile(400, __dirname + 'replies/error.json'); - - a911.getDevices(function(err, devices) { - err.should.equal(testErrorResponse.message); - should.not.exist(devices); - }); - }); + beforeEach(function(done) { + a911 = new Active911.RefreshClient('CLIENT'); + done(); + }); + + describe('#getAgency', function() { + it('Should return correct agency data', function() { + nock('https://access.active911.com') + .get('/interface/open_api/api/') + .replyWithFile(200, __dirname + 'replies/agency.json'); + a911.getAgency().then(function(agency) { + agency.should.equal(testAgencyResponse.message.agency); + }).catch(function(err) { + should.fail(); + }); }); - describe('#getDevice', function() { - it('Should return correct device data (String id)', function() { - nock('https://access.active911.com') - .get('/interface/open_api/api/devices/1') - .replyWithFile(200, __dirname + 'replies/device.json'); - - a911.getDevice('1', function(err, device) { - should.not.exist(err); - device.should.equal(testDeviceResponse.message.device); - }); - }); - - it('Should return correct device data (Number id)', function() { - nock('https://access.active911.com') - .get('/interface/open_api/api/devices/1') - .replyWithFile(200, __dirname + 'replies/device.json'); - - a911.getDevice(1, function(err, device) { - should.not.exist(err); - device.should.equal(testDeviceResponse.message.device); - }); - }); - - it('Should give an error if the API gives an error', function() { - nock('https://access.active911.com') - .get('/interface/open_api/api/devices/1') - .replyWithFile(400, __dirname + 'replies/error.json'); - - a911.getDevice(1, function(err, device) { - err.should.equal(testErrorResponse.message); - should.not.exist(device); - }); - }); + it('Should give an error if the API gives an error', function() { + nock('https://access.active911.com') + .get('/interface/open_api/api/') + .replyWithFile(400, __dirname + 'replies/error.json'); + a911.getAgency().then(function(err, agency) { + should.fail(); + }).catch(function(err) { + err.should.equal(testErrorResponse.message); + }); }); - + }); }); diff --git a/tests/replies/devices.json b/tests/replies/devices.json deleted file mode 100644 index e69de29..0000000 -- cgit v1.2.3 From 7636932bc6af6b509269c6adfd620c5283051b3f Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Wed, 16 Dec 2015 22:09:50 -0500 Subject: Update unit tests --- tests/active911.js | 145 ++++++++++++++++++++++++++++++++++++------- tests/replies/alert.json | 45 ++++++++++++++ tests/replies/alerts.json | 15 +++++ tests/replies/location.json | 22 +++++++ tests/replies/locations.json | 11 ++++ tests/replies/resource.json | 22 +++++++ 6 files changed, 237 insertions(+), 23 deletions(-) create mode 100644 tests/replies/alert.json create mode 100644 tests/replies/alerts.json create mode 100644 tests/replies/location.json create mode 100644 tests/replies/locations.json create mode 100644 tests/replies/resource.json (limited to 'tests') diff --git a/tests/active911.js b/tests/active911.js index c0a9e1b..0aa9ffa 100644 --- a/tests/active911.js +++ b/tests/active911.js @@ -1,39 +1,138 @@ -var should = require('should'); +var chai = require('chai'); +var should = chai.should(); +var chaiAsPromised = require('chai-as-promised'); +chai.use(chaiAsPromised); var nock = require('nock'); +var Timestamp = require('unix-timestamp'); var Active911 = require('../lib/active911.js'); -var a911; -var testDeviceResponse = require('./replies/device.json'); -var testAgencyResponse = require('./replies/agency.json'); -var testErrorResponse = require('./replies/error.json'); +var client; +var deviceResponse = require('./replies/device.json'); +var agencyResponse = require('./replies/agency.json'); +var alertsResponse = require('./replies/alerts.json'); +var alertResponse = require('./replies/alert.json'); +var locationsResponse = require('./replies/locations.json'); +var locationResponse = require('./replies/location.json'); +var resourceResponse = require('./replies/resource.json'); +var errorResponse = require('./replies/error.json'); + +var nockPath = function(path, response) { + nock('https://access.active911.com') + .get('/interface/open_api/api' + path) + .reply(200, response); +}; + +var nockError = function(path) { + nock('https://access.active911.com') + .get('/interface/open_api/api' + path) + .reply(400, errorResponse); +}; describe('Active911 API', function() { beforeEach(function(done) { - a911 = new Active911.RefreshClient('CLIENT'); + client = new Active911.RefreshClient('CLIENT'); + nock('https://www.active911.com') + .post('/interface/dev/api_access.php') + .reply(200, { + access_token: 'DUMMY', + expiration: Timestamp.now(100) + }); done(); }); describe('#getAgency', function() { - it('Should return correct agency data', function() { - nock('https://access.active911.com') - .get('/interface/open_api/api/') - .replyWithFile(200, __dirname + 'replies/agency.json'); - a911.getAgency().then(function(agency) { - agency.should.equal(testAgencyResponse.message.agency); - }).catch(function(err) { - should.fail(); - }); + it('Should return correct data', function() { + nockPath('/', agencyResponse); + return client.getAgency().should.eventually.deep.equal(agencyResponse.message.agency); }); it('Should give an error if the API gives an error', function() { - nock('https://access.active911.com') - .get('/interface/open_api/api/') - .replyWithFile(400, __dirname + 'replies/error.json'); - a911.getAgency().then(function(err, agency) { - should.fail(); - }).catch(function(err) { - err.should.equal(testErrorResponse.message); - }); + nockError('/'); + return client.getAgency().should.be.rejectedWith(errorResponse.message); + }); + }); + + describe('#getDevice', function() { + it('Should return correct data', function() { + nockPath('/devices/1', deviceResponse); + return client.getDevice(1).should.eventually.deep.equal(deviceResponse.message.device); + }); + + it('Should give an error if the API gives an error', function() { + nockError('/devices/1'); + return client.getDevice(1).should.be.rejectedWith(errorResponse.message); + }); + }); + + describe('#getAlerts', function() { + it('Should return correct data', function() { + nockPath('/alerts', alertsResponse); + return client.getAlerts().should.eventually.deep.equal(alertsResponse.message.alerts); + }); + + it('Should give an error if the API gives an error', function() { + nockError('/alerts'); + return client.getAlerts().should.be.rejectedWith(errorResponse.message); + }); + }); + + describe('#getDeviceAlerts', function() { + it('Should return correct data', function() { + nockPath('/devices/1/alerts', alertsResponse); + return client.getDeviceAlerts(1).should.eventually.deep.equal(alertsResponse.message.alerts); + }); + + it('Should give an error if the API gives an error', function() { + nockError('/devices/1/alerts'); + return client.getDeviceAlerts(1).should.be.rejectedWith(errorResponse.message); + }); + }); + + describe('#getAlert', function() { + it('Should return correct data', function() { + nockPath('/alerts/1', alertResponse); + return client.getAlert(1).should.eventually.deep.equal(alertResponse.message.alert); + }); + + it('Should give an error if the API gives an error', function() { + nockError('/alerts/1'); + return client.getAlert(1).should.be.rejectedWith(errorResponse.message); + }); + }); + + describe('#getLocations', function() { + it('Should return correct data', function() { + nockPath('/locations', locationsResponse); + return client.getLocations().should.eventually.deep.equal(locationsResponse.message.locations); + }); + + it('Should give an error if the API gives an error', function() { + nockError('/locations'); + return client.getLocations().should.be.rejectedWith(errorResponse.message); + }); + }); + + describe('#getLocation', function() { + it('Should return correct data', function() { + nockPath('/locations/1', locationResponse); + return client.getLocation(1).should.eventually.deep.equal(locationResponse.message.location); + }); + + it('Should give an error if the API gives an error', function() { + nockError('/locations/1'); + return client.getLocation(1).should.be.rejectedWith(errorResponse.message); + }); + }); + + describe('#getResource', function() { + it('Should return correct data', function() { + nockPath('/resources/1', resourceResponse); + return client.getResource(1).should.eventually.deep.equal(resourceResponse.message.resource); + }); + + it('Should give an error if the API gives an error', function() { + nockError('/resources/1'); + return client.getResource(1).should.be.rejectedWith(errorResponse.message); }); }); }); diff --git a/tests/replies/alert.json b/tests/replies/alert.json new file mode 100644 index 0000000..f069ae9 --- /dev/null +++ b/tests/replies/alert.json @@ -0,0 +1,45 @@ +{ + "result": "success", + "message": { + "alert": { + "id": "1", + "agency": { + "id": "1", + "uri": "https://access.active911.com/interface/open_api/api/" + }, + "place": "My Place", + "address": "123 Main Street", + "unit": "", + "city": "Allentown", + "state": "PA", + "latitude": 40, + "longitude": -70, + "source": "Source", + "units": "My Units", + "cad_code": "29A1", + "priority": "Priority 3", + "details": "Medical emergency", + "sent": "timestamp", + "description": "My description", + "pagegroups": [ + { + "title": "Group 1", + "prefix": "My Prefix" + } + ], + "map_code": "None", + "received": "Received", + "cross_street": "Cross Street", + "responses": [ + { + "device": { + "id": "1", + "uri": "https://access.active911.com/interface/open_api/api/devices/1" + }, + "timestamp": "123", + "response": "Responding" + } + ] + } + } +} diff --git a/tests/replies/alerts.json b/tests/replies/alerts.json new file mode 100644 index 0000000..0fa10e5 --- /dev/null +++ b/tests/replies/alerts.json @@ -0,0 +1,15 @@ +{ + "result": "success", + "message": { + "alerts": [ + { + "id": 1, + "uri": "https://access.active911.com/interface/open_api/api/alerts/1" + }, + { + "id": 2, + "uri": "https://access.active911.com/interface/open_api/api/alerts/2" + } + ] + } +} diff --git a/tests/replies/location.json b/tests/replies/location.json new file mode 100644 index 0000000..114307e --- /dev/null +++ b/tests/replies/location.json @@ -0,0 +1,22 @@ +{ + "result": "success", + "message": { + "location": { + "id": "1", + "name": "My Resource", + "description": "My Description", + "icon_id": "1", + "icon_color": "blue", + "latitude": 40, + "longitude": -70, + "location_type": "Hydrant", + "resources": [ + { + "id": "1", + "uri": "https://access.active911.com/interface/open_api/api/resources/1" + } + ] + } + } +} + diff --git a/tests/replies/locations.json b/tests/replies/locations.json new file mode 100644 index 0000000..ec12c28 --- /dev/null +++ b/tests/replies/locations.json @@ -0,0 +1,11 @@ +{ + "result": "success", + "message": { + "locations": [ + { + "id": "1", + "uri": "https://access.active911.com/interface/open_api/api/locations/1" + } + ] + } +} diff --git a/tests/replies/resource.json b/tests/replies/resource.json new file mode 100644 index 0000000..19762db --- /dev/null +++ b/tests/replies/resource.json @@ -0,0 +1,22 @@ +{ + "result": "success", + "message": { + "resource": { + "id": "1", + "title": "My Resource", + "filename": "resource.pdf", + "extension": "pdf", + "size": 0, + "details": "My resource details", + "agency": { + "id": "1", + "uri": "https://access.active911.com/interface/open_api/api/" + }, + "location": { + "id": "1", + "uri": "https://access.active911.com/interface/open_api/api/locations/1" + } + } + } +} + -- cgit v1.2.3