2022-08-25 11:03:21 +03:00
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2023-02-03 17:46:39 +03:00
|
|
|
import FormUtils from '../../../../media/js/newsletter/form-utils.es6';
|
|
|
|
|
|
|
|
const TOKEN_MOCK = 'a1a2a3a4-abc1-12ab-a123-12345a12345b';
|
2022-08-25 11:03:21 +03:00
|
|
|
|
|
|
|
describe('checkEmailValidity', function () {
|
|
|
|
it('should return true for primitive email format', function () {
|
2023-02-03 17:46:39 +03:00
|
|
|
expect(FormUtils.checkEmailValidity('a@a')).toBeTruthy();
|
|
|
|
expect(
|
|
|
|
FormUtils.checkEmailValidity('example@example.com')
|
|
|
|
).toBeTruthy();
|
2022-08-25 11:03:21 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false for anything else', function () {
|
2023-02-03 17:46:39 +03:00
|
|
|
expect(FormUtils.checkEmailValidity(1234567890)).toBeFalsy();
|
|
|
|
expect(FormUtils.checkEmailValidity('aaa')).toBeFalsy();
|
|
|
|
expect(FormUtils.checkEmailValidity(null)).toBeFalsy();
|
|
|
|
expect(FormUtils.checkEmailValidity(undefined)).toBeFalsy();
|
|
|
|
expect(FormUtils.checkEmailValidity(true)).toBeFalsy();
|
|
|
|
expect(FormUtils.checkEmailValidity(false)).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getURLToken', function () {
|
|
|
|
it('should return a UUID token from a URL', function () {
|
|
|
|
const location = {
|
|
|
|
pathname: `/en-US/newsletter/existing/${TOKEN_MOCK}/`
|
|
|
|
};
|
|
|
|
expect(FormUtils.getURLToken(location)).toEqual(TOKEN_MOCK);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return an empty string if a valid token is not found', function () {
|
|
|
|
const location = {
|
|
|
|
pathname: `/en-US/newsletter/existing/`
|
|
|
|
};
|
|
|
|
expect(FormUtils.getURLToken(location)).toEqual('');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getUserToken', function () {
|
2023-07-31 11:33:28 +03:00
|
|
|
afterEach(function () {
|
|
|
|
FormUtils.userToken = '';
|
|
|
|
});
|
|
|
|
|
2023-02-03 17:46:39 +03:00
|
|
|
it('should return a UUID token from cookie', function () {
|
2023-07-31 11:33:28 +03:00
|
|
|
spyOn(Mozilla.Cookies, 'hasItem').and.returnValue(true);
|
2023-02-03 17:46:39 +03:00
|
|
|
spyOn(Mozilla.Cookies, 'getItem').and.returnValue(TOKEN_MOCK);
|
|
|
|
expect(FormUtils.getUserToken()).toEqual(TOKEN_MOCK);
|
|
|
|
});
|
|
|
|
|
2023-07-31 11:33:28 +03:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2023-02-03 17:46:39 +03:00
|
|
|
it('should return an empty string if token is invalid', function () {
|
|
|
|
const token = 'some invalid string';
|
2023-07-31 13:17:54 +03:00
|
|
|
spyOn(Mozilla.Cookies, 'hasItem').and.returnValue(true);
|
2023-02-03 17:46:39 +03:00
|
|
|
spyOn(Mozilla.Cookies, 'getItem').and.returnValue(token);
|
|
|
|
expect(FormUtils.getUserToken()).toEqual('');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('isValidToken', function () {
|
|
|
|
it('should return true for a valid token', function () {
|
|
|
|
expect(FormUtils.isValidToken(TOKEN_MOCK)).toBeTrue();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false if a token is too long', function () {
|
|
|
|
expect(
|
|
|
|
FormUtils.isValidToken(
|
|
|
|
'ffa1a2a3a4-abgc1-1g2ab-a12f3-12345a12345bgf'
|
|
|
|
)
|
|
|
|
).toBeFalse();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false if a token is too short', function () {
|
|
|
|
expect(FormUtils.isValidToken('a1-abc1-12ab-a123-12345a')).toBeFalse();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false for everything else', function () {
|
|
|
|
expect(FormUtils.isValidToken('some-string')).toBeFalse();
|
|
|
|
expect(FormUtils.isValidToken(true)).toBeFalse();
|
|
|
|
expect(FormUtils.isValidToken(null)).toBeFalse();
|
|
|
|
expect(FormUtils.isValidToken(undefined)).toBeFalse();
|
|
|
|
expect(FormUtils.isValidToken()).toBeFalse();
|
|
|
|
expect(FormUtils.isValidToken(123456789)).toBeFalse();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('isWellFormedURL', function () {
|
|
|
|
it('should return true for absolute URLs', function () {
|
|
|
|
expect(
|
|
|
|
FormUtils.isWellFormedURL(
|
|
|
|
'http://localhost:8000/en-US/newsletter/updated/'
|
|
|
|
)
|
|
|
|
).toBeTrue();
|
|
|
|
expect(
|
|
|
|
FormUtils.isWellFormedURL(
|
|
|
|
'https://www.mozilla.org/en-US/newsletter/updated/'
|
|
|
|
)
|
|
|
|
).toBeTrue();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true for relative URLs', function () {
|
|
|
|
expect(
|
|
|
|
FormUtils.isWellFormedURL('/en-US/newsletter/updated/')
|
|
|
|
).toBeTrue();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false for anything else', function () {
|
|
|
|
expect(
|
|
|
|
FormUtils.isWellFormedURL('undefined?unsub=1&token=null')
|
|
|
|
).toBeFalse();
|
|
|
|
expect(FormUtils.isWellFormedURL(undefined)).toBeFalse();
|
|
|
|
expect(FormUtils.isWellFormedURL(null)).toBeFalse();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('removeTokenFromURL', function () {
|
|
|
|
it('should remove token from URL path name', function () {
|
|
|
|
spyOn(window.history, 'replaceState');
|
|
|
|
const location = {
|
|
|
|
pathname: `/en-US/newsletter/existing/${TOKEN_MOCK}/`,
|
|
|
|
search: ''
|
|
|
|
};
|
|
|
|
FormUtils.removeTokenFromURL(location, TOKEN_MOCK);
|
|
|
|
expect(window.history.replaceState).toHaveBeenCalledWith(
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
'/en-US/newsletter/existing/'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should maintain existing query parameters', function () {
|
|
|
|
spyOn(window.history, 'replaceState');
|
|
|
|
const location = {
|
|
|
|
pathname: `/en-US/newsletter/existing/${TOKEN_MOCK}/`,
|
|
|
|
search: '?fxa=1'
|
|
|
|
};
|
|
|
|
FormUtils.removeTokenFromURL(location, TOKEN_MOCK);
|
|
|
|
expect(window.history.replaceState).toHaveBeenCalledWith(
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
'/en-US/newsletter/existing/?fxa=1'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not update the URL if a token is not present', function () {
|
|
|
|
spyOn(window.history, 'replaceState');
|
|
|
|
const location = {
|
|
|
|
pathname: '/en-US/newsletter/existing/'
|
|
|
|
};
|
|
|
|
FormUtils.removeTokenFromURL(location, TOKEN_MOCK);
|
|
|
|
expect(window.history.replaceState).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setUserToken', function () {
|
2023-07-31 11:33:28 +03:00
|
|
|
afterEach(function () {
|
|
|
|
FormUtils.userToken = '';
|
|
|
|
});
|
|
|
|
|
2023-02-03 17:46:39 +03:00
|
|
|
it('should set a cookie with a valid UUID token', function () {
|
|
|
|
spyOn(Mozilla.Cookies, 'setItem');
|
|
|
|
FormUtils.setUserToken(TOKEN_MOCK);
|
|
|
|
expect(Mozilla.Cookies.setItem).toHaveBeenCalledOnceWith(
|
|
|
|
'nl-token',
|
|
|
|
TOKEN_MOCK,
|
|
|
|
jasmine.any(String),
|
|
|
|
'/',
|
|
|
|
undefined,
|
|
|
|
false,
|
|
|
|
'lax'
|
|
|
|
);
|
2023-07-31 11:33:28 +03:00
|
|
|
expect(FormUtils.userToken).toEqual(TOKEN_MOCK);
|
2023-02-03 17:46:39 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not set a cookie if token is invalid', function () {
|
|
|
|
const token = 'some invalid string';
|
|
|
|
spyOn(Mozilla.Cookies, 'setItem');
|
|
|
|
FormUtils.setUserToken(token);
|
|
|
|
expect(Mozilla.Cookies.setItem).not.toHaveBeenCalled();
|
2022-08-25 11:03:21 +03:00
|
|
|
});
|
|
|
|
});
|
2022-08-10 18:11:50 +03:00
|
|
|
|
|
|
|
describe('serialize', function () {
|
|
|
|
afterEach(function () {
|
|
|
|
const form = document.querySelector('.send-to-device-form');
|
|
|
|
form.parentNode.removeChild(form);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a query string as expected', function () {
|
|
|
|
const form = `<form class="send-to-device-form" action="https://basket.mozilla.org/news/subscribe/" method="post">
|
|
|
|
<div class="send-to-device-form-fields">
|
|
|
|
<div class="platform-container">
|
|
|
|
<input type="hidden" name="newsletters" value="download-firefox-mobile-reco">
|
|
|
|
<input type="hidden" name="source-url" value="https://www.mozilla.org/en-US/firefox/browsers/mobile/ios/">
|
|
|
|
</div>
|
|
|
|
<div class="mzp-c-field mzp-l-stretch">
|
|
|
|
<label class="mzp-c-field-label" for="s2d-hero-input">Enter your email</label>
|
|
|
|
<input id="s2d-hero-input" class="mzp-c-field-control send-to-device-input" name="email" type="text" required="" value="example@example.com">
|
|
|
|
</div>
|
|
|
|
<div class="mzp-c-button-container mzp-l-stretch">
|
|
|
|
<button type="submit" class="button mzp-c-button mzp-t-product ">Send</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>`;
|
|
|
|
document.body.insertAdjacentHTML('beforeend', form);
|
|
|
|
expect(
|
2023-02-03 17:46:39 +03:00
|
|
|
FormUtils.serialize(document.querySelector('.send-to-device-form'))
|
2022-08-10 18:11:50 +03:00
|
|
|
).toEqual(
|
|
|
|
'newsletters=download-firefox-mobile-reco&source-url=https%3A%2F%2Fwww.mozilla.org%2Fen-US%2Ffirefox%2Fbrowsers%2Fmobile%2Fios%2F&email=example%40example.com'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return an empty string if no form elements are found', function () {
|
|
|
|
const form = `<form class="send-to-device-form" action="https://basket.mozilla.org/news/subscribe/" method="post"></form>`;
|
|
|
|
document.body.insertAdjacentHTML('beforeend', form);
|
|
|
|
expect(
|
2023-02-03 17:46:39 +03:00
|
|
|
FormUtils.serialize(document.querySelector('.send-to-device-form'))
|
2022-08-10 18:11:50 +03:00
|
|
|
).toEqual('');
|
|
|
|
});
|
|
|
|
});
|
2023-01-05 13:27:52 +03:00
|
|
|
|
|
|
|
describe('stripHTML', function () {
|
|
|
|
it('should strip HTML tags from strings as expected', function () {
|
|
|
|
expect(
|
2023-02-03 17:46:39 +03:00
|
|
|
FormUtils.stripHTML('This string should not contain HTML.')
|
|
|
|
).toEqual('This string should not contain HTML.');
|
|
|
|
expect(
|
|
|
|
FormUtils.stripHTML(
|
2023-01-05 13:27:52 +03:00
|
|
|
'This string<script></script> should <strong>not</strong> contain <br>HTML<img src="/img/placeholder.png" />.'
|
|
|
|
)
|
|
|
|
).toEqual('This string should not contain HTML.');
|
|
|
|
expect(
|
2023-02-03 17:46:39 +03:00
|
|
|
FormUtils.stripHTML(
|
2023-01-05 13:27:52 +03:00
|
|
|
'This%20string%3Cscript%3E%3C%2Fscript%3E%20should%20%3Cstrong%3Enot%3C%2Fstrong%3E%20contain%20%3Cbr%3EHTML%3Cimg%20src%3D%22%2Fimg%2Fplaceholder.png%22%20%2F%3E.'
|
|
|
|
)
|
|
|
|
).toEqual('This string should not contain HTML.');
|
|
|
|
});
|
|
|
|
});
|