Skip to content

Commit

Permalink
Fix newsletter management page form submission when cookie has expired (
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgibson committed Jul 26, 2023
1 parent bed6c28 commit 0c2b8ec
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
19 changes: 13 additions & 6 deletions media/js/newsletter/form-utils.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ const errorList = {
REASON_ERROR: 'Reason not selected'
};

let _userToken;

const FormUtils = {
errorList,
userToken: '',

/**
* Really primitive validation (e.g a@a)
Expand Down Expand Up @@ -71,6 +70,11 @@ const FormUtils = {
if (!token) {
reject();
} else {
// always store token as a local variable in memory, so the
// form still works if left open long enough for cookie to
// expire: see issue 13324.
FormUtils.userToken = token;

resolve();
}
});
Expand Down Expand Up @@ -122,10 +126,10 @@ const FormUtils = {
const cookiesEnabled =
typeof Mozilla.Cookies !== 'undefined' && Mozilla.Cookies.enabled();

if (cookiesEnabled) {
if (cookiesEnabled && Mozilla.Cookies.hasItem(BASKET_COOKIE_ID)) {
token = Mozilla.Cookies.getItem(BASKET_COOKIE_ID);
} else {
token = _userToken;
token = FormUtils.userToken;
}

if (FormUtils.isValidToken(token)) {
Expand Down Expand Up @@ -268,9 +272,12 @@ const FormUtils = {
false,
'lax'
);
} else {
_userToken = token;
}

// always store token as a local variable in memory, so the
// form still works if left open long enough for cookie to
// expire: see issue 13324.
FormUtils.userToken = token;
}
},

Expand Down
16 changes: 16 additions & 0 deletions tests/unit/spec/newsletter/form-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,22 @@ describe('getURLToken', function () {
});

describe('getUserToken', function () {
afterEach(function () {
FormUtils.userToken = '';
});

it('should return a UUID token from cookie', function () {
spyOn(Mozilla.Cookies, 'hasItem').and.returnValue(true);
spyOn(Mozilla.Cookies, 'getItem').and.returnValue(TOKEN_MOCK);
expect(FormUtils.getUserToken()).toEqual(TOKEN_MOCK);
});

it('should return a token from local memory if cookie has expired', function () {
spyOn(Mozilla.Cookies, 'hasItem').and.returnValue(false);
FormUtils.userToken = TOKEN_MOCK;
expect(FormUtils.getUserToken()).toEqual(TOKEN_MOCK);
});

it('should return an empty string if token is invalid', function () {
const token = 'some invalid string';
spyOn(Mozilla.Cookies, 'getItem').and.returnValue(token);
Expand Down Expand Up @@ -151,6 +162,10 @@ describe('removeTokenFromURL', function () {
});

describe('setUserToken', function () {
afterEach(function () {
FormUtils.userToken = '';
});

it('should set a cookie with a valid UUID token', function () {
spyOn(Mozilla.Cookies, 'setItem');
FormUtils.setUserToken(TOKEN_MOCK);
Expand All @@ -163,6 +178,7 @@ describe('setUserToken', function () {
false,
'lax'
);
expect(FormUtils.userToken).toEqual(TOKEN_MOCK);
});

it('should not set a cookie if token is invalid', function () {
Expand Down
1 change: 1 addition & 0 deletions tests/unit/spec/newsletter/management.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ describe('management.es6.js', function () {
afterEach(function () {
const form = document.getElementById('newsletter-management-test-form');
form.parentNode.removeChild(form);
FormUtils.userToken = '';
});

describe('isFxALocale', function () {
Expand Down

0 comments on commit 0c2b8ec

Please sign in to comment.