-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from cowlicks/selenium
Add selenium tests
- Loading branch information
Showing
13 changed files
with
224 additions
and
17 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,22 @@ | ||
dist: trusty | ||
sudo: required | ||
language: node_js | ||
node_js: | ||
- "10" | ||
install: make npm_install | ||
script: make test | ||
matrix: | ||
include: | ||
- name: "Node Unit Tests" | ||
install: make npm_install_node | ||
script: make test_node | ||
- name: "Selenium Unit Tests" | ||
addons: | ||
chrome: stable | ||
hosts: | ||
- firstparty.local | ||
- thirdparty.local | ||
before_install: | ||
- export DISPLAY=:99.0 | ||
- sh -e /etc/init.d/xvfb start | ||
install: make npm_install_selenium | ||
script: make test_selenium |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
test: | ||
test_node: | ||
./scripts/test.sh | ||
|
||
npm_install: | ||
./scripts/npm_install.sh | ||
test_selenium: | ||
./scripts/selenium_test.sh | ||
|
||
npm_install_node: | ||
./scripts/npm_install.sh src/js/. | ||
|
||
npm_install_selenium: | ||
./scripts/npm_install.sh selenium/. | ||
|
||
psl: | ||
./scripts/getpsl.py > src/js/domains/psl.js | ||
|
||
release: | ||
./scripts/release.sh | ||
|
||
.PHONY: test npm_install psl release | ||
.PHONY: test_node test_selenium npm_install_node npm_install_selenium psl release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
#!/usr/bin/env bash | ||
source $(git rev-parse --show-toplevel)/scripts/source_me.sh | ||
|
||
pushd ${js_dir} > /dev/null | ||
trap "popd > /dev/null" EXIT | ||
|
||
npm install | ||
run_in_dir $1 "npm install" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env bash | ||
source $(git rev-parse --show-toplevel)/scripts/source_me.sh | ||
|
||
run_in_dir $selenium_dir "npm test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
#!/usr/bin/env bash | ||
source $(git rev-parse --show-toplevel)/scripts/source_me.sh | ||
|
||
pushd ${js_dir} > /dev/null | ||
trap "popd > /dev/null" EXIT | ||
|
||
npm test | ||
run_in_dir $js_dir "npm test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use strict'; | ||
|
||
const express = require('express'), | ||
cookieParser = require('cookie-parser'), | ||
{firstPartyHostname, thirdPartyHostname, thirdPartyHost} = require('./utils'), | ||
vhost = require('vhost'); | ||
|
||
let fpcookie = {name: '1pname', value: '1pvalue'}, | ||
tpcookie = {name: '3pname', value: '3pvalue'}; | ||
|
||
class Channel { | ||
constructor() { | ||
this.items = []; | ||
this.waiting = []; | ||
} | ||
async popQueue() { | ||
if (this.items.length > 0) { | ||
return this.items.pop(); | ||
} else { | ||
return new Promise((resolve) => { | ||
this.waiting.push(resolve); | ||
}); | ||
} | ||
} | ||
async next() { | ||
return await this.popQueue(); | ||
} | ||
push(item) { | ||
if (this.waiting.length > 0) { | ||
this.waiting.shift()(item); | ||
} else { | ||
this.items.push(item); | ||
} | ||
} | ||
} | ||
|
||
function firstPartyApp(app = express(), tpHost = thirdPartyHost) { | ||
app.use(cookieParser()); | ||
app.requests = new Channel(); | ||
|
||
app.get('/', (req, res) => { | ||
app.requests.push(req); | ||
res.cookie(fpcookie.name, fpcookie.value); | ||
return res.send( | ||
`<script type="text/javascript" src="http://${tpHost}/tracker.js"></script>` | ||
); | ||
}); | ||
return app; | ||
} | ||
|
||
function thirdPartyApp(app = express()) { | ||
app.use(cookieParser()); | ||
app.requests = new Channel(); | ||
|
||
app.get('/tracker.js', (req, res) => { | ||
app.requests.push(req); | ||
res.cookie(tpcookie.name, tpcookie.value); | ||
return res.send('console.log("third party script")'); | ||
}); | ||
return app; | ||
} | ||
|
||
function cookieApp(app = express(), fpHostname = firstPartyHostname, tpHostname = thirdPartyHostname) { | ||
let firstParty = firstPartyApp(), | ||
thirdParty = thirdPartyApp(); | ||
app.use(vhost(fpHostname, firstParty)); | ||
app.use(vhost(tpHostname, thirdParty)); | ||
Object.assign(app, {firstParty, thirdParty}); | ||
return app; | ||
} | ||
|
||
Object.assign(module.exports, {cookieApp, fpcookie, tpcookie}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
|
||
const {assert} = require('chai'); | ||
|
||
const {newDriver, startApp, stopApp, firstPartyHost} = require('./utils'), | ||
{cookieApp, fpcookie} = require("./cookies"); | ||
|
||
describe('cookie tests', function() { | ||
beforeEach(function() { | ||
this.app = cookieApp(); | ||
this.driver = newDriver(); | ||
startApp(this.app); | ||
}); | ||
afterEach(function() { | ||
stopApp(this.app); | ||
this.driver.quit(); | ||
}); | ||
|
||
it('blocks cookies', async function() { | ||
let {app, driver} = this; | ||
driver.get(firstPartyHost); | ||
let request = await app.firstParty.requests.next(); | ||
// no cookies initially | ||
assert.deepEqual(request.cookies, {}); | ||
request = await app.thirdParty.requests.next(); | ||
assert.deepEqual(request.cookies, {}); | ||
|
||
driver.get(firstPartyHost); | ||
request = await app.firstParty.requests.next(); | ||
// now we have first party cookies set | ||
assert.deepEqual(request.cookies, {[fpcookie.name]: fpcookie.value}); | ||
request = await app.thirdParty.requests.next(); | ||
// but not third party cookies | ||
assert.deepEqual(request.cookies, {}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "privacy-possum-selenium-tests", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "integration_tests.js", | ||
"scripts": { | ||
"test": "mocha ./integration_tests.js" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"chai": "^4.2.0", | ||
"chromedriver": "^2.42.0", | ||
"cookie-parser": "^1.4.3", | ||
"express": "^4.16.3", | ||
"mocha": "^5.2.0", | ||
"selenium-webdriver": "^4.0.0-alpha.1", | ||
"vhost": "^3.0.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
|
||
const sw = require('selenium-webdriver'), | ||
{createServer} = require('http'); | ||
|
||
function startApp(app, port=PORT) { | ||
app.server = createServer(app); | ||
app.server.listen(port); | ||
} | ||
|
||
const path = '../src/.', | ||
PORT = 8000, | ||
host = (hostname, port) => `${hostname}:${port}`, | ||
firstPartyHostname = 'firstparty.local', | ||
thirdPartyHostname = 'thirdparty.local', | ||
firstPartyHost = host(firstPartyHostname, PORT), | ||
thirdPartyHost = host(thirdPartyHostname, PORT); | ||
|
||
function startApp(app, port=PORT) { | ||
app.server = createServer(app); | ||
app.server.listen(port); | ||
} | ||
|
||
function stopApp(app) { | ||
app.server.close(); | ||
} | ||
|
||
/* | ||
* in /etc/hosts this requires: | ||
* 127.0.0.1 firstparty.local | ||
* 127.0.0.1 thirdparty.local | ||
*/ | ||
|
||
function loadDriverWithExtension(extPath) { | ||
let chromeOptions = sw.Capabilities.chrome(); | ||
chromeOptions.set("chromeOptions", {"args": [ | ||
`--load-extension=${extPath}`, | ||
'--no-sandbox', | ||
]}); | ||
return new sw.Builder() | ||
.forBrowser('chrome') | ||
.withCapabilities(chromeOptions) | ||
.build(); | ||
} | ||
|
||
function newDriver() { | ||
return loadDriverWithExtension(path); | ||
} | ||
|
||
|
||
Object.assign(module.exports, {newDriver, startApp, stopApp, PORT, firstPartyHostname, thirdPartyHostname, firstPartyHost, thirdPartyHost}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters