| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- SandboxedModule = require('sandboxed-module')
- should = require('chai').should()
- expect = require('chai').expect
- sinon = require 'sinon'
- assert = require("chai").assert
- modulePath = "../../../../app/js/Features/References/ReferencesHandler"
- describe 'ReferencesHandler', ->
- beforeEach ->
- @projectId = '222'
- @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'}],
- fileRefs: [{name: 'four.bib', _id: 'ghg'}],
- folders: []
- }
- ]
- ]
- @docIds = ['aaa', 'ccc']
- @handler = SandboxedModule.require modulePath, requires:
- 'logger-sharelatex': {
- log: ->
- err: ->
- }
- 'settings-sharelatex': @settings = {
- apis:
- references: {url: 'http://some.url/references'}
- docstore: {url: 'http://some.url/docstore'}
- filestore: {url: 'http://some.url/filestore'}
- }
- 'request': @request = {
- get: sinon.stub()
- post: sinon.stub()
- }
- '../Project/ProjectGetter': @ProjectGetter = {
- getProject: sinon.stub().callsArgWith(2, null, @fakeProject)
- }
- '../User/UserGetter': @UserGetter = {
- getUser: sinon.stub()
- }
- '../DocumentUpdater/DocumentUpdaterHandler': @DocumentUpdaterHandler = {
- flushDocToMongo: sinon.stub().callsArgWith(2, null)
- }
- @fakeResponseData =
- projectId: @projectId
- keys: ['k1', 'k2']
- describe 'index', ->
- beforeEach ->
- sinon.stub(@handler, '_findBibDocIds')
- sinon.stub(@handler, '_findBibFileIds')
- 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 ProjectGetter.getProject', (done) ->
- @call (err, data) =>
- @ProjectGetter.getProject.callCount.should.equal 1
- @ProjectGetter.getProject.calledWith(@projectId).should.equal true
- done()
- it 'should not call _findBibDocIds', (done) ->
- @call (err, data) =>
- @handler._findBibDocIds.callCount.should.equal 0
- done()
- it 'should call DocumentUpdaterHandler.flushDocToMongo', (done) ->
- @call (err, data) =>
- @DocumentUpdaterHandler.flushDocToMongo.callCount.should.equal 2
- @docIds.forEach (docId) =>
- @DocumentUpdaterHandler.flushDocToMongo.calledWith(@projectId, docId).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 'when ProjectGetter.getProject produces an error', ->
- beforeEach ->
- @ProjectGetter.getProject.callsArgWith(2, 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 ->
- @ProjectGetter.getProject.callsArgWith(2, 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 flushDocToMongo produces an error', ->
- beforeEach ->
- @ProjectGetter.getProject.callsArgWith(2, null, @fakeProject)
- @handler._isFullIndex.callsArgWith(1, false)
- @DocumentUpdaterHandler.flushDocToMongo.callsArgWith(2, 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 ->
- @ProjectGetter.getProject.callsArgWith(2, null, @fakeProject)
- @handler._isFullIndex.callsArgWith(1, null, false)
- @DocumentUpdaterHandler.flushDocToMongo.callsArgWith(2, null)
- @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 ->
- @ProjectGetter.getProject.callsArgWith(2, 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 'indexAll', ->
- beforeEach ->
- sinon.stub(@handler, '_findBibDocIds').returns(['aaa', 'ccc'])
- sinon.stub(@handler, '_findBibFileIds').returns(['fff', 'ggg'])
- sinon.stub(@handler, '_isFullIndex').callsArgWith(1, null, true)
- @request.post.callsArgWith(1, null, {statusCode: 200}, @fakeResponseData)
- @call = (callback) =>
- @handler.indexAll @projectId, callback
- it 'should call _findBibDocIds', (done) ->
- @call (err, data) =>
- @handler._findBibDocIds.callCount.should.equal 1
- @handler._findBibDocIds.calledWith(@fakeProject).should.equal true
- done()
- it 'should call _findBibFileIds', (done) ->
- @call (err, data) =>
- @handler._findBibDocIds.callCount.should.equal 1
- @handler._findBibDocIds.calledWith(@fakeProject).should.equal true
- done()
- it 'should call DocumentUpdaterHandler.flushDocToMongo', (done) ->
- @call (err, data) =>
- @DocumentUpdaterHandler.flushDocToMongo.callCount.should.equal 2
- 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 4
- 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 'when ProjectGetter.getProject produces an error', ->
- beforeEach ->
- @ProjectGetter.getProject.callsArgWith(2, 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 ->
- @ProjectGetter.getProject.callsArgWith(2, 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 flushDocToMongo produces an error', ->
- beforeEach ->
- @ProjectGetter.getProject.callsArgWith(2, null, @fakeProject)
- @handler._isFullIndex.callsArgWith(1, false)
- @DocumentUpdaterHandler.flushDocToMongo.callsArgWith(2, 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 '_findBibDocIds', ->
- beforeEach ->
- @fakeProject =
- rootFolder: [
- docs: [
- {name: 'one.bib', _id: 'aaa'},
- {name: 'two.txt', _id: 'bbb'},
- ]
- folders: [
- {docs: [{name: 'three.bib', _id: 'ccc'}], folders: []}
- ]
- ]
- @expectedIds = ['aaa', 'ccc']
- it 'should select the correct docIds', ->
- result = @handler._findBibDocIds(@fakeProject)
- expect(result).to.deep.equal @expectedIds
- it 'should not error with a non array of folders from dirty data', ->
- @fakeProject.rootFolder[0].folders[0].folders = {}
- result = @handler._findBibDocIds(@fakeProject)
- expect(result).to.deep.equal @expectedIds
- describe '_findBibFileIds', ->
- beforeEach ->
- @fakeProject =
- rootFolder: [
- docs: [
- {name: 'one.bib', _id: 'aaa'},
- {name: 'two.txt', _id: 'bbb'},
- ]
- fileRefs: [
- {name: 'other.bib', _id: 'ddd'}
- ],
- folders: [
- {
- docs: [{name: 'three.bib', _id: 'ccc'}],
- fileRefs: [{name: 'four.bib', _id: 'ghg'}],
- folders: []
- }
- ]
- ]
- @expectedIds = ['ddd', 'ghg']
- it 'should select the correct docIds', ->
- result = @handler._findBibFileIds(@fakeProject)
- expect(result).to.deep.equal @expectedIds
- describe '_isFullIndex', ->
- beforeEach ->
- @fakeProject =
- owner_ref: @owner_ref = "owner-ref-123"
- @owner =
- features:
- references: false
- @UserGetter.getUser = sinon.stub()
- @UserGetter.getUser.withArgs(@owner_ref, {features: true}).yields(null, @owner)
- @call = (callback) =>
- @handler._isFullIndex @fakeProject, callback
- describe 'with references feature on', ->
- beforeEach ->
- @owner.features.references = true
- it 'should return true', ->
- @call (err, isFullIndex) =>
- expect(err).to.equal null
- expect(isFullIndex).to.equal true
- describe 'with references feature off', ->
- beforeEach ->
- @owner.features.references = false
- it 'should return false', ->
- @call (err, isFullIndex) =>
- expect(err).to.equal null
- expect(isFullIndex).to.equal false
- describe 'with referencesSearch', ->
- beforeEach ->
- @owner.features = {referencesSearch: true, references: false}
- it 'should return true', ->
- @call (err, isFullIndex) =>
- expect(err).to.equal null
- expect(isFullIndex).to.equal true
|