Skip to content

Commit

Permalink
Remove custom cookie policies on initialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosgood committed Jan 28, 2025
1 parent 3602a1f commit 8347dba
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/nationalarchives/tna-frontend/compare/v0.11.1...HEAD)
## [Unreleased](https://github.com/nationalarchives/tna-frontend/compare/v0.11.2...HEAD)

### Added
### Changed
Expand All @@ -14,6 +14,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
### Security

## [0.11.2](https://github.com/nationalarchives/tna-frontend/compare/v0.11.1...v0.11.2) - 2025-01-28

### Changed

- All custom cookie policies (other than `usage`, `settings`, `marketing` and `essential`) are filtered out upon initialisation
- Small font size tweak to the strapline in a header component on tiny devices

## [0.11.1](https://github.com/nationalarchives/tna-frontend/compare/v0.11.0...v0.11.1) - 2025-01-28

### Fixed
Expand Down
15 changes: 12 additions & 3 deletions src/nationalarchives/lib/cookies.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export class CookieEventHandler {
}
}

const tnaCookiePolicies = ["usage", "settings", "marketing", "essential"];

/**
* Class to handle cookies.
* @class Cookies
Expand Down Expand Up @@ -126,8 +128,8 @@ export default class Cookies {
}
this.events = new CookieEventHandler();
this.completePoliciesOnInit =
Object.keys(this.policies).length === 4 &&
["usage", "settings", "marketing", "essential"].every(
Object.keys(this.policies).length === tnaCookiePolicies.length &&
tnaCookiePolicies.every(
(policy) =>
Object.keys(this.policies).includes(policy) &&
typeof this.policies[policy] === "boolean",
Expand All @@ -140,11 +142,18 @@ export default class Cookies {

/** @protected */
init() {
const existingPolicies = this.policies;
const filteredExistingPolicies = Object.fromEntries(
Object.keys(existingPolicies)
.filter((policy) => tnaCookiePolicies.includes(policy))
.map((policy) => [policy, existingPolicies[policy]]),
);
console.log(existingPolicies, filteredExistingPolicies);
this.savePolicies({
usage: false,
settings: false,
marketing: false,
...this.policies,
...filteredExistingPolicies,
essential: true,
});
}
Expand Down
24 changes: 24 additions & 0 deletions test/cookies.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,30 @@ describe("Existing partial cookie policies", () => {
});
});

describe("Existing unknown cookie policies", () => {
beforeEach(() => {
document.clearAllCookies();
document.cookie = "cookies_policy=%7B%22custom%22%3Atrue%7D";
new Cookies().destroyInstance();
});

test("Initialisation", async () => {
const cookies = new Cookies();

expect(cookies.completePoliciesOnInit).toEqual(false);
expect(cookies.all).toHaveProperty("cookies_policy");
expect(cookies.policies).toHaveProperty("essential");
expect(cookies.isPolicyAccepted("essential")).toEqual(true);
expect(cookies.policies).toHaveProperty("settings");
expect(cookies.isPolicyAccepted("settings")).toEqual(false);
expect(cookies.policies).toHaveProperty("usage");
expect(cookies.isPolicyAccepted("usage")).toEqual(false);
expect(cookies.policies).toHaveProperty("marketing");
expect(cookies.isPolicyAccepted("marketing")).toEqual(false);
expect(cookies.policies).not.toHaveProperty("custom");
});
});

describe("Existing malformed cookie policies", () => {
beforeEach(() => {
document.clearAllCookies();
Expand Down

0 comments on commit 8347dba

Please # to comment.