diff --git a/services/web/app/src/Features/ThirdPartyDataStore/TpdsController.js b/services/web/app/src/Features/ThirdPartyDataStore/TpdsController.js index 70ebb6ae4d..1ba7f2f670 100644 --- a/services/web/app/src/Features/ThirdPartyDataStore/TpdsController.js +++ b/services/web/app/src/Features/ThirdPartyDataStore/TpdsController.js @@ -133,13 +133,12 @@ module.exports = { }) }, - async getQueues(req, res) { + async getQueues(req, res, next) { const userId = AuthenticationController.getLoggedInUserId(req) try { res.json(await TdpsQueueManager.getQueues(userId)) } catch (err) { - logger.error({ err, userId }, 'error getting queues') - res.json({ error: 'error getting queues' }) + next(err) } }, diff --git a/services/web/test/unit/src/ThirdPartyDataStore/TpdsControllerTests.js b/services/web/test/unit/src/ThirdPartyDataStore/TpdsControllerTests.js index 4adbf25104..59a28dc2aa 100644 --- a/services/web/test/unit/src/ThirdPartyDataStore/TpdsControllerTests.js +++ b/services/web/test/unit/src/ThirdPartyDataStore/TpdsControllerTests.js @@ -21,6 +21,14 @@ const modulePath = require('path').join( describe('TpdsController', function() { beforeEach(function() { this.TpdsUpdateHandler = {} + this.AuthenticationController = { + getLoggedInUserId: sinon.stub().returns('user-id') + } + this.TpdsQueueManager = { + promises: { + getQueues: sinon.stub().returns('queues') + } + } this.TpdsController = SandboxedModule.require(modulePath, { globals: { console: console @@ -31,6 +39,9 @@ describe('TpdsController', function() { '../Notifications/NotificationsBuilder': (this.NotificationsBuilder = { tpdsFileLimit: sinon.stub().returns({ create: sinon.stub() }) }), + '../Authentication/AuthenticationController': this + .AuthenticationController, + './TpdsQueueManager': this.TpdsQueueManager, 'logger-sharelatex': { log() {}, warn() {}, @@ -266,4 +277,46 @@ describe('TpdsController', function() { this.res.sendStatus.calledWith(200).should.equal(true) }) }) + + describe('getQueues', function() { + beforeEach(function() { + this.req = {} + this.res = { json: sinon.stub() } + this.next = sinon.stub() + }) + + describe('success', function() { + beforeEach(async function() { + await this.TpdsController.getQueues(this.req, this.res, this.next) + }) + + it('should use userId from session', function() { + this.AuthenticationController.getLoggedInUserId.should.have.been + .calledOnce + this.TpdsQueueManager.promises.getQueues.should.have.been.calledWith( + 'user-id' + ) + }) + + it('should call json with response', function() { + this.res.json.should.have.been.calledWith('queues') + this.next.should.not.have.been.called + }) + }) + + describe('error', function() { + beforeEach(async function() { + this.err = new Error() + this.TpdsQueueManager.promises.getQueues = sinon + .stub() + .rejects(this.err) + await this.TpdsController.getQueues(this.req, this.res, this.next) + }) + + it('should call next with error', function() { + this.res.json.should.not.have.been.called + this.next.should.have.been.calledWith(this.err) + }) + }) + }) })