Skip to content

Commit cfe62fa

Browse files
authored
Merge pull request #145 from boscop-fr/fix-cookie-deletion
Fixed cookie deletion
2 parents 31c53f2 + 0c396c2 commit cfe62fa

5 files changed

Lines changed: 82 additions & 18 deletions

File tree

e2e/OrejimePage.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import {expect, BrowserContext, Page, Locator} from '@playwright/test';
1+
import {BrowserContext, expect, Page} from '@playwright/test';
22
import Cookie from 'js-cookie';
3+
import {OrejimeInstance} from '../src/setup';
34
import {Config} from '../src/ui/types';
45

6+
declare global {
7+
interface Window {
8+
orejimeConfig: Partial<Config>;
9+
orejime: OrejimeInstance;
10+
}
11+
}
12+
513
export class OrejimePage {
614
constructor(
715
public readonly page: Page,
@@ -80,6 +88,12 @@ export class OrejimePage {
8088
await this.page.getByTestId('orejime-banner-decline').click();
8189
}
8290

91+
async acceptAllFromManager() {
92+
return await this.page.evaluate(() => {
93+
window.orejime.manager.acceptAll();
94+
});
95+
}
96+
8397
async openModalFromBanner() {
8498
await this.learnMoreBannerButton.click();
8599
}
@@ -119,15 +133,34 @@ export class OrejimePage {
119133
await this.page.getByTestId('orejime-contextual-notice-accept').click();
120134
}
121135

136+
async expectUndefinedConsents() {
137+
await expect(await this.getConsentsFromCookies()).toBeUndefined();
138+
}
139+
140+
async expectAnyConsents() {
141+
await expect(await this.getConsentsFromCookies()).not.toBeUndefined();
142+
}
143+
122144
async expectConsents(consents: Record<string, unknown>) {
123145
await expect(await this.getConsentsFromCookies()).toEqual(consents);
124146
}
125147

126148
async getConsentsFromCookies() {
127149
const name = 'eu-consent';
128150
const cookies = await this.context.cookies();
129-
const {value} = cookies.find((cookie) => cookie.name === name)!;
130-
return JSON.parse(Cookie.converter.read(value, name));
151+
const cookie = cookies.find((cookie) => cookie.name === name)!;
152+
153+
if (!cookie) {
154+
return undefined;
155+
}
156+
157+
return JSON.parse(Cookie.converter.read(cookie.value, cookie.name));
158+
}
159+
160+
async clearConsents() {
161+
return await this.page.evaluate(() => {
162+
window.orejime.manager.clearConsents();
163+
});
131164
}
132165

133166
// In specific conditions, browser events can get queued

e2e/orejime.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ test.describe('Orejime', () => {
255255
const position2 = await orejimePage.banner.boundingBox();
256256
await expect(position2).toEqual(initalPosition);
257257
});
258+
259+
test('should clear consents', async () => {
260+
await orejimePage.expectUndefinedConsents();
261+
await orejimePage.acceptAllFromManager();
262+
await orejimePage.expectAnyConsents();
263+
await orejimePage.clearConsents();
264+
await orejimePage.expectUndefinedConsents();
265+
});
258266
});
259267

260268
test.describe('Orejime with forced banner', () => {

src/core/utils/cookies.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Cookie from 'js-cookie';
2+
import {listSubdomains} from './urls';
23

34
export type CookieSameSite = 'strict' | 'lax' | 'none';
45

@@ -24,24 +25,15 @@ export const setCookie = (
2425
});
2526
};
2627

27-
export const deleteCookie = (name: string, path?: string, domain?: string) => {
28-
if (domain) {
28+
export const deleteCookie = (name: string, path = '/', domain?: string) => {
29+
// If no domain is specified, we try deleting the cookie
30+
// on the current domain or any of its subdomains.
31+
const domains = domain ? [domain] : listSubdomains(location.hostname);
32+
33+
domains.forEach((domain) => {
2934
Cookie.remove(name, {
3035
path,
3136
domain
3237
});
33-
34-
return;
35-
}
36-
37-
// if domain is not defined, try to delete cookie on multiple default domains
38-
Cookie.remove(name, {
39-
path,
40-
domain: location.hostname
41-
});
42-
43-
Cookie.remove(name, {
44-
path,
45-
domain: location.hostname.split('.').slice(-2).join('.')
4638
});
4739
};

src/core/utils/urls.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {listSubdomains} from './urls';
2+
3+
test('listSubdomains', () => {
4+
expect(listSubdomains('localhost')).toEqual(
5+
expect.arrayContaining(['localhost'])
6+
);
7+
8+
expect(listSubdomains('foo.com')).toEqual(
9+
expect.arrayContaining(['foo.com'])
10+
);
11+
12+
expect(listSubdomains('foo.bar.com')).toEqual(
13+
expect.arrayContaining(['bar.com', 'foo.bar.com'])
14+
);
15+
16+
expect(listSubdomains('foo.bar.baz.com')).toEqual(
17+
expect.arrayContaining(['baz.com', 'bar.baz.com', 'foo.bar.baz.com'])
18+
);
19+
});

src/core/utils/urls.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export const listSubdomains = (hostname: string) => {
2+
const parts = hostname.split('.');
3+
const primary = parts.slice(-2);
4+
const subs = parts.slice(0, -2);
5+
const all = [primary];
6+
7+
for (let i = 0; i < subs.length; i++) {
8+
all.push(subs.slice(i).concat(primary));
9+
}
10+
11+
return all.map((parts) => parts.join('.'));
12+
};

0 commit comments

Comments
 (0)