Skip to content

Commit abfbab1

Browse files
committed
saves policy date and url
1 parent e2a868a commit abfbab1

File tree

7 files changed

+154
-12
lines changed

7 files changed

+154
-12
lines changed

package-lock.json

+100
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"build": "rollup -c rollup.config.js",
88
"watch": "npm run build -- -w",
9-
"test": "mocha src/**/*.spec.js --require babel-register --require babel-polyfill",
9+
"test": "mocha src/**/*.spec.js --require babel-register --require babel-polyfill --require testSetup.js",
1010
"test-watch": "npm run test -- -w"
1111
},
1212
"repository": {
@@ -25,8 +25,11 @@
2525
"babel-register": "^6.26.0",
2626
"chai": "^4.2.0",
2727
"mocha": "^5.2.0",
28+
"node-fetch": "^2.3.0",
2829
"rollup": "^1.0.0",
29-
"rollup-copy-plugin": "^0.1.0"
30+
"rollup-copy-plugin": "^0.1.0",
31+
"sinon": "^7.2.2",
32+
"sinon-chai": "^3.3.0"
3033
},
3134
"dependencies": {}
3235
}

src/backgroundScript/applyRules.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const policy = {
1717
describe('applyRules', () => {
1818
it('cancels blocked url', () => {
1919
const blockingResponse = applyRules(policy)({ url: 'http://www.notarealwebsite.com' })
20-
const expected = "src/blockedPage/blocked.html?union=pineapple&msg=not%20okay"
20+
const expected = "blocked.html?union=pineapple&msg=not%20okay"
2121
expect(blockingResponse).to.deep.equal({ redirectUrl: expected })
2222
})
2323

src/popup/loadPolicy.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,9 @@ const fetchPolicyFile = (policyFileUrl) => {
99
.then(resp => resp.json())
1010
}
1111

12-
const loadPolicy = (form) => (ev) => {
13-
ev.preventDefault();
12+
export default (form) => {
1413
const policyFileUrl = new FormData(form).get('policyFile')
15-
fetchPolicyFile(policyFileUrl).then(p => storePolicyForUser(p, policyFileUrl));
14+
return fetchPolicyFile(policyFileUrl)
15+
.then(p => storePolicyForUser(p, policyFileUrl));
1616
}
1717

18-
document.addEventListener('DOMContentLoaded', () => {
19-
const policyFileForm = document.getElementById('policyFileForm')
20-
policyFileForm.addEventListener('submit', loadPolicy(policyFileForm))
21-
})
22-

src/popup/loadPolicy.spec.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import loadPolicy from './loadPolicy.js'
2+
import { stub } from 'sinon'
3+
import { expect } from 'chai'
4+
5+
describe('loadPolicy', () => {
6+
beforeEach(() => {
7+
global.fetch = stub().resolves({ json: () => ({ policy: 'somepolicy' }) })
8+
global.chrome = { storage: { sync: { set: stub() } } }
9+
global.FormData = class FormData { get() { return 'policyFileURL' } }
10+
})
11+
12+
afterEach(() => {
13+
delete global.fetch
14+
delete global.chrome
15+
delete global.FormData
16+
})
17+
18+
it('fetches policy from url', () => {
19+
return loadPolicy().then(() => {
20+
expect(global.fetch).to.have.been.calledWith('policyFileURL')
21+
})
22+
})
23+
24+
it('sets policy in storage', () => {
25+
return loadPolicy().then(() => {
26+
const args = global.chrome.storage.sync.set.firstCall.args[0]
27+
expect(args.policy).to.deep.equal({ policy: 'somepolicy' })
28+
expect(args.policyFileUrl).to.deep.equal('policyFileURL')
29+
})
30+
})
31+
})

src/popup/popup.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
import './loadPolicy'
1+
import loadPolicy from './loadPolicy'
2+
3+
document.addEventListener('DOMContentLoaded', () => {
4+
const policyFileForm = document.getElementById('policyFileForm')
5+
policyFileForm.addEventListener('submit', (ev) => {
6+
ev.preventDefault();
7+
loadPolicy(policyFileForm)
8+
})
9+
})

testSetup.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const chai = require("chai");
2+
const sinonChai = require("sinon-chai");
3+
4+
chai.use(sinonChai);
5+

0 commit comments

Comments
 (0)