From 4a4e8e4265b57fc7b11e9eb367b4076621ec1d07 Mon Sep 17 00:00:00 2001 From: ankush2805 Date: Wed, 20 Oct 2021 04:30:49 +0530 Subject: [PATCH] feat: Adding getGovClientBrowserPluginsHeader function (#147) * feat: Adding getGovClientBrowserPluginsHeader function * Changes to return only header values instead of entire header * Updating the comments section of Usage file * feat: correcting test cases by adding headerValue check * feat: adding test cases for error scenario * feat: fixing the circleci changes Co-authored-by: Sharma, Ankush --- USAGE.md | 11 ++++++-- src/js/hmrc/mtdFraudPrevention.js | 11 ++++++++ src/js/index.js | 2 ++ tests/unit/hmrc/mtdFraudPrevention.test.js | 29 ++++++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/USAGE.md b/USAGE.md index d6ec17d..9f86342 100644 --- a/USAGE.md +++ b/USAGE.md @@ -1,4 +1,4 @@ -## Usage +## Usage In your project root run: @@ -22,4 +22,11 @@ const fraudHeaders = await getFraudPreventionHeaders(); const timezoneHeader = fraudHeaders.get('Gov-Client-Timezone'); // Or const timezoneHeader = fraudHeaders.get(fraudPreventionHeadersEnum.TIMEZONE); -``` \ No newline at end of file +``` + +If you want only a specific header value, then you can use below functions that are available to get individual header values: +* To get Gov-Client-Browser_plugins HMRC Fraud prevention header: +``` +import getGovClientBrowserPluginsHeader from 'user-data-for-fraud-prevention'; +const {headerValue, error} = getGovClientBrowserPluginsHeader(); +``` diff --git a/src/js/hmrc/mtdFraudPrevention.js b/src/js/hmrc/mtdFraudPrevention.js index 70fcfba..e26b71d 100644 --- a/src/js/hmrc/mtdFraudPrevention.js +++ b/src/js/hmrc/mtdFraudPrevention.js @@ -102,3 +102,14 @@ export const getFraudPreventionHeaders = async () => { return { headers, errors }; }; + +/** + * Returns the value for Gov-Client-Browser-Plugins HMRC Fraud prevention header. + */ +export const getGovClientBrowserPluginsHeader = () => { + try { + return {headerValue: encodeURI(getBrowserPluginsAsString())}; + } catch (error) { + return {error}; + } +} diff --git a/src/js/index.js b/src/js/index.js index 41682d3..9d4ad53 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -3,10 +3,12 @@ import { getFraudPreventionHeaders, getScreenDetails, windowDetails, + getGovClientBrowserPluginsHeader, } from "./hmrc/mtdFraudPrevention"; exports.fraudPreventionHeadersEnum = fraudPreventionHeadersEnum; exports.getFraudPreventionHeaders = getFraudPreventionHeaders; +exports.getGovClientBrowserPluginsHeader = getGovClientBrowserPluginsHeader; exports.getScreenDetails = getScreenDetails; exports.windowDetails = windowDetails; diff --git a/tests/unit/hmrc/mtdFraudPrevention.test.js b/tests/unit/hmrc/mtdFraudPrevention.test.js index 0a661cc..6602524 100644 --- a/tests/unit/hmrc/mtdFraudPrevention.test.js +++ b/tests/unit/hmrc/mtdFraudPrevention.test.js @@ -12,6 +12,7 @@ import { import * as browserInfoHelper from "../../../src/js/common/browserInfoHelper"; import { resetDeviceIpString, resetDeviceIpTimeStamp } from "../../../src/js/common/browserInfoHelper"; import uuid from "uuid"; +import {getGovClientBrowserPluginsHeader} from "../../../src/js/hmrc/mtdFraudPrevention"; describe("FraudPreventionHeaders", () => { resetDeviceIpString(); @@ -204,3 +205,31 @@ describe("FraudPreventionHeaders", () => { }); }); + +describe("getGovClientBrowserPluginsHeader", () => { + let navigatorSpy; + + beforeEach(() => { + navigatorSpy = jest.spyOn(global, 'navigator', 'get'); + }); + + it("no error", () => { + navigatorSpy.mockImplementation(() => ({ + plugins: getMockBrowserPluginDetails(), + doNotTrack: "yes", + })); + const {headerValue, error} = getGovClientBrowserPluginsHeader(); + expect(error).toBe(undefined); + expect(headerValue).toBe("ABC%20Plugin,XYZ%20Plugin"); + }); + + it("getGovClientBrowserPluginsHeader throws error", () => { + + const browserPluginMock = jest.spyOn(browserInfoHelper, "getBrowserPluginsAsString").mockImplementation(() => { throw Error("Something went wrong.")}); + const {headerValue, error} = getGovClientBrowserPluginsHeader(); + expect(error).toEqual(Error("Something went wrong.")); + expect(headerValue).toBe(undefined); + browserPluginMock.mockRestore(); + }); + +});