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

Fix erroneous socket connections to undefinedCHANGE_ME_PATH #4799

Merged
merged 6 commits into from
Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 3 additions & 13 deletions packages/extension/app/background.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ map = require("lodash/map")
pick = require("lodash/pick")
once = require("lodash/once")
Promise = require("bluebird")
{ client, circularParser } = require("@packages/socket/lib/browser")

HOST = "CHANGE_ME_HOST"
PATH = "CHANGE_ME_PATH"
client = require("./client")

httpRe = /^http/

Expand Down Expand Up @@ -35,11 +32,7 @@ connect = (host, path) ->
.catch (err) ->
fail(id, err)

ws = client.connect(host, {
path: path,
transports: ["websocket"]
parser: circularParser
})
ws = client.connect(host, path)

ws.on "automation:request", (id, msg, data) ->
switch msg
Expand Down Expand Up @@ -68,10 +61,7 @@ connect = (host, path) ->
ws.emit("automation:client:connected")

return ws

## initially connect
connect(HOST, PATH)


automation = {
connect

Expand Down
18 changes: 18 additions & 0 deletions packages/extension/app/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { client, circularParser } = require('@packages/socket/lib/browser')

const connect = (host, path) => {
return client.connect(host, {
path,
transports: ['websocket'],
// @ts-ignore
parser: circularParser,
})
}

module.exports = {
connect,

socketIoClient: client,

socketIoParser: circularParser,
}
7 changes: 7 additions & 0 deletions packages/extension/app/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const background = require('./background.coffee')

const HOST = 'CHANGE_ME_HOST'
const PATH = 'CHANGE_ME_PATH'

// immediately connect
background.connect(HOST, PATH)
2 changes: 1 addition & 1 deletion packages/extension/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ gulp.task('backup', () => {

gulp.task('background', () => {
return browserify({
entries: 'app/background.coffee',
entries: 'app/init.js',
transform: coffeeify,
})
.bundle()
Expand Down
3 changes: 2 additions & 1 deletion packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"gulp": "4.0.2",
"gulp-clean": "0.4.0",
"gulp-rename": "1.4.0",
"sinon": "1.17.7",
"run-sequence": "1.2.2",
"sinon": "7.3.2",
"sinon-chai": "3.3.0",
"vinyl-source-stream": "2.0.0"
},
Expand Down
50 changes: 25 additions & 25 deletions packages/extension/test/integration/background_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ describe "app/background", ->
it "emits 'automation:client:connected'", (done) ->
client = background.connect("http://localhost:#{PORT}", "/__socket.io")

@sandbox.spy(client, "emit")
sinon.spy(client, "emit")

client.on "connect", _.once ->
expect(client.emit).to.be.calledWith("automation:client:connected")
done()

it "listens to cookie changes", (done) ->
addListener = @sandbox.stub(chrome.cookies.onChanged, "addListener")
addListener = sinon.stub(chrome.cookies.onChanged, "addListener")
client = background.connect("http://localhost:#{PORT}", "/__socket.io")

client.on "connect", _.once ->
Expand All @@ -128,10 +128,10 @@ describe "app/background", ->

context "onChanged", ->
it "does not emit when cause is overwrite", (done) ->
addListener = @sandbox.stub(chrome.cookies.onChanged, "addListener")
addListener = sinon.stub(chrome.cookies.onChanged, "addListener")
client = background.connect("http://localhost:#{PORT}", "/__socket.io")

@sandbox.spy(client, "emit")
sinon.spy(client, "emit")

client.on "connect", _.once ->
fn = addListener.getCall(0).args[0]
Expand All @@ -144,7 +144,7 @@ describe "app/background", ->
it "emits 'automation:push:request'", (done) ->
info = { cause: "explicit", cookie: {name: "foo", value: "bar"} }

addListener = @sandbox.stub(chrome.cookies.onChanged, "addListener").yieldsAsync(info)
addListener = sinon.stub(chrome.cookies.onChanged, "addListener").yieldsAsync(info)
client = background.connect("http://localhost:#{PORT}", "/__socket.io")

client.on "connect", ->
Expand All @@ -156,7 +156,7 @@ describe "app/background", ->

context ".getAll", ->
it "resolves with specific cookie properties", ->
@sandbox.stub(chrome.cookies, "getAll")
sinon.stub(chrome.cookies, "getAll")
.withArgs({domain: "localhost"})
.yieldsAsync([
{name: "foo", value: "f", path: "/", domain: "localhost", secure: true, httpOnly: true, expirationDate: 123}
Expand All @@ -175,11 +175,11 @@ describe "app/background", ->
@code = "var s; (s = document.getElementById('__cypress-string')) && s.textContent"

it "resolves on the 1st tab", ->
@sandbox.stub(chrome.tabs, "query")
sinon.stub(chrome.tabs, "query")
.withArgs({windowType: "normal"})
.yieldsAsync([tab1])

@sandbox.stub(chrome.tabs, "executeScript")
sinon.stub(chrome.tabs, "executeScript")
.withArgs(tab1.id, {code: @code})
.yieldsAsync(["1234"])

Expand All @@ -189,11 +189,11 @@ describe "app/background", ->
})

it "resolves on the 2nd tab", ->
@sandbox.stub(chrome.tabs, "query")
sinon.stub(chrome.tabs, "query")
.withArgs({windowType: "normal"})
.yieldsAsync([tab1, tab2])

@sandbox.stub(chrome.tabs, "executeScript")
sinon.stub(chrome.tabs, "executeScript")
.withArgs(tab1.id, {code: @code})
.yieldsAsync(["foobarbaz"])
.withArgs(tab2.id, {code: @code})
Expand All @@ -205,7 +205,7 @@ describe "app/background", ->
})

it "filters out tabs that don't start with http", ->
@sandbox.stub(chrome.tabs, "query")
sinon.stub(chrome.tabs, "query")
.yieldsAsync([tab3])

background.query({
Expand All @@ -219,11 +219,11 @@ describe "app/background", ->
expect(err).to.be.instanceof(Promise.RangeError)

it "rejects if no tab matches", ->
@sandbox.stub(chrome.tabs, "query")
sinon.stub(chrome.tabs, "query")
.withArgs({windowType: "normal"})
.yieldsAsync([tab1, tab2])

@sandbox.stub(chrome.tabs, "executeScript")
sinon.stub(chrome.tabs, "executeScript")
.withArgs(tab1.id, {code: @code})
.yieldsAsync(["foobarbaz"])
.withArgs(tab2.id, {code: @code})
Expand All @@ -241,7 +241,7 @@ describe "app/background", ->
expect(err).to.be.instanceof(Promise.AggregateError)

it "rejects if no tabs were found", ->
@sandbox.stub(chrome.tabs, "query")
sinon.stub(chrome.tabs, "query")
.yieldsAsync([])

background.query({
Expand All @@ -263,7 +263,7 @@ describe "app/background", ->

describe "get:cookies", ->
beforeEach ->
@sandbox.stub(chrome.cookies, "getAll")
sinon.stub(chrome.cookies, "getAll")
.withArgs({domain: "google.com"})
.yieldsAsync([{}, {}])

Expand All @@ -277,7 +277,7 @@ describe "app/background", ->

describe "get:cookie", ->
beforeEach ->
@sandbox.stub(chrome.cookies, "getAll")
sinon.stub(chrome.cookies, "getAll")
.withArgs({domain: "google.com", name: "session"})
.yieldsAsync([
{name: "session", value: "key", path: "/login", domain: "google", secure: true, httpOnly: true, expirationDate: 123}
Expand Down Expand Up @@ -305,7 +305,7 @@ describe "app/background", ->
beforeEach ->
chrome.runtime.lastError = {message: "some error"}

@sandbox.stub(chrome.cookies, "set")
sinon.stub(chrome.cookies, "set")
.withArgs({domain: "google.com", name: "session", value: "key", path: "/", secure: false, url: "http://google.com/"})
.yieldsAsync(
{name: "session", value: "key", path: "/", domain: "google", secure: false, httpOnly: false}
Expand Down Expand Up @@ -358,7 +358,7 @@ describe "app/background", ->
beforeEach ->
chrome.runtime.lastError = {message: "some error"}

@sandbox.stub(chrome.cookies, "getAll")
sinon.stub(chrome.cookies, "getAll")
.withArgs({domain: "google.com"})
.yieldsAsync([
{name: "session", value: "key", path: "/", domain: "google.com", secure: true, httpOnly: true, expirationDate: 123}
Expand All @@ -369,7 +369,7 @@ describe "app/background", ->
{name: "shouldThrow", value: "key", path: "/assets", domain: "cdn.github.com", secure: false, httpOnly: true, expirationDate: 123}
])

@sandbox.stub(chrome.cookies, "remove")
sinon.stub(chrome.cookies, "remove")
.withArgs({name: "session", url: "https://google.com/"})
.yieldsAsync(
{name: "session", url: "https://google.com/", storeId: "123"}
Expand Down Expand Up @@ -417,7 +417,7 @@ describe "app/background", ->
beforeEach ->
chrome.runtime.lastError = {message: "some error"}

@sandbox.stub(chrome.cookies, "getAll")
sinon.stub(chrome.cookies, "getAll")
.withArgs({domain: "google.com", name: "session"})
.yieldsAsync([
{name: "session", value: "key", path: "/", domain: "google.com", secure: true, httpOnly: true, expirationDate: 123}
Expand All @@ -429,7 +429,7 @@ describe "app/background", ->
{name: "shouldThrow", value: "key", path: "/assets", domain: "cdn.github.com", secure: false, httpOnly: true, expirationDate: 123}
])

@sandbox.stub(chrome.cookies, "remove")
sinon.stub(chrome.cookies, "remove")
.withArgs({name: "session", url: "https://google.com/"})
.yieldsAsync(
{name: "session", url: "https://google.com/", storeId: "123"}
Expand Down Expand Up @@ -468,7 +468,7 @@ describe "app/background", ->

describe "is:automation:client:connected", ->
beforeEach ->
@sandbox.stub(chrome.tabs, "query")
sinon.stub(chrome.tabs, "query")
.withArgs({url: "CHANGE_ME_HOST/*", windowType: "normal"})
.yieldsAsync([])

Expand All @@ -482,13 +482,13 @@ describe "app/background", ->

describe "take:screenshot", ->
beforeEach ->
@sandbox.stub(chrome.windows, "getLastFocused").yieldsAsync({id: 1})
sinon.stub(chrome.windows, "getLastFocused").yieldsAsync({id: 1})

afterEach ->
delete chrome.runtime.lastError

it "resolves with screenshot", (done) ->
@sandbox.stub(chrome.tabs, "captureVisibleTab").withArgs(1, {format: "png"}).yieldsAsync("foobarbaz")
sinon.stub(chrome.tabs, "captureVisibleTab").withArgs(1, {format: "png"}).yieldsAsync("foobarbaz")

@socket.on "automation:response", (id, obj = {}) ->
expect(id).to.eq(123)
Expand All @@ -499,7 +499,7 @@ describe "app/background", ->

it "rejects with chrome.runtime.lastError", (done) ->
chrome.runtime.lastError = {message: "some error"}
@sandbox.stub(chrome.tabs, "captureVisibleTab").withArgs(1, {format: "png"}).yieldsAsync(undefined)
sinon.stub(chrome.tabs, "captureVisibleTab").withArgs(1, {format: "png"}).yieldsAsync(undefined)

@socket.on "automation:response", (id, obj = {}) ->
expect(id).to.eq(123)
Expand Down
6 changes: 2 additions & 4 deletions packages/extension/test/spec_helper.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ sinonChai = require("sinon-chai")

chai.use(sinonChai)

global.sinon = sinon
global.expect = chai.expect

beforeEach ->
@sandbox = sinon.sandbox.create()

afterEach ->
@sandbox.restore()
sinon.restore()
8 changes: 4 additions & 4 deletions packages/extension/test/unit/extension_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ require("../spec_helper")

exec = require("child_process").exec
fs = require("fs-extra")
EE = require("events")
eol = require("eol")
path = require("path")
Promise = require("bluebird")
eol = require("eol")
extension = require("../../index")
cwd = process.cwd()

cwd = process.cwd()
fs = Promise.promisifyAll(fs)
exec = Promise.promisify(exec)

Expand Down Expand Up @@ -47,7 +48,7 @@ describe "Extension", ->
beforeEach ->
@src = path.join(cwd, "test", "helpers", "background.js")

@sandbox.stub(extension, "getPathToExtension")
sinon.stub(extension, "getPathToExtension")
.withArgs("background.js").returns(@src)

it "rewrites the background.js source", ->
Expand Down Expand Up @@ -98,4 +99,3 @@ describe "Extension", ->
exec(cmd)
.then (stdout) ->
expect(stdout).to.eq("caljajdfkjjjdehjdoimjkkakekklcck")

4 changes: 2 additions & 2 deletions packages/server/test/e2e/2_browser_path_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ describe "e2e launching browsers by path", ->
launcher.detect().then (browsers) =>
browsers.find (browser) =>
browser.family == "chrome"
.then (browser) =>
.tap (browser) =>
if !browser
throw new Error("A 'chrome' family browser must be installed for this test")
browser.path
.get("path")
## turn binary browser names ("google-chrome") into their absolute paths
## so that server recognizes them as a path, not as a browser name
.then (absPath)
Expand Down