Skip to content

Commit

Permalink
Fixed error message when empty string passed as expiresIn or notBefor…
Browse files Browse the repository at this point in the history
…e option (#531)

* Fixed error message when empty string passed as expiresIn or notBefore option

* Moved tests to option validation block
  • Loading branch information
andrewnester authored and ziluvatar committed Oct 22, 2018
1 parent cfd1079 commit 7f9604a
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 22 deletions.
4 changes: 2 additions & 2 deletions sign.js
Expand Up @@ -9,8 +9,8 @@ var isString = require('lodash.isstring');
var once = require('lodash.once');

var sign_options_schema = {
expiresIn: { isValid: function(value) { return isInteger(value) || isString(value); }, message: '"expiresIn" should be a number of seconds or string representing a timespan' },
notBefore: { isValid: function(value) { return isInteger(value) || isString(value); }, message: '"notBefore" should be a number of seconds or string representing a timespan' },
expiresIn: { isValid: function(value) { return isInteger(value) || (isString(value) && value); }, message: '"expiresIn" should be a number of seconds or string representing a timespan' },
notBefore: { isValid: function(value) { return isInteger(value) || (isString(value) && value); }, message: '"notBefore" should be a number of seconds or string representing a timespan' },
audience: { isValid: function(value) { return isString(value) || Array.isArray(value); }, message: '"audience" must be a string or array' },
algorithm: { isValid: includes.bind(null, ['RS256', 'RS384', 'RS512', 'ES256', 'ES384', 'ES512', 'HS256', 'HS384', 'HS512', 'none']), message: '"algorithm" must be a valid string enum value' },
header: { isValid: isPlainObject, message: '"header" must be an object' },
Expand Down
11 changes: 1 addition & 10 deletions test/claim-exp.test.js
Expand Up @@ -29,6 +29,7 @@ describe('expires', function() {
Infinity,
NaN,
' ',
'',
'invalid',
[],
['foo'],
Expand All @@ -46,16 +47,6 @@ describe('expires', function() {
});
});

// TODO this should throw the same error as other invalid inputs
it(`should error with with value ''`, function (done) {
signWithExpiresIn('', {}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(Error);
expect(err).to.have.property('message', 'val is not a non-empty string or a valid number. val=""');
});
});
});

// undefined needs special treatment because {} is not the same as {expiresIn: undefined}
it('should error with with value undefined', function (done) {
testUtils.signJWTHelper({}, undefined, {expiresIn: undefined, algorithm: 'none'}, (err) => {
Expand Down
11 changes: 1 addition & 10 deletions test/claim-nbf.test.js
Expand Up @@ -28,6 +28,7 @@ describe('not before', function() {
-Infinity,
Infinity,
NaN,
'',
' ',
'invalid',
[],
Expand All @@ -46,16 +47,6 @@ describe('not before', function() {
});
});

// TODO this should throw the same error as other invalid inputs
it(`should error with with value ''`, function (done) {
signWithNotBefore('', {}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(Error);
expect(err).to.have.property('message', 'val is not a non-empty string or a valid number. val=""');
});
});
});

// undefined needs special treatment because {} is not the same as {notBefore: undefined}
it('should error with with value undefined', function (done) {
testUtils.signJWTHelper({}, undefined, {notBefore: undefined, algorithm: 'none'}, (err) => {
Expand Down

0 comments on commit 7f9604a

Please sign in to comment.