From bfeefe8406fdcbf51e243d1437e3e8e77136e326 Mon Sep 17 00:00:00 2001 From: Ersun Warncke Date: Tue, 18 Dec 2018 10:56:40 -0400 Subject: [PATCH] Merge pull request #1282 from sharelatex/ew-collabratec-support-imported-projects Support importing projects with collabratec users to v2 GitOrigin-RevId: 2dd3781dde158cdab6bf95138311224a032ab8a4 --- .../ProjectCollabratecDetailsHandler.coffee | 45 ++++++--- .../Features/Project/ProjectGetter.coffee | 6 ++ .../ProjectCollabratecDetailsTest.coffee | 98 +++++++++++++++++++ .../coffee/Project/ProjectGetterTests.coffee | 30 ++++++ 4 files changed, 166 insertions(+), 13 deletions(-) diff --git a/services/web/app/coffee/Features/Project/ProjectCollabratecDetailsHandler.coffee b/services/web/app/coffee/Features/Project/ProjectCollabratecDetailsHandler.coffee index 02d54cf1fe..e4b47c9da9 100644 --- a/services/web/app/coffee/Features/Project/ProjectCollabratecDetailsHandler.coffee +++ b/services/web/app/coffee/Features/Project/ProjectCollabratecDetailsHandler.coffee @@ -3,18 +3,12 @@ Project = require("../../models/Project").Project module.exports = ProjectCollabratecDetailsHandler = initializeCollabratecProject: (project_id, user_id, collabratec_document_id, collabratec_privategroup_id, callback=(err)->) -> - try - project_id = ObjectId(project_id) - user_id = ObjectId(user_id) - catch err - return callback err - update = $set: { collabratecUsers: [ { user_id, collabratec_document_id, collabratec_privategroup_id } ] } - Project.update { _id: project_id }, update, callback + ProjectCollabratecDetailsHandler.setCollabratecUsers project_id, [ { user_id, collabratec_document_id, collabratec_privategroup_id } ], callback isLinkedCollabratecUserProject: (project_id, user_id, callback=(err, isLinked)->) -> try - project_id = ObjectId(project_id) - user_id = ObjectId(user_id) + project_id = ObjectId project_id + user_id = ObjectId user_id catch err return callback err query = @@ -27,8 +21,8 @@ module.exports = ProjectCollabratecDetailsHandler = linkCollabratecUserProject: (project_id, user_id, collabratec_document_id, callback=(err)->) -> try - project_id = ObjectId(project_id) - user_id = ObjectId(user_id) + project_id = ObjectId project_id + user_id = ObjectId user_id catch err return callback err query = @@ -41,10 +35,24 @@ module.exports = ProjectCollabratecDetailsHandler = user_id: user_id Project.update query, update, callback + setCollabratecUsers: (project_id, collabratec_users, callback=(err)->) -> + try + project_id = ObjectId project_id + catch err + return callback err + callback(new Error "collabratec_users must be array") unless Array.isArray(collabratec_users) + for collabratec_user in collabratec_users + try + collabratec_user.user_id = ObjectId(collabratec_user.user_id) + catch err + return callback err + update = $set: { collabratecUsers: collabratec_users } + Project.update { _id: project_id }, update, callback + unlinkCollabratecUserProject: (project_id, user_id, callback=(err)->) -> try - project_id = ObjectId(project_id) - user_id = ObjectId(user_id) + project_id = ObjectId project_id + user_id = ObjectId user_id catch err return callback err query = @@ -52,3 +60,14 @@ module.exports = ProjectCollabratecDetailsHandler = update = $pull: collabratecUsers: user_id: user_id Project.update query, update, callback + + updateCollabratecUserIds: (old_user_id, new_user_id, callback=(err)->) -> + try + old_user_id = ObjectId old_user_id + new_user_id = ObjectId new_user_id + catch err + return callback err + query = "collabratecUsers.user_id": old_user_id + update = $set: "collabratecUsers.$.user_id": new_user_id + options = multi: true + Project.update query, update, options, callback diff --git a/services/web/app/coffee/Features/Project/ProjectGetter.coffee b/services/web/app/coffee/Features/Project/ProjectGetter.coffee index 8a32556638..8acb46e0a2 100644 --- a/services/web/app/coffee/Features/Project/ProjectGetter.coffee +++ b/services/web/app/coffee/Features/Project/ProjectGetter.coffee @@ -70,6 +70,12 @@ module.exports = ProjectGetter = return callback(err) callback(null, project?[0]) + getProjectIdByReadAndWriteToken: (token, callback=(err, project_id)->) -> + Project.findOne {'tokens.readAndWrite': token}, {_id: 1}, (err, project) -> + return callback err if err? + return callback() unless project? + callback null, project._id + findAllUsersProjects: ( user_id, fields, diff --git a/services/web/test/unit/coffee/Project/ProjectCollabratecDetailsTest.coffee b/services/web/test/unit/coffee/Project/ProjectCollabratecDetailsTest.coffee index e3b4d2cc2d..6d9e4be79e 100644 --- a/services/web/test/unit/coffee/Project/ProjectCollabratecDetailsTest.coffee +++ b/services/web/test/unit/coffee/Project/ProjectCollabratecDetailsTest.coffee @@ -15,6 +15,7 @@ describe "ProjectCollabratecDetailsHandler", -> beforeEach -> @projectId = ObjectId("5bea8747c7bba6012fcaceb3") @userId = ObjectId("5be316a9c7f6aa03802ea8fb") + @userId2 = ObjectId("5c1794b3f0e89b1d1c577eca") @ProjectModel = {} @ProjectCollabratecDetailsHandler = SandboxedModule.require modulePath, requires: "../../models/Project": { Project: @ProjectModel } @@ -140,6 +141,63 @@ describe "ProjectCollabratecDetailsHandler", -> it "should callback with error", -> expect(@callback.firstCall.args[0]).to.be.instanceOf Error + describe "setCollabratecUsers", -> + beforeEach -> + @collabratecUsers = [ + { + user_id: @userId + collabratec_document_id: "collabratec-document-id-1" + collabratec_privategroup_id: "collabratec-private-group-id-1" + }, + { + user_id: @userId2 + collabratec_document_id: "collabratec-document-id-2" + collabratec_privategroup_id: "collabratec-private-group-id-2" + } + ] + + describe "when update succeeds", -> + beforeEach -> + @ProjectModel.update = sinon.stub().yields() + @ProjectCollabratecDetailsHandler.setCollabratecUsers @projectId, @collabratecUsers, @callback + + it "should update project model", -> + update = $set: { + collabratecUsers: @collabratecUsers + } + expect(@ProjectModel.update).to.have.been.calledWith { _id: @projectId }, update, @callback + + describe "when update has error", -> + beforeEach -> + @ProjectModel.update = sinon.stub().yields("error") + @ProjectCollabratecDetailsHandler.setCollabratecUsers @projectId, @collabratecUsers, @callback + + it "should callback with error", -> + expect(@callback).to.have.been.calledWith("error") + + describe "with invalid project_id", -> + beforeEach -> + @ProjectModel.update = sinon.stub() + @ProjectCollabratecDetailsHandler.setCollabratecUsers "bad-project-id", @collabratecUsers, @callback + + it "should not update", -> + expect(@ProjectModel.update).not.to.have.beenCalled + + it "should callback with error", -> + expect(@callback.firstCall.args[0]).to.be.instanceOf Error + + describe "with invalid user_id", -> + beforeEach -> + @collabratecUsers[1].user_id = "bad-user-id" + @ProjectModel.update = sinon.stub() + @ProjectCollabratecDetailsHandler.setCollabratecUsers @projectId, @collabratecUsers, @callback + + it "should not update", -> + expect(@ProjectModel.update).not.to.have.beenCalled + + it "should callback with error", -> + expect(@callback.firstCall.args[0]).to.be.instanceOf Error + describe "unlinkCollabratecUserProject", -> describe "when update succeeds", -> @@ -172,3 +230,43 @@ describe "ProjectCollabratecDetailsHandler", -> it "should callback with error", -> expect(@callback.firstCall.args[0]).to.be.instanceOf Error + + describe "updateCollabratecUserIds", -> + + describe "when update succeeds", -> + beforeEach -> + @ProjectModel.update = sinon.stub().yields() + @ProjectCollabratecDetailsHandler.updateCollabratecUserIds @userId, @userId2, @callback + + it "should update project model", -> + expect(@ProjectModel.update).to.have.been.calledWith { "collabratecUsers.user_id": @userId }, { $set: "collabratecUsers.$.user_id": @userId2 }, { multi: true}, @callback + + describe "when update has error", -> + beforeEach -> + @ProjectModel.update = sinon.stub().yields("error") + @ProjectCollabratecDetailsHandler.updateCollabratecUserIds @userId, @userId2, @callback + + it "should callback with error", -> + expect(@callback).to.have.been.calledWith("error") + + describe "with invalid old_user_id", -> + beforeEach -> + @ProjectModel.update = sinon.stub() + @ProjectCollabratecDetailsHandler.updateCollabratecUserIds "bad-user-id", @userId2, @callback + + it "should not update", -> + expect(@ProjectModel.update).not.to.have.beenCalled + + it "should callback with error", -> + expect(@callback.firstCall.args[0]).to.be.instanceOf Error + + describe "with invalid new_user_id", -> + beforeEach -> + @ProjectModel.update = sinon.stub() + @ProjectCollabratecDetailsHandler.updateCollabratecUserIds @userId, "bad-user-id", @callback + + it "should not update", -> + expect(@ProjectModel.update).not.to.have.beenCalled + + it "should callback with error", -> + expect(@callback.firstCall.args[0]).to.be.instanceOf Error \ No newline at end of file diff --git a/services/web/test/unit/coffee/Project/ProjectGetterTests.coffee b/services/web/test/unit/coffee/Project/ProjectGetterTests.coffee index eac6837d45..a287542128 100644 --- a/services/web/test/unit/coffee/Project/ProjectGetterTests.coffee +++ b/services/web/test/unit/coffee/Project/ProjectGetterTests.coffee @@ -177,3 +177,33 @@ describe "ProjectGetter", -> tokenReadOnly: ['mock-token-ro-projects'] }) .should.equal true + + describe "getProjectIdByReadAndWriteToken", -> + describe "when project find returns project", -> + @beforeEach -> + @Project.findOne = sinon.stub().yields(null, {_id: "project-id"}) + @ProjectGetter.getProjectIdByReadAndWriteToken "token", @callback + + it "should find project with token", -> + @Project.findOne.calledWithMatch( + {'tokens.readAndWrite': "token"} + ).should.equal true + + it "should callback with project id", -> + @callback.calledWith(null, "project-id").should.equal true + + describe "when project not found", -> + @beforeEach -> + @Project.findOne = sinon.stub().yields() + @ProjectGetter.getProjectIdByReadAndWriteToken "token", @callback + + it "should callback empty", -> + expect(@callback.firstCall.args.length).to.equal 0 + + describe "when project find returns error", -> + @beforeEach -> + @Project.findOne = sinon.stub().yields("error") + @ProjectGetter.getProjectIdByReadAndWriteToken "token", @callback + + it "should callback with error", -> + @callback.calledWith("error").should.equal true