From 2776946627018dba812ba92f648a2eca578796d1 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Fri, 26 Mar 2021 01:09:56 -0400 Subject: [PATCH] tests: Use cookie libraries to manipulate cookies --- src/static/js/pad_cookie.js | 4 +++ src/tests/frontend/helper.js | 17 +++++------ src/tests/frontend/index.html | 1 + .../frontend/specs/authorship_of_editions.js | 4 +-- src/tests/frontend/specs/helper.js | 30 +++++++++++-------- src/tests/frontend/specs/language.js | 6 +--- ...ultiple_authors_clear_authorship_colors.js | 6 ++-- 7 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/static/js/pad_cookie.js b/src/static/js/pad_cookie.js index e4f748d45..0e946ea5c 100644 --- a/src/static/js/pad_cookie.js +++ b/src/static/js/pad_cookie.js @@ -63,4 +63,8 @@ exports.padcookie = new class { prefs[prefName] = value; this.writePrefs_(prefs); } + + clear() { + this.writePrefs_({}); + } }(); diff --git a/src/tests/frontend/helper.js b/src/tests/frontend/helper.js index 2a406a4f3..05c5f3347 100644 --- a/src/tests/frontend/helper.js +++ b/src/tests/frontend/helper.js @@ -51,22 +51,21 @@ const helper = {}; }; helper.clearSessionCookies = () => { - // Expire cookies, so author and language are changed after reloading the pad. See: - // https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie#example_4_reset_the_previous_cookie - window.document.cookie = 'token=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/'; - window.document.cookie = 'language=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/'; + window.Cookies.remove('token'); + window.Cookies.remove('language'); }; // Can only happen when the iframe exists, so we're doing it separately from other cookies helper.clearPadPrefCookie = () => { - helper.padChrome$.document.cookie = 'prefsHttp=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/'; + const {padcookie} = helper.padChrome$.window.require('ep_etherpad-lite/static/js/pad_cookie'); + padcookie.clear(); }; - // Overwrite all prefs in pad cookie. Assumes http, not https. + // Overwrite all prefs in pad cookie. helper.setPadPrefCookie = (prefs) => { - const val = encodeURIComponent(JSON.stringify(prefs)); - helper.padChrome$.document.cookie = - `prefsHttp=${val};expires=Thu, 01 Jan 3000 00:00:00 GMT; path=/`; + const {padcookie} = helper.padChrome$.window.require('ep_etherpad-lite/static/js/pad_cookie'); + padcookie.clear(); + for (const [key, value] of Object.entries(prefs)) padcookie.setPref(key, value); }; // Functionality for knowing what key event type is required for tests diff --git a/src/tests/frontend/index.html b/src/tests/frontend/index.html index e8a8012fb..3b0f1c6e0 100644 --- a/src/tests/frontend/index.html +++ b/src/tests/frontend/index.html @@ -11,6 +11,7 @@ + diff --git a/src/tests/frontend/specs/authorship_of_editions.js b/src/tests/frontend/specs/authorship_of_editions.js index 2552ab9e0..f78b9d1e1 100644 --- a/src/tests/frontend/specs/authorship_of_editions.js +++ b/src/tests/frontend/specs/authorship_of_editions.js @@ -50,8 +50,8 @@ describe('author of pad edition', function () { await new Promise((resolve) => setTimeout(resolve, 1000)); // Expire cookie, so author is changed after reloading the pad. - // See https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie#Example_4_Reset_the_previous_cookie - helper.padChrome$.document.cookie = 'token=foo;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/'; + const {Cookies} = helper.padChrome$.window.require('ep_etherpad-lite/static/js/pad_utils'); + Cookies.remove('token'); // Reload pad, to make changes as a second user. await helper.aNewPad({id: padId}); diff --git a/src/tests/frontend/specs/helper.js b/src/tests/frontend/specs/helper.js index bac14ce53..d0a69c563 100644 --- a/src/tests/frontend/specs/helper.js +++ b/src/tests/frontend/specs/helper.js @@ -45,8 +45,8 @@ describe('the test helper', function () { this.timeout(60000); // set cookies far into the future to make sure they're not expired yet - window.document.cookie = 'token=foo;expires=Thu, 01 Jan 3030 00:00:00 GMT; path=/'; - window.document.cookie = 'language=bar;expires=Thu, 01 Jan 3030 00:00:00 GMT; path=/'; + window.Cookies.set('token', 'foo', {expires: 7 /* days */}); + window.Cookies.set('language', 'bar', {expires: 7 /* days */}); expect(window.document.cookie).to.contain('token=foo'); expect(window.document.cookie).to.contain('language=bar'); @@ -56,8 +56,8 @@ describe('the test helper', function () { // helper function seems to have cleared cookies // NOTE: this doesn't yet mean it's proven to have taken effect by this point in execution const firstCookie = window.document.cookie; - expect(firstCookie).to.not.contain('token=foo'); - expect(firstCookie).to.not.contain('language=bar'); + expect(window.Cookies.get('token')).to.not.be('foo'); + expect(window.Cookies.get('language') == null).to.be(true); let chrome$ = helper.padChrome$; @@ -76,21 +76,26 @@ describe('the test helper', function () { // Now that we have a chrome, we can set a pad cookie // so we can confirm it gets wiped as well - chrome$.document.cookie = 'prefsHttp=baz;expires=Thu, 01 Jan 3030 00:00:00 GMT; path=/'; - expect(chrome$.document.cookie).to.contain('prefsHttp=baz'); + const getPadcookie = + () => helper.padChrome$.window.require('ep_etherpad-lite/static/js/pad_cookie').padcookie; + let padcookie = getPadcookie(); + padcookie.clear(); + padcookie.setPref('foo', 'bar'); + expect(padcookie.getPref('foo')).to.be('bar'); // give it a second to save the username on the server side await new Promise((resolve) => setTimeout(resolve, 1000)); await helper.aNewPad(); // get a new pad, let it clear the cookies chrome$ = helper.padChrome$; + padcookie = getPadcookie(); // helper function seems to have cleared cookies // NOTE: this doesn't yet mean cookies were cleared effectively. // We still need to test below that we're in a new session - expect(window.document.cookie).to.not.contain('token=foo'); - expect(window.document.cookie).to.not.contain('language=bar'); - expect(chrome$.document.cookie).to.contain('prefsHttp=baz'); + expect(window.Cookies.get('token')).to.not.be('foo'); + expect(window.Cookies.get('language') == null).to.be(true); + expect(padcookie.getPref('foo') == null).to.be(true); expect(window.document.cookie).to.not.be(firstCookie); @@ -105,10 +110,9 @@ describe('the test helper', function () { it('sets pad prefs cookie', async function () { this.timeout(60000); - await helper.aNewPad({padPrefs: {foo: 'bar'}}); - const chrome$ = helper.padChrome$; - expect(chrome$.document.cookie).to.contain('prefsHttp=%7B%22'); - expect(chrome$.document.cookie).to.contain('foo%22%3A%22bar'); + await helper.aNewPad({padPrefs: {foo: 'padPrefs test'}}); + const {padcookie} = helper.padChrome$.window.require('ep_etherpad-lite/static/js/pad_cookie'); + expect(padcookie.getPref('foo')).to.be('padPrefs test'); }); }); diff --git a/src/tests/frontend/specs/language.js b/src/tests/frontend/specs/language.js index afc1e2402..fa361a7ff 100644 --- a/src/tests/frontend/specs/language.js +++ b/src/tests/frontend/specs/language.js @@ -1,12 +1,8 @@ 'use strict'; -const deletecookie = (name) => { - document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/`; -}; - describe('Language select and change', function () { // Destroy language cookies - deletecookie('language', null); + window.Cookies.remove('language'); // create a new pad before each test run beforeEach(function (cb) { diff --git a/src/tests/frontend/specs/multiple_authors_clear_authorship_colors.js b/src/tests/frontend/specs/multiple_authors_clear_authorship_colors.js index b7cea5545..4f081e313 100755 --- a/src/tests/frontend/specs/multiple_authors_clear_authorship_colors.js +++ b/src/tests/frontend/specs/multiple_authors_clear_authorship_colors.js @@ -16,9 +16,9 @@ describe('author of pad edition', function () { // Need a timeout here to make sure all changes were saved. await new Promise((resolve) => setTimeout(resolve, 1000)); - // Expire cookie, so author is changed after reloading the pad. - // See https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie#Example_4_Reset_the_previous_cookie - helper.padChrome$.document.cookie = 'token=foo;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/'; + // Delete token cookie, so author is changed after reloading the pad. + const {Cookies} = helper.padChrome$.window.require('ep_etherpad-lite/static/js/pad_utils'); + Cookies.remove('token'); // Reload pad, to make changes as a second user. await helper.aNewPad({id: padId});