-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: using jsonwebtoken to decode token payload instead of atob and j…
…son parse token (#572)
- Loading branch information
Showing
6 changed files
with
116 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,121 @@ | ||
import { utils } from './utils-service'; | ||
import { config } from '../../config'; | ||
import axios from 'axios'; | ||
import { exceptions } from 'winston'; | ||
import jwt from 'jsonwebtoken'; | ||
|
||
jest.mock('axios'); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('postDataToDocGenService (&& postData)', () => { | ||
describe('when the config parameter is omitted', () => { | ||
it('creates a new config', async () => { | ||
const body = 'test'; | ||
const url = 'http://localhost'; | ||
const corr = '1234asdf'; | ||
|
||
const configResult = { | ||
headers: { | ||
'x-correlation-id': corr, | ||
'x-api-key': config.get('docGenService:apiKey'), | ||
}, | ||
}; | ||
|
||
const spyPostData = jest.spyOn(axios, 'post').mockResolvedValue({ | ||
data: 'test', | ||
describe('utils-service', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
describe('postDataToDocGenService (&& postData)', () => { | ||
describe('when the config parameter is omitted', () => { | ||
it('creates a new config', async () => { | ||
const body = 'test'; | ||
const url = 'http://localhost'; | ||
const corr = '1234asdf'; | ||
|
||
const configResult = { | ||
headers: { | ||
'x-correlation-id': corr, | ||
'x-api-key': config.get('docGenService:apiKey'), | ||
}, | ||
}; | ||
|
||
const spyPostData = jest.spyOn(axios, 'post').mockResolvedValue({ | ||
data: 'test', | ||
}); | ||
|
||
await utils.postDataToDocGenService(body, url, corr); | ||
|
||
expect(spyPostData).toHaveBeenCalledWith(url, body, configResult); | ||
}); | ||
}); | ||
describe('when the config parameter is provided', () => { | ||
it('adds the required headers', async () => { | ||
const body = 'test'; | ||
const url = 'http://localhost'; | ||
const corr = '1234asdf'; | ||
|
||
const configParam = { | ||
headers: { | ||
Accept: 'application/pdf', | ||
}, | ||
responseType: 'stream', | ||
}; | ||
|
||
const configResult = { | ||
headers: { | ||
Accept: 'application/pdf', | ||
'x-correlation-id': corr, | ||
'x-api-key': config.get('docGenService:apiKey'), | ||
}, | ||
responseType: 'stream', | ||
}; | ||
|
||
await utils.postDataToDocGenService(body, url, corr); | ||
const spyPostData = jest.spyOn(axios, 'post').mockResolvedValue({ | ||
data: 'test', | ||
}); | ||
|
||
expect(spyPostData).toHaveBeenCalledWith(url, body, configResult); | ||
await utils.postDataToDocGenService(body, url, corr, configParam); | ||
|
||
expect(spyPostData).toHaveBeenCalledWith(url, body, configResult); | ||
}); | ||
}); | ||
}); | ||
describe('when the config parameter is provided', () => { | ||
it('adds the required headers', async () => { | ||
const body = 'test'; | ||
const url = 'http://localhost'; | ||
const corr = '1234asdf'; | ||
|
||
const configParam = { | ||
headers: { | ||
Accept: 'application/pdf', | ||
}, | ||
responseType: 'stream', | ||
}; | ||
|
||
const configResult = { | ||
headers: { | ||
Accept: 'application/pdf', | ||
'x-correlation-id': corr, | ||
'x-api-key': config.get('docGenService:apiKey'), | ||
}, | ||
responseType: 'stream', | ||
}; | ||
|
||
const spyPostData = jest.spyOn(axios, 'post').mockResolvedValue({ | ||
data: 'test', | ||
}); | ||
|
||
await utils.postDataToDocGenService(body, url, corr, configParam); | ||
describe('parseJwt', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('when the JWT is invalid', () => { | ||
it('throws an error', () => { | ||
jest.spyOn(jwt, 'decode').mockImplementationOnce(() => { | ||
throw new Error('test'); | ||
}); | ||
|
||
expect(spyPostData).toHaveBeenCalledWith(url, body, configResult); | ||
expect(utils.parseJwt('test')).toBe(null); | ||
}); | ||
}); | ||
|
||
describe('when the JWT is valid', () => { | ||
describe('with special characters', () => { | ||
it('parses a JWT successfully', () => { | ||
const expectedPayload = { | ||
sub: '1234567890', | ||
name: 'John Doe', | ||
iat: 1516239022, | ||
display_name: 'Kevin O’Riely', | ||
}; | ||
|
||
const output = utils.parseJwt('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJkaXNwbGF5X25hbWUiOiJLZXZpbiBP4oCZUmllbHkifQ.ab6ATknTP8_gksT7mnGV9XdEbE8JatEEeAYD4ipPQMg'); | ||
expect(output).toEqual(expectedPayload); | ||
}); | ||
}); | ||
|
||
describe('without special characters', () => { | ||
it('parses a JWT successfully', () => { | ||
const expectedPayload = { | ||
sub: '1234567890', | ||
name: 'John Doe', | ||
iat: 1516239022, | ||
display_name: 'Kevin ORiely', | ||
}; | ||
|
||
const output = utils.parseJwt( | ||
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJkaXNwbGF5X25hbWUiOiJLZXZpbiBPUmllbHkifQ.Q7ZwUBIr5hwFuPKq4twT_nE7J3PVQj6hVukhT0xurrY', | ||
); | ||
expect(output).toEqual(expectedPayload); | ||
}); | ||
}); | ||
|
||
}) | ||
|
||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters