Skip to content

Commit

Permalink
Add API schema tests for Click to Load surrogate scripts (#1041)
Browse files Browse the repository at this point in the history
The Click to Load feature redirects requests to certain scripts in
order to replace them with our "surrogate" scripts. It's important we
know when the original script has changed materially, so that we can
update the corresponding surrogate script. To that end, let's add
integration tests that check the schema of important Objects provided
by those scripts against the schema that we expect.
  • Loading branch information
kzar committed Feb 17, 2022
1 parent 05a9f0a commit 600ee03
Show file tree
Hide file tree
Showing 10 changed files with 872 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ jobs:
sudo apt-get install xvfb
npm run install-ci
- run: npm run test-int-x
- name: Store test artifacts
uses: actions/upload-artifact@v2
with:
name: integration-test-artifacts
path: integration-test/artifacts
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ build/chrome
build/test
test/background.js
shared/content-scope-scripts/
integration-test/artifacts/
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ grunt-dev:
tosdr:
grunt execute:tosdr --browser=$(browser) --type=$(type)

setup-artifacts-dir:
rm -rf integration-test/artifacts
mkdir -p integration-test/artifacts/screenshots
mkdir -p integration-test/artifacts/api_schemas

setup-build-dir:
mkdir -p build/$(browser)
rm -rf build/$(browser)/$(type)
Expand Down
38 changes: 38 additions & 0 deletions integration-test/background/click-to-load-facebook.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const { getDomain } = require('tldts')

const harness = require('../helpers/harness')
const { logPageRequests } = require('../helpers/requests')
const backgroundWait = require('../helpers/backgroundWait')
const { setupAPISchemaTest } = require('../helpers/apiSchema')

const testSite = 'https://privacy-test-pages.glitch.me/privacy-protections/click-to-load/'
const facebookDomains = new Set(['facebook.com', 'facebook.net', 'fbcdn.net'])
Expand Down Expand Up @@ -129,3 +131,39 @@ describe('Test Facebook Click To Load', () => {
page.close()
})
})

describe('Facebook SDK schema', () => {
beforeAll(async () => {
({ browser, bgPage, teardown } = await harness.setup({ loadExtension: false }))
})

afterAll(async () => {
try {
await teardown()
} catch (e) {}
})

it('CTL: Facebook SDK schema hasn\'t changed', async () => {
const page = await browser.newPage()
await page.goto(testSite, { waitUntil: 'networkidle2' })

// Note: If these tests fail, update the
// /integration-test/data/api_schemas/facebook-sdk.json file
// to match
// /integration-test/artifacts/api_schemas/facebook-sdk.json
// and make any corresponding changes required to the surrogate
// scripts /shared/data/web_accessible_resources/facebook-sdk.js
// and /shared/js/content-scripts/fb-surrogate-xray.js.
// If no changes to the surrogate scripts are required, please
// explain why to the reviewer!
//
// See also https://developers.facebook.com/docs/graph-api/changelog

const { actualSchema, expectedSchema } = await setupAPISchemaTest(
page, 'facebook-sdk.json', ['FB']
)
expect(actualSchema).toEqual(expectedSchema)

page.close()
})
})
35 changes: 35 additions & 0 deletions integration-test/background/click-to-load-youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const harness = require('../helpers/harness')
const { logPageRequests } = require('../helpers/requests')
const { loadTestConfig, unloadTestConfig } = require('../helpers/testConfig')
const backgroundWait = require('../helpers/backgroundWait')
const { setupAPISchemaTest } = require('../helpers/apiSchema')

const testSite = 'https://privacy-test-pages.glitch.me/privacy-protections/youtube-click-to-load/'
const testConfig = {
Expand Down Expand Up @@ -293,3 +294,37 @@ describe('Test YouTube Click To Load', () => {
page.close()
})
})

describe('YouTube Iframe Player API schema', () => {
beforeAll(async () => {
({ browser, bgPage, teardown } =
await harness.setup({ loadExtension: false }))
})

afterAll(async () => {
try {
await teardown()
} catch (e) {}
})

it('CTL: Iframe Player API schema hasn\'t changed', async () => {
const page = await browser.newPage()
await page.goto(testSite, { waitUntil: 'networkidle2' })

// Note: If this test fails, update
// /integration-test/data/api_schemas/youtube-iframe-api.json file
// to match
// /integration-test/artifacts/api_schemas/youtube-iframe-api.json
// and make any corresponding changes required to the surrogate
// script /shared/data/web_accessible_resources/youtube-iframe-api.js
// If no changes to the surrogate script are required, please
// explain why to the reviewer!

const { actualSchema, expectedSchema } = await setupAPISchemaTest(
page, 'youtube-iframe-api.json', ['YT', 'YTConfig']
)
expect(actualSchema).toEqual(expectedSchema)

page.close()
})
})

0 comments on commit 600ee03

Please sign in to comment.