Skip to content

Commit

Permalink
feat: Adding getGovClientBrowserPluginsHeader function (#147)
Browse files Browse the repository at this point in the history
* 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 <Ankush_Sharma@intuit.com>
  • Loading branch information
ankush2805 and Sharma, Ankush committed Oct 19, 2021
1 parent e5f2638 commit 4a4e8e4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
11 changes: 9 additions & 2 deletions USAGE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Usage
## Usage

In your project root run:

Expand All @@ -22,4 +22,11 @@ const fraudHeaders = await getFraudPreventionHeaders();
const timezoneHeader = fraudHeaders.get('Gov-Client-Timezone');
// Or
const timezoneHeader = fraudHeaders.get(fraudPreventionHeadersEnum.TIMEZONE);
```
```

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();
```
11 changes: 11 additions & 0 deletions src/js/hmrc/mtdFraudPrevention.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};
}
}
2 changes: 2 additions & 0 deletions src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
29 changes: 29 additions & 0 deletions tests/unit/hmrc/mtdFraudPrevention.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
});

});

0 comments on commit 4a4e8e4

Please sign in to comment.