Skip to content

Commit

Permalink
Minor test refactoring for recently added tests (#504)
Browse files Browse the repository at this point in the history
* Prefix claim- to claim related test files

* Fix typo of "signWithNoBfore" in notBefore tests
  • Loading branch information
MitMaro authored and ziluvatar committed Jul 20, 2018
1 parent 53d405e commit e2860a9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
File renamed without changes.
File renamed without changes.
44 changes: 22 additions & 22 deletions test/nbf.test.js → test/claim-nbf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const testUtils = require('./test-utils');
const base64UrlEncode = testUtils.base64UrlEncode;
const noneAlgorithmHeader = 'eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0';

function signWithNoBefore(payload, notBefore) {
function signWithNotBefore(payload, notBefore) {
const options = {algorithm: 'none'};
if (notBefore !== undefined) {
options.notBefore = notBefore;
Expand All @@ -36,15 +36,15 @@ describe('not before', function() {
{foo: 'bar'},
].forEach((notBefore) => {
it(`should error with with value ${util.inspect(notBefore)}`, function () {
expect(() => signWithNoBefore({}, notBefore)).to.throw(
expect(() => signWithNotBefore({}, notBefore)).to.throw(
'"notBefore" should be a number of seconds or string representing a timespan'
);
});
});

// TODO this should throw the same error as other invalid inputs
it(`should error with with value ''`, function () {
expect(() => signWithNoBefore({}, '')).to.throw(
expect(() => signWithNotBefore({}, '')).to.throw(
'val is not a non-empty string or a valid number. val=""'
);
});
Expand All @@ -57,19 +57,19 @@ describe('not before', function() {
});

it('should error when "nbf" is in payload', function () {
expect(() => signWithNoBefore({nbf: 100}, 100)).to.throw(
expect(() => signWithNotBefore({nbf: 100}, 100)).to.throw(
'Bad "options.notBefore" option the payload already has an "nbf" property.'
);
});

it('should error with a string payload', function () {
expect(() => signWithNoBefore('a string payload', 100)).to.throw(
expect(() => signWithNotBefore('a string payload', 100)).to.throw(
'invalid notBefore option for string payload'
);
});

it('should error with a Buffer payload', function () {
expect(() => signWithNoBefore(new Buffer('a Buffer payload'), 100)).to.throw(
expect(() => signWithNotBefore(new Buffer('a Buffer payload'), 100)).to.throw(
'invalid notBefore option for object payload'
);
});
Expand All @@ -90,7 +90,7 @@ describe('not before', function() {
{foo: 'bar'},
].forEach((nbf) => {
it(`should error with with value ${util.inspect(nbf)}`, function () {
expect(() => signWithNoBefore({nbf})).to.throw(
expect(() => signWithNotBefore({nbf})).to.throw(
'"nbf" should be a number of seconds'
);
});
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('not before', function() {
});

it('should set correct "nbf" with negative number of seconds', function () {
const token = signWithNoBefore({}, -10);
const token = signWithNotBefore({}, -10);
const decoded = jwt.decode(token);

const verified = jwt.verify(token, undefined);
Expand All @@ -144,7 +144,7 @@ describe('not before', function() {
});

it('should set correct "nbf" with positive number of seconds', function () {
const token = signWithNoBefore({}, 10);
const token = signWithNotBefore({}, 10);

fakeClock.tick(10000);
const decoded = jwt.decode(token);
Expand All @@ -155,7 +155,7 @@ describe('not before', function() {
});

it('should set correct "nbf" with zero seconds', function () {
const token = signWithNoBefore({}, 0);
const token = signWithNotBefore({}, 0);

const decoded = jwt.decode(token);

Expand All @@ -165,7 +165,7 @@ describe('not before', function() {
});

it('should set correct "nbf" with negative string timespan', function () {
const token = signWithNoBefore({}, '-10 s');
const token = signWithNotBefore({}, '-10 s');

const decoded = jwt.decode(token);

Expand All @@ -176,7 +176,7 @@ describe('not before', function() {


it('should set correct "nbf" with positive string timespan', function () {
const token = signWithNoBefore({}, '10 s');
const token = signWithNotBefore({}, '10 s');

fakeClock.tick(10000);
const decoded = jwt.decode(token);
Expand All @@ -187,7 +187,7 @@ describe('not before', function() {
});

it('should set correct "nbf" with zero string timespan', function () {
const token = signWithNoBefore({}, '0 s');
const token = signWithNotBefore({}, '0 s');

const decoded = jwt.decode(token);

Expand All @@ -198,30 +198,30 @@ describe('not before', function() {

// TODO an nbf of -Infinity should fail validation
it('should set null "nbf" when given -Infinity', function () {
const token = signWithNoBefore({nbf: -Infinity});
const token = signWithNotBefore({nbf: -Infinity});

const decoded = jwt.decode(token);
expect(decoded.nbf).to.be.null;
});

// TODO an nbf of Infinity should fail validation
it('should set null "nbf" when given value Infinity', function () {
const token = signWithNoBefore({nbf: Infinity});
const token = signWithNotBefore({nbf: Infinity});

const decoded = jwt.decode(token);
expect(decoded.nbf).to.be.null;
});

// TODO an nbf of NaN should fail validation
it('should set null "nbf" when given value NaN', function () {
const token = signWithNoBefore({nbf: NaN});
const token = signWithNotBefore({nbf: NaN});

const decoded = jwt.decode(token);
expect(decoded.nbf).to.be.null;
});

it('should set correct "nbf" when "iat" is passed', function () {
const token = signWithNoBefore({iat: 40}, -10);
const token = signWithNotBefore({iat: 40}, -10);

const decoded = jwt.decode(token);

Expand All @@ -231,31 +231,31 @@ describe('not before', function() {
});

it('should verify "nbf" using "clockTimestamp"', function () {
const token = signWithNoBefore({}, 10);
const token = signWithNotBefore({}, 10);

const verified = jwt.verify(token, undefined, {clockTimestamp: 70});
expect(verified.iat).to.equal(60);
expect(verified.nbf).to.equal(70);
});

it('should verify "nbf" using "clockTolerance"', function () {
const token = signWithNoBefore({}, 5);
const token = signWithNotBefore({}, 5);

const verified = jwt.verify(token, undefined, {clockTolerance: 6});
expect(verified.iat).to.equal(60);
expect(verified.nbf).to.equal(65);
});

it('should ignore a not active token when "ignoreNotBefore" is true', function () {
const token = signWithNoBefore({}, '10 s');
const token = signWithNotBefore({}, '10 s');

const verified = jwt.verify(token, undefined, {ignoreNotBefore: true});
expect(verified.iat).to.equal(60);
expect(verified.nbf).to.equal(70);
});

it('should error on verify if "nbf" is after current time', function () {
const token = signWithNoBefore({nbf: 61});
const token = signWithNotBefore({nbf: 61});

expect(() => jwt.verify(token, undefined)).to.throw(
jwt.NotBeforeError,
Expand All @@ -264,7 +264,7 @@ describe('not before', function() {
});

it('should error on verify if "nbf" is after current time using clockTolerance', function () {
const token = signWithNoBefore({}, 5);
const token = signWithNotBefore({}, 5);

expect(() => jwt.verify(token, undefined, {clockTolerance: 4})).to.throw(
jwt.NotBeforeError,
Expand Down

0 comments on commit e2860a9

Please sign in to comment.