Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix newsletter management page form submission when cookie has expired (Fixes #13324) #13448

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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