This repository has been archived by the owner on Oct 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Issue 6] Filter out execute msgs (from client) with actual code
Also added tests. (c) Copyright IBM Corp. 2016
- Loading branch information
1 parent
20ef64d
commit 0fb64a6
Showing
4 changed files
with
202 additions
and
5 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -86,6 +86,7 @@ define([ | |
resultHandler(msg); | ||
} | ||
}; | ||
return future; | ||
// TODO error handling | ||
} | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,173 @@ | ||
/** | ||
* Copyright (c) Jupyter Development Team. | ||
* Distributed under the terms of the Modified BSD License. | ||
*/ | ||
var sinon = require('sinon'); | ||
var chai = require('chai').use(require('sinon-chai')); | ||
var expect = chai.expect; | ||
var proxyquire = require('proxyquire'); | ||
var Promise = require('es6-promise').Promise; | ||
|
||
/////////////////// | ||
// MODULE STUBS | ||
/////////////////// | ||
|
||
var httpProxyStub = { | ||
createProxyServer: function() { | ||
return { | ||
on: function() {}, | ||
web: function() {}, | ||
ws: function() {} | ||
}; | ||
} | ||
}; | ||
|
||
var wsutilsStub = { | ||
decodeWebSocket: function(data) { | ||
return data; | ||
}, | ||
|
||
encodeWebSocket: function(data) { | ||
return data; | ||
} | ||
}; | ||
|
||
var notebookData = { | ||
cells: [ | ||
{ | ||
source: [ | ||
'line 1;', | ||
'line 2;' | ||
] | ||
}, | ||
{ | ||
source: [ | ||
'line 3;', | ||
'line 4;' | ||
] | ||
} | ||
] | ||
}; | ||
|
||
var nbstoreStub = { | ||
get: function() { | ||
return new Promise(function(resolve, reject) { | ||
resolve(notebookData); | ||
}); | ||
} | ||
}; | ||
|
||
var api = proxyquire('../../routes/api', { | ||
'http-proxy': httpProxyStub, | ||
'../app/ws-utils': wsutilsStub, | ||
'../app/notebook-store': nbstoreStub | ||
}); | ||
|
||
|
||
////////// | ||
// TESTS | ||
////////// | ||
|
||
describe('routes: api', function() { | ||
var serverUpgradeCallback = null; | ||
var req = { | ||
connection: { | ||
server: { | ||
on: function(topic, callback) { | ||
if (topic === 'upgrade') { | ||
serverUpgradeCallback = callback; | ||
} | ||
} | ||
} | ||
}, | ||
url: '/api/kernel' | ||
}; | ||
var emitSpy = sinon.spy(); | ||
var socket = { | ||
emit: emitSpy | ||
}; | ||
|
||
before(function() { | ||
// initialize state of websocket handling | ||
api(req, {}, null); | ||
expect(serverUpgradeCallback).to.not.be.null; | ||
serverUpgradeCallback(req, socket, null); | ||
}); | ||
|
||
beforeEach(function() { | ||
emitSpy.reset(); | ||
}); | ||
|
||
it('should allow execute request with integer', function(done) { | ||
var payload = { | ||
"header": { | ||
"session": "12345", | ||
"msg_type": "execute_request" | ||
}, | ||
"content": { | ||
"code": "0", | ||
} | ||
}; | ||
var data = [ | ||
{ | ||
payload: JSON.stringify(payload) | ||
} | ||
]; | ||
socket.emit('data', data); | ||
|
||
setTimeout(function() { | ||
expect(emitSpy).calledOnce; | ||
var emittedData = emitSpy.firstCall.args[1]; | ||
expect(emittedData).to.have.length(1); | ||
expect(emittedData[0]).to.include.keys('payload'); | ||
expect(emittedData[0].payload).to.contain('line 1;line 2;'); | ||
done(); | ||
}, 0); | ||
}); | ||
|
||
it('should filter execute request with code', function(done) { | ||
// should be passed along | ||
var payload1 = { | ||
"header": { | ||
"session": "12345", | ||
"msg_type": "execute_request" | ||
}, | ||
"content": { | ||
"code": "0", | ||
} | ||
}; | ||
// tests that code is filtered | ||
var payload2 = { | ||
"header": { | ||
"session": "12345", | ||
"msg_type": "execute_request" | ||
}, | ||
"content": { | ||
"code": "foo = 1; print(foo)", | ||
} | ||
}; | ||
// tests that code starting with integer is also filtered | ||
var payload3 = { | ||
"header": { | ||
"session": "12345", | ||
"msg_type": "execute_request" | ||
}, | ||
"content": { | ||
"code": "456; foo = 1; print(foo)", | ||
} | ||
}; | ||
var data = [ | ||
{ payload: JSON.stringify(payload1) }, | ||
{ payload: JSON.stringify(payload2) }, | ||
{ payload: JSON.stringify(payload3) } | ||
]; | ||
socket.emit('data', data); | ||
|
||
setTimeout(function() { | ||
expect(emitSpy).calledOnce; | ||
var emittedData = emitSpy.firstCall.args[1]; | ||
expect(emittedData).to.have.length(1); | ||
done(); | ||
}, 0); | ||
}); | ||
}); |