tests: Use cookie libraries to manipulate cookies

This commit is contained in:
Richard Hansen 2021-03-26 01:09:56 -04:00 committed by webzwo0i
parent 202d65d2bb
commit 2776946627
7 changed files with 36 additions and 32 deletions

View file

@ -63,4 +63,8 @@ exports.padcookie = new class {
prefs[prefName] = value;
this.writePrefs_(prefs);
}
clear() {
this.writePrefs_({});
}
}();

View file

@ -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

View file

@ -11,6 +11,7 @@
<script src="/static/js/vendors/jquery.js"></script>
<script src="/static/js/vendors/browser.js"></script>
<script src="/static/plugins/js-cookie/src/js.cookie.js"></script>
<script src="lib/underscore.js"></script>
<script src="lib/mocha.js"></script>

View file

@ -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});

View file

@ -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');
});
});

View file

@ -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) {

View file

@ -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});