diff --git a/.gitignore b/.gitignore index 146ec77..e36764e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,14 @@ logs *.log npm-debug.log* +# Development +.idea/ + +# Test data +test/database_test/ + # Runtime data +lib/database/ pids *.pid *.seed @@ -30,4 +37,4 @@ node_modules .npm # Optional REPL history -.node_repl_history \ No newline at end of file +.node_repl_history diff --git a/lib/coap-shepherd.js b/lib/coap-shepherd.js index eebaf98..7a9586c 100644 --- a/lib/coap-shepherd.js +++ b/lib/coap-shepherd.js @@ -233,6 +233,22 @@ CoapShepherd.prototype.permitJoin = function (time) { return true; }; +CoapShepherd.prototype.alwaysPermitJoin = function (permit) { + proving.boolean(permit, 'permit should be a boolean.'); + + if (!this._enabled) + return false; + + this._joinable = permit; + + if (this._permitJoinTimer) { + clearInterval(this._permitJoinTimer); + this._permitJoinTimer = null; + } + + return true; +}; + CoapShepherd.prototype.list = function () { var devList = []; diff --git a/test/coap-shepherd.test.js b/test/coap-shepherd.test.js index 6752320..180aa96 100644 --- a/test/coap-shepherd.test.js +++ b/test/coap-shepherd.test.js @@ -115,6 +115,23 @@ describe('coap-shepherd', function () { }); }); + describe('#.alwaysPermitJoin()', function () { + it('should throw TypeError if permit is not a boolean', function () { + expect(function () { return shepherd.alwaysPermitJoin(); }).to.throw(TypeError); + expect(function () { return shepherd.alwaysPermitJoin(undefined); }).to.throw(TypeError); + expect(function () { return shepherd.alwaysPermitJoin(null); }).to.throw(TypeError); + expect(function () { return shepherd.alwaysPermitJoin('xx'); }).to.throw(TypeError); + expect(function () { return shepherd.alwaysPermitJoin(NaN); }).to.throw(TypeError); + expect(function () { return shepherd.alwaysPermitJoin(10); }).to.throw(TypeError); + expect(function () { return shepherd.alwaysPermitJoin([]); }).to.throw(TypeError); + expect(function () { return shepherd.alwaysPermitJoin({}); }).to.throw(TypeError); + expect(function () { return shepherd.alwaysPermitJoin(new Date()); }).to.throw(TypeError); + expect(function () { return shepherd.alwaysPermitJoin(function () {}); }).to.throw(TypeError); + + expect(function () { return shepherd.alwaysPermitJoin(true); }).not.to.throw(TypeError); + }); + }); + describe('#.request()', function () { it('should throw TypeError if reqObj is not an object', function () { expect(function () { return shepherd.request(); }).to.throw(TypeError); @@ -213,9 +230,68 @@ describe('coap-shepherd', function () { }); describe('#.permitJoin()', function () { - it('should open permitJoin', function () { + it('should open permitJoin when time > 0', function () { + shepherd.permitJoin(180); + expect(shepherd._joinable).to.be.eql(true); + }); + + it('should close permitJoin when time == 0', function () { + shepherd.permitJoin(0); + expect(shepherd._joinable).to.be.eql(false); + }); + + it('should open permitJoin when time > 0 after alwaysPermitJoin(false)', function () { + shepherd.alwaysPermitJoin(false); + shepherd.permitJoin(180); + expect(shepherd._joinable).to.be.eql(true); + }); + + it('should close permitJoin when time == 0 after alwaysPermitJoin(true)', function () { + shepherd.alwaysPermitJoin(true); + shepherd.permitJoin(0); + expect(shepherd._joinable).to.be.eql(false); + }); + }); + + describe('#.alwaysPermitJoin()', function () { + it('should open permitJoin when permit is true', function () { + var result = shepherd.alwaysPermitJoin(true); + expect(result).to.be.eql(true); + expect(shepherd._joinable).to.be.eql(true); + }); + + it('should close permitJoin when permit is false', function () { + shepherd.alwaysPermitJoin(false); + expect(shepherd._joinable).to.be.eql(false); + }); + + it('should clear _permitJoinTimer when permit is true', function () { shepherd.permitJoin(180); + var result = shepherd.alwaysPermitJoin(true); + expect(result).to.be.eql(true); expect(shepherd._joinable).to.be.eql(true); + expect(shepherd._permitJoinTimer).to.be.eql(null); + }); + + it('should clear _permitJoinTimer when permit is false', function () { + shepherd.permitJoin(180); + var result = shepherd.alwaysPermitJoin(false); + expect(result).to.be.eql(true); + expect(shepherd._joinable).to.be.eql(false); + expect(shepherd._permitJoinTimer).to.be.eql(null); + }); + + it('should not open permitJoin when server is not enabled', function () { + shepherd._joinable = false; + shepherd._enabled = false; + var result = shepherd.alwaysPermitJoin(true); + expect(result).to.be.eql(false); + expect(shepherd._joinable).to.be.eql(false); + }); + + after(function () { + shepherd._enabled = true; + shepherd.alwaysPermitJoin(true); }); });