Skip to content

Commit

Permalink
Add unit test cases to achieve full coverage (#2)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jeandek authored Jun 28, 2021
1 parent 43e93ac commit 840591f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
package-lock.json
output-template.yml
*.tgz
coverage/
13 changes: 12 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]: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
Expand Down
15 changes: 13 additions & 2 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { Authenticator } = require('../index');
const DATE = new Date('2017');
global.Date = class extends Date {
constructor() {
super();
return DATE;
}
};
Expand Down Expand Up @@ -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' } });
Expand All @@ -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';
Expand Down Expand Up @@ -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',
}],
},
},
Expand Down Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"index.js"
],
"scripts": {
"test": "jest"
"test": "jest --coverage"
},
"dependencies": {
"axios": "^0.21.1",
Expand Down

0 comments on commit 840591f

Please sign in to comment.