From 09aa880c2a19c22ca8f78cc0cf8fd784d0b11ed0 Mon Sep 17 00:00:00 2001 From: Riya John Date: Thu, 14 Oct 2021 23:24:14 +0530 Subject: [PATCH 1/9] feat: getGovClientScreensHeader --- src/js/hmrc/mtdFraudPrevention.js | 9 ++++++ tests/unit/hmrc/mtdFraudPrevention.test.js | 32 ++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/js/hmrc/mtdFraudPrevention.js b/src/js/hmrc/mtdFraudPrevention.js index 70fcfba..0060dc5 100644 --- a/src/js/hmrc/mtdFraudPrevention.js +++ b/src/js/hmrc/mtdFraudPrevention.js @@ -102,3 +102,12 @@ export const getFraudPreventionHeaders = async () => { return { headers, errors }; }; + +/** + * Returns Gov-Client-Screens HMRC Fraud prevention header. + */ +export const getGovClientScreensHeader = async () => { + const header = new Map(); + header.set( fraudPreventionHeadersEnum.SCREENS_DETAILS, await getScreenData()) + return header +} \ No newline at end of file diff --git a/tests/unit/hmrc/mtdFraudPrevention.test.js b/tests/unit/hmrc/mtdFraudPrevention.test.js index 0a661cc..46f3633 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 {getGovClientScreensHeader} from "../../../src/js/hmrc/mtdFraudPrevention"; describe("FraudPreventionHeaders", () => { resetDeviceIpString(); @@ -204,3 +205,34 @@ describe("FraudPreventionHeaders", () => { }); }); + +describe("getGovClientScreensHeader", () => { + let screenSpy, windowSpy; + + beforeEach(() => { + screenSpy = jest.spyOn(global, 'screen', 'get'); + windowSpy = jest.spyOn(global, 'window', 'get'); + }); + + afterEach(() => { + screenSpy.mockRestore(); + windowSpy.mockRestore(); + }); + + it("no error", async () => { + screenSpy.mockImplementation(() => ({ + width: 1019, + height: 1021, + colorDepth: 17, + })); + + windowSpy.mockImplementation(() => ({ + devicePixelRatio: 2, + })); + + const {header, error} = await getGovClientScreensHeader() + expect(header.size).toBe(1); + expect(error).toBe(undefined); + expect(header.get("Gov-Client-Screens")).toBe(`width=1019&height=1021&scaling-factor=2&colour-depth=17`); + }); +}); From 61095dfb6170604f80195ca206da037a90860846 Mon Sep 17 00:00:00 2001 From: Riya John Date: Thu, 14 Oct 2021 23:25:43 +0530 Subject: [PATCH 2/9] fix: retain format --- src/js/hmrc/mtdFraudPrevention.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/hmrc/mtdFraudPrevention.js b/src/js/hmrc/mtdFraudPrevention.js index 0060dc5..446a03d 100644 --- a/src/js/hmrc/mtdFraudPrevention.js +++ b/src/js/hmrc/mtdFraudPrevention.js @@ -109,5 +109,5 @@ export const getFraudPreventionHeaders = async () => { export const getGovClientScreensHeader = async () => { const header = new Map(); header.set( fraudPreventionHeadersEnum.SCREENS_DETAILS, await getScreenData()) - return header + return {header, undefined} } \ No newline at end of file From dec72aa374046587658db60313928fb4e3a7ed25 Mon Sep 17 00:00:00 2001 From: Riya John Date: Sat, 16 Oct 2021 20:09:16 +0530 Subject: [PATCH 3/9] fix: return format --- src/js/hmrc/mtdFraudPrevention.js | 4 ++-- src/js/index.js | 2 ++ tests/unit/hmrc/mtdFraudPrevention.test.js | 3 +-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/js/hmrc/mtdFraudPrevention.js b/src/js/hmrc/mtdFraudPrevention.js index 446a03d..2dba3bd 100644 --- a/src/js/hmrc/mtdFraudPrevention.js +++ b/src/js/hmrc/mtdFraudPrevention.js @@ -105,9 +105,9 @@ export const getFraudPreventionHeaders = async () => { /** * Returns Gov-Client-Screens HMRC Fraud prevention header. + * @returns {object} with two fields headerValue and error if there is an error else undefined */ export const getGovClientScreensHeader = async () => { - const header = new Map(); - header.set( fraudPreventionHeadersEnum.SCREENS_DETAILS, await getScreenData()) + let header = await getScreenData() return {header, undefined} } \ No newline at end of file diff --git a/src/js/index.js b/src/js/index.js index 41682d3..ab8b5da 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -1,12 +1,14 @@ import { fraudPreventionHeadersEnum, getFraudPreventionHeaders, + getGovClientScreensHeader, getScreenDetails, windowDetails, } from "./hmrc/mtdFraudPrevention"; exports.fraudPreventionHeadersEnum = fraudPreventionHeadersEnum; exports.getFraudPreventionHeaders = getFraudPreventionHeaders; +exports.getGovClientScreensHeader = getGovClientScreensHeader; exports.getScreenDetails = getScreenDetails; exports.windowDetails = windowDetails; diff --git a/tests/unit/hmrc/mtdFraudPrevention.test.js b/tests/unit/hmrc/mtdFraudPrevention.test.js index 46f3633..f99dd60 100644 --- a/tests/unit/hmrc/mtdFraudPrevention.test.js +++ b/tests/unit/hmrc/mtdFraudPrevention.test.js @@ -231,8 +231,7 @@ describe("getGovClientScreensHeader", () => { })); const {header, error} = await getGovClientScreensHeader() - expect(header.size).toBe(1); expect(error).toBe(undefined); - expect(header.get("Gov-Client-Screens")).toBe(`width=1019&height=1021&scaling-factor=2&colour-depth=17`); + expect(header).toBe(`width=1019&height=1021&scaling-factor=2&colour-depth=17`); }); }); From 88ac12fc36ae9ecf519c3ead09e4ce5b05ab7052 Mon Sep 17 00:00:00 2001 From: Riya John Date: Thu, 21 Oct 2021 11:19:26 +0530 Subject: [PATCH 4/9] fix: review comments --- USAGE.md | 8 +++++++- src/js/hmrc/mtdFraudPrevention.js | 7 +++---- tests/unit/hmrc/mtdFraudPrevention.test.js | 8 ++++---- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/USAGE.md b/USAGE.md index d6ec17d..e9783ad 100644 --- a/USAGE.md +++ b/USAGE.md @@ -22,4 +22,10 @@ const fraudHeaders = await getFraudPreventionHeaders(); const timezoneHeader = fraudHeaders.get('Gov-Client-Timezone'); // Or const timezoneHeader = fraudHeaders.get(fraudPreventionHeadersEnum.TIMEZONE); -``` \ No newline at end of file +``` + +* To get Gov-Client-Screens HMRC Fraud prevention header: + ```js + import getGovClientTimezoneHeader from 'user-data-for-fraud-prevention'; + const {headerValue, error} = getGovClientTimezoneHeader(); + ``` diff --git a/src/js/hmrc/mtdFraudPrevention.js b/src/js/hmrc/mtdFraudPrevention.js index 2dba3bd..1614090 100644 --- a/src/js/hmrc/mtdFraudPrevention.js +++ b/src/js/hmrc/mtdFraudPrevention.js @@ -105,9 +105,8 @@ export const getFraudPreventionHeaders = async () => { /** * Returns Gov-Client-Screens HMRC Fraud prevention header. - * @returns {object} with two fields headerValue and error if there is an error else undefined + * @returns {object} with headerValue field */ -export const getGovClientScreensHeader = async () => { - let header = await getScreenData() - return {header, undefined} +export const getGovClientScreensHeader = () => { + return {headerValue: getScreenData()}; } \ No newline at end of file diff --git a/tests/unit/hmrc/mtdFraudPrevention.test.js b/tests/unit/hmrc/mtdFraudPrevention.test.js index f99dd60..102d5fe 100644 --- a/tests/unit/hmrc/mtdFraudPrevention.test.js +++ b/tests/unit/hmrc/mtdFraudPrevention.test.js @@ -206,7 +206,7 @@ describe("FraudPreventionHeaders", () => { }); -describe("getGovClientScreensHeader", () => { +describe("GovClientScreensHeader", () => { let screenSpy, windowSpy; beforeEach(() => { @@ -219,7 +219,7 @@ describe("getGovClientScreensHeader", () => { windowSpy.mockRestore(); }); - it("no error", async () => { + it("no error", () => { screenSpy.mockImplementation(() => ({ width: 1019, height: 1021, @@ -230,8 +230,8 @@ describe("getGovClientScreensHeader", () => { devicePixelRatio: 2, })); - const {header, error} = await getGovClientScreensHeader() + const {headerValue, error} = getGovClientScreensHeader() expect(error).toBe(undefined); - expect(header).toBe(`width=1019&height=1021&scaling-factor=2&colour-depth=17`); + expect(headerValue).toBe(`width=1019&height=1021&scaling-factor=2&colour-depth=17`); }); }); From 9edc6e179dbae39107402628da0fa3285fa3420d Mon Sep 17 00:00:00 2001 From: Riya John Date: Fri, 22 Oct 2021 09:31:55 +0530 Subject: [PATCH 5/9] fix: review changes --- src/js/hmrc/mtdFraudPrevention.js | 8 ++++++-- tests/unit/hmrc/mtdFraudPrevention.test.js | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/js/hmrc/mtdFraudPrevention.js b/src/js/hmrc/mtdFraudPrevention.js index 0eba6d9..acaa261 100644 --- a/src/js/hmrc/mtdFraudPrevention.js +++ b/src/js/hmrc/mtdFraudPrevention.js @@ -127,8 +127,12 @@ export const getGovClientBrowserPluginsHeader = () => { /** * Returns Gov-Client-Screens HMRC Fraud prevention header. - * @returns {object} with headerValue field + * @returns {object} with headerValue field and error if an error is caught */ export const getGovClientScreensHeader = () => { - return {headerValue: getScreenData()}; + try { + return {headerValue: getScreenData()}; + } 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 5eb279e..5f207c4 100644 --- a/tests/unit/hmrc/mtdFraudPrevention.test.js +++ b/tests/unit/hmrc/mtdFraudPrevention.test.js @@ -261,7 +261,7 @@ describe("GovClientScreensHeader", () => { windowSpy.mockRestore(); }); - it("no error", () => { + it("returns correct headerValue when there is no error", () => { screenSpy.mockImplementation(() => ({ width: 1019, height: 1021, @@ -272,8 +272,20 @@ describe("GovClientScreensHeader", () => { devicePixelRatio: 2, })); - const {headerValue, error} = getGovClientScreensHeader() + const {headerValue, error} = getGovClientScreensHeader(); expect(error).toBe(undefined); expect(headerValue).toBe(`width=1019&height=1021&scaling-factor=2&colour-depth=17`); }); + + it("returns error when there is an error", () => { + screenSpy.mockImplementation(() => {throw Error("Something went wrong.")}); + + windowSpy.mockImplementation(() => ({ + devicePixelRatio: 2, + })); + + const {headerValue, error} = getGovClientScreensHeader(); + expect(error).toEqual(Error("Something went wrong.")); + expect(headerValue).toBe(undefined); + }); }); From 67785d9d28ff78ba33f17e63c4319b03f6e6b1eb Mon Sep 17 00:00:00 2001 From: Riya John Date: Sun, 24 Oct 2021 22:49:46 +0530 Subject: [PATCH 6/9] fix: review --- USAGE.md | 5 ++--- src/js/hmrc/mtdFraudPrevention.js | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/USAGE.md b/USAGE.md index db19eea..ca5a511 100644 --- a/USAGE.md +++ b/USAGE.md @@ -48,7 +48,6 @@ import getGovClientBrowserDoNotTrackHeader from 'user-data-for-fraud-prevention' const {headerValue, error} = getGovClientBrowserDoNotTrackHeader(); ``` - * To get Gov-Client-Timezone HMRC Fraud prevention header: ```js import getGovClientTimezoneHeader from 'user-data-for-fraud-prevention'; @@ -57,6 +56,6 @@ const {headerValue, error} = getGovClientBrowserDoNotTrackHeader(); * To get Gov-Client-Screens HMRC Fraud prevention header: ```js - import getGovClientTimezoneHeader from 'user-data-for-fraud-prevention'; - const {headerValue, error} = getGovClientTimezoneHeader(); + import getGovClientScreensHeader from 'user-data-for-fraud-prevention'; + const {headerValue, error} = getGovClientScreensHeader(); ``` diff --git a/src/js/hmrc/mtdFraudPrevention.js b/src/js/hmrc/mtdFraudPrevention.js index 5c025ca..0f82440 100644 --- a/src/js/hmrc/mtdFraudPrevention.js +++ b/src/js/hmrc/mtdFraudPrevention.js @@ -159,7 +159,7 @@ export const getGovClientTimezoneHeader = () => { } /** - * Returns Gov-Client-Screens HMRC Fraud prevention header. + * Returns the value for Gov-Client-Screens HMRC Fraud prevention header. * @returns {object} with headerValue field and error if an error is caught */ export const getGovClientScreensHeader = () => { From 13d43465358f6a513d774ad42427d5283a3fd19e Mon Sep 17 00:00:00 2001 From: Riya John Date: Tue, 26 Oct 2021 13:35:04 +0530 Subject: [PATCH 7/9] fix: comment --- src/js/hmrc/mtdFraudPrevention.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/hmrc/mtdFraudPrevention.js b/src/js/hmrc/mtdFraudPrevention.js index 767841a..576dfd9 100644 --- a/src/js/hmrc/mtdFraudPrevention.js +++ b/src/js/hmrc/mtdFraudPrevention.js @@ -174,7 +174,7 @@ export const getGovClientLocalIPsHeader = async () => { } /** * Returns the value for Gov-Client-Screens HMRC Fraud prevention header. - * @returns {object} with headerValue field and error if an error is caught + * @returns {object} with headerValue key having the value of the header or error key if there is an error */ export const getGovClientScreensHeader = () => { try { From afe8691b8965d7f38490db83c8bdd29fd4143559 Mon Sep 17 00:00:00 2001 From: Riya John Date: Fri, 29 Oct 2021 15:29:48 +0530 Subject: [PATCH 8/9] fix: review --- USAGE.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/USAGE.md b/USAGE.md index 3afabf6..3f91804 100644 --- a/USAGE.md +++ b/USAGE.md @@ -67,7 +67,7 @@ const {headerValue, error} = getGovClientWindowSizeHeader(); ``` * To get Gov-Client-Screens HMRC Fraud prevention header: - ```js - import {getGovClientScreensHeader} from 'user-data-for-fraud-prevention'; - const {headerValue, error} = getGovClientScreensHeader(); - ``` +```js +import {getGovClientScreensHeader} from 'user-data-for-fraud-prevention'; +const {headerValue, error} = getGovClientScreensHeader(); +``` From 127c7239af456933271b6caeebf4772fb8b6d6f6 Mon Sep 17 00:00:00 2001 From: Riya John Date: Sat, 30 Oct 2021 14:24:49 +0530 Subject: [PATCH 9/9] fix: add to ts file --- src/js/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/js/index.ts b/src/js/index.ts index d4b2637..77857e3 100644 --- a/src/js/index.ts +++ b/src/js/index.ts @@ -9,5 +9,6 @@ export { getGovClientDeviceID, getGovClientTimezoneHeader, getGovClientLocalIPsHeader, - getGovClientWindowSizeHeader + getGovClientWindowSizeHeader, + getGovClientScreensHeader } from './hmrc/mtdFraudPrevention';