/* global global, jest */ import Cookies from "js-cookie"; import ConsentManager from "src/decidim/data_consent/consent_manager"; // Mock js-cookie so that we can store the return values from the "set" method // in order to inspect their flags. jest.mock("js-cookie", () => { const originalModule = jest.requireActual("js-cookie"); return { // ...originalModule, cookieStorage: {}, get: originalModule.get, set: function (name, value, options) { this.cookieStorage[name] = originalModule.set(name, value, options); } }; }); Reflect.deleteProperty(global.window, "location"); global.window = Object.create(window); global.window.location = { protocol: "https:", hostname: "decidim.dev" }; describe("ConsentManager", () => { const dialogContent = `
Information about the cookies used on the website
We use cookies on our website to improve the performance and content of the site. The cookies enable us to provide a more individual user experience and social media channels.
`; const modalContent = ` `; const cookieWarningContent = `

You need to enable all cookies in order to see this content.

Change cookie settings
`; const getCookie = (name) => { for (const [key, value] of Object.entries(Cookies.cookieStorage)) { if (key === name) { const cookie = {}; value.split(";").forEach((cookieString) => { const kv = cookieString.trim().split("="); const val = kv[1] || true; if (kv[0] === key) { cookie.value = val; } else { cookie[kv[0]] = val; } }); return cookie; } } return null; }; let manager = null; const originalLocation = window.location; beforeEach(() => { Cookies.cookieStorage = {}; document.body.innerHTML = dialogContent + modalContent + cookieWarningContent; const modal = document.querySelector("#dc-modal"); const categories = [...modal.querySelectorAll(".category-wrapper")].map((el) => el.dataset.id); manager = new ConsentManager({ modal: modal, categories: categories, cookieName: "decidim-consent", warningElement: document.querySelector("[data-dataconsent-warning]") }); }); afterEach(() => { window.location = originalLocation; }); describe("updateState", () => { let cookie = null; describe("when the current URL is secure", () => { beforeEach(() => { manager.updateState({ foo: "bar" }); cookie = getCookie("decidim-consent"); }); it("sets the cookie with the correct value", () => { expect(cookie.value).toEqual("{%22foo%22:%22bar%22}"); }); it("sets the cookie with the correct expiry", () => { const expiry = Date.parse(cookie.expires); const oneYearFromNow = Date.now() + 3600 * 24 * 1000 * 365; // Expect it to be within a second of the expected expiry time because // it would be pretty hard to try to guess the exact same millisecond. expect(oneYearFromNow - 1000).toBeLessThan(expiry); expect(oneYearFromNow + 1000).toBeGreaterThan(expiry); }); it("sets the cookie with the correct flags", () => { expect(cookie.domain).toEqual("decidim.dev"); expect(cookie.sameSite).toEqual("Lax"); expect(cookie.secure).toBe(true); }); }); describe("when the current URL is insecure", () => { beforeEach(() => { Reflect.defineProperty(window.location, "protocol", { value: "http:" }) manager.updateState({ foo: "bar" }); cookie = getCookie("decidim-consent"); }); it("does not set the secure flag", () => { expect(cookie.secure).toBeUndefined(); }); }); }); });