diff --git a/services/web/app/coffee/Features/Tags/TagsController.coffee b/services/web/app/coffee/Features/Tags/TagsController.coffee index 8536f7a114..76f0976c4d 100644 --- a/services/web/app/coffee/Features/Tags/TagsController.coffee +++ b/services/web/app/coffee/Features/Tags/TagsController.coffee @@ -2,19 +2,18 @@ TagsHandler = require("./TagsHandler") logger = require("logger-sharelatex") module.exports = - - processTagsUpdate: (req, res)-> - user_id = req.session.user._id - project_id = req.params.project_id - tag = req.body.tag - TagsHandler.addTag user_id, project_id, tag, -> - res.send() - logger.log user_id:user_id, project_id:project_id, body:req.body, "processing tag update" - getAllTags: (req, res)-> TagsHandler.getAllTags req.session.user._id, (err, allTags)-> res.send(allTags) + addProjectToTag: (req, res, next) -> + user_id = req.session.user._id + {tag_id, project_id} = req.params + logger.log {user_id, tag_id, project_id}, "adding tag to project" + TagsHandler.addProjectToTag user_id, tag_id, project_id, (error) -> + return next(error) if error? + res.status(204).end() + removeProjectFromTag: (req, res, next) -> user_id = req.session.user._id {tag_id, project_id} = req.params diff --git a/services/web/app/coffee/Features/Tags/TagsHandler.coffee b/services/web/app/coffee/Features/Tags/TagsHandler.coffee index cc6b3d151d..61e335356a 100644 --- a/services/web/app/coffee/Features/Tags/TagsHandler.coffee +++ b/services/web/app/coffee/Features/Tags/TagsHandler.coffee @@ -38,15 +38,10 @@ module.exports = TagsHandler = request.del {url, timeout: TIMEOUT}, (err, res, body) -> TagsHandler._handleResponse res, {url, user_id, tag_id, project_id}, callback - addTag: (user_id, project_id, tag, callback)-> - uri = buildUri(user_id, project_id) - opts = - uri:uri - json: - name:tag - timeout: TIMEOUT - logger.log user_id:user_id, project_id:project_id, tag:tag, "send add tag to tags api" - request.post opts, callback + addProjectToTag: (user_id, tag_id, project_id, callback)-> + url = "#{settings.apis.tags.url}/user/#{user_id}/tag/#{tag_id}/project/#{project_id}" + request.post {url, timeout: TIMEOUT}, (err, res, body) -> + TagsHandler._handleResponse res, {url, user_id, tag_id, project_id}, callback requestTags: (user_id, callback)-> opts = diff --git a/services/web/app/coffee/router.coffee b/services/web/app/coffee/router.coffee index d241525e8d..d5510976a0 100644 --- a/services/web/app/coffee/router.coffee +++ b/services/web/app/coffee/router.coffee @@ -132,7 +132,7 @@ module.exports = class Router webRouter.get '/project/download/zip', SecurityManager.requestCanAccessMultipleProjects, ProjectDownloadsController.downloadMultipleProjects webRouter.get '/tag', AuthenticationController.requireLogin(), TagsController.getAllTags - webRouter.post '/project/:project_id/tag', AuthenticationController.requireLogin(), TagsController.processTagsUpdate + webRouter.post '/tag/:tag_id/project/:project_id', AuthenticationController.requireLogin(), TagsController.addProjectToTag webRouter.delete '/tag/:tag_id/project/:project_id', AuthenticationController.requireLogin(), TagsController.removeProjectFromTag webRouter.delete '/tag/:tag_id', AuthenticationController.requireLogin(), TagsController.deleteTag webRouter.post '/tag/:tag_id/rename', AuthenticationController.requireLogin(), TagsController.renameTag diff --git a/services/web/public/coffee/main/project-list/project-list.coffee b/services/web/public/coffee/main/project-list/project-list.coffee index ca844b2072..a1ab71555a 100644 --- a/services/web/public/coffee/main/project-list/project-list.coffee +++ b/services/web/public/coffee/main/project-list/project-list.coffee @@ -198,8 +198,7 @@ define [ project.tags.push tag for project_id in added_project_ids - queuedHttp.post "/project/#{project_id}/tag", { - tag: tag.name + queuedHttp.post "/tag/#{tag._id}/project/#{project_id}", { _csrf: window.csrfToken } diff --git a/services/web/test/UnitTests/coffee/Tags/TagsControllerTests.coffee b/services/web/test/UnitTests/coffee/Tags/TagsControllerTests.coffee index 38908ec71d..e63e619bdf 100644 --- a/services/web/test/UnitTests/coffee/Tags/TagsControllerTests.coffee +++ b/services/web/test/UnitTests/coffee/Tags/TagsControllerTests.coffee @@ -5,14 +5,14 @@ sinon = require('sinon') modulePath = require('path').join __dirname, '../../../../app/js/Features/Tags/TagsController.js' -describe 'Tags controller', -> +describe 'TagsController', -> user_id = "123nd3ijdks" project_id = "123njdskj9jlk" tag = "some_class101" beforeEach -> @handler = - addTag: sinon.stub().callsArgWith(3) + addProjectToTag: sinon.stub().callsArgWith(3) removeProjectFromTag: sinon.stub().callsArgWith(3) deleteTag: sinon.stub().callsArg(2) renameTag: sinon.stub().callsArg(3) @@ -31,13 +31,6 @@ describe 'Tags controller', -> @res = {} @res.status = sinon.stub().returns @res @res.end = sinon.stub() - - describe "processTagsUpdate", -> - it 'Should post the request to the tags api with the user id in the url', (done)-> - @req.body = {tag:tag} - @controller.processTagsUpdate @req, send:=> - @handler.addTag.calledWith(user_id, project_id, tag).should.equal true - done() describe "getAllTags", -> it 'should ask the handler for all tags', (done)-> @@ -93,6 +86,22 @@ describe 'Tags controller', -> @res.status.calledWith(400).should.equal true @res.end.called.should.equal true + describe "addProjectToTag", -> + beforeEach -> + @req.params.tag_id = @tag_id = "tag-id-123" + @req.params.project_id = @project_id = "project-id-123" + @req.session.user._id = @user_id = "user-id-123" + @controller.addProjectToTag @req, @res + + it "should add the tag to the project in the backend", -> + @handler.addProjectToTag + .calledWith(@user_id, @tag_id, @project_id) + .should.equal true + + it "should return 204 status code", -> + @res.status.calledWith(204).should.equal true + @res.end.called.should.equal true + describe "removeProjectFromTag", -> beforeEach -> @req.params.tag_id = @tag_id = "tag-id-123" diff --git a/services/web/test/UnitTests/coffee/Tags/TagsHandlerTests.coffee b/services/web/test/UnitTests/coffee/Tags/TagsHandlerTests.coffee index 88ebb7b182..f0b57c813d 100644 --- a/services/web/test/UnitTests/coffee/Tags/TagsHandlerTests.coffee +++ b/services/web/test/UnitTests/coffee/Tags/TagsHandlerTests.coffee @@ -26,12 +26,6 @@ describe 'TagsHandler', -> log:-> err:-> - describe "addTag", -> - it 'Should post the request to the tags api with the user id in the url', (done)-> - @handler.addTag user_id, project_id, tag, => - @request.post.calledWith({uri:"#{tagsUrl}/user/#{user_id}/project/#{project_id}/tag", timeout:1000, json:{name:tag}}).should.equal true - done() - describe "removeProjectFromAllTags", -> it 'should tell the tags api to remove the project_id from all the users tags', (done)-> @handler.removeProjectFromAllTags user_id, project_id, => @@ -173,5 +167,30 @@ describe 'TagsHandler', -> @request.del = sinon.stub().callsArgWith(1, null, {statusCode: 500}, "") @handler.removeProjectFromTag user_id, tag_id, project_id, @callback + it "should call the callback with an Error", -> + @callback.calledWith(new Error()).should.equal true + + describe "addProjectToTag", -> + describe "successfully", -> + beforeEach -> + @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "") + @handler.addProjectToTag user_id, tag_id, project_id, @callback + + it "should send a request to the tag backend", -> + @request.post + .calledWith({ + url: "#{tagsUrl}/user/#{user_id}/tag/#{tag_id}/project/#{project_id}" + timeout: 1000 + }) + .should.equal true + + it "should call the callback with no error", -> + @callback.calledWith(null).should.equal true + + describe "with error", -> + beforeEach -> + @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 500}, "") + @handler.addProjectToTag user_id, tag_id, project_id, @callback + it "should call the callback with an Error", -> @callback.calledWith(new Error()).should.equal true \ No newline at end of file