From b97becc7a22b226b9efc68f80f3a3ebba5d7d922 Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Tue, 13 Jun 2017 11:38:15 +0100 Subject: [PATCH] Change `getLabelsForDoc` to `broadcastLabelsForDoc` --- .../Features/Labels/LabelsController.coffee | 4 ++-- services/web/app/coffee/router.coffee | 2 +- .../web/public/coffee/services/labels.coffee | 7 +++--- .../Labels/LabelsControllerTests.coffee | 24 +++++++++---------- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/services/web/app/coffee/Features/Labels/LabelsController.coffee b/services/web/app/coffee/Features/Labels/LabelsController.coffee index b3c56cc79c..dfb1d60ccf 100644 --- a/services/web/app/coffee/Features/Labels/LabelsController.coffee +++ b/services/web/app/coffee/Features/Labels/LabelsController.coffee @@ -13,7 +13,7 @@ module.exports = LabelsController = return next(err) res.json {projectId: project_id, projectLabels: projectLabels} - getLabelsForDoc: (req, res, next) -> + broadcastLabelsForDoc: (req, res, next) -> project_id = req.params.project_id doc_id = req.params.doc_id LabelsHandler.getLabelsForDoc project_id, doc_id, (err, docLabels) -> @@ -23,4 +23,4 @@ module.exports = LabelsController = EditorRealTimeController.emitToRoom project_id, 'broadcastDocLabels', { docId: doc_id, labels: docLabels } - res.json {projectId: project_id, docId: doc_id, labels: docLabels} + res.sendStatus(200) diff --git a/services/web/app/coffee/router.coffee b/services/web/app/coffee/router.coffee index b9c21898f9..3679b29a40 100644 --- a/services/web/app/coffee/router.coffee +++ b/services/web/app/coffee/router.coffee @@ -195,7 +195,7 @@ module.exports = class Router webRouter.get '/project/download/zip', AuthorizationMiddlewear.ensureUserCanReadMultipleProjects, ProjectDownloadsController.downloadMultipleProjects webRouter.get '/project/:project_id/labels', AuthorizationMiddlewear.ensureUserCanReadProject, AuthenticationController.requireLogin(), LabelsController.getAllLabels - webRouter.get '/project/:project_id/doc/:doc_id/labels', AuthorizationMiddlewear.ensureUserCanReadProject, AuthenticationController.requireLogin(), LabelsController.getLabelsForDoc + webRouter.post '/project/:project_id/doc/:doc_id/labels', AuthorizationMiddlewear.ensureUserCanReadProject, AuthenticationController.requireLogin(), LabelsController.broadcastLabelsForDoc webRouter.get '/tag', AuthenticationController.requireLogin(), TagsController.getAllTags webRouter.post '/tag', AuthenticationController.requireLogin(), TagsController.createTag diff --git a/services/web/public/coffee/services/labels.coffee b/services/web/public/coffee/services/labels.coffee index 19de5d52f1..3794b309d2 100644 --- a/services/web/public/coffee/services/labels.coffee +++ b/services/web/public/coffee/services/labels.coffee @@ -35,9 +35,10 @@ define [ labels.loadDocLabelsFromServer = (docId) -> $http - .get("/project/#{window.project_id}/doc/#{docId}/labels") + .post( + "/project/#{window.project_id}/doc/#{docId}/labels", + {_csrf: window.csrfToken} + ) .success (data) -> - if data.docId and data.labels - state.documents[data.docId] = data.labels return labels diff --git a/services/web/test/UnitTests/coffee/Labels/LabelsControllerTests.coffee b/services/web/test/UnitTests/coffee/Labels/LabelsControllerTests.coffee index e695139071..7e8a7ba91e 100644 --- a/services/web/test/UnitTests/coffee/Labels/LabelsControllerTests.coffee +++ b/services/web/test/UnitTests/coffee/Labels/LabelsControllerTests.coffee @@ -64,31 +64,31 @@ describe 'LabelsController', -> @LabelsController.getAllLabels(@req, @res, @next) @res.json.callCount.should.equal 0 - describe 'getLabelsForDoc', -> + describe 'broadcastLabelsForDoc', -> beforeEach -> @LabelsHandler.getLabelsForDoc = sinon.stub().callsArgWith(2, null, @fakeLabels) @EditorRealTimeController.emitToRoom = sinon.stub() @docId = 'somedoc' @req = {params: {project_id: @projectId, doc_id: @docId}} - @res = {json: sinon.stub()} + @res = {sendStatus: sinon.stub()} @next = sinon.stub() it 'should call LabelsHandler.getLabelsForDoc', () -> - @LabelsController.getLabelsForDoc(@req, @res, @next) + @LabelsController.broadcastLabelsForDoc(@req, @res, @next) @LabelsHandler.getLabelsForDoc.callCount.should.equal 1 @LabelsHandler.getLabelsForDoc.calledWith(@projectId).should.equal true it 'should call not call next with an error', () -> - @LabelsController.getLabelsForDoc(@req, @res, @next) + @LabelsController.broadcastLabelsForDoc(@req, @res, @next) @next.callCount.should.equal 0 - it 'should send a json response', () -> - @LabelsController.getLabelsForDoc(@req, @res, @next) - @res.json.callCount.should.equal 1 - expect(@res.json.lastCall.args[0]).to.have.all.keys ['projectId', 'docId', 'labels'] + it 'should send a success response', () -> + @LabelsController.broadcastLabelsForDoc(@req, @res, @next) + @res.sendStatus.callCount.should.equal 1 + @res.sendStatus.calledWith(200).should.equal true it 'should emit a message to room', () -> - @LabelsController.getLabelsForDoc(@req, @res, @next) + @LabelsController.broadcastLabelsForDoc(@req, @res, @next) @EditorRealTimeController.emitToRoom.callCount.should.equal 1 lastCall = @EditorRealTimeController.emitToRoom.lastCall expect(lastCall.args[0]).to.equal @projectId @@ -105,15 +105,15 @@ describe 'LabelsController', -> @next = sinon.stub() it 'should call LabelsHandler.getLabelsForDoc', () -> - @LabelsController.getLabelsForDoc(@req, @res, @next) + @LabelsController.broadcastLabelsForDoc(@req, @res, @next) @LabelsHandler.getLabelsForDoc.callCount.should.equal 1 @LabelsHandler.getLabelsForDoc.calledWith(@projectId).should.equal true it 'should call next with an error', -> - @LabelsController.getLabelsForDoc(@req, @res, @next) + @LabelsController.broadcastLabelsForDoc(@req, @res, @next) @next.callCount.should.equal 1 expect(@next.lastCall.args[0]).to.be.instanceof Error it 'should not send a json response', -> - @LabelsController.getLabelsForDoc(@req, @res, @next) + @LabelsController.broadcastLabelsForDoc(@req, @res, @next) @res.json.callCount.should.equal 0