@ministryofjustice/fb-jwt-client-node
Advanced tools
Comparing version 0.0.16 to 0.0.17
@@ -187,2 +187,5 @@ const request = require('request-promise-native') | ||
* | ||
* @param {boolean} [qs] | ||
* Send payload as query strinf | ||
* | ||
* @return {object} | ||
@@ -192,6 +195,7 @@ * Request options | ||
**/ | ||
createRequestOptions (urlPattern, urlKeys, payload = {}) { | ||
createRequestOptions (urlPattern, urlKeys, payload = {}, qs) { | ||
const accessToken = this.generateAccessToken(payload) | ||
const url = this.createEndpointUrl(urlPattern, urlKeys) | ||
const json = Object.keys(payload).length ? payload : true | ||
const isPayload = Object.keys(payload).length | ||
const json = isPayload && !qs ? payload : true | ||
const requestOptions = { | ||
@@ -204,2 +208,7 @@ url, | ||
} | ||
if (qs && isPayload) { | ||
requestOptions.qs = { | ||
payload: Buffer.from(JSON.stringify(payload)).toString('Base64') | ||
} | ||
} | ||
return requestOptions | ||
@@ -217,2 +226,5 @@ } | ||
* | ||
* @param {object} [payload] | ||
* Payload to send as query param to endpoint | ||
* | ||
* @return {object} | ||
@@ -222,5 +234,5 @@ * Returns JSON object or handles exception | ||
**/ | ||
sendGet (urlPattern, urlKeys) { | ||
sendGet (urlPattern, urlKeys, payload) { | ||
const client = this | ||
const options = this.createRequestOptions(urlPattern, urlKeys) | ||
const options = this.createRequestOptions(urlPattern, urlKeys, payload, true) | ||
return request.get(options) | ||
@@ -227,0 +239,0 @@ .catch(e => client.handleRequestError(e)) |
@@ -20,2 +20,5 @@ const test = require('tape') | ||
const encryptedData = 'RRqDeJRQlZULKx1NYql/imRmDsy9AZshKozgLuY=' | ||
const userIdTokenData = {userId, userToken} | ||
const encryptedUserIdTokenData = 'Ejo7ypk1TFQNAbbkUFW8NeQhcZt1Wxf1IJNLhDjbtpoUdfluylSqWDCRXuulEqMiCdiQzhjIeLHANj9mMK0sMl6jTA==' | ||
const expectedEncryptedData = 'pOXXs5YW9mUW1weBLNawiMRFdk6Hh92YBfGqmg8ych8PqnZ5l8JbcqHXHKjmcrKYJqZXn53sFr/eCq7Mbh5j9rj87w==' | ||
@@ -109,2 +112,23 @@ // Ensure that client is properly instantiated | ||
test('Wnen creating request options', t => { | ||
const generateAccessTokenStub = stub(jwtClient, 'generateAccessToken') | ||
generateAccessTokenStub.callsFake(() => 'testAccessToken') | ||
const requestOptions = jwtClient.createRequestOptions('/foo', {}, {foo: 'bar'}) | ||
t.deepEqual(requestOptions, { | ||
url: 'https://microservice/foo', | ||
headers: {'x-access-token': 'testAccessToken'}, | ||
json: {foo: 'bar'} | ||
}, 'it should set the correct url, headers and json object') | ||
const requestGetOptions = jwtClient.createRequestOptions('/foo', {}, {foo: 'bar'}, true) | ||
t.deepEqual(requestGetOptions, { | ||
url: 'https://microservice/foo', | ||
headers: {'x-access-token': 'testAccessToken'}, | ||
json: true, | ||
qs: {payload: 'eyJmb28iOiJiYXIifQ=='} | ||
}, 'and when a querystring is specified, it should set json option to true and the qs option to the payload’s value') | ||
generateAccessTokenStub.restore() | ||
t.end() | ||
}) | ||
// Decrypting user data | ||
@@ -158,5 +182,39 @@ test('When decrypting data', async t => { | ||
// Encrypting user ID and token | ||
test('When encrypting the user ID and token', async t => { | ||
const encryptedData = jwtClient.encryptUserIdAndToken(userId, userToken) | ||
t.equal(encryptedData, expectedEncryptedData, 'it should encrypt the data correctly') | ||
const encryptedDataAgain = jwtClient.encryptUserIdAndToken(userId, userToken) | ||
t.equal(encryptedDataAgain, encryptedData, 'it should return the same value for the same input') | ||
t.end() | ||
}) | ||
// Decrypting user ID and token | ||
test('When decrypting the user’s ID and token', async t => { | ||
const decryptedData = jwtClient.decryptUserIdAndToken(encryptedUserIdTokenData) | ||
t.deepEqual(userIdTokenData, decryptedData, 'it should return the correct data from valid encrypted input') | ||
t.end() | ||
}) | ||
test('When decrypting invalid user ID and token', async t => { | ||
t.plan(4) | ||
let invalidData | ||
try { | ||
invalidData = jwtClient.decryptUserIdAndToken(userToken, 'invalid') | ||
} catch (e) { | ||
t.equal(e.name, 'FBJWTClientError', 'it should return an error object of the correct type') | ||
t.equal(e.code, 500, 'it should return correct error code') | ||
t.equal(e.message, 'EINVALIDPAYLOAD', 'it should return the correct error message') | ||
} | ||
t.equal(invalidData, undefined, 'it should not return anything if data is invalid') | ||
t.end() | ||
}) | ||
// Sending gets | ||
test('When sending gets', async t => { | ||
t.plan(3) | ||
t.plan(7) | ||
@@ -176,4 +234,15 @@ const stubAccessToken = stub(jwtClient, 'generateAccessToken') | ||
t.equal(callArgs.headers['x-access-token'], 'testAccessToken', 'it should add the correct x-access-token header') | ||
t.equal(callArgs.json, true, 'it should expect a json response') | ||
t.deepEqual(fetchedData, data, 'it should return the unencrypted data') | ||
stubAccessToken.restore() | ||
await jwtClient.sendGet('/user/:userId', {userId}, {foo: 'bar'}) | ||
const callArgsB = stubRequest.getCall(0).args[0] | ||
// NB. querystring checking handled in createRequestOptions tests | ||
// since qs options get stashed on request agent's internal self object | ||
t.equal(callArgsB.url, `${microserviceUrl}/user/testUserId`, 'it should call the correct url') | ||
t.equal(callArgsB.headers['x-access-token'], 'testAccessToken', 'it should add the correct x-access-token header') | ||
t.equal(callArgsB.json, true, 'it should expect a json response') | ||
stubAccessToken.restore() | ||
@@ -199,3 +268,2 @@ stubRequest.restore() | ||
const responseBody = await jwtClient.sendPost('/user/:userId', {userId}, data) | ||
// jwtClient.setData(userId, userToken, data) | ||
@@ -202,0 +270,0 @@ const callArgs = stubRequest.getCall(0).args[0] |
{ | ||
"name": "@ministryofjustice/fb-jwt-client-node", | ||
"version": "0.0.16", | ||
"version": "0.0.17", | ||
"description": "Form Builder JSON Web Token Client (Node)", | ||
@@ -5,0 +5,0 @@ "main": "lib/fb-jwt-client.js", |
@@ -19,3 +19,3 @@ # Form Builder JSON Web Token client (Node) | ||
// load client class | ||
const {FBJWTClient} = require('@ministryofjustice/fb-jwt-client-node') | ||
const FBJWTClient = require('@ministryofjustice/fb-jwt-client-node') | ||
@@ -22,0 +22,0 @@ // initialise client |
34503
858