Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to "inject" location into /fides.js bundles and cache responses for one hour #3272

Merged
merged 11 commits into from
May 10, 2023
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The types of changes are:
- Privacy Experience Bulk Create, Bulk Update, and Detail Endpoints [#3185](https://github.com/ethyca/fides/pull/3185)
- Initial privacy experience UI [#3186](https://github.com/ethyca/fides/pull/3186)
- Access and erasure support for OneSignal [#3199](https://github.com/ethyca/fides/pull/3199)
- Add the ability to "inject" location into `/fides.js` bundles and cache responses for one hour [#3272](https://github.com/ethyca/fides/pull/3272)

### Changed

Expand Down
138 changes: 138 additions & 0 deletions clients/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 108 additions & 0 deletions clients/privacy-center/__tests__/common/location.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { createRequest } from "node-mocks-http";

import { getLocation } from "~/common/location";

describe("getLocation", () => {
describe("when using location headers", () => {
it("returns location data from country & region headers", () => {
const req = createRequest({
url: "https://privacy.example.com/fides.js",
headers: {
"CloudFront-Viewer-Country": "US",
NevilleS marked this conversation as resolved.
Show resolved Hide resolved
"CloudFront-Viewer-Country-Region": "NY",
},
});
const location = getLocation(req);
expect(location).toEqual({
country: "US",
location: "US-NY",
region: "NY",
});
});

it("returns location data from country header", () => {
const req = createRequest({
url: "https://privacy.example.com/fides.js",
headers: {
"CloudFront-Viewer-Country": "FR",
},
});
const location = getLocation(req);
expect(location).toEqual({
country: "FR",
location: "FR",
});
});

it("ignores only region headers", () => {
const req = createRequest({
url: "https://privacy.example.com/fides.js",
headers: {
"CloudFront-Viewer-Country-Region": "NY",
},
});
const location = getLocation(req);
expect(location).toBeUndefined();
});

it("handles invalid location headers", () => {
const req = createRequest({
url: "https://privacy.example.com/fides.js",
headers: {
"CloudFront-Viewer-Country": "Magicland",
},
});
const location = getLocation(req);
expect(location).toBeUndefined();
});
});

describe("when using ?location query param", () => {
it("returns location data from query param", () => {
const req = createRequest({
url: "https://privacy.example.com/fides.js?location=FR-IDF",
});
const location = getLocation(req);
expect(location).toEqual({
country: "FR",
location: "FR-IDF",
region: "IDF",
});
});

it("handles invalid location query param", () => {
const req = createRequest({
url: "https://privacy.example.com/fides.js?location=America",
});
const location = getLocation(req);
expect(location).toBeUndefined();
});
});

describe("when using both headers and query param", () => {
it("overrides headers with explicit location query param", () => {
const req = createRequest({
url: "https://privacy.example.com/fides.js?location=US-CA",
headers: {
"CloudFront-Viewer-Country": "FR",
},
});
const location = getLocation(req);
expect(location).toEqual({
country: "US",
location: "US-CA",
region: "CA",
});
});
});

describe("when using neither headers nor query param", () => {
it("returns undefined location", () => {
const req = createRequest({
url: "https://privacy.example.com/fides.js",
});
const location = getLocation(req);
expect(location).toBeUndefined();
});
});
});
Loading