Skip to content

Commit

Permalink
Issue 145:Export browser user-agent details header for HMRC (Gov-Clie…
Browse files Browse the repository at this point in the history
…nt-Browser-JS-User-Agent (#154)

* feat(header): implement getGovClientBrowserHeader

* test(header): unit tests for getGovClientBrowserHeader

* docs(usage): updated markdown for individual header function

* style(header): formatting

* docs(usage.md): documentation for header function

Co-authored-by: Susmitha Kodamarthi <f2004156@gmail.com>

* style(header): formatting

Co-authored-by: Susmitha Kodamarthi <f2004156@gmail.com>

* refactor(test): assertion

Co-authored-by: Susmitha Kodamarthi <f2004156@gmail.com>

* refactor(test): assertion

Co-authored-by: Susmitha Kodamarthi <f2004156@gmail.com>

* docs(usage.md): update markdown

* style(test): formatting

* test(header): restore navigator mocks

* style(index): semi-colon

* style(usage.md): formatting

* docs(usage): Update USAGE.md

Co-authored-by: Susmitha Kodamarthi <f2004156@gmail.com>

* refactor(header): Update function name

Co-authored-by: Susmitha Kodamarthi <f2004156@gmail.com>

* docs(usage): Update USAGE.md

Co-authored-by: Susmitha Kodamarthi <f2004156@gmail.com>

* refactor(header): function name

Co-authored-by: Susmitha Kodamarthi <f2004156@gmail.com>
  • Loading branch information
anuragb26 and skodamarthi authored Oct 21, 2021
1 parent c7a8187 commit 28c41d6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
6 changes: 5 additions & 1 deletion USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ If you want only a specific header value, then you can use below functions that
import getGovClientBrowserPluginsHeader from 'user-data-for-fraud-prevention';
const {headerValue, error} = getGovClientBrowserPluginsHeader();
```

* To get Gov-Client-Browser-JS-User-Agent HMRC Fraud prevention header:
```js
import {getGovClientBrowserJSUserAgentHeader} from 'user-data-for-fraud-prevention';
const { headerValue, error } = getGovClientBrowserJSUserAgentHeader();
```
* To get Gov-Client-Device Id HMRC Fraud prevention header:
```js
import getGovClientDeviceID from 'user-data-for-fraud-prevention';
Expand Down
12 changes: 11 additions & 1 deletion src/js/hmrc/mtdFraudPrevention.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,23 @@ export const getFraudPreventionHeaders = async () => {
const ipAddress = await getDeviceLocalIPAsString();
headers.set(fraudPreventionHeadersEnum.DEVICE_LOCAL_IPS, encodeURI(ipAddress.deviceIpString));
headers.set(fraudPreventionHeadersEnum.DEVICE_LOCAL_IPS_TIMESTAMP, ipAddress.deviceIpTimeStamp);

} catch (error) {
errors.push(error);
}

return { headers, errors };
};
/**
* Returns "Gov-Client-Browser-JS-User-Agent" header.
* @returns {object} which has headerValue key having the value of the header or error key if there is an error
*/
export const getGovClientBrowserJSUserAgentHeader = () => {
try {
return { headerValue: getUserAgent() };
} catch (error) {
return { error };
}
}

/**
* Returns the value for Gov-Client-Device-ID HMRC Fraud prevention header.
Expand Down
5 changes: 4 additions & 1 deletion src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getFraudPreventionHeaders,
getScreenDetails,
windowDetails,
getGovClientBrowserJSUserAgentHeader,
getGovClientBrowserPluginsHeader,
getGovClientDeviceID
} from "./hmrc/mtdFraudPrevention";
Expand All @@ -12,4 +13,6 @@ exports.getFraudPreventionHeaders = getFraudPreventionHeaders;
exports.getGovClientBrowserPluginsHeader = getGovClientBrowserPluginsHeader;
exports.getGovClientDeviceID = getGovClientDeviceID;
exports.getScreenDetails = getScreenDetails;
exports.windowDetails = windowDetails;
exports.getGovClientBrowserJSUserAgentHeader = getGovClientBrowserJSUserAgentHeader;
exports.windowDetails = windowDetails;

17 changes: 16 additions & 1 deletion tests/unit/hmrc/mtdFraudPrevention.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getFraudPreventionHeaders,
getScreenDetails,
windowDetails,
getGovClientBrowserJSUserAgentHeader,
} from "../../../src/js";
import {
MockRTCPeerConnection,
Expand Down Expand Up @@ -204,7 +205,21 @@ describe("FraudPreventionHeaders", () => {
it("width", () => expect(windowDetails().width).toBe(1009));
it("height", () => expect(windowDetails().height).toBe(1013));
});

describe("getGovClientBrowserHeader", () => {
it("returns correct headerValue when there is no error", () => {
const userAgent = "Mozilla/5.0"
navigatorSpy.mockImplementationOnce(() => ({
userAgent
}));
expect(getGovClientBrowserJSUserAgentHeader()).toEqual({ headerValue: userAgent })
navigatorSpy.mockRestore();
})
it("returns error when there is an error", () => {
navigatorSpy.mockImplementationOnce(() => null);
expect(getGovClientBrowserJSUserAgentHeader().error.toString()).toEqual("TypeError: Cannot read property 'userAgent' of null")
navigatorSpy.mockRestore();
})
})
});

describe("getGovClientBrowserPluginsHeader", () => {
Expand Down

0 comments on commit 28c41d6

Please sign in to comment.