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

feat(mv3): Redirection Tests #1236

Merged
merged 55 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from 49 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
7b4d93d
feat(mv3): :sparkles: Patching countly-sdk-web
whizzzkid Jul 12, 2023
f3305fe
feat(mv3): :sparkles: Implementing Custom Async Store.
whizzzkid Jul 12, 2023
83af58f
chore(mv3): :adhesive_bandage: Hooking everything up together.
whizzzkid Jul 12, 2023
d504c71
fix(mv3): Countly Patching + ignite-metrics@2.0.0
whizzzkid Jul 13, 2023
4fab16a
fix(mv3): :adhesive_bandage: Patching the Patch
whizzzkid Jul 13, 2023
f02d6a2
fix: tests
whizzzkid Jul 13, 2023
dbab08c
fix: lint
whizzzkid Jul 13, 2023
394b13b
fix(mv3): :recycle: Refactoring `supportsBlock` Checks.
whizzzkid Jul 18, 2023
cbb4ac6
fix(mv3): Regex Bug
whizzzkid Jul 18, 2023
5794aea
feat: Migrating blocking redirection test to observing redirection test
whizzzkid Jul 18, 2023
0b0af76
fix(mv3): :wrench: Fixing the mocha-setup.
whizzzkid Jul 18, 2023
0de2e89
fix(mv3): :recycle: Moving Setup Files.
whizzzkid Jul 19, 2023
1d46a57
fix(mv3): gateway-redirect tests now fixed.
whizzzkid Jul 19, 2023
44653de
fix: :adhesive_bandage: Patching error messages
whizzzkid Jul 19, 2023
64fcde7
fix(patch): countly-web-sdk
whizzzkid Jul 19, 2023
66b13dc
fix(patch): :pin: Pinning countly-web-sdk to 23.2.2
whizzzkid Jul 19, 2023
39af513
Merge branch 'fix/analytics' into feat/redirection-tests
whizzzkid Jul 19, 2023
bf98b8f
fix(mv3): :lipstick: Fixing Lint
whizzzkid Jul 19, 2023
0b8b308
Merge branch 'rc/3.0-mv3' into feat/redirection-tests
whizzzkid Jul 19, 2023
3b80b2a
Merge branch 'rc/3.0-mv3' into feat/redirection-tests
whizzzkid Jul 19, 2023
50797d0
feat: protocol-handler-redirection-tests
whizzzkid Jul 19, 2023
46fca05
feat: more tests fixed
whizzzkid Jul 19, 2023
8e3aee2
fix: More tests
whizzzkid Jul 19, 2023
83ff056
fix: lint fix
whizzzkid Jul 19, 2023
38d7b60
test: merge mocha-setup files (#1246)
SgtPooki Jul 25, 2023
4039f0f
Merge branch 'rc/3.0-mv3' into feat/redirection-tests
whizzzkid Jul 25, 2023
dd86afa
test: fix after merge
SgtPooki Jul 25, 2023
0e316a2
test: fix after merge
SgtPooki Jul 25, 2023
7b94054
fix: typerrors for localstorage
whizzzkid Jul 25, 2023
8d95a1f
fix: Updating test:functional_MV3 command.
whizzzkid Jul 26, 2023
9bbf271
fix: setup
whizzzkid Jul 26, 2023
5ce5de8
fix(test): Fixing tests
whizzzkid Jul 26, 2023
e5d42e0
fix(lint): Becuase Ofcourse
whizzzkid Jul 26, 2023
a260eb9
feat(test): scaffolding mv3 + mv2 calls in a single check.
whizzzkid Jul 27, 2023
eb8fea0
feat(test): unskipping and upgrading dnslink tests to mv3
whizzzkid Jul 27, 2023
cc109b0
feat(test): Upgrading workaround tests to MV3
whizzzkid Jul 27, 2023
441191e
fix(test): removing all skips with better checks.
whizzzkid Jul 27, 2023
5ac8417
fix(test): :recycle: Refactoring tests and removing redundant calls.
whizzzkid Jul 27, 2023
02042cb
fix(test): More Dryer
whizzzkid Jul 27, 2023
f3d4605
fix(test): one more
whizzzkid Jul 27, 2023
47d72f8
fix(test): :pencil2: Renaming isMv3TestingEnabled -> isManifestV3
whizzzkid Jul 27, 2023
239984d
fix(test): refactor expectNoRedirect
whizzzkid Jul 27, 2023
94cb141
fix(test): :recycle: Refactoring more.
whizzzkid Jul 27, 2023
e3e73c9
fix: replacing checks to undefined
whizzzkid Jul 27, 2023
80e7d51
fix: renaming expectNoRedirect -> ensureNoRedirect
whizzzkid Jul 27, 2023
7d1e97c
fix(test): Adding missing JSDoc
whizzzkid Jul 27, 2023
33b8062
fix(test): :shrug: how did this get removed.
whizzzkid Jul 28, 2023
ae23f4c
fix(test): :wastebasket: removed.
whizzzkid Jul 28, 2023
b77ab7b
fix(test): Suggestion
whizzzkid Jul 28, 2023
bb85dbb
Merge branch 'rc/3.0-mv3' into feat/redirection-tests
whizzzkid Jul 28, 2023
6fe39a3
fix(test): :adhesive_bandage: Bad Merge
whizzzkid Jul 28, 2023
e56bb6a
fix(test): sequential expectNoRedirect
lidel Aug 3, 2023
6bd8c3c
Update add-on/src/lib/redirect-handler/blockOrObserve.ts
whizzzkid Aug 4, 2023
47ea944
Merge branch 'rc/3.0-mv3' into feat/redirection-tests
whizzzkid Aug 4, 2023
3013400
fix(rules): Better Redirect Rules (#1256)
whizzzkid Aug 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions add-on/src/lib/ipfs-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export function createRequestModifier (getState, dnslinkResolver, ipfsPathValida
}
// poor-mans protocol handlers - https://github.com/ipfs/ipfs-companion/issues/164#issuecomment-328374052
if (state.catchUnhandledProtocols && mayContainUnhandledIpfsProtocol(request)) {
const fix = normalizedUnhandledIpfsProtocol(request, state.pubGwURLString)
const fix = await normalizedUnhandledIpfsProtocol(request, state.pubGwURLString)
if (fix) {
return fix
}
Expand Down Expand Up @@ -478,14 +478,14 @@ export function createRequestModifier (getState, dnslinkResolver, ipfsPathValida
* @param {object} input contains originUrl and redirectUrl.
* @returns
*/
function handleRedirection ({ originUrl, redirectUrl }) {
async function handleRedirection ({ originUrl, redirectUrl }) {
if (redirectUrl !== '' && originUrl !== '' && redirectUrl !== originUrl) {
if (supportsBlock) {
if (supportsBlock()) {
return { redirectUrl }
}

// Let browser handle redirection MV3 style.
addRuleToDynamicRuleSet({ originUrl, redirectUrl })
await addRuleToDynamicRuleSet({ originUrl, redirectUrl })
}
}

Expand Down
18 changes: 11 additions & 7 deletions add-on/src/lib/redirect-handler/blockOrObserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ interface messageToSelf {
// We need to check if the browser supports the declarativeNetRequest API.
// TODO: replace with check for `Blocking` in `chrome.webRequest.OnBeforeRequestOptions`
// which is currently a bug https://bugs.chromium.org/p/chromium/issues/detail?id=1427952
export const supportsBlock = !(browser.declarativeNetRequest?.MAX_NUMBER_OF_DYNAMIC_AND_SESSION_RULES === 5000)
// this needs to be a function call, because in tests we mock browser.declarativeNetRequest
// the way sinon ends up stubbing it, it's not directly available in the global scope on import
// rather it gets replaced dynamically when the module is imported. Which means, we can't
// just check for the existence of the property, we need to call the browser instance at that point.
export const supportsBlock = (): boolean => !(browser.declarativeNetRequest?.MAX_NUMBER_OF_DYNAMIC_AND_SESSION_RULES === 5000)
whizzzkid marked this conversation as resolved.
Show resolved Hide resolved
export const GLOBAL_STATE_CHANGE = 'GLOBAL_STATE_CHANGE'
export const GLOBAL_STATE_OPTION_CHANGE = 'GLOBAL_STATE_OPTION_CHANGE'

Expand All @@ -51,7 +55,7 @@ export async function notifyOptionChange (): Promise<void> {
*/
async function sendMessageToSelf (msg: typeof GLOBAL_STATE_CHANGE | typeof GLOBAL_STATE_OPTION_CHANGE): Promise<void> {
// this check ensures we don't send messages to ourselves if blocking mode is enabled.
if (!supportsBlock) {
if (!supportsBlock()) {
const message: messageToSelf = { type: msg }
await browser.runtime.sendMessage(message)
}
Expand Down Expand Up @@ -90,7 +94,7 @@ export function isLocalHost (url: string): boolean {
function escapeURLRegex (str: string): string {
// these characters are allowed in the URL, but not in the regex.
// eslint-disable-next-line no-useless-escape
const ALLOWED_CHARS_URL_REGEX = /([:\/\?#\[\]@!$&'\(\ )\*\+,;=-_\.~])/g
const ALLOWED_CHARS_URL_REGEX = /([:\/\?#\[\]@!$&'\(\ )\*\+,;=\-_\.~])/g
return str.replace(ALLOWED_CHARS_URL_REGEX, '\\$1')
}

Expand Down Expand Up @@ -137,7 +141,7 @@ function constructRegexFilter ({ originUrl, redirectUrl }: redirectHandlerInput)

// If the browser supports the declarativeNetRequest API, we can block the request.
export function getExtraInfoSpec<T> (additionalParams: T[] = []): T[] {
if (supportsBlock) {
if (supportsBlock()) {
return ['blocking' as T, ...additionalParams]
}
return additionalParams
Expand Down Expand Up @@ -222,7 +226,7 @@ async function reconcileRulesAndRemoveOld (state: CompanionState): Promise<void>
if (rules.length === 0) {
// we need to populate old rules.
for (const [regexFilter, { regexSubstitution, id }] of savedRegexFilters.entries()) {
addRules.push(generateRule(id, regexFilter, regexSubstitution))
addRules.push(generateAddRule(id, regexFilter, regexSubstitution))
}
}

Expand Down Expand Up @@ -259,7 +263,7 @@ function saveAndGenerateRule (
const id = Math.floor(Math.random() * 29999)
// We need to save the regex filter and ID to check if the rule already exists later.
savedRegexFilters.set(regexFilter, { id, regexSubstitution })
return generateRule(id, regexFilter, regexSubstitution, excludedInitiatorDomains)
return generateAddRule(id, regexFilter, regexSubstitution, excludedInitiatorDomains)
}

/**
Expand All @@ -270,7 +274,7 @@ function saveAndGenerateRule (
* @param excludedInitiatorDomains - The domains that are excluded from the rule.
* @returns
*/
function generateRule (
export function generateAddRule (
id: number,
regexFilter: string,
regexSubstitution: string,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@
"watch:js:webpack": "webpack --watch --mode development --devtool inline-source-map --config ./webpack.config.js",
"test": "run-s test:*",
"test:e2e": "mocha --timeout 300000 \"test/e2e/**/*.test.js\"",
"test:functional": "c8 mocha --timeout 5000 \"test/functional/**/*.test.js\" \"test/functional/**/*.test.ts\"",
"test:functional": "c8 mocha --timeout 5000 --file \"test/setup/mocha-setup.js\" \"test/functional/**/*.test.js\" \"test/functional/**/*.test.ts\"",
"lint": "run-s lint:*",
"test:functional_MV3": "cross-env TEST_MV3=true npm run test:functional",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

"lint:standard": "ts-standard -v \"*.js\" \"add-on/src/**/*.js\" \"add-on/src/**/*.ts\" \"test/**/*.js\" \"scripts/**/*.js\"",
"lint:web-ext": "shx cat add-on/manifest.common.json add-on/manifest.chromium.json add-on/manifest.firefox-beta.json | json --deep-merge > add-on/manifest.json && web-ext lint --firefox-preview",
"fix:lint": "run-s fix:lint:*",
Expand Down
20 changes: 2 additions & 18 deletions test/functional/lib/ipfs-companion.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
import { describe, it, before, after } from 'mocha'
import { expect } from 'chai'
import { after, before, describe, it } from 'mocha'
import browser from 'sinon-chrome'
import AbortController from 'abort-controller'
import { URL } from 'url'
import { optionDefaults } from '../../../add-on/src/lib/options.js'
browser.runtime.id = 'testid'
global.browser = browser
global.AbortController = AbortController
global.chrome = browser
global.navigator = {
clipboard: {
writeText: () => {}
}
}

// We need to do this because global is not mapped otherwise, we need to stub browser and chrome runtime
// so that the webextension-polyfill does not complain about the test runner not being a browser instance.
const init = async () => (await import('../../../add-on/src/lib/ipfs-companion.js')).default()

describe('lib/ipfs-companion.js', function () {
describe('init', function () {
before(function () {
global.localStorage = global.localStorage || {}
global.URL = global.URL || URL
global.screen = { width: 1024, height: 720 }
global.addEventListener = () => { }
Comment on lines 14 to 16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to be careful about overriding globals in specific tests. I also thought about throwing together some helper functions that would do global setup via something like setupBrowserBefore and setupTabsBefore that would allow us to componentize our setups and more explicitly control what is being done in each test suite, but maybe we can take that on when/if it bites us later.

Expand All @@ -44,12 +34,6 @@ describe('lib/ipfs-companion.js', function () {
})

describe.skip('onStorageChange()', function () {
before(function () {
global.window = {}
global.browser = browser
global.URL = URL
})

it('should update ipfs API instance on IPFS API URL change', async function () {
browser.storage.local.get.resolves(optionDefaults)
browser.storage.local.set.resolves()
Expand Down
33 changes: 1 addition & 32 deletions test/functional/lib/ipfs-import.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
'use strict'
import { describe, it, before, after } from 'mocha'
import { expect } from 'chai'
import { useFakeTimers } from 'sinon'
import browser from 'sinon-chrome'
import { describe, it } from 'mocha'
import { formatImportDirectory } from '../../../add-on/src/lib/ipfs-import.js'

describe('ipfs-import.js', function () {
before(function () {
browser.runtime.id = 'testid'
global.document = {}
global.browser = browser
// ipfs-import depends on webextension/polyfill which can't be imported
// in a non-browser environment unless global.browser is stubbed

// need to force Date to return a particular date
global.clock = useFakeTimers({
now: new Date(2017, 10, 5, 12, 1, 1)
})
})

describe('formatImportDirectory', function () {
it('should change nothing if path is properly formatted and date wildcards are not provided', function () {
const path = '/ipfs-companion-imports/my-directory/'
Expand All @@ -45,20 +30,4 @@ describe('ipfs-import.js', function () {
expect(formatImportDirectory(path)).to.equal('/ipfs-companion-imports/2017-11-05_120101/')
})
})
// TODO: complete tests
// describe('openFilesAtWebUI', function () {
// })
//
// describe('openFilesAtGateway', function () {
// })
//
// describe('importFiles', function () {
// })

after(function () {
global.browser.flush()
global.clock.restore()
delete global.document
delete global.browser
})
})
Loading