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-1084 Fix Piwik FPE on ghostery.com #72

Merged
merged 1 commit into from May 22, 2018
Merged
Changes from all commits
Commits
File filter
Filter file types
Jump to
Jump to file
Failed to load files.

Always

Just for now

@@ -145,9 +145,20 @@ class PolicySmartBlock {
* @param {string} requestHost host of the request url
* @return {boolean}
*/
isFirstPartyRequest(tabId, pageHost = '', requestHost = '') {
isFirstPartyRequest(tabId, inputPageHost = '', inputRequestHost = '') {
let pageHost = inputPageHost;
let requestHost = inputRequestHost;

if (!this.shouldCheck(tabId)) { return false; }

// Strip out www. to fix the most common sub-domain issue. See ToDo in function comment.
if (pageHost.startsWith('www.')) {
pageHost = pageHost.slice(4);
}
if (requestHost.startsWith('www.')) {
requestHost = requestHost.slice(4);
}

const min = Math.min(requestHost.length, pageHost.length);
let matches = true;
let i = 0;
@@ -0,0 +1,71 @@
/**
* /src/classes/PolicySmartBlock.js Unit Tests
*
* Ghostery Browser Extension
* http://www.ghostery.com/
*
* Copyright 2018 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 PolicySmartBlock from '../../src/classes/PolicySmartBlock';
let policySmartBlock = new PolicySmartBlock();

// Mock imports for dependencies
jest.mock('../../src/classes/TabInfo', () => {});

describe('src/classes/PolicySmartBlock.js', () => {
describe('PolicySmartBlock constructor tests', () => {
test('policy is constructed correctly', () => {
return expect(policySmartBlock.policy.constructor.name).toBe('Policy');
});
test('allowedCategoriesList is constructed correctly', () => {
const allowedCategoriesList = [
'essential',
'audio_video_player',
'comments',
];
return expect(policySmartBlock.allowedCategoriesList).toEqual(allowedCategoriesList);
});
test('allowedTypesList is constructed correctly', () => {
const allowedTypesList = [
'stylesheet',
'image',
'font',
];
return expect(policySmartBlock.allowedTypesList).toEqual(allowedTypesList);
});
});

describe('PolicySmartBlock isFirstPartyRequest tests', () => {
beforeAll(() => {
policySmartBlock.shouldCheck = jest.fn(() => true);
});

afterAll(() => {
policySmartBlock.mockClear();
});

test('PolicySmartBlock isFirstPartyRequest truthy assertion', () => {
expect(policySmartBlock.isFirstPartyRequest('tabId', 'example.com', 'example.com')).toBeTruthy();
expect(policySmartBlock.isFirstPartyRequest('tabId', 'www.example.com', 'example.com')).toBeTruthy();
expect(policySmartBlock.isFirstPartyRequest('tabId', 'example.com', 'www.example.com')).toBeTruthy();
expect(policySmartBlock.isFirstPartyRequest('tabId', 'www.example.com', 'www.example.com')).toBeTruthy();
expect(policySmartBlock.isFirstPartyRequest('tabId', 'sub.example.com', 'example.com')).toBeTruthy();
expect(policySmartBlock.isFirstPartyRequest('tabId', 'example.com', 'sub.example.com')).toBeTruthy();
expect(policySmartBlock.isFirstPartyRequest('tabId', 'sub1.sub2.sub3.example.com', 'sub2.sub3.example.com')).toBeTruthy();
expect(policySmartBlock.isFirstPartyRequest('tabId', 'www.ghostery.com', 'analytics.ghostery.com')).toBeTruthy();
});

test('PolicySmartBlock isFirstPartyRequest falsy assertion', () => {
expect(policySmartBlock.isFirstPartyRequest('tabId', 'sub1.example.com', 'sub2.example.com')).toBeFalsy();
expect(policySmartBlock.isFirstPartyRequest('tabId', 'sub1.sub2.example.com', 'sub2.sub1.example.com')).toBeFalsy();
expect(policySmartBlock.isFirstPartyRequest('tabId', 'example.com', 'test.com')).toBeFalsy();
expect(policySmartBlock.isFirstPartyRequest('tabId', 'www.example.com', 'www.test.com')).toBeFalsy();
});
});
});
ProTip! Use n and p to navigate between commits in a pull request.