Test ReferencesSearchHandler.index

This commit is contained in:
Shane Kilkelly 2016-01-26 16:14:17 +00:00
parent 8a991b0d06
commit d1e53f0cb8
2 changed files with 159 additions and 164 deletions

View file

@ -1,11 +1,9 @@
logger = require("logger-sharelatex") logger = require("logger-sharelatex")
request = require("request") request = require("request")
settings = require("settings-sharelatex") settings = require("settings-sharelatex")
ProjectLocator = require("../Project/ProjectLocator") Project = require("../../models/Project").Project
U = require('underscore') U = require('underscore')
Async = require('async') Async = require('async')
Project = require("../../models/Project").Project
UserGetter = require "../User/UserGetter"
oneMinInMs = 60 * 1000 oneMinInMs = 60 * 1000
fiveMinsInMs = oneMinInMs * 5 fiveMinsInMs = oneMinInMs * 5
@ -13,6 +11,9 @@ fiveMinsInMs = oneMinInMs * 5
module.exports = ReferencesSearchHandler = module.exports = ReferencesSearchHandler =
_buildDocUrl: (projectId, docId) ->
"#{settings.apis.web.url}/project/#{projectId}/doc/#{docId}"
_findBibDocIds: (project) -> _findBibDocIds: (project) ->
ids = [] ids = []
@ -64,61 +65,3 @@ module.exports = ReferencesSearchHandler =
err = new Error("references api responded with non-success code: #{res.statusCode}") err = new Error("references api responded with non-success code: #{res.statusCode}")
logger.log {err, projectId}, "error updating references" logger.log {err, projectId}, "error updating references"
return callback(err) return callback(err)
## ## ## ##
# indexProjectReferences: (project, callback = (err) ->) ->
# logger.log {projectId: project._id}, "try indexing references from project"
# ids = ReferencesSearchHandler._findBibDocIds(project)
# logger.log {projectId: project._id, count: ids.length}, "found bib files in project"
# Async.eachSeries(
# ids,
# (docId, next) ->
# ReferencesSearchHandler.indexFile project._id, docId, (err) ->
# next(err)
# , (err) ->
# logger.log {projectId: project._id, count: ids.length}, "done index bib files in project"
# callback(err)
# )
# indexFile: (projectId, fileId, callback = (err)->) ->
# target_url = "#{settings.apis.references.url}/project/#{projectId}"
# fileUrl = ReferencesSearchHandler._buildDocUrl projectId, fileId
# logger.log {projectId, fileId}, "checking if file should be fully indexed"
# ReferencesSearchHandler._isFullIndex projectId, (err, isFullIndex) ->
# if err
# logger.err {projectId, fileId, err}, "error checking if file should be fully indexed"
# return callback(err)
# logger.log {projectId, fileId, isFullIndex}, "sending index request to references api"
# request.post {
# url: target_url
# json:
# referencesUrl: fileUrl
# }, (err, res, result) ->
# if err
# return callback(err)
# if 200 <= res.statusCode < 300
# return callback(null)
# else
# err = new Error("references api responded with non-success code: #{res.statusCode}")
# logger.log {err, projectId, fileUrl}, "error updating references"
# return callback(err)
# getKeys: (projectId, callback = (err, result)->) ->
# logger.log {projectId}, "getting keys from remote references api"
# url = "#{settings.apis.references.url}/project/#{projectId}/keys"
# request.get {
# url: url
# json: true
# }, (err, res, result) ->
# if err
# return callback(err)
# if 200 <= res.statusCode < 300
# return callback(null, result)
# else
# err = new Error("references api responded with non-success code: #{res.statusCode}")
# logger.log {err, projectId}, "error getting references keys"
# return callback(err)
# _buildDocUrl: (projectId, docId) ->
# "#{settings.apis.web.url}/project/#{projectId}/doc/#{docId}"

View file

@ -8,8 +8,23 @@ modulePath = "../../../../app/js/Features/ReferencesSearch/ReferencesSearchHandl
describe 'ReferencesSearchHandler', -> describe 'ReferencesSearchHandler', ->
beforeEach -> beforeEach ->
@project_id = '222' @projectId = '222'
@file_id = '111111' @fakeProject =
_id: @projectId
owner_ref: @fakeOwner =
_id: 'some_owner'
features:
references: false
rootFolder: [
docs: [
{name: 'one.bib', _id: 'aaa'},
{name: 'two.txt', _id: 'bbb'},
]
folders: [
{docs: [{name: 'three.bib', _id: 'ccc'}], folders: []}
]
]
@docIds = ['aaa', 'ccc']
@handler = SandboxedModule.require modulePath, requires: @handler = SandboxedModule.require modulePath, requires:
'logger-sharelatex': { 'logger-sharelatex': {
log: -> log: ->
@ -24,15 +39,147 @@ describe 'ReferencesSearchHandler', ->
get: sinon.stub() get: sinon.stub()
post: sinon.stub() post: sinon.stub()
} }
'../../models/Project': @Project = { '../../models/Project': {
Project: { Project: @Project = {
findById: sinon.stub().callsArgWith(2, null, {owner_ref: '111'}) findPopulatedById: sinon.stub().callsArgWith(1, null, @fakeProject)
} }
} }
'../User/UserGetter': @UserGetter = { @fakeResponseData =
getUser: sinon.stub().callsArgWith(2, null, {features: {references: false}}) projectId: @projectId
} keys: ['k1', 'k2']
describe 'index', ->
beforeEach ->
sinon.stub(@handler, '_findBibDocIds')
sinon.stub(@handler, '_isFullIndex').callsArgWith(1, null, true)
@request.post.callsArgWith(1, null, {statusCode: 200}, @fakeResponseData)
@call = (callback) =>
@handler.index @projectId, @docIds, callback
describe 'with docIds as an array', ->
beforeEach ->
@docIds = ['aaa', 'ccc']
it 'should not call _findBibDocIds', (done) ->
@call (err, data) =>
@handler._findBibDocIds.callCount.should.equal 0
done()
it 'should call Project.findPopulatedById', (done) ->
@call (err, data) =>
@Project.findPopulatedById.callCount.should.equal 1
@Project.findPopulatedById.calledWith(@projectId).should.equal true
done()
it 'should make a request to references service', (done) ->
@call (err, data) =>
@request.post.callCount.should.equal 1
arg = @request.post.firstCall.args[0]
expect(arg.json).to.have.all.keys 'docUrls', 'fullIndex'
expect(arg.json.docUrls.length).to.equal 2
expect(arg.json.fullIndex).to.equal true
done()
it 'should not produce an error', (done) ->
@call (err, data) =>
expect(err).to.equal null
done()
it 'should return data', (done) ->
@call (err, data) =>
expect(data).to.not.equal null
expect(data).to.not.equal undefined
expect(data).to.equal @fakeResponseData
done()
describe 'with docIds as "ALL"', ->
beforeEach ->
@docIds = 'ALL'
@handler._findBibDocIds.returns(['aaa', 'ccc'])
it 'should call _findBibDocIds', (done) ->
@call (err, data) =>
@handler._findBibDocIds.callCount.should.equal 1
@handler._findBibDocIds.calledWith(@fakeProject).should.equal true
done()
it 'should not produce an error', (done) ->
@call (err, data) =>
expect(err).to.equal null
done()
it 'should return data', (done) ->
@call (err, data) =>
expect(data).to.not.equal null
expect(data).to.not.equal undefined
expect(data).to.equal @fakeResponseData
done()
describe 'when Project.findPopulatedById produces an error', ->
beforeEach ->
@Project.findPopulatedById.callsArgWith(1, new Error('woops'))
it 'should produce an error', (done) ->
@call (err, data) =>
expect(err).to.not.equal null
expect(err).to.be.instanceof Error
expect(data).to.equal undefined
done()
it 'should not send request', (done) ->
@call (err, data) =>
@request.post.callCount.should.equal 0
done()
describe 'when _isFullIndex produces an error', ->
beforeEach ->
@Project.findPopulatedById.callsArgWith(1, null, @fakeProject)
@handler._isFullIndex.callsArgWith(1, new Error('woops'))
it 'should produce an error', (done) ->
@call (err, data) =>
expect(err).to.not.equal null
expect(err).to.be.instanceof Error
expect(data).to.equal undefined
done()
it 'should not send request', (done) ->
@call (err, data) =>
@request.post.callCount.should.equal 0
done()
describe 'when request produces an error', ->
beforeEach ->
@Project.findPopulatedById.callsArgWith(1, null, @fakeProject)
@handler._isFullIndex.callsArgWith(1, null, false)
@request.post.callsArgWith(1, new Error('woops'))
it 'should produce an error', (done) ->
@call (err, data) =>
expect(err).to.not.equal null
expect(err).to.be.instanceof Error
expect(data).to.equal undefined
done()
describe 'when request responds with error status', ->
beforeEach ->
@Project.findPopulatedById.callsArgWith(1, null, @fakeProject)
@handler._isFullIndex.callsArgWith(1, null, false)
@request.post.callsArgWith(1, null, {statusCode: 500}, null)
it 'should produce an error', (done) ->
@call (err, data) =>
expect(err).to.not.equal null
expect(err).to.be.instanceof Error
expect(data).to.equal undefined
done()
describe '_findBibDocIds', -> describe '_findBibDocIds', ->
@ -82,98 +229,3 @@ describe 'ReferencesSearchHandler', ->
@call (err, isFullIndex) => @call (err, isFullIndex) =>
expect(err).to.equal null expect(err).to.equal null
expect(isFullIndex).to.equal false expect(isFullIndex).to.equal false
describe 'index', ->
# describe 'indexFile', ->
# describe 'full index or not', ->
# beforeEach ->
# @request.post.callsArgWith(1, null, {statusCode: 200}, {})
# describe 'when full index is not required', ->
# beforeEach ->
# @UserGetter.getUser.callsArgWith(2, null, {features: {references: false}})
# it 'should set fullIndex to true', (done) ->
# @handler.indexFile @project_id, @file_id, (err) =>
# @request.post.calledOnce.should.equal true
# options = @request.post.firstCall.args[0]
# options.json.fullIndex.should.equal false
# done()
# describe 'when full index is required', ->
# beforeEach ->
# @UserGetter.getUser.callsArgWith(2, null, {features: {references: true}})
# it 'should set fullIndex to true', (done) ->
# @handler.indexFile @project_id, @file_id, (err) =>
# @request.post.calledOnce.should.equal true
# options = @request.post.firstCall.args[0]
# options.json.fullIndex.should.equal true
# done()
# describe 'when index operation is successful', ->
# beforeEach ->
# @request.post.callsArgWith(1, null, {statusCode: 201}, {})
# it 'should not produce an error', (done) ->
# @handler.indexFile @project_id, @file_id, (err) =>
# expect(err).to.equal null
# @request.post.calledOnce.should.equal true
# options = @request.post.firstCall.args[0]
# options.json.fullIndex.should.equal false
# options.json.referencesUrl.should.not.be.undefined
# options.url.should.not.be.undefined
# done()
# describe 'when index operation fails', ->
# beforeEach ->
# @request.post.callsArgWith(1, null, {statusCode: 500}, {})
# it 'should produce an error', (done) ->
# @handler.indexFile @project_id, @file_id, (err) =>
# expect(err).to.not.equal null
# done()
# describe 'getKeys', ->
# describe 'when request is successful', ->
# beforeEach ->
# @data =
# projectId: @projectId
# keys: ['a', 'b', 'c']
# @request.get.callsArgWith(1, null, {statusCode: 200}, @data)
# it 'should not produce an error', ->
# @handler.getKeys @project_id, (err, result) =>
# expect(err).to.equal null
# it 'should produce a result object', ->
# @handler.getKeys @project_id, (err, result) =>
# expect(result).to.not.equal null
# expect(result).to.deep.equal @data
# describe 'when request fails', ->
# beforeEach ->
# @data =
# projectId: @project_Id
# keys: ['a', 'b', 'c']
# @request.get.callsArgWith(1, null, {statusCode: 500}, null)
# it 'should produce an error', ->
# @handler.getKeys @project_id, (err, result) =>
# expect(err).to.not.equal null
# it 'should not produce a result', ->
# @handler.getKeys @project_id, (err, result) =>
# expect(result).to.not.equal null