Skip to content

Commit 8cc9bb7

Browse files
authored
Merge pull request #719 from imnasnainaec/eraseCookies-tests
Fix eraseCookies domain bug and expand tests
2 parents e7ad799 + bebdd6f commit 8cc9bb7

8 files changed

+57
-20
lines changed

dist/cookieconsent.esm.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/cookieconsent.umd.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core/cookieconsent-core.esm.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core/cookieconsent-core.umd.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/api-reference.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -269,20 +269,28 @@ Removes one or multiple cookies.
269269
): void
270270
```
271271
272-
- **Examples** <br>
272+
- **Details**
273273
274-
Delete the plugin's own cookie
274+
This function uses `document.cookie` to expire cookies.
275+
According to the [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie#write_a_new_cookie):
276+
"The domain _must_ match the domain of the JavaScript origin. Setting cookies to foreign domains will be silently ignored."
277+
278+
- **Examples**
275279
280+
Delete the plugin's own cookie:
276281
```javascript
277282
CookieConsent.eraseCookies('cc_cookie');
278283
```
279284

280285
Delete the `_gid` and all cookies starting with `_ga`:
281286
```javascript
282-
CookieConsent.eraseCookies(['_gid', /^_ga/], '/', location.hostname);
287+
CookieConsent.eraseCookies(['_gid', /^_ga/]);
283288
```
284289

285-
290+
Delete all cookies except the plugin's own cookie:
291+
```javascript
292+
CookieConsent.eraseCookies(/^(?!cc_cookie$)/);
293+
```
286294
287295
## loadScript
288296

src/utils/cookies.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -316,21 +316,24 @@ export const eraseCookiesHelper = (cookies, customPath, customDomain) => {
316316
* @param {string} [domain]
317317
*/
318318
const erase = (cookie, domain) => {
319+
if (domain && domain.slice(0, 1) !== '.')
320+
domain = '.' + domain;
319321
document.cookie = cookie + '='
320322
+ '; path=' + path
321-
+ (domain ? '; domain=.' + domain : '')
323+
+ (domain ? '; domain=' + domain : '')
322324
+ '; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
323325
};
324326

325327
for (const cookieName of cookies) {
328+
erase(cookieName, customDomain);
326329

327330
/**
328-
* 2 attempts to erase the cookie:
329-
* - without domain
330-
* - with domain
331+
* If custom domain not specified,
332+
* also erase config domain
331333
*/
332-
erase(cookieName);
333-
erase(cookieName, domain);
334+
if (!customDomain) {
335+
erase(cookieName, domain);
336+
}
334337

335338
/**
336339
* If domain starts with 'www.',

tests/api.test.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,16 @@ describe("API tests", () => {
155155

156156
it('Should erase cookie by string', () => {
157157
document.cookie = 'test_cookie=21; expires=Sun, 1 Jan 2063 00:00:00 UTC; path=/';
158+
expect(api.validCookie('test_cookie')).toBe(true);
158159
api.eraseCookies('test_cookie');
159160
expect(api.validCookie('test_cookie')).toBe(false);
160161
})
161162

162163
it('Should erase cookie by regex', () => {
163164
document.cookie = 'test_cookie1=21; expires=Sun, 1 Jan 2063 00:00:00 UTC; path=/';
164165
document.cookie = 'test_cookie2=21; expires=Sun, 1 Jan 2063 00:00:00 UTC; path=/';
166+
expect(api.validCookie('test_cookie1')).toBe(true);
167+
expect(api.validCookie('test_cookie2')).toBe(true);
165168
api.eraseCookies(/^test_cookie/);
166169
expect(api.validCookie('test_cookie1')).toBe(false);
167170
expect(api.validCookie('test_cookie2')).toBe(false);
@@ -171,18 +174,36 @@ describe("API tests", () => {
171174
document.cookie = 'test_cookie1=21; expires=Sun, 1 Jan 2063 00:00:00 UTC; path=/';
172175
document.cookie = 'test_cookie2=21; expires=Sun, 1 Jan 2063 00:00:00 UTC; path=/';
173176
document.cookie = 'new_cookie=21; expires=Sun, 1 Jan 2063 00:00:00 UTC; path=/';
177+
expect(api.validCookie('test_cookie1')).toBe(true);
178+
expect(api.validCookie('test_cookie2')).toBe(true);
179+
expect(api.validCookie('new_cookie')).toBe(true);
174180
api.eraseCookies([/^test_cookie/, 'new_cookie']);
175181
expect(api.validCookie('test_cookie1')).toBe(false);
176182
expect(api.validCookie('test_cookie2')).toBe(false);
177183
expect(api.validCookie('new_cookie')).toBe(false);
178184
})
179185

180186
it('Should erase cookie with specific path and domain', () => {
181-
document.cookie = 'test_cookie5=21; expires=Sun, 1 Jan 2063 00:00:00 UTC; path=/ciao; domain='+location.host;
187+
document.cookie = 'test_cookie5=21; expires=Sun, 1 Jan 2063 00:00:00 UTC; path=/; domain='+location.host;
188+
expect(api.validCookie('test_cookie5')).toBe(true);
182189
api.eraseCookies('test_cookie5', '/', location.host);
183190
expect(api.validCookie('test_cookie5')).toBe(false);
184191
});
185192

193+
it('Should not erase cookie with wrong path', () => {
194+
document.cookie = 'test_cookie6=28; expires=Mon, 1 Jan 2064 00:00:00 UTC; path=/; domain='+location.host;
195+
expect(api.validCookie('test_cookie6')).toBe(true);
196+
api.eraseCookies('test_cookie6', '/other', location.host);
197+
expect(api.validCookie('test_cookie6')).toBe(true);
198+
});
199+
200+
it('Should not erase cookie with wrong domain', () => {
201+
document.cookie = 'test_cookie7=35; expires=Wed, 1 Jan 2065 00:00:00 UTC; path=/; domain='+location.host;
202+
expect(api.validCookie('test_cookie7')).toBe(true);
203+
api.eraseCookies('test_cookie7', '/', 'wrong.domain');
204+
expect(api.validCookie('test_cookie7')).toBe(true);
205+
});
206+
186207
it('Should show the consent modal', async () => {
187208
api.reset(true);
188209
testConfig.autoShow = false;

tests/cookies.test.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,16 @@ describe("Cookie should be created successfully", () => {
4141
});
4242
});
4343

44-
it('Should erase cookie', () => {
45-
setCookie('test_cookie', '{"ciao": 21}');
46-
eraseCookiesHelper(['test_cookie'], '/', [location.host]);
47-
const ccCookie = getSingleCookie('test_cookie');
48-
expect(ccCookie).toBeFalsy();
44+
it('Should erase cookies', () => {
45+
const name1 = 'test_cookie1';
46+
const name2 = 'test_cookie2';
47+
setCookie(name1, '{"ciao": 11}');
48+
setCookie(name2, '{"aloha": 22}');
49+
expect(getSingleCookie(name1)).toBeTruthy();
50+
expect(getSingleCookie(name2)).toBeTruthy();
51+
eraseCookiesHelper([name1, name2]);
52+
expect(getSingleCookie(name1)).toBeFalsy();
53+
expect(getSingleCookie(name2)).toBeFalsy();
4954
});
5055

5156
it('Should set the cookie', () => {

0 commit comments

Comments
 (0)