Skip to content

Minor test refactoring for recently added tests #504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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