Skip to content
This repository has been archived by the owner on Mar 22, 2018. It is now read-only.

Commit

Permalink
Merge branch 'jamesrichford-issue/150'
Browse files Browse the repository at this point in the history
  • Loading branch information
arafato committed Feb 5, 2018
2 parents d1ab2be + 817e989 commit fa71d5a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
10 changes: 2 additions & 8 deletions lib/validation/queue/QueueName.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('-', ''))) { // Fixme: exclude '-' in regex

if (/^([a-z0-9]+)(-[a-z0-9]+)*$/i.test(name) === false) {
throw new AError(ErrorCodes.InvalidInput);
}
}
Expand Down
50 changes: 50 additions & 0 deletions test/validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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-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);
expect(() => QueueName.validate(createQueueNameRequest("queue@name"))).to.throw(AError, ErrorCodes.InvalidInput);
expect(() => QueueName.validate(createQueueNameRequest("queue:name"))).to.throw(AError, ErrorCodes.InvalidInput);
});
});
});

0 comments on commit fa71d5a

Please sign in to comment.