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

Improved C2P Script Injection #528

Merged
merged 11 commits into from Apr 28, 2020

add unit tests for buildRedirectC2P()

  • Loading branch information
christophertino committed Apr 26, 2020
commit 87effee593e0d450b22346afbb9a23f57599bca5
@@ -694,7 +694,7 @@ class EventHandlers {
};
}
} else if (fromRedirect) {
const url = buildRedirectC2P(requestId, globals.REDIRECT_MAP.get(requestId), appId);
const url = buildRedirectC2P(globals.REDIRECT_MAP.get(requestId), appId);
setTimeout(() => {
chrome.tabs.update(details.tabId, { url });
}, 0);
@@ -147,16 +147,16 @@ export function buildC2P(details, app_id) {
}

/**
* Build blocked redirect data global structure Inject Page-Level Click2Play on Redirect.
* Inject page-level Click2Play on redirect. Build blocked redirect
* data global structure.
* @memberOf BackgroundUtils
*
* @param {number} requestId request id
* @param {Object} redirectUrls original url and redirect url as properties
* @param {Object} redirectUrls original url and redirect url as properties
* @param {number} app_id tracker id
*
* @return {string} url of the internal template of the blocked redirect page
*/
export function buildRedirectC2P(requestId, redirectUrls, app_id) {
export function buildRedirectC2P(redirectUrls, app_id) {
const host_url = processUrl(redirectUrls.url).hostname;
const redirect_url = processUrl(redirectUrls.redirectUrl).hostname;
const app_name = bugDb.db.apps[app_id].name;
@@ -171,7 +171,7 @@ export function buildRedirectC2P(requestId, redirectUrls, app_id) {
blocked_redirect_prevent: t(
'blocked_redirect_prevent',
// It is unlikely that apps pages will ever be translated
// [host_url, redirect_url, app_name, 'https://' + globals.APPS_SUB_DOMAIN + '.ghostery.com/' + conf.language + '/apps/' + encodeURIComponent(app_name.replace(/\s+/g, '_').toLowerCase())]),
// [host_url, redirect_url, app_name, 'https://' + globals.APPS_SUB_DOMAIN + '.ghostery.com/' + conf.language + '/apps/' + encodeURIComponent(app_name.replace(/\s+/g, '_').toLowerCase())]),
[host_url, redirect_url, app_name, `${globals.APPS_BASE_URL}/en/apps/${encodeURIComponent(app_name.replace(/\s+/g, '_').toLowerCase())}`]
),
blocked_redirect_action_always_title: t('blocked_redirect_action_always_title'),
@@ -194,7 +194,6 @@ export function allowAllwaysC2P(app_id, tab_host) {
delete selected_app_ids[app_id];
conf.selected_app_ids = selected_app_ids;


// Remove fron site-specific-blocked
if (conf.site_specific_blocks.hasOwnProperty(tab_host) && conf.site_specific_blocks[tab_host].includes(+app_id)) {
const index = conf.site_specific_blocks[tab_host].indexOf(+app_id);
@@ -11,11 +11,12 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0
*/

import { buildC2P } from '../../src/utils/click2play';
import { buildC2P, buildRedirectC2P } from '../../src/utils/click2play';
import tabInfo from '../../src/classes/TabInfo';
import Policy from '../../src/classes/Policy';
import globals from '../../src/classes/Globals';
import c2p_tpl from '../../app/templates/click2play.html';
import { injectScript, sendMessage } from '../../src/utils/utils';
import * as utils from '../../src/utils/utils';

// Mock imports for dependencies
jest.mock('../../app/templates/click2play.html', () => {
@@ -33,11 +34,7 @@ jest.mock('../../src/classes/TabInfo', () => ({
jest.mock('../../src/classes/Policy');
jest.mock('../../src/classes/Globals', () => ({
BROWSER_INFO: { displayName: '', name: '', token: '', version: '', os: 'other' },
EXCLUDES: []
}));
jest.mock('../../src/utils/utils', () => ({
sendMessage: jest.fn(),
injectScript: jest.fn(() => Promise.resolve())
EXCLUDES: [],
}));
jest.mock('../../src/classes/Click2PlayDb', () => ({
type: 'click2play',
@@ -77,9 +74,9 @@ jest.mock('../../src/classes/Click2PlayDb', () => ({
jest.mock('../../src/classes/BugDb', () => ({
db: {
apps: {
464: [{
464: {
name: "Facebook Social Plugins"
}],
},
}
}
}));
@@ -91,19 +88,23 @@ tabInfo.setTabInfo = jest.fn().mockImplementation((tab_id, property, value) => {
tabInfo[property] = value;
});

// Mock utils functions
utils.sendMessage = jest.fn();
utils.injectScript = jest.fn(() => Promise.resolve());
utils.processUrl = jest.requireActual('../../src/utils/utils').processUrl;

describe('src/utils/click2play.js', () => {
const details = {
tab_id: 1
};
const tab = tabInfo.getTabInfo(details.tab_id);

describe('testing buildC2P()', () => {

describe('c2pStatus is "none"', () => {
beforeAll(() => {
tabInfo.c2pStatus = 'none';
sendMessage.mockClear();
injectScript.mockClear();
utils.sendMessage.mockClear();
utils.injectScript.mockClear();
});

test('c2pStatus defaults to "none"', () => {
@@ -112,7 +113,7 @@ describe('src/utils/click2play.js', () => {

test('injectScript() is called', () => {
buildC2P(details, 464);
expect(injectScript).toHaveBeenCalledWith(details.tab_id, 'dist/click_to_play.js', '', 'document_idle');
expect(utils.injectScript).toHaveBeenCalledWith(details.tab_id, 'dist/click_to_play.js', '', 'document_idle');
});

test('c2pApp and c2pHtml data added to c2pQueue', () => {
@@ -123,7 +124,7 @@ describe('src/utils/click2play.js', () => {

test('sendMessage() called with correct C2P data', () => {
const c2pQueue = tabInfo.setTabInfo.mock.calls[1][2];
expect(sendMessage).toHaveBeenCalledWith(details.tab_id, 'c2p', c2pQueue);
expect(utils.sendMessage).toHaveBeenCalledWith(details.tab_id, 'c2p', c2pQueue);
});

test('c2pStatus set to "done"', () => {
@@ -137,15 +138,15 @@ describe('src/utils/click2play.js', () => {

describe('c2pStatus is "loading"', () => {
beforeAll(() => {
sendMessage.mockClear();
injectScript.mockClear();
utils.sendMessage.mockClear();
utils.injectScript.mockClear();
tabInfo.c2pStatus = 'loading';
buildC2P(details, 464);
});

test('injectScript() and sendMessage() are not called', () => {
expect(injectScript).not.toHaveBeenCalled();
expect(sendMessage).not.toHaveBeenCalled();
expect(utils.injectScript).not.toHaveBeenCalled();
expect(utils.sendMessage).not.toHaveBeenCalled();
});

test('c2pApp and c2pHtml data added to c2pQueue', () => {
@@ -155,21 +156,31 @@ describe('src/utils/click2play.js', () => {

describe('c2pStatus is "done"', () => {
beforeAll(() => {
sendMessage.mockClear();
injectScript.mockClear();
utils.sendMessage.mockClear();
utils.injectScript.mockClear();
tabInfo.c2pStatus = 'done';
tabInfo.c2pQueue = {};
buildC2P(details, 464);
});

test('injectScript() is not called. sendMessage() is called', () => {
expect(injectScript).not.toHaveBeenCalled();
expect(sendMessage).toHaveBeenCalled();
expect(utils.injectScript).not.toHaveBeenCalled();
expect(utils.sendMessage).toHaveBeenCalled();
});

test('c2pQueue is empty', () => {
expect(tab.c2pQueue).toEqual({});
});
});
});

describe('testing buildRedirectC2P()', () => {
const REDIRECT_MAP = new Map([[100, { url: 'https://cnn.com/', redirectUrl: 'https://fake-redirect.com/' }]]);

test('app_id is added to BLOCKED_REDIRECT_DATA global', () => {
buildRedirectC2P(REDIRECT_MAP.get(100), 464);
expect(globals.BLOCKED_REDIRECT_DATA.app_id).toBe(464);
expect(globals.BLOCKED_REDIRECT_DATA.url).toBe('https://fake-redirect.com/');
});
});
});
ProTip! Use n and p to navigate between commits in a pull request.