Skip to content

Commit

Permalink
- added a todo list
Browse files Browse the repository at this point in the history
- exercised the 'regex' mode with unit tests
  • Loading branch information
robertbullen committed Dec 16, 2018
1 parent 5c8187a commit 3d77019
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 44 deletions.
5 changes: 5 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# TODOs

## Issue #1

- add unit tests for `JwtMode 'verify'`
96 changes: 52 additions & 44 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,67 @@ import * as yup from 'yup';

import { defaultMessage } from '../src/index';

describe('`yup.string().jwt()`', () => {
const nonsenseToken: string = 'foo.bar.qux';
const schema: yup.StringSchema = yup.string().jwt('decode');

describe('`isValidSync()`', () => {
const stringPayload: string = 'payload';
const objectPayload: object = { stringPayload };
const secret: string = 'secret';

describe('returns true', () => {
it('when testing a valid token with a string payload', () => {
const token: string = jwt.sign(stringPayload, secret);
expect(schema.isValidSync(token)).toBe(true);
});
const modes: Array<'regex' | 'decode'> = ['regex', 'decode'];
for (const mode of modes) {
describe(`yup.string().jwt('${mode}')`, () => {
const schema: yup.StringSchema = yup.string().jwt(mode);

it('when testing a valid token with an object payload', () => {
const token: string = jwt.sign(objectPayload, secret);
expect(schema.isValidSync(token)).toBe(true);
});
const nonsenseToken: string = 'qwerty12345';

it('when testing a valid token that has expired', () => {
const payload = { stringPayload, exp: Math.floor(Date.now() / 1000) - 30 };
const token: string = jwt.sign(payload, secret);
expect(schema.isValidSync(token)).toBe(true);
});
});
describe('isValidSync()', () => {
const stringPayload: string = 'payload';
const objectPayload: object = { stringPayload };
const secret: string = 'secret';

describe('returns true', () => {
it('when testing a valid token with a string payload', () => {
const token: string = jwt.sign(stringPayload, secret);
expect(schema.isValidSync(token)).toBe(true);
});

describe('returns false', () => {
it('when testing an nonsense token', () => {
expect(schema.isValidSync(nonsenseToken)).toBe(false);
it('when testing a valid token with an object payload', () => {
const token: string = jwt.sign(objectPayload, secret);
expect(schema.isValidSync(token)).toBe(true);
});

it('when testing a valid token that has expired', () => {
const payload = { stringPayload, exp: Math.floor(Date.now() / 1000) - 30 };
const token: string = jwt.sign(payload, secret);
expect(schema.isValidSync(token)).toBe(true);
});
});

it('when testing a corrupted token', () => {
const token: string = jwt.sign(stringPayload, secret);
const corruptedToken: string = token + '#';
expect(schema.isValidSync(corruptedToken)).toBe(false);
describe('returns false', () => {
it('when testing an nonsense token', () => {
expect(schema.isValidSync(nonsenseToken)).toBe(false);
});

it('when testing a corrupted token', () => {
const token: string = jwt.sign(stringPayload, secret);
const corruptedToken: string = token + '#';
expect(schema.isValidSync(corruptedToken)).toBe(false);
});
});
});
});

describe('`validateSync()`', () => {
describe('throws an error', () => {
it('with the default message when no custom message is supplied', () => {
// tslint:disable-next-line:no-invalid-template-strings
const expectedMessage: string = defaultMessage.replace('${path}', 'this');
expect(() => schema.validateSync(nonsenseToken)).toThrowError(expectedMessage);
});
describe('validateSync()', () => {
describe('throws an error', () => {
it('with the default message when no custom message is supplied', () => {
// tslint:disable-next-line:no-invalid-template-strings
const expectedMessage: string = defaultMessage.replace('${path}', 'this');
expect(() => schema.validateSync(nonsenseToken)).toThrowError(expectedMessage);
});

it('with a custom message when one is supplied', () => {
const customMessage: string = 'custom message';
const customMessageSchema: yup.StringSchema = yup.string().jwt('decode', customMessage);
expect(() => customMessageSchema.validateSync(nonsenseToken)).toThrowError(customMessage);
it('with a custom message when one is supplied', () => {
const customMessage: string = 'custom message';
const customMessageSchema: yup.StringSchema = yup
.string()
.jwt('decode', customMessage);
expect(() => customMessageSchema.validateSync(nonsenseToken)).toThrowError(
customMessage,
);
});
});
});
});
});
}

0 comments on commit 3d77019

Please sign in to comment.