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 units tests for allowAllwaysC2P

  • Loading branch information
christophertino committed Apr 26, 2020
commit dcbcc8df6ec2bb0e706d0b5bb0e6abb877c4feb6
@@ -198,7 +198,7 @@ export function allowAllwaysC2P(app_id, tab_host) {
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);
const { site_specific_blocks } = conf;
site_specific_blocks[tab_host].splice(index);
site_specific_blocks[tab_host].splice(0, 1);
conf.site_specific_blocks = site_specific_blocks;
}

@@ -11,10 +11,11 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0
*/

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

@@ -80,6 +81,19 @@ jest.mock('../../src/classes/BugDb', () => ({
}
}
}));
jest.mock('../../src/classes/Conf', () => ({
selected_app_ids: {
15: 1,
41: 1,
},
site_specific_blocks: {
'www.ghostery.com': [15, 100],
'www.cnn.com': [41],
},
site_specific_unblocks: {
'www.cnn.com': [15, 50],
},
}));

// Mock TabInfo data
tabInfo.getTabInfo = jest.fn();
@@ -183,4 +197,29 @@ describe('src/utils/click2play.js', () => {
expect(globals.BLOCKED_REDIRECT_DATA.url).toBe('https://fake-redirect.com/');
});
});

describe('testing allowAllwaysC2P()', () => {
test('app_id is removed from selected_app_ids', () => {
allowAllwaysC2P(15, 'www.espn.com');
expect(conf.selected_app_ids).not.toHaveProperty('15');
expect(conf.selected_app_ids).toMatchObject({41: 1});
});

test('app_id is removed from site_specific_blocks', () => {
allowAllwaysC2P(15, 'www.ghostery.com');
expect(conf.site_specific_blocks['www.ghostery.com']).toEqual(expect.not.arrayContaining([15]));
expect(conf.site_specific_blocks['www.ghostery.com']).toEqual(expect.arrayContaining([100]));
});

test('app_id is added to site_specific_unblocks', () => {
allowAllwaysC2P(41, 'www.cnn.com');
expect(conf.site_specific_unblocks['www.cnn.com']).toEqual(expect.arrayContaining([15,50,41]));
// Check results from preceding tests
expect(conf.site_specific_unblocks['www.espn.com']).toEqual(expect.arrayContaining([15]));
expect(conf.site_specific_unblocks['www.ghostery.com']).toEqual(expect.arrayContaining([15]));
// Check removal from selected_app_ids and site_specific_blocks as well
expect(conf.selected_app_ids).not.toHaveProperty('41');
expect(conf.site_specific_blocks['www.cnn.com']).toEqual(expect.not.arrayContaining([41]));
});
});
});
ProTip! Use n and p to navigate between commits in a pull request.