forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifications-api.test.js
86 lines (70 loc) · 2.16 KB
/
notifications-api.test.js
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
'use strict';
var request = require('supertest');
var should = require('should');
var Stream = require('stream');
var levels = require('../lib/levels');
var notificationsAPI = require('../lib/api/notifications-api');
function examplePlugin () {}
describe('Notifications API', function ( ) {
it('ack notifications', function (done) {
var known = 'b723e97aa97846eb92d5264f084b2823f57c4aa1';
delete process.env.API_SECRET;
process.env.API_SECRET = 'this is my long pass phrase';
var env = require('../lib/server/env')( );
env.enclave.isApiKey(known).should.equal(true);
env.testMode = true;
var ctx = {
bus: new Stream
, ddata: {
lastUpdated: Date.now()
}
, store: {
collection: function ( ) {
return { };
}
}
, levels: levels
};
ctx.authorization = require('../lib/authorization')(env, ctx);
var notifications = require('../lib/notifications')(env, ctx);
ctx.notifications = notifications;
//start fresh to we don't pick up other notifications
ctx.bus = new Stream;
//if notification doesn't get called test will time out
ctx.bus.on('notification', function callback (notify) {
if (notify.clear) {
done();
}
});
var exampleWarn = {
title: 'test'
, message: 'testing'
, level: levels.WARN
, plugin: examplePlugin
};
notifications.resetStateForTests();
notifications.initRequests();
notifications.requestNotify(exampleWarn);
notifications.findHighestAlarm().should.equal(exampleWarn);
notifications.process();
var app = require('express')();
app.enable('api');
var wares = require('../lib/middleware/')(env);
app.use('/', notificationsAPI(app, wares, ctx));
function makeRequest () {
request(app)
.get('/notifications/ack?level=1')
.set('api-secret', known || '')
.expect(200)
.end(function (err) {
should.not.exist(err);
if (err) {
console.error(err);
}
});
}
makeRequest();
//2nd call should have no effect, done should NOT be called again
makeRequest();
});
});