Skip to content

Commit 6c19a29

Browse files
committed
Testing notify-one and notify-one-challenge
1 parent cae59f5 commit 6c19a29

File tree

4 files changed

+226
-17
lines changed

4 files changed

+226
-17
lines changed

test/services/error-result.js

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
(function () {
2+
"use strict";
3+
4+
var getRandomPassword = require('../../services/get-random-password.js');
5+
6+
describe('services/get-random-password.js', function () {
7+
it('should return 8 digit password', function () {
8+
getRandomPassword(8).should.have.lengthOf(8);
9+
});
10+
11+
it('should return 16 digit password', function () {
12+
getRandomPassword(16).should.have.lengthOf(16);
13+
});
14+
});
15+
}());
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
(function () {
2+
"use strict";
3+
4+
var crypto = require('crypto'),
5+
notifyOneChallenge = require('../../services/notify-one-challenge.js'),
6+
request = require('request'),
7+
sinon = require('sinon'),
8+
clock;
9+
10+
describe('services/notify-one-challenge.js correct api', function () {
11+
before(function(done) {
12+
sinon
13+
.stub(crypto, 'randomBytes')
14+
.returns('CHALLENGE');
15+
sinon
16+
.stub(request, 'get')
17+
.yields(null, {statusCode: 200}, 'CHALLENGE');
18+
clock = sinon.useFakeTimers();
19+
clock.tick(300000);
20+
done();
21+
});
22+
23+
after(function(done) {
24+
crypto.randomBytes.restore();
25+
request.get.restore();
26+
clock.restore();
27+
done();
28+
});
29+
30+
it('should notify api correctly', function (done) {
31+
var data = {
32+
subscriptions: {},
33+
prefs: {
34+
ctSecsResourceExpire: 25 * 60 * 60
35+
}
36+
};
37+
notifyOneChallenge(data, 'http://www.google.com/', 'http://192.168.0.1/', function (err) {
38+
if (err) { return done(err); }
39+
data.subscriptions.should.have.property('http://www.google.com/');
40+
data.subscriptions['http://www.google.com/'].should.have.property('http://192.168.0.1/');
41+
data.subscriptions['http://www.google.com/']['http://192.168.0.1/']
42+
.should.have.property('ctUpdates', 1);
43+
data.subscriptions['http://www.google.com/']['http://192.168.0.1/']
44+
.should.have.property('ctConsecutiveErrors', 0);
45+
data.subscriptions['http://www.google.com/']['http://192.168.0.1/'].whenLastUpdate.unix()
46+
.should.equal(300);
47+
done();
48+
});
49+
});
50+
});
51+
52+
describe('services/notify-one-challenge.js invalid api', function () {
53+
before(function(done) {
54+
sinon
55+
.stub(request, 'get')
56+
.yields(null, {statusCode: 404});
57+
clock = sinon.useFakeTimers();
58+
clock.tick(300000);
59+
done();
60+
});
61+
62+
after(function(done) {
63+
request.get.restore();
64+
clock.restore();
65+
done();
66+
});
67+
68+
it('should fail to notify api correctly', function (done) {
69+
var data = {
70+
subscriptions: {},
71+
prefs: {
72+
ctSecsResourceExpire: 25 * 60 * 60
73+
}
74+
};
75+
notifyOneChallenge(data, 'http://www.google.com/', 'http://192.168.0.1/', function (err) {
76+
err.should.not.be.empty;
77+
data.subscriptions.should.have.property('http://www.google.com/');
78+
data.subscriptions['http://www.google.com/'].should.have.property('http://192.168.0.1/');
79+
data.subscriptions['http://www.google.com/']['http://192.168.0.1/']
80+
.should.have.property('ctErrors', 1);
81+
data.subscriptions['http://www.google.com/']['http://192.168.0.1/']
82+
.should.have.property('ctConsecutiveErrors', 1);
83+
data.subscriptions['http://www.google.com/']['http://192.168.0.1/'].whenLastError.unix()
84+
.should.equal(300);
85+
done();
86+
});
87+
});
88+
});
89+
90+
describe('services/notify-one-challenge.js invalid challenge', function () {
91+
before(function(done) {
92+
sinon
93+
.stub(request, 'get')
94+
.yields(null, {statusCode: 200});
95+
clock = sinon.useFakeTimers();
96+
clock.tick(300000);
97+
done();
98+
});
99+
100+
after(function(done) {
101+
request.get.restore();
102+
clock.restore();
103+
done();
104+
});
105+
106+
it('should fail to notify api correctly', function (done) {
107+
var data = {
108+
subscriptions: {},
109+
prefs: {
110+
ctSecsResourceExpire: 25 * 60 * 60
111+
}
112+
};
113+
notifyOneChallenge(data, 'http://www.google.com/', 'http://192.168.0.1/', function (err) {
114+
err.should.not.be.empty;
115+
data.subscriptions.should.have.property('http://www.google.com/');
116+
data.subscriptions['http://www.google.com/'].should.have.property('http://192.168.0.1/');
117+
data.subscriptions['http://www.google.com/']['http://192.168.0.1/']
118+
.should.have.property('ctErrors', 1);
119+
data.subscriptions['http://www.google.com/']['http://192.168.0.1/']
120+
.should.have.property('ctConsecutiveErrors', 1);
121+
data.subscriptions['http://www.google.com/']['http://192.168.0.1/'].whenLastError.unix()
122+
.should.equal(300);
123+
done();
124+
});
125+
});
126+
});
127+
}());

test/services/notify-one.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
(function () {
2+
"use strict";
3+
4+
var notifyOne = require('../../services/notify-one.js'),
5+
request = require('request'),
6+
sinon = require('sinon'),
7+
clock;
8+
9+
describe('services/notify-one.js correct api', function () {
10+
before(function(done) {
11+
sinon
12+
.stub(request, 'post')
13+
.yields(null, {statusCode: 200});
14+
clock = sinon.useFakeTimers();
15+
clock.tick(300000);
16+
done();
17+
});
18+
19+
after(function(done) {
20+
request.post.restore();
21+
clock.restore();
22+
done();
23+
});
24+
25+
it('should notify api correctly', function (done) {
26+
var data = {
27+
subscriptions: {},
28+
prefs: {
29+
ctSecsResourceExpire: 25 * 60 * 60
30+
}
31+
};
32+
notifyOne(data, 'http://www.google.com/', 'http://192.168.0.1/', false, function (err) {
33+
if (err) { return done(err); }
34+
data.subscriptions.should.have.property('http://www.google.com/');
35+
data.subscriptions['http://www.google.com/'].should.have.property('http://192.168.0.1/');
36+
data.subscriptions['http://www.google.com/']['http://192.168.0.1/']
37+
.should.have.property('ctUpdates', 1);
38+
data.subscriptions['http://www.google.com/']['http://192.168.0.1/']
39+
.should.have.property('ctConsecutiveErrors', 0);
40+
data.subscriptions['http://www.google.com/']['http://192.168.0.1/'].whenLastUpdate.unix()
41+
.should.equal(300);
42+
done();
43+
});
44+
});
45+
});
46+
47+
describe('services/notify-one.js invalid api', function () {
48+
before(function(done) {
49+
sinon
50+
.stub(request, 'post')
51+
.yields(null, {statusCode: 404});
52+
clock = sinon.useFakeTimers();
53+
clock.tick(300000);
54+
done();
55+
});
56+
57+
after(function(done) {
58+
request.post.restore();
59+
clock.restore();
60+
done();
61+
});
62+
63+
it('should fail to notify api correctly', function (done) {
64+
var data = {
65+
subscriptions: {},
66+
prefs: {
67+
ctSecsResourceExpire: 25 * 60 * 60
68+
}
69+
};
70+
notifyOne(data, 'http://www.google.com/', 'http://192.168.0.1/', false, function (err) {
71+
err.should.not.be.empty;
72+
data.subscriptions.should.have.property('http://www.google.com/');
73+
data.subscriptions['http://www.google.com/'].should.have.property('http://192.168.0.1/');
74+
data.subscriptions['http://www.google.com/']['http://192.168.0.1/']
75+
.should.have.property('ctErrors', 1);
76+
data.subscriptions['http://www.google.com/']['http://192.168.0.1/']
77+
.should.have.property('ctConsecutiveErrors', 1);
78+
data.subscriptions['http://www.google.com/']['http://192.168.0.1/'].whenLastError.unix()
79+
.should.equal(300);
80+
done();
81+
});
82+
});
83+
});
84+
}());

0 commit comments

Comments
 (0)