From 840591f47a88adaeb03c1b1af172c1f1a765d920 Mon Sep 17 00:00:00 2001 From: Jean de K Date: Mon, 28 Jun 2021 12:10:24 +0200 Subject: [PATCH] Add unit test cases to achieve full coverage (#2) * Add unit test cases to achieve full coverage * Add setup instructions in CONTRIBUTING.md * Fix typo * Add call to parent's constructor in tests' custom Date class --- .gitignore | 2 ++ CONTRIBUTING.md | 13 ++++++++++++- __tests__/index.test.js | 15 +++++++++++++-- package.json | 2 +- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 200aa91..9b5e8ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ node_modules package-lock.json output-template.yml +*.tgz +coverage/ \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7d457e3..c565add 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -43,7 +43,18 @@ GitHub provides additional document on [forking a repository](https://help.githu ## Finding contributions to work on Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any 'help wanted' issues is a great place to start. -## Testing the code +## Development Process + +### Setting up for local development + +``` +git clone git@github.com:awslabs/cognito-at-edge.git +cd cognito-at-edge/ + +npm install +``` + +### Testing the code Tests are written using jest. To run tests invoke ``` npm test diff --git a/__tests__/index.test.js b/__tests__/index.test.js index f90c0bf..24bdaf8 100644 --- a/__tests__/index.test.js +++ b/__tests__/index.test.js @@ -10,6 +10,7 @@ const { Authenticator } = require('../index'); const DATE = new Date('2017'); global.Date = class extends Date { constructor() { + super(); return DATE; } }; @@ -43,6 +44,11 @@ describe('private functions', () => { }); }); + test('should throw if unable to fetch JWKS', () => { + axios.get.mockRejectedValue(new Error('Unexpected error')); + return expect(() => authenticator._fetchJWKS('http://something')).rejects.toThrow(); + }); + test('should get valid decoded token', () => { authenticator._jwks = {}; jwt.decode.mockReturnValueOnce({ header: { kid: 'kid' } }); @@ -59,6 +65,11 @@ describe('private functions', () => { }); }); + test('should throw if unable to fetch token', () => { + axios.request.mockRejectedValue(new Error('Unexpected error')); + return expect(() => authenticator._fetchTokensFromCode('htt://redirect', 'AUTH_CODE')).rejects.toThrow(); + }); + test('should getRedirectResponse', () => { const username = 'toto'; const domain = 'example.com'; @@ -235,7 +246,7 @@ describe('handle', () => { headers: { location: [{ key: 'Location', - value: 'https://my-cognito-domain.auth.us-east-1.amazoncognito.com/authorize?redirect_uri=https://d111111abcdef8.cloudfront.net&response_type=code&client_id=123456789qwertyuiop987abcd&state=/lol', + value: 'https://my-cognito-domain.auth.us-east-1.amazoncognito.com/authorize?redirect_uri=https://d111111abcdef8.cloudfront.net&response_type=code&client_id=123456789qwertyuiop987abcd&state=/lol%3F%3Fparam%3D1', }], }, }, @@ -281,7 +292,7 @@ const getCloudfrontRequest = () => ({ "inputTruncated": false }, "clientIp": "2001:0db8:85a3:0:0:8a2e:0370:7334", - "querystring": "", + "querystring": "?param=1", "uri": "/lol", "method": "GET", "headers": { diff --git a/package.json b/package.json index 1c206fd..259d552 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "index.js" ], "scripts": { - "test": "jest" + "test": "jest --coverage" }, "dependencies": { "axios": "^0.21.1",