ReferencesSearchHandlerTests.coffee 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. SandboxedModule = require('sandboxed-module')
  2. should = require('chai').should()
  3. expect = require('chai').expect
  4. sinon = require 'sinon'
  5. assert = require("chai").assert
  6. modulePath = "../../../../app/js/Features/ReferencesSearch/ReferencesSearchHandler"
  7. describe 'ReferencesSearchHandler', ->
  8. beforeEach ->
  9. @projectId = '222'
  10. @fakeProject =
  11. _id: @projectId
  12. owner_ref: @fakeOwner =
  13. _id: 'some_owner'
  14. features:
  15. references: false
  16. rootFolder: [
  17. docs: [
  18. {name: 'one.bib', _id: 'aaa'},
  19. {name: 'two.txt', _id: 'bbb'},
  20. ]
  21. folders: [
  22. {docs: [{name: 'three.bib', _id: 'ccc'}], folders: []}
  23. ]
  24. ]
  25. @docIds = ['aaa', 'ccc']
  26. @handler = SandboxedModule.require modulePath, requires:
  27. 'logger-sharelatex': {
  28. log: ->
  29. err: ->
  30. }
  31. 'settings-sharelatex': @settings = {
  32. apis:
  33. references: {url: 'http://some.url/references'}
  34. docstore: {url: 'http://some.url/docstore'}
  35. }
  36. 'request': @request = {
  37. get: sinon.stub()
  38. post: sinon.stub()
  39. }
  40. '../../models/Project': {
  41. Project: @Project = {
  42. findPopulatedById: sinon.stub().callsArgWith(1, null, @fakeProject)
  43. }
  44. }
  45. '../DocumentUpdater/DocumentUpdaterHandler': @DocumentUpdaterHandler = {
  46. flushDocToMongo: sinon.stub().callsArgWith(2, null)
  47. }
  48. @fakeResponseData =
  49. projectId: @projectId
  50. keys: ['k1', 'k2']
  51. describe 'index', ->
  52. beforeEach ->
  53. sinon.stub(@handler, '_findBibDocIds')
  54. sinon.stub(@handler, '_isFullIndex').callsArgWith(1, null, true)
  55. @request.post.callsArgWith(1, null, {statusCode: 200}, @fakeResponseData)
  56. @call = (callback) =>
  57. @handler.index @projectId, @docIds, callback
  58. describe 'with docIds as an array', ->
  59. beforeEach ->
  60. @docIds = ['aaa', 'ccc']
  61. it 'should not call _findBibDocIds', (done) ->
  62. @call (err, data) =>
  63. @handler._findBibDocIds.callCount.should.equal 0
  64. done()
  65. it 'should call Project.findPopulatedById', (done) ->
  66. @call (err, data) =>
  67. @Project.findPopulatedById.callCount.should.equal 1
  68. @Project.findPopulatedById.calledWith(@projectId).should.equal true
  69. done()
  70. it 'should call DocumentUpdaterHandler.flushDocToMongo', (done) ->
  71. @call (err, data) =>
  72. @DocumentUpdaterHandler.flushDocToMongo.callCount.should.equal 2
  73. @docIds.forEach (docId) =>
  74. @DocumentUpdaterHandler.flushDocToMongo.calledWith(@projectId, docId).should.equal true
  75. done()
  76. it 'should make a request to references service', (done) ->
  77. @call (err, data) =>
  78. @request.post.callCount.should.equal 1
  79. arg = @request.post.firstCall.args[0]
  80. expect(arg.json).to.have.all.keys 'docUrls', 'fullIndex'
  81. expect(arg.json.docUrls.length).to.equal 2
  82. expect(arg.json.fullIndex).to.equal true
  83. done()
  84. it 'should not produce an error', (done) ->
  85. @call (err, data) =>
  86. expect(err).to.equal null
  87. done()
  88. it 'should return data', (done) ->
  89. @call (err, data) =>
  90. expect(data).to.not.equal null
  91. expect(data).to.not.equal undefined
  92. expect(data).to.equal @fakeResponseData
  93. done()
  94. describe 'with docIds as "ALL"', ->
  95. beforeEach ->
  96. @docIds = 'ALL'
  97. @handler._findBibDocIds.returns(['aaa', 'ccc'])
  98. it 'should call _findBibDocIds', (done) ->
  99. @call (err, data) =>
  100. @handler._findBibDocIds.callCount.should.equal 1
  101. @handler._findBibDocIds.calledWith(@fakeProject).should.equal true
  102. done()
  103. it 'should call DocumentUpdaterHandler.flushDocToMongo', (done) ->
  104. @call (err, data) =>
  105. @DocumentUpdaterHandler.flushDocToMongo.callCount.should.equal 2
  106. done()
  107. it 'should not produce an error', (done) ->
  108. @call (err, data) =>
  109. expect(err).to.equal null
  110. done()
  111. it 'should return data', (done) ->
  112. @call (err, data) =>
  113. expect(data).to.not.equal null
  114. expect(data).to.not.equal undefined
  115. expect(data).to.equal @fakeResponseData
  116. done()
  117. describe 'when Project.findPopulatedById produces an error', ->
  118. beforeEach ->
  119. @Project.findPopulatedById.callsArgWith(1, new Error('woops'))
  120. it 'should produce an error', (done) ->
  121. @call (err, data) =>
  122. expect(err).to.not.equal null
  123. expect(err).to.be.instanceof Error
  124. expect(data).to.equal undefined
  125. done()
  126. it 'should not send request', (done) ->
  127. @call (err, data) =>
  128. @request.post.callCount.should.equal 0
  129. done()
  130. describe 'when _isFullIndex produces an error', ->
  131. beforeEach ->
  132. @Project.findPopulatedById.callsArgWith(1, null, @fakeProject)
  133. @handler._isFullIndex.callsArgWith(1, new Error('woops'))
  134. it 'should produce an error', (done) ->
  135. @call (err, data) =>
  136. expect(err).to.not.equal null
  137. expect(err).to.be.instanceof Error
  138. expect(data).to.equal undefined
  139. done()
  140. it 'should not send request', (done) ->
  141. @call (err, data) =>
  142. @request.post.callCount.should.equal 0
  143. done()
  144. describe 'when flushDocToMongo produces an error', ->
  145. beforeEach ->
  146. @Project.findPopulatedById.callsArgWith(1, null, @fakeProject)
  147. @handler._isFullIndex.callsArgWith(1, false)
  148. @DocumentUpdaterHandler.flushDocToMongo.callsArgWith(2, new Error('woops'))
  149. it 'should produce an error', (done) ->
  150. @call (err, data) =>
  151. expect(err).to.not.equal null
  152. expect(err).to.be.instanceof Error
  153. expect(data).to.equal undefined
  154. done()
  155. it 'should not send request', (done) ->
  156. @call (err, data) =>
  157. @request.post.callCount.should.equal 0
  158. done()
  159. describe 'when request produces an error', ->
  160. beforeEach ->
  161. @Project.findPopulatedById.callsArgWith(1, null, @fakeProject)
  162. @handler._isFullIndex.callsArgWith(1, null, false)
  163. @DocumentUpdaterHandler.flushDocToMongo.callsArgWith(2, null)
  164. @request.post.callsArgWith(1, new Error('woops'))
  165. it 'should produce an error', (done) ->
  166. @call (err, data) =>
  167. expect(err).to.not.equal null
  168. expect(err).to.be.instanceof Error
  169. expect(data).to.equal undefined
  170. done()
  171. describe 'when request responds with error status', ->
  172. beforeEach ->
  173. @Project.findPopulatedById.callsArgWith(1, null, @fakeProject)
  174. @handler._isFullIndex.callsArgWith(1, null, false)
  175. @request.post.callsArgWith(1, null, {statusCode: 500}, null)
  176. it 'should produce an error', (done) ->
  177. @call (err, data) =>
  178. expect(err).to.not.equal null
  179. expect(err).to.be.instanceof Error
  180. expect(data).to.equal undefined
  181. done()
  182. describe '_findBibDocIds', ->
  183. beforeEach ->
  184. @fakeProject =
  185. rootFolder: [
  186. docs: [
  187. {name: 'one.bib', _id: 'aaa'},
  188. {name: 'two.txt', _id: 'bbb'},
  189. ]
  190. folders: [
  191. {docs: [{name: 'three.bib', _id: 'ccc'}], folders: []}
  192. ]
  193. ]
  194. @expectedIds = ['aaa', 'ccc']
  195. it 'should select the correct docIds', ->
  196. result = @handler._findBibDocIds(@fakeProject)
  197. expect(result).to.deep.equal @expectedIds
  198. describe '_isFullIndex', ->
  199. beforeEach ->
  200. @fakeProject =
  201. owner_ref:
  202. features:
  203. references: false
  204. @call = (callback) =>
  205. @handler._isFullIndex @fakeProject, callback
  206. describe 'with references feature on', ->
  207. beforeEach ->
  208. @fakeProject.owner_ref.features.references = true
  209. it 'should return true', ->
  210. @call (err, isFullIndex) =>
  211. expect(err).to.equal null
  212. expect(isFullIndex).to.equal true
  213. describe 'with references feature off', ->
  214. beforeEach ->
  215. @fakeProject.owner_ref.features.references = false
  216. it 'should return false', ->
  217. @call (err, isFullIndex) =>
  218. expect(err).to.equal null
  219. expect(isFullIndex).to.equal false