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

GH-1989: Fixes site-specific whitelisting for unblocked trackers #522

Merged
merged 5 commits into from Apr 14, 2020
Merged
Changes from 1 commit
Commits
File filter
Filter file types
Jump to
Jump to file
Failed to load files.

Always

Just for now

add unit tests for policy.shouldBlock()

  • Loading branch information
christophertino committed Apr 3, 2020
commit 270742ce2b29d4ca18dac30f7f7e9b0b29d2d0cf
@@ -1,11 +1,200 @@
import Policy from '../../src/classes/Policy';
/**
* /src/classes/Policy.js Unit Tests
*
* Ghostery Browser Extension
* http://www.ghostery.com/
*
* Copyright 2019 Ghostery, Inc. All rights reserved.
*
* 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 http://mozilla.org/MPL/2.0
*/

import Policy, {
BLOCK_REASON_BLOCK_PAUSED,
BLOCK_REASON_GLOBAL_BLOCKED,
BLOCK_REASON_GLOBAL_UNBLOCKED,
BLOCK_REASON_WHITELISTED,
BLOCK_REASON_BLACKLISTED,
BLOCK_REASON_SS_UNBLOCKED,
BLOCK_REASON_SS_BLOCKED,
BLOCK_REASON_C2P_ALLOWED_ONCE,
} from '../../src/classes/Policy';
import c2pDb from '../../src/classes/Click2PlayDb';
import conf from '../../src/classes/Conf';
import globals from '../../src/classes/Globals';
import { processUrl } from '../../src/utils/utils';

let policy = new Policy();

// Mock imports for dependencies
jest.mock('../../src/classes/TabInfo', () => {});
jest.mock('../../src/classes/TabInfo');
jest.mock('../../src/classes/Conf', () => {
return {
selected_app_ids: {
15: 1,
41: 1,
},
toggle_individual_trackers: true,
site_specific_blocks: {
'www.espn.com': [50],
'www.cnn.com': [13],
'www.ghostery.com': [15],
},
site_specific_unblocks: {
'www.cnn.com': [15, 50],
'www.tmz.com': [41, 50],
},
site_blacklist: ['tmz.com'],
site_whitelist: ['ghostery.com', 'cliqz.com'],
}
});
jest.mock('../../src/classes/Globals', () => {
return {
BLACKLISTED: 1,
WHITELISTED: 2,
SESSION : {
paused_blocking: false,
},
BROWSER_INFO: { displayName: '', name: '', token: '', version: '', os: 'other' },
}
});

describe('src/classes/Policy.js', () => {
describe('test matchesWildcard', () => {
describe('testing shouldBlock()', () => {
beforeAll(() => {
// Mock C2P allow-once check
c2pDb.allowedOnce = jest.fn();
c2pDb.allowedOnce.mockReturnValue(false);
});

describe('with Ghostery paused', () => {
beforeEach(() => {
globals.SESSION.paused_blocking = true;
});
afterEach(() => {
globals.SESSION.paused_blocking = false;
});
test('a blocked tracker is unblocked with reason BLOCK_REASON_BLOCK_PAUSED', () => {
const { block, reason } = policy.shouldBlock(41, 'advertising', 1, 'www.cnn.com', 'https://www.cnn.com/');
expect(block).toBeFalsy();
expect(reason).toBe(BLOCK_REASON_BLOCK_PAUSED);
});
test('an unblocked tracker remains unblocked with reason BLOCK_REASON_BLOCK_PAUSED', () => {
const { block, reason } = policy.shouldBlock(50, 'essential', 1, 'www.cnn.com', 'https://www.cnn.com/');
expect(block).toBeFalsy();
expect(reason).toBe(BLOCK_REASON_BLOCK_PAUSED);
});
test('a tracker on a white-listed site is unblocked with reason BLOCK_REASON_BLOCK_PAUSED', () => {
const { block, reason } = policy.shouldBlock(41, 'advertising', 1, 'www.ghostery.com', 'https://www.ghostery.com/');
expect(block).toBeFalsy();
expect(reason).toBe(BLOCK_REASON_BLOCK_PAUSED);
});
test('a tracker on a black-listed site is unblocked with reason BLOCK_REASON_BLOCK_PAUSED', () => {
const { block, reason } = policy.shouldBlock(50, 'essential', 1, 'www.tmz.com', 'https://www.tmz.com/');
expect(block).toBeFalsy();
expect(reason).toBe(BLOCK_REASON_BLOCK_PAUSED);
});
});

describe('with Click2Play Allow Once enabled', () => {
beforeAll(() => {
c2pDb.allowedOnce.mockReturnValue(true);
});
afterAll(() => {
c2pDb.allowedOnce.mockReturnValue(false);
});
test('a blocked tracker on the site-specific allow list on a black-listed site is unblocked with reason BLOCK_REASON_C2P_ALLOWED_ONCE', () => {
const { block, reason } = policy.shouldBlock(41, 'advertising', 1, 'www.tmz.com', 'https://www.tmz.com/');
expect(block).toBeFalsy();
expect(reason).toBe(BLOCK_REASON_C2P_ALLOWED_ONCE);
});
test('a blocked tracker is unblocked with reason BLOCK_REASON_C2P_ALLOWED_ONCE', () => {
const { block, reason } = policy.shouldBlock(41, 'advertising', 1, 'www.cnn.com', 'https://www.cnn.com/');
expect(block).toBeFalsy();
expect(reason).toBe(BLOCK_REASON_C2P_ALLOWED_ONCE);
});
test('an unblocked tracker on the site-specific block list remains unblocked with reason BLOCK_REASON_C2P_ALLOWED_ONCE', () => {
const { block, reason } = policy.shouldBlock(50, 'essential', 1, 'www.espn.com', 'https://www.espn.com/');
expect(block).toBeFalsy();
expect(reason).toBe(BLOCK_REASON_C2P_ALLOWED_ONCE);
});
test('an unblocked tracker on the site-specific allow list on a black-listed site remains unblocked with reason BLOCK_REASON_C2P_ALLOWED_ONCE', () => {
const { block, reason } = policy.shouldBlock(50, 'essential', 1, 'www.tmz.com', 'https://www.tmz.com/');
expect(block).toBeFalsy();
expect(reason).toBe(BLOCK_REASON_C2P_ALLOWED_ONCE);
});
test('an unblocked tracker on a black-listed site remains unblocked with reason BLOCK_REASON_C2P_ALLOWED_ONCE', () => {
const { block, reason } = policy.shouldBlock(55, 'essential', 1, 'www.tmz.com', 'https://www.tmz.com/');
expect(block).toBeFalsy();
expect(reason).toBe(BLOCK_REASON_C2P_ALLOWED_ONCE);
});
test('an unblocked tracker remains unblocked with reason BLOCK_REASON_C2P_ALLOWED_ONCE', () => {
const { block, reason } = policy.shouldBlock(55, 'essential', 1, 'www.cnn.com', 'https://www.cnn.com/');
expect(block).toBeFalsy();
expect(reason).toBe(BLOCK_REASON_C2P_ALLOWED_ONCE);
});
});

describe('with a globally blocked tracker', () => {
test('a tracker on the site-specific allow list is unblocked with reason BLOCK_REASON_SS_UNBLOCKED', () => {
const { block, reason } = policy.shouldBlock(15, 'site_analytics', 1, 'www.cnn.com', 'https://www.cnn.com/');
expect(block).toBeFalsy();
expect(reason).toBe(BLOCK_REASON_SS_UNBLOCKED);
});
test('a tracker on the site-specific allow list on a black-listed site remains blocked with reason BLOCK_REASON_BLACKLISTED', () => {
const { block, reason } = policy.shouldBlock(41, 'advertising', 1, 'www.tmz.com', 'https://www.tmz.com/');
expect(block).toBeTruthy();
expect(reason).toBe(BLOCK_REASON_BLACKLISTED);
});
test('a tracker on a white-listed site is unblocked with reason BLOCK_REASON_WHITELISTED', () => {
const { block, reason } = policy.shouldBlock(41, 'advertising', 1, 'www.ghostery.com', 'https://www.ghostery.com/');
expect(block).toBeFalsy();
expect(reason).toBe(BLOCK_REASON_WHITELISTED);
});
test('a tracker is blocked with reason BLOCK_REASON_GLOBAL_BLOCKED', () => {
const { block, reason } = policy.shouldBlock(41, 'advertising', 1, 'www.cnn.com', 'https://www.cnn.com/');
expect(block).toBeTruthy();
expect(reason).toBe(BLOCK_REASON_GLOBAL_BLOCKED);
});
});

describe('with a globally unblocked tracker', () => {
test('a tracker on the site-specific block list is blocked with reason BLOCK_REASON_SS_BLOCKED', () => {
const { block, reason } = policy.shouldBlock(13, 'site_analytics', 1, 'www.cnn.com', 'https://www.cnn.com/');
expect(block).toBeTruthy();
expect(reason).toBe(BLOCK_REASON_SS_BLOCKED);
});
test('a tracker on the site-specific block list on a white-listed site is unblocked with reason BLOCK_REASON_WHITELISTED', () => {
const { block, reason } = policy.shouldBlock(15, 'site_analytics', 1, 'www.ghostery.com', 'https://www.ghostery.com/');
expect(block).toBeFalsy();
expect(reason).toBe(BLOCK_REASON_WHITELISTED);
});
test('a tracker on the site-specific allow list is unblocked with reason BLOCK_REASON_SS_UNBLOCKED', () => {
const { block, reason } = policy.shouldBlock(50, 'essential', 1, 'www.cnn.com', 'https://www.cnn.com/');
expect(block).toBeFalsy();
expect(reason).toBe(BLOCK_REASON_SS_UNBLOCKED);
});
test('a tracker on the site-specific allow list on a black-listed site is blocked with reason BLOCK_REASON_BLACKLISTED', () => {
const { block, reason } = policy.shouldBlock(50, 'essential', 1, 'www.tmz.com', 'https://www.tmz.com/');
expect(block).toBeTruthy();
expect(reason).toBe(BLOCK_REASON_BLACKLISTED);
});
test('a tracker on a black-listed site is blocked with reason BLOCK_REASON_BLACKLISTED', () => {
const { block, reason } = policy.shouldBlock(55, 'essential', 1, 'www.tmz.com', 'https://www.tmz.com/');
expect(block).toBeTruthy();
expect(reason).toBe(BLOCK_REASON_BLACKLISTED);
});
test('a tracker is unblocked with reason BLOCK_REASON_GLOBAL_UNBLOCKED', () => {
const { block, reason } = policy.shouldBlock(55, 'essential', 1, 'www.cnn.com', 'https://www.cnn.com/');
expect(block).toBeFalsy();
expect(reason).toBe(BLOCK_REASON_GLOBAL_UNBLOCKED);
});
});
});

describe('test matchesWildcard()', () => {
test('matchesWildcard should return true with wildcard entered ', () => {
let url = 'developer.mozilla.org';
let input = 'developer.*.org';
ProTip! Use n and p to navigate between commits in a pull request.