From 28c41d6e98cae3b8b4b330f90e7195bad511a60d Mon Sep 17 00:00:00 2001 From: Anurag Bajaj Date: Thu, 21 Oct 2021 15:43:20 -0400 Subject: [PATCH] Issue 145:Export browser user-agent details header for HMRC (Gov-Client-Browser-JS-User-Agent (#154) * feat(header): implement getGovClientBrowserHeader * test(header): unit tests for getGovClientBrowserHeader * docs(usage): updated markdown for individual header function * style(header): formatting * docs(usage.md): documentation for header function Co-authored-by: Susmitha Kodamarthi * style(header): formatting Co-authored-by: Susmitha Kodamarthi * refactor(test): assertion Co-authored-by: Susmitha Kodamarthi * refactor(test): assertion Co-authored-by: Susmitha Kodamarthi * docs(usage.md): update markdown * style(test): formatting * test(header): restore navigator mocks * style(index): semi-colon * style(usage.md): formatting * docs(usage): Update USAGE.md Co-authored-by: Susmitha Kodamarthi * refactor(header): Update function name Co-authored-by: Susmitha Kodamarthi * docs(usage): Update USAGE.md Co-authored-by: Susmitha Kodamarthi * refactor(header): function name Co-authored-by: Susmitha Kodamarthi --- USAGE.md | 6 +++++- src/js/hmrc/mtdFraudPrevention.js | 12 +++++++++++- src/js/index.js | 5 ++++- tests/unit/hmrc/mtdFraudPrevention.test.js | 17 ++++++++++++++++- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/USAGE.md b/USAGE.md index 21cb09c..222a575 100644 --- a/USAGE.md +++ b/USAGE.md @@ -30,7 +30,11 @@ If you want only a specific header value, then you can use below functions that import getGovClientBrowserPluginsHeader from 'user-data-for-fraud-prevention'; const {headerValue, error} = getGovClientBrowserPluginsHeader(); ``` - +* To get Gov-Client-Browser-JS-User-Agent HMRC Fraud prevention header: +```js +import {getGovClientBrowserJSUserAgentHeader} from 'user-data-for-fraud-prevention'; +const { headerValue, error } = getGovClientBrowserJSUserAgentHeader(); +``` * To get Gov-Client-Device Id HMRC Fraud prevention header: ```js import getGovClientDeviceID from 'user-data-for-fraud-prevention'; diff --git a/src/js/hmrc/mtdFraudPrevention.js b/src/js/hmrc/mtdFraudPrevention.js index b90cb17..3530630 100644 --- a/src/js/hmrc/mtdFraudPrevention.js +++ b/src/js/hmrc/mtdFraudPrevention.js @@ -95,13 +95,23 @@ export const getFraudPreventionHeaders = async () => { const ipAddress = await getDeviceLocalIPAsString(); headers.set(fraudPreventionHeadersEnum.DEVICE_LOCAL_IPS, encodeURI(ipAddress.deviceIpString)); headers.set(fraudPreventionHeadersEnum.DEVICE_LOCAL_IPS_TIMESTAMP, ipAddress.deviceIpTimeStamp); - } catch (error) { errors.push(error); } return { headers, errors }; }; +/** + * Returns "Gov-Client-Browser-JS-User-Agent" header. + * @returns {object} which has headerValue key having the value of the header or error key if there is an error + */ +export const getGovClientBrowserJSUserAgentHeader = () => { + try { + return { headerValue: getUserAgent() }; + } catch (error) { + return { error }; + } +} /** * Returns the value for Gov-Client-Device-ID HMRC Fraud prevention header. diff --git a/src/js/index.js b/src/js/index.js index 8075351..49bf721 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -3,6 +3,7 @@ import { getFraudPreventionHeaders, getScreenDetails, windowDetails, + getGovClientBrowserJSUserAgentHeader, getGovClientBrowserPluginsHeader, getGovClientDeviceID } from "./hmrc/mtdFraudPrevention"; @@ -12,4 +13,6 @@ exports.getFraudPreventionHeaders = getFraudPreventionHeaders; exports.getGovClientBrowserPluginsHeader = getGovClientBrowserPluginsHeader; exports.getGovClientDeviceID = getGovClientDeviceID; exports.getScreenDetails = getScreenDetails; -exports.windowDetails = windowDetails; \ No newline at end of file +exports.getGovClientBrowserJSUserAgentHeader = getGovClientBrowserJSUserAgentHeader; +exports.windowDetails = windowDetails; + diff --git a/tests/unit/hmrc/mtdFraudPrevention.test.js b/tests/unit/hmrc/mtdFraudPrevention.test.js index 9f8f520..4da4d08 100644 --- a/tests/unit/hmrc/mtdFraudPrevention.test.js +++ b/tests/unit/hmrc/mtdFraudPrevention.test.js @@ -3,6 +3,7 @@ import { getFraudPreventionHeaders, getScreenDetails, windowDetails, + getGovClientBrowserJSUserAgentHeader, } from "../../../src/js"; import { MockRTCPeerConnection, @@ -204,7 +205,21 @@ describe("FraudPreventionHeaders", () => { it("width", () => expect(windowDetails().width).toBe(1009)); it("height", () => expect(windowDetails().height).toBe(1013)); }); - + describe("getGovClientBrowserHeader", () => { + it("returns correct headerValue when there is no error", () => { + const userAgent = "Mozilla/5.0" + navigatorSpy.mockImplementationOnce(() => ({ + userAgent + })); + expect(getGovClientBrowserJSUserAgentHeader()).toEqual({ headerValue: userAgent }) + navigatorSpy.mockRestore(); + }) + it("returns error when there is an error", () => { + navigatorSpy.mockImplementationOnce(() => null); + expect(getGovClientBrowserJSUserAgentHeader().error.toString()).toEqual("TypeError: Cannot read property 'userAgent' of null") + navigatorSpy.mockRestore(); + }) + }) }); describe("getGovClientBrowserPluginsHeader", () => {