Skip to content

Commit

Permalink
#43 - move optional permission to the permission
Browse files Browse the repository at this point in the history
  • Loading branch information
Manvel committed Oct 19, 2019
1 parent 7cb5ab7 commit 8935e11
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"build": "cd src; zip -r ../privacy-manager.zip .",
"test": "npm run test:eslint && npm run test:stylelint && npm run test:puppeteer",
"test:puppeteer": "npm run build:webpack && mocha test/*.js",
"test:puppeteer": "npm run build:webpack && mocha test/puppeteer.js",
"test:eslint": "eslint src",
"test:stylelint": "stylelint src/**/*.css",
"import:components": "pm-components --single-bundle --output dist/js",
Expand Down
58 changes: 58 additions & 0 deletions test/manifest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* This file is part of Privacy Manager.
* Copyright (C) 2017-present Manvel Saroyan
*
* Privacy Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Privacy Manager is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Privacy Manager. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* Currently there is no way to request and remove "<all_urls>" optional
* permissions when running tests in puppeteer, so in order to test the
* functionality that rely on the permission, the manifest file needs to be
* modified before running the puppeteer test.
* https://github.com/GoogleChrome/puppeteer/issues/5054
*/

const {readFile, writeFile} = require("fs").promises;
const manifestFile = "dist/manifest.json";
const allOrigins = "<all_urls>";

/**
* Move "<all_urls>" from optional_permissions to permissions
*/
async function allUrlsToPermissions()
{
const manifest = await getManifestFile();
manifest.permissions.push(manifest.optional_permissions[0]);
delete manifest.optional_permissions;
await writeFile(manifestFile, JSON.stringify(manifest, null, 2), "utf8");
}

/**
* Move "<all_urls>" back from permissions to optional_permissions.
*/
async function restorePermissions()
{
const manifest = await getManifestFile();
manifest.optional_permissions = [allOrigins];
manifest.permissions = manifest.permissions.filter(e => e !== allOrigins);
await writeFile(manifestFile, JSON.stringify(manifest, null, 2), "utf8");
}

async function getManifestFile()
{
return JSON.parse(await readFile(manifestFile));
}

module.exports = {allUrlsToPermissions, restorePermissions};
3 changes: 3 additions & 0 deletions test/index.js → test/puppeteer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const puppeteer = require("puppeteer");
const assert = require("assert");
const {allUrlsToPermissions, restorePermissions} = require("./manifest");
const additionalPermission = {"origins": ["<all_urls>"]};

const extensionPath = "dist";
Expand All @@ -11,6 +12,7 @@ let additionalPermissionHandles;

before(async() =>
{
await allUrlsToPermissions();
// https://gokatz.me/blog/automate-chrome-extension-testing/
browser = await puppeteer.launch({
headless: false, // extension are allowed only in the head-full mode
Expand Down Expand Up @@ -149,5 +151,6 @@ describe("Testing Privacy Manager extension", () =>

after(async() =>
{
await restorePermissions();
await browser.close();
});

0 comments on commit 8935e11

Please sign in to comment.