Skip to content

Commit

Permalink
Merge pull request #149 from RiyaJohn/screen-hdr
Browse files Browse the repository at this point in the history
Fixes #138 Export screen details header for HMRC (Gov-Client-Screens)
  • Loading branch information
reubenae authored Oct 30, 2021
2 parents 0dd9973 + 127c723 commit 02491b1
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
10 changes: 8 additions & 2 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ const {headerValue, error} = getGovClientBrowserDoNotTrackHeader();

* To get Gov-Client-Timezone HMRC Fraud prevention header:
```js
import getGovClientTimezoneHeader from 'user-data-for-fraud-prevention';
import {getGovClientTimezoneHeader} from 'user-data-for-fraud-prevention';
const {headerValue, error} = getGovClientTimezoneHeader();
```

* To get Gov-Client-Local-IPs HMRC Fraud prevention header:
```js
import getGovClientLocalIPsHeader from 'user-data-for-fraud-prevention';
import {getGovClientLocalIPsHeader} from 'user-data-for-fraud-prevention';
const {headerValue, error} = await getGovClientLocalIPsHeader();
```

Expand All @@ -65,3 +65,9 @@ const {headerValue, error} = await getGovClientLocalIPsHeader();
import {getGovClientWindowSizeHeader} from 'user-data-for-fraud-prevention';
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();
```
12 changes: 12 additions & 0 deletions src/js/hmrc/mtdFraudPrevention.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,15 @@ export const getGovClientWindowSizeHeader = () => {
};



/**
* Returns the value for Gov-Client-Screens HMRC Fraud prevention header.
* @returns {object} with headerValue key having the value of the header or error key if there is an error
*/
export const getGovClientScreensHeader = () => {
try {
return {headerValue: getScreenData()};
} catch (error) {
return {error};
}
}
3 changes: 2 additions & 1 deletion src/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export {
getGovClientDeviceID,
getGovClientTimezoneHeader,
getGovClientLocalIPsHeader,
getGovClientWindowSizeHeader
getGovClientWindowSizeHeader,
getGovClientScreensHeader
} from './hmrc/mtdFraudPrevention';
43 changes: 43 additions & 0 deletions tests/unit/hmrc/mtdFraudPrevention.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {getGovClientDeviceID} from "../../../src/js/hmrc/mtdFraudPrevention";
import {getGovClientBrowserDoNotTrackHeader} from "../../../src/js/hmrc/mtdFraudPrevention";
import {getGovClientTimezoneHeader} from "../../../src/js/hmrc/mtdFraudPrevention";
import {getGovClientWindowSizeHeader} from "../../../src/js/hmrc/mtdFraudPrevention"
import {getGovClientScreensHeader} from "../../../src/js/hmrc/mtdFraudPrevention";

describe("FraudPreventionHeaders", () => {
resetDeviceIpString();
Expand Down Expand Up @@ -372,3 +373,45 @@ describe("getGovClientWindowSizeHeader", () => {




describe("GovClientScreensHeader", () => {
let screenSpy, windowSpy;

beforeEach(() => {
screenSpy = jest.spyOn(global, 'screen', 'get');
windowSpy = jest.spyOn(global, 'window', 'get');
});

afterEach(() => {
screenSpy.mockRestore();
windowSpy.mockRestore();
});

it("returns correct headerValue when there is no error", () => {
screenSpy.mockImplementation(() => ({
width: 1019,
height: 1021,
colorDepth: 17,
}));

windowSpy.mockImplementation(() => ({
devicePixelRatio: 2,
}));

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);
});
});

0 comments on commit 02491b1

Please sign in to comment.