Skip to content

Commit

Permalink
fix(test): 🤷 how did this get removed.
Browse files Browse the repository at this point in the history
Signed-off-by: Nishant Arora <[email protected]>
  • Loading branch information
whizzzkid committed Jul 28, 2023
1 parent 7d1e97c commit 33b8062
Showing 1 changed file with 134 additions and 69 deletions.
203 changes: 134 additions & 69 deletions test/functional/lib/redirect-handler/blockOrObserve.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { expect } from 'chai'
import { before, describe, it } from 'mocha'
import sinon from 'sinon'
import browserMock from 'sinon-chrome'
import { expect } from 'chai';
import { before, describe, it } from 'mocha';
import sinon from 'sinon';
import browserMock from 'sinon-chrome';

import { optionDefaults } from '../../../../add-on/src/lib/options.js'
import { addRuleToDynamicRuleSetGenerator, cleanupRules, isLocalHost, supportsBlock } from '../../../../add-on/src/lib/redirect-handler/blockOrObserve'
import { initState } from '../../../../add-on/src/lib/state.js'
import isManifestV3, { manifestVersion } from '../../../helpers/is-mv3-testing-enabled.js'
import DeclarativeNetRequestMock from './declarativeNetRequest.mock.js'
import { optionDefaults } from '../../../../add-on/src/lib/options.js';
import { addRuleToDynamicRuleSetGenerator, cleanupRules, isLocalHost } from '../../../../add-on/src/lib/redirect-handler/blockOrObserve';
import { initState } from '../../../../add-on/src/lib/state.js';
import isManifestV3 from '../../../helpers/is-mv3-testing-enabled';
import DeclarativeNetRequestMock from './declarativeNetRequest.mock.js';

const dynamicRulesConditions = (regexFilter) => ({
regexFilter,
Expand All @@ -31,7 +31,52 @@ const dynamicRulesConditions = (regexFilter) => ({
]
})

const manifestVeresion = isManifestV3 ? 'MV3' : 'MV2'
const LAST_GROUP_REGEX = '((?:[^\\.]|$).*)$'

/**
* Ensures that the tab is redirected to the given url on the first request.
*
* @param url
*/
function ensureTabRedirected (url): void {
expect(browserMock.tabs.query.called).to.be.true
expect(browserMock.tabs.update.called).to.be.true
expect(browserMock.tabs.update.lastCall.args).to.deep.equal([40, { url }])
}

/**
* Ensures that the declarativeNetRequest API is called with the expected rule.
* @param expectedCondition
* @param regexSubstitution
*/
function ensureDeclrativeNetRequetRuleIsAdded ({
addRuleLength = 1,
callIndex = 0,
expectedCondition,
regexSubstitution,
removedRulesIds = [],
}: {
addRuleLength?: number
callIndex?: number
expectedCondition: string
regexSubstitution: string
removedRulesIds?: number[]
}): void {
if (callIndex < 0) {
callIndex = browserMock.declarativeNetRequest.updateDynamicRules.getCalls().length + callIndex
}
expect(browserMock.declarativeNetRequest.updateDynamicRules.called).to.be.true
const [{ addRules, removeRuleIds }] = browserMock.declarativeNetRequest.updateDynamicRules.getCall(callIndex).args
expect(removeRuleIds).to.deep.equal(removedRulesIds)
if (addRuleLength > 0) {
expect(addRules).to.have.lengthOf(addRuleLength)
const [{ id, priority, action, condition }] = addRules
expect(id).to.be.a('number')
expect(priority).to.equal(1)
expect(action).to.deep.equal({ type: 'redirect', redirect: { regexSubstitution } })
expect(condition).to.deep.equal(dynamicRulesConditions(expectedCondition))
}
}

describe('lib/redirect-handler/blockOrObserve', () => {
before(function () {
Expand Down Expand Up @@ -59,16 +104,6 @@ describe('lib/redirect-handler/blockOrObserve', () => {
})
})

describe(`supportsBlock on ${manifestVersion}` , () => {
it(`should return ${isManifestV3 ? false : true} for ${manifestVersion}`, function () {
if (isManifestV3) {
expect(supportsBlock()).to.be.false
} else {
expect(supportsBlock()).to.be.true
}
})
})

describe('addRuleToDynamicRuleSetGenerator', () => {
let addRuleToDynamicRuleSet
let state
Expand All @@ -82,6 +117,9 @@ describe('lib/redirect-handler/blockOrObserve', () => {

beforeEach(async () => {
sinonSandbox.restore()
browserMock.tabs.query.resetHistory()
browserMock.tabs.reload.resetHistory()
browserMock.tabs.update.resetHistory()
browserMock.declarativeNetRequest = sinonSandbox.spy(new DeclarativeNetRequestMock())
// this cleans up the rules from the previous test stored in memory.
await cleanupRules(true)
Expand All @@ -97,6 +135,7 @@ describe('lib/redirect-handler/blockOrObserve', () => {
// when redirectUrl is different from originUrl, but both are localhost.
await addRuleToDynamicRuleSet({ originUrl: 'http://localhost:9001/foo', redirectUrl: 'http://localhost:9001/bar' })
expect(browserMock.declarativeNetRequest.updateDynamicRules.called).to.be.false
expect (browserMock.tabs.query.called).to.be.false
})

it('Should allow pages to be recovered', async () => {
Expand All @@ -105,78 +144,104 @@ describe('lib/redirect-handler/blockOrObserve', () => {
originUrl: 'http://localhost:8080',
redirectUrl: 'chrome-extension://some-path/dist/recover/recovery.html'
})
expect(browserMock.declarativeNetRequest.updateDynamicRules.called).to.be.true
ensureTabRedirected('chrome-extension://some-path/dist/recover/recovery.html')
ensureDeclrativeNetRequetRuleIsAdded({
expectedCondition: `^https?\\:\\/\\/localhost\\:8080${LAST_GROUP_REGEX}`,
regexSubstitution: 'chrome-extension://some-path/dist/recover/recovery.html\\1',
})
})

it('Should add redirect rules for local gateway', async () => {
await addRuleToDynamicRuleSet({
originUrl: 'https://ipfs.io/ipns/en.wikipedia-on-ipfs.org',
redirectUrl: 'http://localhost:8080/ipns/en.wikipedia-on-ipfs.org'
})
expect(browserMock.declarativeNetRequest.updateDynamicRules.called).to.be.true
const [{ addRules, removeRuleIds }] = browserMock.declarativeNetRequest.updateDynamicRules.firstCall.args
expect(removeRuleIds).to.deep.equal([])
expect(addRules).to.have.lengthOf(1)
const [{ id, priority, action, condition }] = addRules
expect(id).to.be.a('number')
expect(priority).to.equal(1)
expect(action).to.deep.equal({ type: 'redirect', redirect: { "regexSubstitution": "http://localhost:8080\\1" } })
expect(condition).to.deep.equal(dynamicRulesConditions('^https?\\:\\/\\/ipfs\\.io((?:[^\\.]|$).*)$'))
ensureTabRedirected('http://localhost:8080/ipns/en.wikipedia-on-ipfs.org')
ensureDeclrativeNetRequetRuleIsAdded({
expectedCondition: `^https?\\:\\/\\/ipfs\\.io${LAST_GROUP_REGEX}`,
regexSubstitution: 'http://localhost:8080\\1'
})
})

it('Should add redirect for local gateway where originUrl is similar to redirectUrl', async () => {
await addRuleToDynamicRuleSet({
originUrl: 'https://docs.ipfs.tech',
redirectUrl: 'http://localhost:8080/ipns/docs.ipfs.tech'
})
expect(browserMock.declarativeNetRequest.updateDynamicRules.called).to.be.true
const [{ addRules, removeRuleIds }] = browserMock.declarativeNetRequest.updateDynamicRules.firstCall.args
expect(removeRuleIds).to.deep.equal([])
expect(addRules).to.have.lengthOf(1)
const [{ id, priority, action, condition }] = addRules
expect(id).to.be.a('number')
expect(priority).to.equal(1)
expect(action).to.deep.equal({
type: 'redirect', redirect: {
"regexSubstitution": "http://localhost:8080/ipns/docs.ipfs.tech\\1"
}
})
expect(condition).to.deep.equal(dynamicRulesConditions('^https?\\:\\/\\/docs\\.ipfs\\.tech((?:[^\\.]|$).*)$'))
ensureTabRedirected('http://localhost:8080/ipns/docs.ipfs.tech')
ensureDeclrativeNetRequetRuleIsAdded({
expectedCondition: `^https?\\:\\/\\/docs\\.ipfs\\.tech${LAST_GROUP_REGEX}`,
regexSubstitution: 'http://localhost:8080/ipns/docs.ipfs.tech\\1'
})
})

it('Should add redirect for local gateway where originUrl is similar to redirectUrl and is not https', async () => {
await addRuleToDynamicRuleSet({
originUrl: 'http://docs.ipfs.tech',
redirectUrl: 'http://localhost:8080/ipns/docs.ipfs.tech'
})
expect(browserMock.declarativeNetRequest.updateDynamicRules.called).to.be.true
const [{ addRules, removeRuleIds }] = browserMock.declarativeNetRequest.updateDynamicRules.firstCall.args
expect(removeRuleIds).to.deep.equal([])
expect(addRules).to.have.lengthOf(1)
const [{ id, priority, action, condition }] = addRules
expect(id).to.be.a('number')
expect(priority).to.equal(1)
expect(action).to.deep.equal({
type: 'redirect', redirect: {
"regexSubstitution": "http://localhost:8080/ipns/docs.ipfs.tech\\1"
}
})
expect(condition).to.deep.equal(dynamicRulesConditions('^https?\\:\\/\\/docs\\.ipfs\\.tech((?:[^\\.]|$).*)$'))
ensureTabRedirected('http://localhost:8080/ipns/docs.ipfs.tech')
ensureDeclrativeNetRequetRuleIsAdded({
expectedCondition: `^https?\\:\\/\\/docs\\.ipfs\\.tech${LAST_GROUP_REGEX}`,
regexSubstitution: 'http://localhost:8080/ipns/docs.ipfs.tech\\1'
})
})

it('Should refresh the tab when redirect URL is added', async () => {
if (!isManifestV3) {
// TODO
} else {

browserMock.tabs.query.resolves([{id: 1234}])
await addRuleToDynamicRuleSet({
originUrl: 'https://ipfs.io/ipns/en.wikipedia-on-ipfs.org',
redirectUrl: 'http://localhost:8080/ipns/en.wikipedia-on-ipfs.org'
})
sinon.assert.calledWith(browserMock.tabs.query, { url: 'https://ipfs.io/ipns/en.wikipedia-on-ipfs.org*' })
sinon.assert.calledWith(browserMock.tabs.update, 1234, { url: 'http://localhost:8080/ipns/en.wikipedia-on-ipfs.org' })
}
it('Should remove the old rule when redirect changes for local gateway', async () => {
await addRuleToDynamicRuleSet({
originUrl: 'http://docs.ipfs.tech',
redirectUrl: 'http://localhost:8080/ipns/docs.ipfs.tech'
})
ensureTabRedirected('http://localhost:8080/ipns/docs.ipfs.tech')
ensureDeclrativeNetRequetRuleIsAdded({
expectedCondition: `^https?\\:\\/\\/docs\\.ipfs\\.tech${LAST_GROUP_REGEX}`,
regexSubstitution: 'http://localhost:8080/ipns/docs.ipfs.tech\\1'
})
const [{ addRules }] = browserMock.declarativeNetRequest.updateDynamicRules.firstCall.args

await browserMock.declarativeNetRequest.updateDynamicRules.resetHistory()
// assuming the localhost changed or the redirectURL changed.
await addRuleToDynamicRuleSet({
originUrl: 'http://docs.ipfs.tech',
redirectUrl: 'http://localhost:8081/ipns/docs.ipfs.tech'
})
ensureTabRedirected('http://localhost:8081/ipns/docs.ipfs.tech')
ensureDeclrativeNetRequetRuleIsAdded({
expectedCondition: `^https?\\:\\/\\/docs\\.ipfs\\.tech${LAST_GROUP_REGEX}`,
regexSubstitution: 'http://localhost:8081/ipns/docs.ipfs.tech\\1',
removedRulesIds: [addRules[0].id]
})
})

it('Should remove the old rules when companion is no longer in active state', async () => {
// first redirect
const getRuleIdsAddedSoFar = () => browserMock.declarativeNetRequest.updateDynamicRules.getCalls().map(({args}) => args[0]?.addRules.map(rule => rule.id).flat()).flat()

await addRuleToDynamicRuleSet({
originUrl: 'http://docs.ipfs.tech',
redirectUrl: 'http://localhost:8080/ipns/docs.ipfs.tech'
})

await addRuleToDynamicRuleSet({
originUrl: 'http://awesome.ipfs.io',
redirectUrl: 'http://localhost:8080/ipns/awesome.ipfs.io'
})

state.active = false
await addRuleToDynamicRuleSet({
originUrl: 'https://ipfs.io/ipns/en.wikipedia-on-ipfs.org',
redirectUrl: 'http://localhost:8080/ipns/en.wikipedia-on-ipfs.org'
})

ensureTabRedirected('http://localhost:8080/ipns/en.wikipedia-on-ipfs.org')
ensureDeclrativeNetRequetRuleIsAdded({
addRuleLength: 0,
callIndex: -1,
expectedCondition: `^https?\\:\\/\\/ipfs\\.io${LAST_GROUP_REGEX}`,
regexSubstitution: 'http://localhost:8080\\1',
removedRulesIds: getRuleIdsAddedSoFar()
})

})
})
})

0 comments on commit 33b8062

Please sign in to comment.