aboutsummaryrefslogtreecommitdiff
path: root/tests/active911.js
blob: de91f1c4a9c94b5d10d7cec791c70cad35c0d8a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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 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) {
  return 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) {
    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 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() {
      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);
    });
  });
});

describe('Alert filtering', function() {
  it('should request alerts filtered by alert_days', function(done) {
    var n = nockPath('/alerts?alert_days=1', alertsResponse);
    client.getAlerts({ alert_days: 1}).then(function() {
      n.isDone().should.be.true;
      done();
    }).catch(function(err) {
      false.should.be.true;
      done();
    });
  });
  it('should request alerts filtered by alert_minutes', function(done) {
    var n = nockPath('/alerts?alert_minutes=1', alertsResponse);
    client.getAlerts({ alert_minutes: 1}).then(function() {
      n.isDone().should.be.true;
      done();
    }).catch(function(err) {
      false.should.be.true;
      done();
    });
  });
});