From a107b0cc334a1b60c31727cd5c83e16cf89714e7 Mon Sep 17 00:00:00 2001 From: Simon Detheridge Date: Fri, 11 Jan 2019 14:14:39 +0000 Subject: [PATCH] Merge pull request #1380 from sharelatex/ew-fix-project-getter-error fix callback is not a function error GitOrigin-RevId: 1bbe80f4086a17576f21ca9fc7b96f59d1b0614c --- .../Features/Project/ProjectGetter.coffee | 12 +- .../coffee/Project/ProjectGetterTests.coffee | 107 +++++++++++++----- 2 files changed, 82 insertions(+), 37 deletions(-) diff --git a/services/web/app/coffee/Features/Project/ProjectGetter.coffee b/services/web/app/coffee/Features/Project/ProjectGetter.coffee index 8acb46e0a2..81d2090bc0 100644 --- a/services/web/app/coffee/Features/Project/ProjectGetter.coffee +++ b/services/web/app/coffee/Features/Project/ProjectGetter.coffee @@ -24,13 +24,11 @@ module.exports = ProjectGetter = ProjectGetter.getProject project_id, excludes, callback getProject: (project_id, projection, callback) -> - if !project_id? - return callback(new Error("no project_id provided")) - if typeof(projection) == "function" && !callback? callback = projection projection = {} - + if !project_id? + return callback(new Error("no project_id provided")) if typeof(projection) != "object" return callback(new Error("projection is not an object")) @@ -43,13 +41,11 @@ module.exports = ProjectGetter = ProjectGetter.getProjectWithoutLock project_id, projection, callback getProjectWithoutLock: (project_id, projection, callback) -> - if !project_id? - return callback(new Error("no project_id provided")) - if typeof(projection) == "function" && !callback? callback = projection projection = {} - + if !project_id? + return callback(new Error("no project_id provided")) if typeof(projection) != "object" return callback(new Error("projection is not an object")) diff --git a/services/web/test/unit/coffee/Project/ProjectGetterTests.coffee b/services/web/test/unit/coffee/Project/ProjectGetterTests.coffee index a287542128..18c6c14b31 100644 --- a/services/web/test/unit/coffee/Project/ProjectGetterTests.coffee +++ b/services/web/test/unit/coffee/Project/ProjectGetterTests.coffee @@ -109,46 +109,95 @@ describe "ProjectGetter", -> _id: @project_id = "56d46b0a1d3422b87c5ebcb1" @db.projects.find = sinon.stub().callsArgWith(2, null, [@project]) - describe "passing an id", -> + describe "without projection", -> + describe "with project id", -> + beforeEach -> + @ProjectGetter.getProject @project_id, @callback + + it "should call find with the project id", -> + expect(@db.projects.find.callCount).to.equal 1 + expect(@db.projects.find.lastCall.args[0]).to.deep.equal { + _id: ObjectId(@project_id) + } + + describe "without project id", -> + beforeEach -> + @ProjectGetter.getProject null, @callback + + it "should callback with error", -> + expect(@db.projects.find.callCount).to.equal 0 + expect(@callback.lastCall.args[0]).to.be.instanceOf Error + + describe "with projection", -> beforeEach -> - @ProjectGetter.getProjectWithOnlyFolders @project_id, @callback + @projection = {_id: 1} - it "should call find with the project id", -> - expect(@db.projects.find.lastCall.args[0]).to.deep.equal { - _id: ObjectId(@project_id) - } + describe "with project id", -> + beforeEach -> + @ProjectGetter.getProject @project_id, @projection, @callback - it "should exclude the docs and files linesaaaa", -> - excludes = - "rootFolder.docs": 0 - "rootFolder.fileRefs": 0 - "rootFolder.folders.docs": 0 - "rootFolder.folders.fileRefs": 0 - "rootFolder.folders.folders.docs": 0 - "rootFolder.folders.folders.fileRefs": 0 - "rootFolder.folders.folders.folders.docs": 0 - "rootFolder.folders.folders.folders.fileRefs": 0 - "rootFolder.folders.folders.folders.folders.docs": 0 - "rootFolder.folders.folders.folders.folders.fileRefs": 0 - "rootFolder.folders.folders.folders.folders.folders.docs": 0 - "rootFolder.folders.folders.folders.folders.folders.fileRefs": 0 - "rootFolder.folders.folders.folders.folders.folders.folders.docs": 0 - "rootFolder.folders.folders.folders.folders.folders.folders.fileRefs": 0 - "rootFolder.folders.folders.folders.folders.folders.folders.folders.docs": 0 - "rootFolder.folders.folders.folders.folders.folders.folders.folders.fileRefs": 0 - @db.projects.find.calledWith(sinon.match.any, excludes).should.equal true + it "should call find with the project id", -> + expect(@db.projects.find.callCount).to.equal 1 + expect(@db.projects.find.lastCall.args[0]).to.deep.equal { + _id: ObjectId(@project_id) + } + expect(@db.projects.find.lastCall.args[1]).to.deep.equal @projection - it "should call the callback with the project", -> - @callback.calledWith(null, @project).should.equal true + describe "without project id", -> + beforeEach -> + @ProjectGetter.getProject null, @callback + it "should callback with error", -> + expect(@db.projects.find.callCount).to.equal 0 + expect(@callback.lastCall.args[0]).to.be.instanceOf Error - - describe "getProject", -> + describe "getProjectWithoutLock", -> beforeEach ()-> @project = _id: @project_id = "56d46b0a1d3422b87c5ebcb1" @db.projects.find = sinon.stub().callsArgWith(2, null, [@project]) + describe "without projection", -> + describe "with project id", -> + beforeEach -> + @ProjectGetter.getProjectWithoutLock @project_id, @callback + + it "should call find with the project id", -> + expect(@db.projects.find.callCount).to.equal 1 + expect(@db.projects.find.lastCall.args[0]).to.deep.equal { + _id: ObjectId(@project_id) + } + + describe "without project id", -> + beforeEach -> + @ProjectGetter.getProjectWithoutLock null, @callback + + it "should callback with error", -> + expect(@db.projects.find.callCount).to.equal 0 + expect(@callback.lastCall.args[0]).to.be.instanceOf Error + + describe "with projection", -> + beforeEach -> + @projection = {_id: 1} + + describe "with project id", -> + beforeEach -> + @ProjectGetter.getProjectWithoutLock @project_id, @projection, @callback + + it "should call find with the project id", -> + expect(@db.projects.find.callCount).to.equal 1 + expect(@db.projects.find.lastCall.args[0]).to.deep.equal { + _id: ObjectId(@project_id) + } + expect(@db.projects.find.lastCall.args[1]).to.deep.equal @projection + + describe "without project id", -> + beforeEach -> + @ProjectGetter.getProjectWithoutLock null, @callback + + it "should callback with error", -> + expect(@db.projects.find.callCount).to.equal 0 + expect(@callback.lastCall.args[0]).to.be.instanceOf Error describe "findAllUsersProjects", -> beforeEach ->