From b752cd25828793a714353d466ab8debbc188103d Mon Sep 17 00:00:00 2001 From: Simran Raj Date: Mon, 18 Oct 2021 11:26:52 +0530 Subject: [PATCH 01/10] feat(hmrc): adding getGovClientBrowserDoNotTrackHeader function --- USAGE.md | 11 +++++++- src/js/hmrc/mtdFraudPrevention.js | 14 ++++++++++ src/js/index.js | 2 ++ tests/unit/hmrc/mtdFraudPrevention.test.js | 32 ++++++++++++++++------ 4 files changed, 50 insertions(+), 9 deletions(-) diff --git a/USAGE.md b/USAGE.md index d6ec17d..3742969 100644 --- a/USAGE.md +++ b/USAGE.md @@ -22,4 +22,13 @@ 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 the value for the Gov-Client-Browser-Do-Not-Track HMRC Fraud prevention header: + + ```js + import getGovClientBrowserDoNotTrackHeader from 'user-data-for-fraud-prevention'; + const {headerValue, error} = getGovClientBrowserDoNotTrackHeader() + ``` \ No newline at end of file diff --git a/src/js/hmrc/mtdFraudPrevention.js b/src/js/hmrc/mtdFraudPrevention.js index 70fcfba..f579558 100644 --- a/src/js/hmrc/mtdFraudPrevention.js +++ b/src/js/hmrc/mtdFraudPrevention.js @@ -102,3 +102,17 @@ export const getFraudPreventionHeaders = async () => { return { headers, errors }; }; + +/** + * Returns the value for Gov-Client-Browser-Do-Not-Track HMRC Fraud prevention header. + */ + export const getGovClientBrowserDoNotTrackHeader = async () => { + let headerValue, error = undefined; + try { + headerValue = await getBrowserDoNotTrackStatus(); + } catch (err) { + error = err + return {headerValue, error} + } + return {headerValue, error} +} \ No newline at end of file diff --git a/src/js/index.js b/src/js/index.js index 41682d3..06b027c 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -1,12 +1,14 @@ import { fraudPreventionHeadersEnum, getFraudPreventionHeaders, + getGovClientBrowserDoNotTrackHeader, getScreenDetails, windowDetails, } from "./hmrc/mtdFraudPrevention"; exports.fraudPreventionHeadersEnum = fraudPreventionHeadersEnum; exports.getFraudPreventionHeaders = getFraudPreventionHeaders; +exports.getGovClientBrowserDoNotTrackHeader = getGovClientBrowserDoNotTrackHeader; exports.getScreenDetails = getScreenDetails; exports.windowDetails = windowDetails; diff --git a/tests/unit/hmrc/mtdFraudPrevention.test.js b/tests/unit/hmrc/mtdFraudPrevention.test.js index 0a661cc..e6aa120 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 { getGovClientBrowserDoNotTrackHeader } from "../../../src/js/hmrc/mtdFraudPrevention"; describe("FraudPreventionHeaders", () => { resetDeviceIpString(); @@ -37,13 +38,13 @@ describe("FraudPreventionHeaders", () => { const mockTimeStamp = "2021-06-03T13:02:22.107Z" global.Date = class DateMock { - constructor() { - } - toString() { - return "Tue May 14 2019 12:01:58 GMT+0100 (British Summer Time)"; - } - toISOString() { - return mockTimeStamp; + constructor() { + } + toString() { + return "Tue May 14 2019 12:01:58 GMT+0100 (British Summer Time)"; + } + toISOString() { + return mockTimeStamp; } }; @@ -202,5 +203,20 @@ describe("FraudPreventionHeaders", () => { it("width", () => expect(windowDetails().width).toBe(1009)); it("height", () => expect(windowDetails().height).toBe(1013)); }); - + describe("getGovClientBrowserDoNotTrackHeader", () => { + + it("no error", async () => { + const {headerValue, error} = await getGovClientBrowserDoNotTrackHeader(); + expect(headerValue).toBe("false"); + expect(error).toBe(undefined); + }) + + it("getBrowserDoNotTrackStatus throws error", async () => { + const browserDoNotTrackStatusMock = jest.spyOn(browserInfoHelper, "getBrowserDoNotTrackStatus").mockReturnValue(Promise.reject("Something went wrong.")); + const {headerValue, error} = await getGovClientBrowserDoNotTrackHeader(); + expect(headerValue).toBe(undefined); + expect(error).toBe("Something went wrong."); + browserDoNotTrackStatusMock.mockRestore(); + }) + }); }); From 3fc1c551abda0f2b616abb31025c920ab19671a2 Mon Sep 17 00:00:00 2001 From: Simran Raj Date: Tue, 19 Oct 2021 09:37:17 +0530 Subject: [PATCH 02/10] removed async/await and updated some linting fixes --- USAGE.md | 2 +- src/js/hmrc/mtdFraudPrevention.js | 13 +++++-------- tests/unit/hmrc/mtdFraudPrevention.test.js | 18 +++++++++--------- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/USAGE.md b/USAGE.md index 3742969..4c6da09 100644 --- a/USAGE.md +++ b/USAGE.md @@ -30,5 +30,5 @@ If you want only a specific header value, then you can use below functions that ```js import getGovClientBrowserDoNotTrackHeader from 'user-data-for-fraud-prevention'; - const {headerValue, error} = getGovClientBrowserDoNotTrackHeader() + const {headerValue, error} = getGovClientBrowserDoNotTrackHeader(); ``` \ No newline at end of file diff --git a/src/js/hmrc/mtdFraudPrevention.js b/src/js/hmrc/mtdFraudPrevention.js index f579558..718494a 100644 --- a/src/js/hmrc/mtdFraudPrevention.js +++ b/src/js/hmrc/mtdFraudPrevention.js @@ -106,13 +106,10 @@ export const getFraudPreventionHeaders = async () => { /** * Returns the value for Gov-Client-Browser-Do-Not-Track HMRC Fraud prevention header. */ - export const getGovClientBrowserDoNotTrackHeader = async () => { - let headerValue, error = undefined; + export const getGovClientBrowserDoNotTrackHeader = () => { try { - headerValue = await getBrowserDoNotTrackStatus(); - } catch (err) { - error = err - return {headerValue, error} - } - return {headerValue, error} + return { headerValue: getBrowserDoNotTrackStatus() }; + } catch (error) { + return { error }; + } } \ No newline at end of file diff --git a/tests/unit/hmrc/mtdFraudPrevention.test.js b/tests/unit/hmrc/mtdFraudPrevention.test.js index e6aa120..274326f 100644 --- a/tests/unit/hmrc/mtdFraudPrevention.test.js +++ b/tests/unit/hmrc/mtdFraudPrevention.test.js @@ -38,13 +38,13 @@ describe("FraudPreventionHeaders", () => { const mockTimeStamp = "2021-06-03T13:02:22.107Z" global.Date = class DateMock { - constructor() { - } - toString() { - return "Tue May 14 2019 12:01:58 GMT+0100 (British Summer Time)"; - } - toISOString() { - return mockTimeStamp; + constructor() { + } + toString() { + return "Tue May 14 2019 12:01:58 GMT+0100 (British Summer Time)"; + } + toISOString() { + return mockTimeStamp; } }; @@ -212,10 +212,10 @@ describe("FraudPreventionHeaders", () => { }) it("getBrowserDoNotTrackStatus throws error", async () => { - const browserDoNotTrackStatusMock = jest.spyOn(browserInfoHelper, "getBrowserDoNotTrackStatus").mockReturnValue(Promise.reject("Something went wrong.")); + const browserDoNotTrackStatusMock = jest.spyOn(browserInfoHelper, "getBrowserDoNotTrackStatus").mockImplementation(() => { throw Error("Something went wrong.")}); const {headerValue, error} = await getGovClientBrowserDoNotTrackHeader(); expect(headerValue).toBe(undefined); - expect(error).toBe("Something went wrong."); + expect(error).toEqual(Error("Something went wrong.")); browserDoNotTrackStatusMock.mockRestore(); }) }); From c16c7b12f0d31fea0ec6acdbdd57fadf1093771c Mon Sep 17 00:00:00 2001 From: Simran Raj Date: Wed, 20 Oct 2021 10:45:59 +0530 Subject: [PATCH 03/10] fix: removed extra lines --- tests/unit/hmrc/mtdFraudPrevention.test.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/unit/hmrc/mtdFraudPrevention.test.js b/tests/unit/hmrc/mtdFraudPrevention.test.js index 2b7a428..601d3a6 100644 --- a/tests/unit/hmrc/mtdFraudPrevention.test.js +++ b/tests/unit/hmrc/mtdFraudPrevention.test.js @@ -233,7 +233,7 @@ describe("getGovClientBrowserPluginsHeader", () => { browserPluginMock.mockRestore(); }); -}); +}); describe("getGovClientBrowserDoNotTrackHeader", () => { @@ -251,6 +251,4 @@ describe("getGovClientBrowserDoNotTrackHeader", () => { browserDoNotTrackStatusMock.mockRestore(); }); -}); - - +}); \ No newline at end of file From 5387ce5fcf7e4bdc1f7eb11edd88e416192c2106 Mon Sep 17 00:00:00 2001 From: SIMRAN RAJ Date: Wed, 20 Oct 2021 11:03:56 +0530 Subject: [PATCH 04/10] fix: fixed test cases --- tests/unit/hmrc/mtdFraudPrevention.test.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/unit/hmrc/mtdFraudPrevention.test.js b/tests/unit/hmrc/mtdFraudPrevention.test.js index 601d3a6..afecb24 100644 --- a/tests/unit/hmrc/mtdFraudPrevention.test.js +++ b/tests/unit/hmrc/mtdFraudPrevention.test.js @@ -237,18 +237,18 @@ describe("getGovClientBrowserPluginsHeader", () => { describe("getGovClientBrowserDoNotTrackHeader", () => { - it("no error", async () => { - const {headerValue, error} = await getGovClientBrowserDoNotTrackHeader(); + it("no error", () => { + const {headerValue, error} = getGovClientBrowserDoNotTrackHeader(); expect(headerValue).toBe("true"); expect(error).toBe(undefined); }) - it("getBrowserDoNotTrackStatus throws error", async () => { + it("getBrowserDoNotTrackStatus throws error", () => { const browserDoNotTrackStatusMock = jest.spyOn(browserInfoHelper, "getBrowserDoNotTrackStatus").mockImplementation(() => { throw Error("Something went wrong.")}); - const {headerValue, error} = await getGovClientBrowserDoNotTrackHeader(); + const {headerValue, error} = getGovClientBrowserDoNotTrackHeader(); expect(headerValue).toBe(undefined); expect(error).toEqual(Error("Something went wrong.")); browserDoNotTrackStatusMock.mockRestore(); }); -}); \ No newline at end of file +}); From 1e31ba3fd9bc6f60af62066557bc90b9e5207d9d Mon Sep 17 00:00:00 2001 From: SIMRAN RAJ Date: Wed, 20 Oct 2021 11:08:53 +0530 Subject: [PATCH 05/10] fix: updated import --- tests/unit/hmrc/mtdFraudPrevention.test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/unit/hmrc/mtdFraudPrevention.test.js b/tests/unit/hmrc/mtdFraudPrevention.test.js index afecb24..16c594b 100644 --- a/tests/unit/hmrc/mtdFraudPrevention.test.js +++ b/tests/unit/hmrc/mtdFraudPrevention.test.js @@ -12,8 +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"; -import { getGovClientBrowserDoNotTrackHeader } from "../../../src/js/hmrc/mtdFraudPrevention"; +import { getGovClientBrowserDoNotTrackHeader, getGovClientBrowserPluginsHeader } from "../../../src/js/hmrc/mtdFraudPrevention"; describe("FraudPreventionHeaders", () => { resetDeviceIpString(); From c27c550684dac9dcc3b75c531cda069da9fecc54 Mon Sep 17 00:00:00 2001 From: Simran Raj Date: Thu, 21 Oct 2021 00:30:02 +0530 Subject: [PATCH 06/10] fix: updated test file --- tests/unit/hmrc/mtdFraudPrevention.test.js | 41 +++++++++++----------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/tests/unit/hmrc/mtdFraudPrevention.test.js b/tests/unit/hmrc/mtdFraudPrevention.test.js index e7a86c4..1ebb097 100644 --- a/tests/unit/hmrc/mtdFraudPrevention.test.js +++ b/tests/unit/hmrc/mtdFraudPrevention.test.js @@ -12,13 +12,9 @@ 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"; - -<<<<<<< HEAD -import { getGovClientBrowserDoNotTrackHeader } from "../../../src/js/hmrc/mtdFraudPrevention"; -======= +import {getGovClientBrowserDoNotTrackHeader} from "../../../src/js/hmrc/mtdFraudPrevention"; import {getGovClientDeviceID} from "../../../src/js/hmrc/mtdFraudPrevention"; ->>>>>>> upstream/master +import {getGovClientBrowserDoNotTrackHeader} from "../../../src/js/hmrc/mtdFraudPrevention"; describe("FraudPreventionHeaders", () => { resetDeviceIpString(); @@ -240,6 +236,24 @@ describe("getGovClientBrowserPluginsHeader", () => { }); +describe("getGovClientDeviceID", () => { + it("throws no error",()=>{ + const deviceIDMock = jest.spyOn(uuid, "v4").mockReturnValue("fce4f7ff-d5f1-4e4f-99a1-aa97bef71e99"); + const {headerValue} = getGovClientDeviceID(); + expect(headerValue).toBe("fce4f7ff-d5f1-4e4f-99a1-aa97bef71e99"); + deviceIDMock.mockRestore(); + }); + it("throws error",()=>{ + const deviceIDMock = jest.spyOn(uuid, "v4").mockImplementation(()=>{ + throw Error("Something went wrong."); + }); + const {error} = getGovClientDeviceID(); + expect(error).toEqual(Error("Something went wrong.")); + deviceIDMock.mockRestore(); + }); + +}); + describe("getGovClientBrowserDoNotTrackHeader", () => { it("no error", async () => { @@ -256,19 +270,4 @@ describe("getGovClientBrowserDoNotTrackHeader", () => { browserDoNotTrackStatusMock.mockRestore(); }); -describe("getGovClientDeviceID", () => { - it("throws no error",()=>{ - const deviceIDMock = jest.spyOn(uuid, "v4").mockReturnValue("fce4f7ff-d5f1-4e4f-99a1-aa97bef71e99"); - const {headerValue} = getGovClientDeviceID(); - expect(headerValue).toBe("fce4f7ff-d5f1-4e4f-99a1-aa97bef71e99"); - deviceIDMock.mockRestore(); - }); - it("throws error",()=>{ - const deviceIDMock = jest.spyOn(uuid, "v4").mockImplementation(()=>{ - throw Error("Something went wrong."); - }); - const {error} = getGovClientDeviceID(); - expect(error).toEqual(Error("Something went wrong.")); - deviceIDMock.mockRestore(); - }); }); \ No newline at end of file From 356a8d02651385d7877dddf8f323d9e4c12087b3 Mon Sep 17 00:00:00 2001 From: Simran Raj Date: Thu, 21 Oct 2021 00:31:42 +0530 Subject: [PATCH 07/10] fix: updated test file --- tests/unit/hmrc/mtdFraudPrevention.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/hmrc/mtdFraudPrevention.test.js b/tests/unit/hmrc/mtdFraudPrevention.test.js index 1ebb097..54f10c0 100644 --- a/tests/unit/hmrc/mtdFraudPrevention.test.js +++ b/tests/unit/hmrc/mtdFraudPrevention.test.js @@ -12,7 +12,7 @@ import { import * as browserInfoHelper from "../../../src/js/common/browserInfoHelper"; import { resetDeviceIpString, resetDeviceIpTimeStamp } from "../../../src/js/common/browserInfoHelper"; import uuid from "uuid"; -import {getGovClientBrowserDoNotTrackHeader} from "../../../src/js/hmrc/mtdFraudPrevention"; +import {getGovClientBrowserPluginsHeader} from "../../../src/js/hmrc/mtdFraudPrevention"; import {getGovClientDeviceID} from "../../../src/js/hmrc/mtdFraudPrevention"; import {getGovClientBrowserDoNotTrackHeader} from "../../../src/js/hmrc/mtdFraudPrevention"; @@ -251,7 +251,7 @@ describe("getGovClientDeviceID", () => { expect(error).toEqual(Error("Something went wrong.")); deviceIDMock.mockRestore(); }); - + }); describe("getGovClientBrowserDoNotTrackHeader", () => { From a91286bac5fb763e2f2dd8880f867e4831f671d3 Mon Sep 17 00:00:00 2001 From: Simran Raj Date: Thu, 21 Oct 2021 00:33:47 +0530 Subject: [PATCH 08/10] fix: updated test file --- tests/unit/hmrc/mtdFraudPrevention.test.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/unit/hmrc/mtdFraudPrevention.test.js b/tests/unit/hmrc/mtdFraudPrevention.test.js index 2359386..464df11 100644 --- a/tests/unit/hmrc/mtdFraudPrevention.test.js +++ b/tests/unit/hmrc/mtdFraudPrevention.test.js @@ -251,7 +251,6 @@ describe("getGovClientDeviceID", () => { expect(error).toEqual(Error("Something went wrong.")); deviceIDMock.mockRestore(); }); - }); describe("getGovClientBrowserDoNotTrackHeader", () => { @@ -269,5 +268,4 @@ describe("getGovClientBrowserDoNotTrackHeader", () => { expect(error).toEqual(Error("Something went wrong.")); browserDoNotTrackStatusMock.mockRestore(); }); - }); From 7a0beaf954da18e873c6d359f9a1f86e5d7ade04 Mon Sep 17 00:00:00 2001 From: Simran Raj Date: Thu, 21 Oct 2021 14:57:23 +0530 Subject: [PATCH 09/10] fix: updated the test file --- tests/unit/hmrc/mtdFraudPrevention.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/hmrc/mtdFraudPrevention.test.js b/tests/unit/hmrc/mtdFraudPrevention.test.js index 1af02c1..9a929db 100644 --- a/tests/unit/hmrc/mtdFraudPrevention.test.js +++ b/tests/unit/hmrc/mtdFraudPrevention.test.js @@ -249,14 +249,14 @@ describe("getGovClientDeviceID", () => { }); describe("getGovClientBrowserDoNotTrackHeader", () => { - it("no error", () => { + it("returns correct headerValue when there is no error", () => { const browserDoNotTrackStatusMock = jest.spyOn(browserInfoHelper, "getBrowserDoNotTrackStatus").mockImplementation(() => "true"); const {headerValue, error} = getGovClientBrowserDoNotTrackHeader(); expect(headerValue).toBe("true"); expect(error).toBe(undefined); browserDoNotTrackStatusMock.mockRestore(); }) - it("getBrowserDoNotTrackStatus throws error", () => { + it("returns error when there is an error", () => { const browserDoNotTrackStatusMock = jest.spyOn(browserInfoHelper, "getBrowserDoNotTrackStatus").mockImplementation(() => { throw Error("Something went wrong.")}); const {headerValue, error} = getGovClientBrowserDoNotTrackHeader(); expect(headerValue).toBe(undefined); From 08204524722968e4bd1a76bebf34851cdfb3c97d Mon Sep 17 00:00:00 2001 From: Simran Raj Date: Fri, 22 Oct 2021 14:11:27 +0530 Subject: [PATCH 10/10] fix: update usage.md for imports --- USAGE.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/USAGE.md b/USAGE.md index 20d032c..e24023d 100644 --- a/USAGE.md +++ b/USAGE.md @@ -28,7 +28,7 @@ If you want only a specific header value, then you can use below functions that * To get Gov-Client-Browser_plugins HMRC Fraud prevention header: ```js -import getGovClientBrowserPluginsHeader from 'user-data-for-fraud-prevention'; +import {getGovClientBrowserPluginsHeader} from 'user-data-for-fraud-prevention'; const {headerValue, error} = getGovClientBrowserPluginsHeader(); ``` * To get Gov-Client-Browser-JS-User-Agent HMRC Fraud prevention header: @@ -38,12 +38,12 @@ const { headerValue, error } = getGovClientBrowserJSUserAgentHeader(); ``` * To get Gov-Client-Device Id HMRC Fraud prevention header: ```js -import getGovClientDeviceID from 'user-data-for-fraud-prevention'; +import {getGovClientDeviceID}from 'user-data-for-fraud-prevention'; const {headerValue, error} = getGovClientDeviceID(); ``` * To get Gov-Client-Browser-Do-Not-Track HMRC Fraud prevention header: ```js -import getGovClientBrowserDoNotTrackHeader from 'user-data-for-fraud-prevention'; +import {getGovClientBrowserDoNotTrackHeader} from 'user-data-for-fraud-prevention'; const {headerValue, error} = getGovClientBrowserDoNotTrackHeader(); ```