From 3d77019a63c3308a5ed2bb4f9e842309ce0d439c Mon Sep 17 00:00:00 2001 From: Robert Bullen Date: Sun, 16 Dec 2018 13:07:00 -0600 Subject: [PATCH] - added a todo list - exercised the 'regex' mode with unit tests --- TODO.md | 5 +++ test/test.ts | 96 ++++++++++++++++++++++++++++------------------------ 2 files changed, 57 insertions(+), 44 deletions(-) create mode 100644 TODO.md diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..32798fe --- /dev/null +++ b/TODO.md @@ -0,0 +1,5 @@ +# TODOs + +## Issue #1 + +- add unit tests for `JwtMode 'verify'` diff --git a/test/test.ts b/test/test.ts index 43e5c90..17e32e5 100644 --- a/test/test.ts +++ b/test/test.ts @@ -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, + ); + }); }); }); }); -}); +}