Don't use deprecated Project.findPopulatedById in ReferencesManager

This commit is contained in:
James Allen 2016-03-08 14:05:56 +00:00
parent b64c8e3d78
commit 0882eb2a99
3 changed files with 31 additions and 24 deletions

View file

@ -229,7 +229,6 @@ module.exports = ProjectController =
title: project.name title: project.name
priority_title: true priority_title: true
bodyClasses: ["editor"] bodyClasses: ["editor"]
project : project
project_id : project._id project_id : project._id
user : { user : {
id : user.id id : user.id

View file

@ -2,6 +2,7 @@ logger = require("logger-sharelatex")
request = require("request") request = require("request")
settings = require("settings-sharelatex") settings = require("settings-sharelatex")
Project = require("../../models/Project").Project Project = require("../../models/Project").Project
User = require("../../models/User").User
DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler') DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
U = require('underscore') U = require('underscore')
Async = require('async') Async = require('async')
@ -31,11 +32,12 @@ module.exports = ReferencesHandler =
return ids return ids
_isFullIndex: (project, callback = (err, result) ->) -> _isFullIndex: (project, callback = (err, result) ->) ->
owner = project.owner_ref User.find { _id: project.owner_ref }, { features: true }, (err, owner) ->
callback(null, owner.features.references == true) return callback(err) if err?
callback(null, owner?.features?.references == true)
indexAll: (projectId, callback=(err, data)->) -> indexAll: (projectId, callback=(err, data)->) ->
Project.findPopulatedById projectId, (err, project) -> Project.find { _id: projectId }, (err, project) ->
if err if err
logger.err {err, projectId}, "error finding project" logger.err {err, projectId}, "error finding project"
return callback(err) return callback(err)
@ -44,7 +46,7 @@ module.exports = ReferencesHandler =
ReferencesHandler._doIndexOperation(projectId, project, docIds, callback) ReferencesHandler._doIndexOperation(projectId, project, docIds, callback)
index: (projectId, docIds, callback=(err, data)->) -> index: (projectId, docIds, callback=(err, data)->) ->
Project.findPopulatedById projectId, (err, project) -> Project.find { _id: projectId }, (err, project) ->
if err if err
logger.err {err, projectId}, "error finding project" logger.err {err, projectId}, "error finding project"
return callback(err) return callback(err)

View file

@ -41,9 +41,12 @@ describe 'ReferencesHandler', ->
} }
'../../models/Project': { '../../models/Project': {
Project: @Project = { Project: @Project = {
findPopulatedById: sinon.stub().callsArgWith(1, null, @fakeProject) find: sinon.stub().callsArgWith(1, null, @fakeProject)
} }
} }
'../../models/User': {
User: @User = {}
}
'../DocumentUpdater/DocumentUpdaterHandler': @DocumentUpdaterHandler = { '../DocumentUpdater/DocumentUpdaterHandler': @DocumentUpdaterHandler = {
flushDocToMongo: sinon.stub().callsArgWith(2, null) flushDocToMongo: sinon.stub().callsArgWith(2, null)
} }
@ -70,10 +73,10 @@ describe 'ReferencesHandler', ->
@handler._findBibDocIds.callCount.should.equal 0 @handler._findBibDocIds.callCount.should.equal 0
done() done()
it 'should call Project.findPopulatedById', (done) -> it 'should call Project.find', (done) ->
@call (err, data) => @call (err, data) =>
@Project.findPopulatedById.callCount.should.equal 1 @Project.find.callCount.should.equal 1
@Project.findPopulatedById.calledWith(@projectId).should.equal true @Project.find.calledWith(_id: @projectId).should.equal true
done() done()
it 'should not call _findBibDocIds', (done) -> it 'should not call _findBibDocIds', (done) ->
@ -109,10 +112,10 @@ describe 'ReferencesHandler', ->
expect(data).to.equal @fakeResponseData expect(data).to.equal @fakeResponseData
done() done()
describe 'when Project.findPopulatedById produces an error', -> describe 'when Project.find produces an error', ->
beforeEach -> beforeEach ->
@Project.findPopulatedById.callsArgWith(1, new Error('woops')) @Project.find.callsArgWith(1, new Error('woops'))
it 'should produce an error', (done) -> it 'should produce an error', (done) ->
@call (err, data) => @call (err, data) =>
@ -129,7 +132,7 @@ describe 'ReferencesHandler', ->
describe 'when _isFullIndex produces an error', -> describe 'when _isFullIndex produces an error', ->
beforeEach -> beforeEach ->
@Project.findPopulatedById.callsArgWith(1, null, @fakeProject) @Project.find.callsArgWith(1, null, @fakeProject)
@handler._isFullIndex.callsArgWith(1, new Error('woops')) @handler._isFullIndex.callsArgWith(1, new Error('woops'))
it 'should produce an error', (done) -> it 'should produce an error', (done) ->
@ -147,7 +150,7 @@ describe 'ReferencesHandler', ->
describe 'when flushDocToMongo produces an error', -> describe 'when flushDocToMongo produces an error', ->
beforeEach -> beforeEach ->
@Project.findPopulatedById.callsArgWith(1, null, @fakeProject) @Project.find.callsArgWith(1, null, @fakeProject)
@handler._isFullIndex.callsArgWith(1, false) @handler._isFullIndex.callsArgWith(1, false)
@DocumentUpdaterHandler.flushDocToMongo.callsArgWith(2, new Error('woops')) @DocumentUpdaterHandler.flushDocToMongo.callsArgWith(2, new Error('woops'))
@ -167,7 +170,7 @@ describe 'ReferencesHandler', ->
describe 'when request produces an error', -> describe 'when request produces an error', ->
beforeEach -> beforeEach ->
@Project.findPopulatedById.callsArgWith(1, null, @fakeProject) @Project.find.callsArgWith(1, null, @fakeProject)
@handler._isFullIndex.callsArgWith(1, null, false) @handler._isFullIndex.callsArgWith(1, null, false)
@DocumentUpdaterHandler.flushDocToMongo.callsArgWith(2, null) @DocumentUpdaterHandler.flushDocToMongo.callsArgWith(2, null)
@request.post.callsArgWith(1, new Error('woops')) @request.post.callsArgWith(1, new Error('woops'))
@ -182,7 +185,7 @@ describe 'ReferencesHandler', ->
describe 'when request responds with error status', -> describe 'when request responds with error status', ->
beforeEach -> beforeEach ->
@Project.findPopulatedById.callsArgWith(1, null, @fakeProject) @Project.find.callsArgWith(1, null, @fakeProject)
@handler._isFullIndex.callsArgWith(1, null, false) @handler._isFullIndex.callsArgWith(1, null, false)
@request.post.callsArgWith(1, null, {statusCode: 500}, null) @request.post.callsArgWith(1, null, {statusCode: 500}, null)
@ -234,10 +237,10 @@ describe 'ReferencesHandler', ->
expect(data).to.equal @fakeResponseData expect(data).to.equal @fakeResponseData
done() done()
describe 'when Project.findPopulatedById produces an error', -> describe 'when Project.find produces an error', ->
beforeEach -> beforeEach ->
@Project.findPopulatedById.callsArgWith(1, new Error('woops')) @Project.find.callsArgWith(1, new Error('woops'))
it 'should produce an error', (done) -> it 'should produce an error', (done) ->
@call (err, data) => @call (err, data) =>
@ -254,7 +257,7 @@ describe 'ReferencesHandler', ->
describe 'when _isFullIndex produces an error', -> describe 'when _isFullIndex produces an error', ->
beforeEach -> beforeEach ->
@Project.findPopulatedById.callsArgWith(1, null, @fakeProject) @Project.find.callsArgWith(1, null, @fakeProject)
@handler._isFullIndex.callsArgWith(1, new Error('woops')) @handler._isFullIndex.callsArgWith(1, new Error('woops'))
it 'should produce an error', (done) -> it 'should produce an error', (done) ->
@ -272,7 +275,7 @@ describe 'ReferencesHandler', ->
describe 'when flushDocToMongo produces an error', -> describe 'when flushDocToMongo produces an error', ->
beforeEach -> beforeEach ->
@Project.findPopulatedById.callsArgWith(1, null, @fakeProject) @Project.find.callsArgWith(1, null, @fakeProject)
@handler._isFullIndex.callsArgWith(1, false) @handler._isFullIndex.callsArgWith(1, false)
@DocumentUpdaterHandler.flushDocToMongo.callsArgWith(2, new Error('woops')) @DocumentUpdaterHandler.flushDocToMongo.callsArgWith(2, new Error('woops'))
@ -312,16 +315,19 @@ describe 'ReferencesHandler', ->
beforeEach -> beforeEach ->
@fakeProject = @fakeProject =
owner_ref: owner_ref: @owner_ref = "owner-ref-123"
@owner =
features: features:
references: false references: false
@User.find = sinon.stub()
@User.find.withArgs({_id: @owner_ref}, {features: true}).yields(null, @owner)
@call = (callback) => @call = (callback) =>
@handler._isFullIndex @fakeProject, callback @handler._isFullIndex @fakeProject, callback
describe 'with references feature on', -> describe 'with references feature on', ->
beforeEach -> beforeEach ->
@fakeProject.owner_ref.features.references = true @owner.features.references = true
it 'should return true', -> it 'should return true', ->
@call (err, isFullIndex) => @call (err, isFullIndex) =>
@ -331,7 +337,7 @@ describe 'ReferencesHandler', ->
describe 'with references feature off', -> describe 'with references feature off', ->
beforeEach -> beforeEach ->
@fakeProject.owner_ref.features.references = false @owner.features.references = false
it 'should return false', -> it 'should return false', ->
@call (err, isFullIndex) => @call (err, isFullIndex) =>