From 81a25528ce7af2dcb02a9ddfd4d50fcd8f2306b9 Mon Sep 17 00:00:00 2001 From: James Richford Date: Sun, 4 Feb 2018 19:11:05 +0000 Subject: [PATCH 1/4] add unit tests for queue name validation --- test/validation.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 test/validation.js diff --git a/test/validation.js b/test/validation.js new file mode 100644 index 0000000..a2fba69 --- /dev/null +++ b/test/validation.js @@ -0,0 +1,49 @@ +const QueueName = require('../lib/validation/queue/QueueName'), + AError = require('../lib/core/AzuriteError') + ErrorCodes = require('../lib/core/ErrorCodes') + expect = require('chai').expect; + +describe('validation', () => { + describe('QueueName', () => { + const createQueueNameRequest = (queueName) => { return { request: { queueName } } }; + + it('should throw out of range if name is less than three characters', () => { + expect(() => QueueName.validate(createQueueNameRequest(''))).to.throw(AError, ErrorCodes.OutOfRangeInput); + expect(() => QueueName.validate(createQueueNameRequest('a'))).to.throw(AError, ErrorCodes.OutOfRangeInput); + expect(() => QueueName.validate(createQueueNameRequest('aa'))).to.throw(AError, ErrorCodes.OutOfRangeInput); + expect(() => QueueName.validate(createQueueNameRequest('aaa'))).not.to.throw(); + }); + + it('should throw out of range if name is greater than sixty three characters', () => { + const sixtyThreeCharacterStringName = '012345678901234567890123456789012345678901234567890123456789012'; + + expect(() => QueueName.validate(createQueueNameRequest(sixtyThreeCharacterStringName))).not.to.throw(); + expect(() => QueueName.validate(createQueueNameRequest(sixtyThreeCharacterStringName + '3'))).to.throw(AError, ErrorCodes.OutOfRangeInput); + expect(() => QueueName.validate(createQueueNameRequest(sixtyThreeCharacterStringName + '34'))).to.throw(AError, ErrorCodes.OutOfRangeInput); + }); + + it('should throw invalid input if name starts with a dash', () => { + expect(() => QueueName.validate(createQueueNameRequest("-queue"))).to.throw(AError, ErrorCodes.InvalidInput); + expect(() => QueueName.validate(createQueueNameRequest("-queue-name"))).to.throw(AError, ErrorCodes.InvalidInput); + }); + + it('should throw invalid input if name ends with a dash', () => { + expect(() => QueueName.validate(createQueueNameRequest("queue-"))).to.throw(AError, ErrorCodes.InvalidInput); + expect(() => QueueName.validate(createQueueNameRequest("queue-name-"))).to.throw(AError, ErrorCodes.InvalidInput); + }); + + it('should throw invalid input if contians two consecutive dashes', () => { + expect(() => QueueName.validate(createQueueNameRequest("queue--name"))).to.throw(AError, ErrorCodes.InvalidInput); + }); + + it('should throw invalid input if contians anything except alphanumeric characters and dashes', () => { + expect(() => QueueName.validate(createQueueNameRequest("queue-name"))).not.to.throw(); + expect(() => QueueName.validate(createQueueNameRequest("queue1"))).not.to.throw(); + expect(() => QueueName.validate(createQueueNameRequest("queue_name"))).to.throw(AError, ErrorCodes.InvalidInput); + expect(() => QueueName.validate(createQueueNameRequest("queue name"))).to.throw(AError, ErrorCodes.InvalidInput); + expect(() => QueueName.validate(createQueueNameRequest("queue~name"))).to.throw(AError, ErrorCodes.InvalidInput); + expect(() => QueueName.validate(createQueueNameRequest("queue@name"))).to.throw(AError, ErrorCodes.InvalidInput); + expect(() => QueueName.validate(createQueueNameRequest("queue:name"))).to.throw(AError, ErrorCodes.InvalidInput); + }); + }); +}); From f689ea37c0b5b1a1ac1b75d5b07646aa7dd0e416 Mon Sep 17 00:00:00 2001 From: James Richford Date: Sun, 4 Feb 2018 19:11:37 +0000 Subject: [PATCH 2/4] add failing unit test --- test/validation.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/validation.js b/test/validation.js index a2fba69..e5cca21 100644 --- a/test/validation.js +++ b/test/validation.js @@ -39,6 +39,7 @@ describe('validation', () => { it('should throw invalid input if contians anything except alphanumeric characters and dashes', () => { expect(() => QueueName.validate(createQueueNameRequest("queue-name"))).not.to.throw(); expect(() => QueueName.validate(createQueueNameRequest("queue1"))).not.to.throw(); + expect(() => QueueName.validate(createQueueNameRequest("QUEUE-name-1"))).not.to.throw(); expect(() => QueueName.validate(createQueueNameRequest("queue_name"))).to.throw(AError, ErrorCodes.InvalidInput); expect(() => QueueName.validate(createQueueNameRequest("queue name"))).to.throw(AError, ErrorCodes.InvalidInput); expect(() => QueueName.validate(createQueueNameRequest("queue~name"))).to.throw(AError, ErrorCodes.InvalidInput); From 5a98e9af14510b25af62fcb4899ad0538855c0fa Mon Sep 17 00:00:00 2001 From: James Richford Date: Sun, 4 Feb 2018 19:12:18 +0000 Subject: [PATCH 3/4] allow multiple dashes in a queue name --- lib/validation/queue/QueueName.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/validation/queue/QueueName.js b/lib/validation/queue/QueueName.js index 57da0f5..e223035 100644 --- a/lib/validation/queue/QueueName.js +++ b/lib/validation/queue/QueueName.js @@ -23,7 +23,7 @@ class QueueName { // Matches non-alphanumeric characters const re = new RegExp(/\W|_/); - if (re.test(name.replace('-', ''))) { // Fixme: exclude '-' in regex + if (re.test(name.replace(/-/g, ''))) { // Fixme: exclude '-' in regex throw new AError(ErrorCodes.InvalidInput); } } From 817e989b7c306e5e51c0da117df0d4e4c330e0aa Mon Sep 17 00:00:00 2001 From: James Richford Date: Sun, 4 Feb 2018 19:13:31 +0000 Subject: [PATCH 4/4] refactor queue name validation --- lib/validation/queue/QueueName.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/validation/queue/QueueName.js b/lib/validation/queue/QueueName.js index e223035..c347502 100644 --- a/lib/validation/queue/QueueName.js +++ b/lib/validation/queue/QueueName.js @@ -16,14 +16,8 @@ class QueueName { if (name.length < 3 || name.length > 63) { throw new AError(ErrorCodes.OutOfRangeInput); } - - if (name.includes('--') || name[0] === '-' || name[name.length - 1] === '-') { - throw new AError(ErrorCodes.InvalidInput); - } - - // Matches non-alphanumeric characters - const re = new RegExp(/\W|_/); - if (re.test(name.replace(/-/g, ''))) { // Fixme: exclude '-' in regex + + if (/^([a-z0-9]+)(-[a-z0-9]+)*$/i.test(name) === false) { throw new AError(ErrorCodes.InvalidInput); } }