Skip to content

Commit

Permalink
Merge pull request #719 from imnasnainaec/eraseCookies-tests
Browse files Browse the repository at this point in the history
Fix eraseCookies domain bug and expand tests
  • Loading branch information
orestbida authored Jan 16, 2025
2 parents e7ad799 + bebdd6f commit 8cc9bb7
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 20 deletions.
2 changes: 1 addition & 1 deletion dist/cookieconsent.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/cookieconsent.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/core/cookieconsent-core.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/core/cookieconsent-core.umd.js

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions docs/reference/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,20 +269,28 @@ Removes one or multiple cookies.
): void
```
- **Examples** <br>
- **Details**
Delete the plugin's own cookie
This function uses `document.cookie` to expire cookies.
According to the [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie#write_a_new_cookie):
"The domain _must_ match the domain of the JavaScript origin. Setting cookies to foreign domains will be silently ignored."
- **Examples**
Delete the plugin's own cookie:
```javascript
CookieConsent.eraseCookies('cc_cookie');
```

Delete the `_gid` and all cookies starting with `_ga`:
```javascript
CookieConsent.eraseCookies(['_gid', /^_ga/], '/', location.hostname);
CookieConsent.eraseCookies(['_gid', /^_ga/]);
```


Delete all cookies except the plugin's own cookie:
```javascript
CookieConsent.eraseCookies(/^(?!cc_cookie$)/);
```
## loadScript
Expand Down
15 changes: 9 additions & 6 deletions src/utils/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,21 +316,24 @@ export const eraseCookiesHelper = (cookies, customPath, customDomain) => {
* @param {string} [domain]
*/
const erase = (cookie, domain) => {
if (domain && domain.slice(0, 1) !== '.')
domain = '.' + domain;
document.cookie = cookie + '='
+ '; path=' + path
+ (domain ? '; domain=.' + domain : '')
+ (domain ? '; domain=' + domain : '')
+ '; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
};

for (const cookieName of cookies) {
erase(cookieName, customDomain);

/**
* 2 attempts to erase the cookie:
* - without domain
* - with domain
* If custom domain not specified,
* also erase config domain
*/
erase(cookieName);
erase(cookieName, domain);
if (!customDomain) {
erase(cookieName, domain);
}

/**
* If domain starts with 'www.',
Expand Down
23 changes: 22 additions & 1 deletion tests/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,16 @@ describe("API tests", () => {

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

it('Should erase cookie by regex', () => {
document.cookie = 'test_cookie1=21; expires=Sun, 1 Jan 2063 00:00:00 UTC; path=/';
document.cookie = 'test_cookie2=21; expires=Sun, 1 Jan 2063 00:00:00 UTC; path=/';
expect(api.validCookie('test_cookie1')).toBe(true);
expect(api.validCookie('test_cookie2')).toBe(true);
api.eraseCookies(/^test_cookie/);
expect(api.validCookie('test_cookie1')).toBe(false);
expect(api.validCookie('test_cookie2')).toBe(false);
Expand All @@ -171,18 +174,36 @@ describe("API tests", () => {
document.cookie = 'test_cookie1=21; expires=Sun, 1 Jan 2063 00:00:00 UTC; path=/';
document.cookie = 'test_cookie2=21; expires=Sun, 1 Jan 2063 00:00:00 UTC; path=/';
document.cookie = 'new_cookie=21; expires=Sun, 1 Jan 2063 00:00:00 UTC; path=/';
expect(api.validCookie('test_cookie1')).toBe(true);
expect(api.validCookie('test_cookie2')).toBe(true);
expect(api.validCookie('new_cookie')).toBe(true);
api.eraseCookies([/^test_cookie/, 'new_cookie']);
expect(api.validCookie('test_cookie1')).toBe(false);
expect(api.validCookie('test_cookie2')).toBe(false);
expect(api.validCookie('new_cookie')).toBe(false);
})

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

it('Should not erase cookie with wrong path', () => {
document.cookie = 'test_cookie6=28; expires=Mon, 1 Jan 2064 00:00:00 UTC; path=/; domain='+location.host;
expect(api.validCookie('test_cookie6')).toBe(true);
api.eraseCookies('test_cookie6', '/other', location.host);
expect(api.validCookie('test_cookie6')).toBe(true);
});

it('Should not erase cookie with wrong domain', () => {
document.cookie = 'test_cookie7=35; expires=Wed, 1 Jan 2065 00:00:00 UTC; path=/; domain='+location.host;
expect(api.validCookie('test_cookie7')).toBe(true);
api.eraseCookies('test_cookie7', '/', 'wrong.domain');
expect(api.validCookie('test_cookie7')).toBe(true);
});

it('Should show the consent modal', async () => {
api.reset(true);
testConfig.autoShow = false;
Expand Down
15 changes: 10 additions & 5 deletions tests/cookies.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@ describe("Cookie should be created successfully", () => {
});
});

it('Should erase cookie', () => {
setCookie('test_cookie', '{"ciao": 21}');
eraseCookiesHelper(['test_cookie'], '/', [location.host]);
const ccCookie = getSingleCookie('test_cookie');
expect(ccCookie).toBeFalsy();
it('Should erase cookies', () => {
const name1 = 'test_cookie1';
const name2 = 'test_cookie2';
setCookie(name1, '{"ciao": 11}');
setCookie(name2, '{"aloha": 22}');
expect(getSingleCookie(name1)).toBeTruthy();
expect(getSingleCookie(name2)).toBeTruthy();
eraseCookiesHelper([name1, name2]);
expect(getSingleCookie(name1)).toBeFalsy();
expect(getSingleCookie(name2)).toBeFalsy();
});

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

0 comments on commit 8cc9bb7

Please sign in to comment.