|
| 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 | +}()); |
0 commit comments