Clsi user id mapping (#4736)

* add user-id into clsi project mapping

* add user_id on backend group clsi key

GitOrigin-RevId: ebbf025f5cd88848b44f35a46045d112ea6b4c3b
This commit is contained in:
Henry Oswald 2021-08-20 09:14:59 +01:00 committed by Copybot
parent 7e6839b0af
commit 2b7c7e84ef
6 changed files with 163 additions and 99 deletions

View file

@ -29,43 +29,50 @@ const clsiCookiesEnabled =
module.exports = function (backendGroup) { module.exports = function (backendGroup) {
return { return {
buildKey(project_id) { buildKey(project_id, user_id) {
if (backendGroup != null) { if (backendGroup != null) {
return `clsiserver:${backendGroup}:${project_id}` return `clsiserver:${backendGroup}:${project_id}:${user_id}`
} else { } else {
return `clsiserver:${project_id}` return `clsiserver:${project_id}:${user_id}`
} }
}, },
_getServerId(project_id, callback) { _getServerId(project_id, user_id, callback) {
if (callback == null) { if (callback == null) {
callback = function (err, serverId) {} callback = function (err, serverId) {}
} }
return rclient.get(this.buildKey(project_id), (err, serverId) => { return rclient.get(
if (err != null) { this.buildKey(project_id, user_id),
return callback(err) (err, serverId) => {
if (err != null) {
return callback(err)
}
if (serverId == null || serverId === '') {
return this._populateServerIdViaRequest(
project_id,
user_id,
callback
)
} else {
return callback(null, serverId)
}
} }
if (serverId == null || serverId === '') { )
return this._populateServerIdViaRequest(project_id, callback)
} else {
return callback(null, serverId)
}
})
}, },
_populateServerIdViaRequest(project_id, callback) { _populateServerIdViaRequest(project_id, user_id, callback) {
if (callback == null) { if (callback == null) {
callback = function (err, serverId) {} callback = function (err, serverId) {}
} }
const url = `${Settings.apis.clsi.url}/project/${project_id}/status` const url = `${Settings.apis.clsi.url}/project/${project_id}/status`
return request.post(url, (err, res, body) => { request.post(url, (err, res, body) => {
if (err != null) { if (err != null) {
OError.tag(err, 'error getting initial server id for project', { OError.tag(err, 'error getting initial server id for project', {
project_id, project_id,
}) })
return callback(err) return callback(err)
} }
return this.setServerId(project_id, res, function (err, serverId) { this.setServerId(project_id, user_id, res, function (err, serverId) {
if (err != null) { if (err != null) {
logger.warn( logger.warn(
{ err, project_id }, { err, project_id },
@ -86,7 +93,7 @@ module.exports = function (backendGroup) {
return cookies != null ? cookies[Settings.clsiCookie.key] : undefined return cookies != null ? cookies[Settings.clsiCookie.key] : undefined
}, },
setServerId(project_id, response, callback) { setServerId(project_id, user_id, response, callback) {
if (callback == null) { if (callback == null) {
callback = function (err, serverId) {} callback = function (err, serverId) {}
} }
@ -96,50 +103,55 @@ module.exports = function (backendGroup) {
const serverId = this._parseServerIdFromResponse(response) const serverId = this._parseServerIdFromResponse(response)
if (serverId == null) { if (serverId == null) {
// We don't get a cookie back if it hasn't changed // We don't get a cookie back if it hasn't changed
return rclient.expire( rclient.expire(
this.buildKey(project_id), this.buildKey(project_id, user_id),
Settings.clsiCookie.ttl, Settings.clsiCookie.ttl,
err => callback(err, undefined) err => callback(err, undefined)
) )
} }
if (rclient_secondary != null) { if (rclient_secondary != null) {
this._setServerIdInRedis(rclient_secondary, project_id, serverId) this._setServerIdInRedis(
rclient_secondary,
project_id,
user_id,
serverId
)
} }
return this._setServerIdInRedis(rclient, project_id, serverId, err => this._setServerIdInRedis(rclient, project_id, user_id, serverId, err =>
callback(err, serverId) callback(err, serverId)
) )
}, },
_setServerIdInRedis(rclient, project_id, serverId, callback) { _setServerIdInRedis(rclient, project_id, user_id, serverId, callback) {
if (callback == null) { if (callback == null) {
callback = function (err) {} callback = function (err) {}
} }
rclient.setex( rclient.setex(
this.buildKey(project_id), this.buildKey(project_id, user_id),
Settings.clsiCookie.ttl, Settings.clsiCookie.ttl,
serverId, serverId,
callback callback
) )
}, },
clearServerId(project_id, callback) { clearServerId(project_id, user_id, callback) {
if (callback == null) { if (callback == null) {
callback = function (err) {} callback = function (err) {}
} }
if (!clsiCookiesEnabled) { if (!clsiCookiesEnabled) {
return callback() return callback()
} }
return rclient.del(this.buildKey(project_id), callback) return rclient.del(this.buildKey(project_id, user_id), callback)
}, },
getCookieJar(project_id, callback) { getCookieJar(project_id, user_id, callback) {
if (callback == null) { if (callback == null) {
callback = function (err, jar, clsiServerId) {} callback = function (err, jar, clsiServerId) {}
} }
if (!clsiCookiesEnabled) { if (!clsiCookiesEnabled) {
return callback(null, request.jar(), undefined) return callback(null, request.jar(), undefined)
} }
return this._getServerId(project_id, (err, serverId) => { return this._getServerId(project_id, user_id, (err, serverId) => {
if (err != null) { if (err != null) {
OError.tag(err, 'error getting server id', { OError.tag(err, 'error getting server id', {
project_id, project_id,

View file

@ -132,7 +132,7 @@ const ClsiManager = {
url: compilerUrl, url: compilerUrl,
method: 'POST', method: 'POST',
} }
ClsiManager._makeRequest(projectId, opts, callback) ClsiManager._makeRequest(projectId, userId, opts, callback)
}, },
deleteAuxFiles(projectId, userId, options, clsiserverid, callback) { deleteAuxFiles(projectId, userId, options, clsiserverid, callback) {
@ -150,13 +150,14 @@ const ClsiManager = {
} }
ClsiManager._makeRequestWithClsiServerId( ClsiManager._makeRequestWithClsiServerId(
projectId, projectId,
userId,
opts, opts,
clsiserverid, clsiserverid,
clsiErr => { clsiErr => {
// always clear the project state from the docupdater, even if there // always clear the project state from the docupdater, even if there
// was a problem with the request to the clsi // was a problem with the request to the clsi
DocumentUpdaterHandler.clearProjectState(projectId, docUpdaterErr => { DocumentUpdaterHandler.clearProjectState(projectId, docUpdaterErr => {
ClsiCookieManager.clearServerId(projectId, redisError => { ClsiCookieManager.clearServerId(projectId, userId, redisError => {
if (clsiErr) { if (clsiErr) {
return callback( return callback(
OError.tag(clsiErr, 'Failed to delete aux files', { projectId }) OError.tag(clsiErr, 'Failed to delete aux files', { projectId })
@ -194,7 +195,7 @@ const ClsiManager = {
} }
if (options.forceNewClsiServer) { if (options.forceNewClsiServer) {
// Clear clsi cookie, then try again // Clear clsi cookie, then try again
return ClsiCookieManager.clearServerId(projectId, err => { return ClsiCookieManager.clearServerId(projectId, userId, err => {
if (err) { if (err) {
return callback(err) return callback(err)
} }
@ -270,7 +271,13 @@ const ClsiManager = {
) )
}, },
_makeRequestWithClsiServerId(projectId, opts, clsiserverid, callback) { _makeRequestWithClsiServerId(
projectId,
userId,
opts,
clsiserverid,
callback
) {
if (clsiserverid) { if (clsiserverid) {
// ignore cookies and newBackend, go straight to the clsi node // ignore cookies and newBackend, go straight to the clsi node
opts.qs = Object.assign({ clsiserverid }, opts.qs) opts.qs = Object.assign({ clsiserverid }, opts.qs)
@ -283,17 +290,18 @@ const ClsiManager = {
callback(null, response, body) callback(null, response, body)
}) })
} else { } else {
ClsiManager._makeRequest(projectId, opts, callback) ClsiManager._makeRequest(projectId, userId, opts, callback)
} }
}, },
_makeRequest(projectId, opts, callback) { _makeRequest(projectId, userId, opts, callback) {
async.series( async.series(
{ {
currentBackend(cb) { currentBackend(cb) {
const startTime = new Date() const startTime = new Date()
ClsiCookieManager.getCookieJar( ClsiCookieManager.getCookieJar(
projectId, projectId,
userId,
(err, jar, clsiServerId) => { (err, jar, clsiServerId) => {
if (err != null) { if (err != null) {
return callback( return callback(
@ -318,6 +326,7 @@ const ClsiManager = {
) )
ClsiCookieManager.setServerId( ClsiCookieManager.setServerId(
projectId, projectId,
userId,
response, response,
(err, newClsiServerId) => { (err, newClsiServerId) => {
if (err != null) { if (err != null) {
@ -350,6 +359,7 @@ const ClsiManager = {
const startTime = new Date() const startTime = new Date()
ClsiManager._makeNewBackendRequest( ClsiManager._makeNewBackendRequest(
projectId, projectId,
userId,
opts, opts,
(err, response, body) => { (err, response, body) => {
if (err != null) { if (err != null) {
@ -396,7 +406,7 @@ const ClsiManager = {
) )
}, },
_makeNewBackendRequest(projectId, baseOpts, callback) { _makeNewBackendRequest(projectId, userId, baseOpts, callback) {
if (Settings.apis.clsi_new == null || Settings.apis.clsi_new.url == null) { if (Settings.apis.clsi_new == null || Settings.apis.clsi_new.url == null) {
return callback() return callback()
} }
@ -407,42 +417,47 @@ const ClsiManager = {
Settings.apis.clsi_new.url Settings.apis.clsi_new.url
), ),
} }
NewBackendCloudClsiCookieManager.getCookieJar(projectId, (err, jar) => { NewBackendCloudClsiCookieManager.getCookieJar(
if (err != null) { projectId,
return callback( userId,
OError.tag(err, 'error getting cookie jar for CLSI request', { (err, jar) => {
projectId,
})
)
}
opts.jar = jar
const timer = new Metrics.Timer('compile.newBackend')
request(opts, (err, response, body) => {
timer.done()
if (err != null) { if (err != null) {
return callback( return callback(
OError.tag(err, 'error making request to new CLSI', { OError.tag(err, 'error getting cookie jar for CLSI request', {
projectId, projectId,
opts,
}) })
) )
} }
NewBackendCloudClsiCookieManager.setServerId( opts.jar = jar
projectId, const timer = new Metrics.Timer('compile.newBackend')
response, request(opts, (err, response, body) => {
err => { timer.done()
if (err != null) { if (err != null) {
return callback( return callback(
OError.tag(err, 'error setting server id on new backend', { OError.tag(err, 'error making request to new CLSI', {
projectId, projectId,
}) opts,
) })
} )
callback(null, response, body)
} }
) NewBackendCloudClsiCookieManager.setServerId(
}) projectId,
}) userId,
response,
err => {
if (err != null) {
return callback(
OError.tag(err, 'error setting server id on new backend', {
projectId,
})
)
}
callback(null, response, body)
}
)
})
}
)
}, },
_getCompilerUrl(compileGroup, projectId, userId, action) { _getCompilerUrl(compileGroup, projectId, userId, action) {
@ -471,6 +486,7 @@ const ClsiManager = {
} }
ClsiManager._makeRequest( ClsiManager._makeRequest(
projectId, projectId,
userId,
opts, opts,
(err, response, body, clsiServerId) => { (err, response, body, clsiServerId) => {
if (err != null) { if (err != null) {
@ -645,7 +661,7 @@ const ClsiManager = {
getOutputFileStream(projectId, userId, buildId, outputFilePath, callback) { getOutputFileStream(projectId, userId, buildId, outputFilePath, callback) {
const url = `${Settings.apis.clsi.url}/project/${projectId}/user/${userId}/build/${buildId}/output/${outputFilePath}` const url = `${Settings.apis.clsi.url}/project/${projectId}/user/${userId}/build/${buildId}/output/${outputFilePath}`
ClsiCookieManager.getCookieJar(projectId, (err, jar) => { ClsiCookieManager.getCookieJar(projectId, userId, (err, jar) => {
if (err != null) { if (err != null) {
return callback( return callback(
OError.tag(err, 'Failed to get cookie jar', { OError.tag(err, 'Failed to get cookie jar', {
@ -876,6 +892,7 @@ const ClsiManager = {
} }
ClsiManager._makeRequestWithClsiServerId( ClsiManager._makeRequestWithClsiServerId(
projectId, projectId,
userId,
opts, opts,
clsiserverid, clsiserverid,
(err, response, body) => { (err, response, body) => {

View file

@ -572,10 +572,11 @@ module.exports = CompileController = {
function _getPersistenceOptions(req, projectId, callback) { function _getPersistenceOptions(req, projectId, callback) {
const { clsiserverid } = req.query const { clsiserverid } = req.query
const user_id = SessionManager.getLoggedInUserId(req)
if (clsiserverid && typeof clsiserverid === 'string') { if (clsiserverid && typeof clsiserverid === 'string') {
callback(null, { qs: { clsiserverid } }) callback(null, { qs: { clsiserverid } })
} else { } else {
ClsiCookieManager.getCookieJar(projectId, (err, jar) => { ClsiCookieManager.getCookieJar(projectId, user_id, (err, jar) => {
callback(err, { jar }) callback(err, { jar })
}) })
} }

View file

@ -25,7 +25,8 @@ describe('ClsiCookieManager', function () {
get: sinon.stub(), get: sinon.stub(),
setex: sinon.stub().callsArg(3), setex: sinon.stub().callsArg(3),
} }
this.project_id = '123423431321' this.project_id = '123423431321-proj-id'
this.user_id = 'abc-user-id'
this.request = { this.request = {
post: sinon.stub(), post: sinon.stub(),
cookie: realRequst.cookie, cookie: realRequst.cookie,
@ -65,9 +66,10 @@ describe('ClsiCookieManager', function () {
this.redis.get.callsArgWith(1, null, 'clsi-7') this.redis.get.callsArgWith(1, null, 'clsi-7')
return this.ClsiCookieManager._getServerId( return this.ClsiCookieManager._getServerId(
this.project_id, this.project_id,
this.user_id,
(err, serverId) => { (err, serverId) => {
this.redis.get this.redis.get
.calledWith(`clsiserver:${this.project_id}`) .calledWith(`clsiserver:${this.project_id}:${this.user_id}`)
.should.equal(true) .should.equal(true)
serverId.should.equal('clsi-7') serverId.should.equal('clsi-7')
return done() return done()
@ -78,13 +80,14 @@ describe('ClsiCookieManager', function () {
it('should _populateServerIdViaRequest if no key is found', function (done) { it('should _populateServerIdViaRequest if no key is found', function (done) {
this.ClsiCookieManager._populateServerIdViaRequest = sinon this.ClsiCookieManager._populateServerIdViaRequest = sinon
.stub() .stub()
.callsArgWith(1) .callsArgWith(2)
this.redis.get.callsArgWith(1, null) this.redis.get.callsArgWith(1, null)
return this.ClsiCookieManager._getServerId( return this.ClsiCookieManager._getServerId(
this.project_id, this.project_id,
this.user_id,
(err, serverId) => { (err, serverId) => {
this.ClsiCookieManager._populateServerIdViaRequest this.ClsiCookieManager._populateServerIdViaRequest
.calledWith(this.project_id) .calledWith(this.project_id, this.user_id)
.should.equal(true) .should.equal(true)
return done() return done()
} }
@ -94,13 +97,14 @@ describe('ClsiCookieManager', function () {
it('should _populateServerIdViaRequest if no key is blank', function (done) { it('should _populateServerIdViaRequest if no key is blank', function (done) {
this.ClsiCookieManager._populateServerIdViaRequest = sinon this.ClsiCookieManager._populateServerIdViaRequest = sinon
.stub() .stub()
.callsArgWith(1) .callsArgWith(2)
this.redis.get.callsArgWith(1, null, '') this.redis.get.callsArgWith(1, null, '')
return this.ClsiCookieManager._getServerId( return this.ClsiCookieManager._getServerId(
this.project_id, this.project_id,
this.user_id,
(err, serverId) => { (err, serverId) => {
this.ClsiCookieManager._populateServerIdViaRequest this.ClsiCookieManager._populateServerIdViaRequest
.calledWith(this.project_id) .calledWith(this.project_id, this.user_id)
.should.equal(true) .should.equal(true)
return done() return done()
} }
@ -114,16 +118,18 @@ describe('ClsiCookieManager', function () {
this.request.post.callsArgWith(1, null, this.response) this.request.post.callsArgWith(1, null, this.response)
return (this.ClsiCookieManager.setServerId = sinon return (this.ClsiCookieManager.setServerId = sinon
.stub() .stub()
.callsArgWith(2, null, 'clsi-9')) .callsArgWith(3, null, 'clsi-9'))
}) })
it('should make a request to the clsi', function (done) { it('should make a request to the clsi', function (done) {
return this.ClsiCookieManager._populateServerIdViaRequest( return this.ClsiCookieManager._populateServerIdViaRequest(
this.project_id, this.project_id,
this.user_id,
(err, serverId) => { (err, serverId) => {
const args = this.ClsiCookieManager.setServerId.args[0] const args = this.ClsiCookieManager.setServerId.args[0]
args[0].should.equal(this.project_id) args[0].should.equal(this.project_id)
args[1].should.deep.equal(this.response) args[1].should.equal(this.user_id)
args[2].should.deep.equal(this.response)
return done() return done()
} }
) )
@ -132,6 +138,7 @@ describe('ClsiCookieManager', function () {
it('should return the server id', function (done) { it('should return the server id', function (done) {
return this.ClsiCookieManager._populateServerIdViaRequest( return this.ClsiCookieManager._populateServerIdViaRequest(
this.project_id, this.project_id,
this.user_id,
(err, serverId) => { (err, serverId) => {
serverId.should.equal('clsi-9') serverId.should.equal('clsi-9')
return done() return done()
@ -151,11 +158,12 @@ describe('ClsiCookieManager', function () {
it('should set the server id with a ttl', function (done) { it('should set the server id with a ttl', function (done) {
return this.ClsiCookieManager.setServerId( return this.ClsiCookieManager.setServerId(
this.project_id, this.project_id,
this.user_id,
this.response, this.response,
err => { err => {
this.redis.setex this.redis.setex
.calledWith( .calledWith(
`clsiserver:${this.project_id}`, `clsiserver:${this.project_id}:${this.user_id}`,
this.settings.clsiCookie.ttl, this.settings.clsiCookie.ttl,
'clsi-8' 'clsi-8'
) )
@ -168,6 +176,7 @@ describe('ClsiCookieManager', function () {
it('should return the server id', function (done) { it('should return the server id', function (done) {
return this.ClsiCookieManager.setServerId( return this.ClsiCookieManager.setServerId(
this.project_id, this.project_id,
this.user_id,
this.response, this.response,
(err, serverId) => { (err, serverId) => {
serverId.should.equal('clsi-8') serverId.should.equal('clsi-8')
@ -186,6 +195,7 @@ describe('ClsiCookieManager', function () {
})() })()
return this.ClsiCookieManager.setServerId( return this.ClsiCookieManager.setServerId(
this.project_id, this.project_id,
this.user_id,
this.response, this.response,
(err, serverId) => { (err, serverId) => {
this.redis.setex.called.should.equal(false) this.redis.setex.called.should.equal(false)
@ -200,6 +210,7 @@ describe('ClsiCookieManager', function () {
.returns(null) .returns(null)
return this.ClsiCookieManager.setServerId( return this.ClsiCookieManager.setServerId(
this.project_id, this.project_id,
this.user_id,
this.response, this.response,
(err, serverId) => { (err, serverId) => {
this.redis.setex.called.should.equal(false) this.redis.setex.called.should.equal(false)
@ -227,11 +238,12 @@ describe('ClsiCookieManager', function () {
.returns('clsi-8') .returns('clsi-8')
return this.ClsiCookieManager.setServerId( return this.ClsiCookieManager.setServerId(
this.project_id, this.project_id,
this.user_id,
this.response, this.response,
(err, serverId) => { (err, serverId) => {
this.redis_secondary.setex this.redis_secondary.setex
.calledWith( .calledWith(
`clsiserver:${this.project_id}`, `clsiserver:${this.project_id}:${this.user_id}`,
this.settings.clsiCookie.ttl, this.settings.clsiCookie.ttl,
'clsi-8' 'clsi-8'
) )
@ -246,12 +258,13 @@ describe('ClsiCookieManager', function () {
beforeEach(function () { beforeEach(function () {
return (this.ClsiCookieManager._getServerId = sinon return (this.ClsiCookieManager._getServerId = sinon
.stub() .stub()
.callsArgWith(1, null, 'clsi-11')) .callsArgWith(2, null, 'clsi-11'))
}) })
it('should return a jar with the cookie set populated from redis', function (done) { it('should return a jar with the cookie set populated from redis', function (done) {
return this.ClsiCookieManager.getCookieJar( return this.ClsiCookieManager.getCookieJar(
this.project_id, this.project_id,
this.user_id,
(err, jar) => { (err, jar) => {
jar._jar.store.idx['clsi.example.com']['/'][ jar._jar.store.idx['clsi.example.com']['/'][
this.settings.clsiCookie.key this.settings.clsiCookie.key
@ -274,6 +287,7 @@ describe('ClsiCookieManager', function () {
})() })()
return this.ClsiCookieManager.getCookieJar( return this.ClsiCookieManager.getCookieJar(
this.project_id, this.project_id,
this.user_id,
(err, jar) => { (err, jar) => {
assert.deepEqual(jar, realRequst.jar()) assert.deepEqual(jar, realRequst.jar())
return done() return done()

View file

@ -8,8 +8,8 @@ describe('ClsiManager', function () {
this.jar = { cookie: 'stuff' } this.jar = { cookie: 'stuff' }
this.ClsiCookieManager = { this.ClsiCookieManager = {
clearServerId: sinon.stub().yields(), clearServerId: sinon.stub().yields(),
getCookieJar: sinon.stub().callsArgWith(1, null, this.jar), getCookieJar: sinon.stub().callsArgWith(2, null, this.jar),
setServerId: sinon.stub().callsArgWith(2), setServerId: sinon.stub().callsArgWith(3),
_getServerId: sinon.stub(), _getServerId: sinon.stub(),
} }
this.ClsiStateManager = { this.ClsiStateManager = {
@ -73,7 +73,7 @@ describe('ClsiManager', function () {
this.ClsiManager._buildRequest = sinon this.ClsiManager._buildRequest = sinon
.stub() .stub()
.callsArgWith(2, null, (this.request = 'mock-request')) .callsArgWith(2, null, (this.request = 'mock-request'))
this.ClsiCookieManager._getServerId.callsArgWith(1, null, 'clsi3') this.ClsiCookieManager._getServerId.callsArgWith(2, null, 'clsi3')
}) })
describe('with a successful compile', function () { describe('with a successful compile', function () {
@ -354,7 +354,7 @@ describe('ClsiManager', function () {
beforeEach(function () { beforeEach(function () {
this.submission_id = 'submission-id' this.submission_id = 'submission-id'
this.clsi_request = 'mock-request' this.clsi_request = 'mock-request'
this.ClsiCookieManager._getServerId.callsArgWith(1, null, 'clsi3') this.ClsiCookieManager._getServerId.callsArgWith(2, null, 'clsi3')
}) })
describe('with a successful compile', function () { describe('with a successful compile', function () {
@ -490,6 +490,7 @@ describe('ClsiManager', function () {
this.ClsiManager._makeRequestWithClsiServerId this.ClsiManager._makeRequestWithClsiServerId
.calledWith( .calledWith(
this.project_id, this.project_id,
this.user_id,
{ {
method: 'DELETE', method: 'DELETE',
url: `${this.settings.apis.clsi.url}/project/${this.project_id}/user/${this.user_id}`, url: `${this.settings.apis.clsi.url}/project/${this.project_id}/user/${this.user_id}`,
@ -507,7 +508,7 @@ describe('ClsiManager', function () {
it('should clear the clsi persistance', function () { it('should clear the clsi persistance', function () {
this.ClsiCookieManager.clearServerId this.ClsiCookieManager.clearServerId
.calledWith(this.project_id) .calledWith(this.project_id, this.user_id)
.should.equal(true) .should.equal(true)
}) })
@ -924,7 +925,7 @@ describe('ClsiManager', function () {
this.ClsiManager._makeRequest = sinon this.ClsiManager._makeRequest = sinon
.stub() .stub()
.callsArgWith( .callsArgWith(
2, 3,
null, null,
{ statusCode: 204 }, { statusCode: 204 },
(this.body = { mock: 'foo' }) (this.body = { mock: 'foo' })
@ -941,7 +942,7 @@ describe('ClsiManager', function () {
it('should send the request to the CLSI', function () { it('should send the request to the CLSI', function () {
const url = `${this.settings.apis.clsi.url}/project/${this.project_id}/user/${this.user_id}/compile` const url = `${this.settings.apis.clsi.url}/project/${this.project_id}/user/${this.user_id}/compile`
this.ClsiManager._makeRequest this.ClsiManager._makeRequest
.calledWith(this.project_id, { .calledWith(this.project_id, this.user_id, {
method: 'POST', method: 'POST',
url, url,
json: this.req, json: this.req,
@ -959,7 +960,7 @@ describe('ClsiManager', function () {
this.ClsiManager._makeRequest = sinon this.ClsiManager._makeRequest = sinon
.stub() .stub()
.callsArgWith( .callsArgWith(
2, 3,
null, null,
{ statusCode: 500 }, { statusCode: 500 },
(this.body = { mock: 'foo' }) (this.body = { mock: 'foo' })
@ -1009,6 +1010,7 @@ describe('ClsiManager', function () {
this.ClsiManager._makeRequestWithClsiServerId this.ClsiManager._makeRequestWithClsiServerId
.calledWith( .calledWith(
this.project_id, this.project_id,
this.user_id,
{ {
method: 'GET', method: 'GET',
url: `http://clsi.example.com/project/${this.project_id}/user/${this.user_id}/wordcount`, url: `http://clsi.example.com/project/${this.project_id}/user/${this.user_id}/wordcount`,
@ -1043,6 +1045,7 @@ describe('ClsiManager', function () {
this.ClsiManager._makeRequestWithClsiServerId this.ClsiManager._makeRequestWithClsiServerId
.calledWith( .calledWith(
this.project_id, this.project_id,
this.user_id,
{ {
method: 'GET', method: 'GET',
url: `http://clsi.example.com/project/${this.project_id}/user/${this.user_id}/wordcount`, url: `http://clsi.example.com/project/${this.project_id}/user/${this.user_id}/wordcount`,
@ -1072,6 +1075,7 @@ describe('ClsiManager', function () {
this.ClsiManager._makeRequestWithClsiServerId this.ClsiManager._makeRequestWithClsiServerId
.calledWith( .calledWith(
this.project_id, this.project_id,
this.user_id,
{ {
method: 'GET', method: 'GET',
url: `http://clsi.example.com/project/${this.project_id}/user/${this.user_id}/wordcount`, url: `http://clsi.example.com/project/${this.project_id}/user/${this.user_id}/wordcount`,
@ -1095,22 +1099,32 @@ describe('ClsiManager', function () {
}) })
it('should process a request with a cookie jar', function (done) { it('should process a request with a cookie jar', function (done) {
this.ClsiManager._makeRequest(this.project_id, this.opts, () => { this.ClsiManager._makeRequest(
const args = this.request.args[0] this.project_id,
args[0].method.should.equal(this.opts.method) this.user_id,
args[0].url.should.equal(this.opts.url) this.opts,
args[0].jar.should.equal(this.jar) () => {
done() const args = this.request.args[0]
}) args[0].method.should.equal(this.opts.method)
args[0].url.should.equal(this.opts.url)
args[0].jar.should.equal(this.jar)
done()
}
)
}) })
it('should set the cookie again on response as it might have changed', function (done) { it('should set the cookie again on response as it might have changed', function (done) {
this.ClsiManager._makeRequest(this.project_id, this.opts, () => { this.ClsiManager._makeRequest(
this.ClsiCookieManager.setServerId this.project_id,
.calledWith(this.project_id, this.response) this.user_id,
.should.equal(true) this.opts,
done() () => {
}) this.ClsiCookieManager.setServerId
.calledWith(this.project_id, this.user_id, this.response)
.should.equal(true)
done()
}
)
}) })
}) })
@ -1128,6 +1142,7 @@ describe('ClsiManager', function () {
it('should process a request with a cookie jar', function (done) { it('should process a request with a cookie jar', function (done) {
this.ClsiManager._makeRequestWithClsiServerId( this.ClsiManager._makeRequestWithClsiServerId(
this.project_id, this.project_id,
this.user_id,
this.opts, this.opts,
undefined, undefined,
err => { err => {
@ -1145,12 +1160,13 @@ describe('ClsiManager', function () {
it('should persist the cookie from the response', function (done) { it('should persist the cookie from the response', function (done) {
this.ClsiManager._makeRequestWithClsiServerId( this.ClsiManager._makeRequestWithClsiServerId(
this.project_id, this.project_id,
this.user_id,
this.opts, this.opts,
undefined, undefined,
err => { err => {
if (err) return done(err) if (err) return done(err)
this.ClsiCookieManager.setServerId this.ClsiCookieManager.setServerId
.calledWith(this.project_id, this.response) .calledWith(this.project_id, this.user_id, this.response)
.should.equal(true) .should.equal(true)
done() done()
} }
@ -1162,6 +1178,7 @@ describe('ClsiManager', function () {
it('should not add a cookie jar', function (done) { it('should not add a cookie jar', function (done) {
this.ClsiManager._makeRequestWithClsiServerId( this.ClsiManager._makeRequestWithClsiServerId(
this.project_id, this.project_id,
this.user_id,
this.opts, this.opts,
'node-1', 'node-1',
err => { err => {
@ -1179,6 +1196,7 @@ describe('ClsiManager', function () {
it('should not persist a cookie on response', function (done) { it('should not persist a cookie on response', function (done) {
this.ClsiManager._makeRequestWithClsiServerId( this.ClsiManager._makeRequestWithClsiServerId(
this.project_id, this.project_id,
this.user_id,
this.opts, this.opts,
'node-1', 'node-1',
err => { err => {
@ -1204,6 +1222,7 @@ describe('ClsiManager', function () {
it('should change the domain on the url', function (done) { it('should change the domain on the url', function (done) {
this.ClsiManager._makeNewBackendRequest( this.ClsiManager._makeNewBackendRequest(
this.project_id, this.project_id,
this.user_id,
this.opts, this.opts,
() => { () => {
const args = this.request.args[0] const args = this.request.args[0]
@ -1219,6 +1238,7 @@ describe('ClsiManager', function () {
this.settings.apis.clsi_new = undefined this.settings.apis.clsi_new = undefined
this.ClsiManager._makeNewBackendRequest( this.ClsiManager._makeNewBackendRequest(
this.project_id, this.project_id,
this.user_id,
this.opts, this.opts,
err => { err => {
expect(err).to.equal(undefined) expect(err).to.equal(undefined)

View file

@ -49,7 +49,7 @@ describe('CompileController', function () {
} }
this.jar = { cookie: 'stuff' } this.jar = { cookie: 'stuff' }
this.ClsiCookieManager = { this.ClsiCookieManager = {
getCookieJar: sinon.stub().callsArgWith(1, null, this.jar), getCookieJar: sinon.stub().callsArgWith(2, null, this.jar),
} }
this.SessionManager = { this.SessionManager = {
getLoggedInUser: sinon.stub().callsArgWith(1, null, this.user), getLoggedInUser: sinon.stub().callsArgWith(1, null, this.user),