|
1 | 1 | import { expect } from 'chai'
|
2 | 2 | import applyRules from './applyRules.js'
|
3 | 3 |
|
4 |
| -global.chrome = { |
5 |
| - runtime: { |
6 |
| - getURL: (str) => str |
7 |
| - } |
8 |
| -} |
9 |
| - |
10 |
| -const policy = { |
11 |
| - "name": "pineapple", |
12 |
| - "rules": [{ |
13 |
| - "sites": ["*://www.notarealwebsite.com/*"], |
14 |
| - "actions": [ { "action": "block", "message": "not okay" } ] |
15 |
| -}]} |
| 4 | +global.chrome = { runtime: { getURL: (str) => str } } |
| 5 | +const today = new Date(Date.now()).toDateString() |
| 6 | +const tomorrow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000).toDateString() |
| 7 | +const yesterday = new Date(new Date().getTime() - 24 * 60 * 60 * 1000).toDateString() |
| 8 | +const dayBefore = new Date(new Date().getTime() - 48 * 60 * 60 * 1000).toDateString() |
16 | 9 |
|
17 | 10 | describe('applyRules', () => {
|
18 |
| - it('cancels blocked url', () => { |
| 11 | + it('cancels blocked url with no dates', () => { |
| 12 | + const policy = { |
| 13 | + "name": "pineapple", |
| 14 | + "rules": [{ |
| 15 | + "sites": ["*://www.notarealwebsite.com/*"], |
| 16 | + "actions": [{ "action": "block", "message": "not okay" }] |
| 17 | + }] |
| 18 | + } |
19 | 19 | const blockingResponse = applyRules(policy)({ url: 'http://www.notarealwebsite.com' })
|
20 | 20 | const expected = "blocked.html?union=pineapple&msg=not%20okay"
|
21 | 21 | expect(blockingResponse).to.deep.equal({ redirectUrl: expected })
|
22 | 22 | })
|
23 | 23 |
|
| 24 | + it('cancels blocked url with start date but no end date', () => { |
| 25 | + const policy = { |
| 26 | + "name": "pineapple", |
| 27 | + "rules": [{ |
| 28 | + "sites": ["*://www.today.com/*"], |
| 29 | + "actions": [{ |
| 30 | + "action": "block", |
| 31 | + "message": "not okay", |
| 32 | + "startDate": today |
| 33 | + }] |
| 34 | + }] |
| 35 | + } |
| 36 | + |
| 37 | + const blockingResponse = applyRules(policy)({ url: 'http://www.today.com' }) |
| 38 | + const expected = "blocked.html?union=pineapple&msg=not%20okay" |
| 39 | + expect(blockingResponse).to.deep.equal({ redirectUrl: expected }) |
| 40 | + }) |
| 41 | + |
| 42 | + it('cancels blocked url with start and end date', () => { |
| 43 | + const policy = { |
| 44 | + "name": "pineapple", |
| 45 | + "rules": [{ |
| 46 | + "sites": ["*://www.today.com/*"], |
| 47 | + "actions": [{ |
| 48 | + "action": "block", |
| 49 | + "message": "not okay", |
| 50 | + "startDate": today, |
| 51 | + "endDate": tomorrow |
| 52 | + }] |
| 53 | + }] |
| 54 | + } |
| 55 | + const blockingResponse = applyRules(policy)({ url: 'http://www.today.com' }) |
| 56 | + const expected = "blocked.html?union=pineapple&msg=not%20okay" |
| 57 | + expect(blockingResponse).to.deep.equal({ redirectUrl: expected }) |
| 58 | + }) |
| 59 | + |
| 60 | + it('does nothing if blocking start date is in the future', () => { |
| 61 | + const policy = { |
| 62 | + "name": "pineapple", |
| 63 | + "rules": [{ |
| 64 | + "sites": ["*://www.tomorrow.com/*"], |
| 65 | + "actions": [{ |
| 66 | + "action": "block", |
| 67 | + "message": "not okay", |
| 68 | + "startDate": tomorrow |
| 69 | + }] |
| 70 | + }] |
| 71 | + } |
| 72 | + const blockingResponse = applyRules(policy)({ url: 'http://www.tomorrow.com' }) |
| 73 | + const expected = "blocked.html?union=pineapple&msg=not%20okay" |
| 74 | + expect(blockingResponse).to.deep.equal({}) |
| 75 | + }) |
| 76 | + |
| 77 | + it('does nothing if blocking end date is passed', () => { |
| 78 | + const policy = { |
| 79 | + "name": "pineapple", |
| 80 | + "rules": [{ |
| 81 | + "sites": ["*://www.yesterday.com/*"], |
| 82 | + "actions": [{ |
| 83 | + "action": "block", |
| 84 | + "message": "not okay", |
| 85 | + "startDate": dayBefore, |
| 86 | + "endDate": yesterday |
| 87 | + }] |
| 88 | + }] |
| 89 | + } |
| 90 | + const blockingResponse = applyRules(policy)({ url: 'http://www.tomorrow.com' }) |
| 91 | + const expected = "blocked.html?union=pineapple&msg=not%20okay" |
| 92 | + expect(blockingResponse).to.deep.equal({}) |
| 93 | + }) |
| 94 | + |
24 | 95 | it('does nothing if url not listed', () => {
|
| 96 | + const policy = { |
| 97 | + "name": "pineapple", |
| 98 | + "rules": [{ |
| 99 | + "sites": ["*://www.notarealwebsite.com/*"], |
| 100 | + "actions": [{ "action": "block", "message": "not okay" }] |
| 101 | + }] |
| 102 | + } |
25 | 103 | const blockingResponse = applyRules(policy)({ url: 'http://abc' })
|
26 | 104 |
|
27 | 105 | expect(blockingResponse).to.deep.equal({})
|
|
0 commit comments