ReferencesSearchHandlerTests.coffee 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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'}
  34. web: {url: 'http://some.url'}
  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. @fakeResponseData =
  46. projectId: @projectId
  47. keys: ['k1', 'k2']
  48. describe 'index', ->
  49. beforeEach ->
  50. sinon.stub(@handler, '_findBibDocIds')
  51. sinon.stub(@handler, '_isFullIndex').callsArgWith(1, null, true)
  52. @request.post.callsArgWith(1, null, {statusCode: 200}, @fakeResponseData)
  53. @call = (callback) =>
  54. @handler.index @projectId, @docIds, callback
  55. describe 'with docIds as an array', ->
  56. beforeEach ->
  57. @docIds = ['aaa', 'ccc']
  58. it 'should not call _findBibDocIds', (done) ->
  59. @call (err, data) =>
  60. @handler._findBibDocIds.callCount.should.equal 0
  61. done()
  62. it 'should call Project.findPopulatedById', (done) ->
  63. @call (err, data) =>
  64. @Project.findPopulatedById.callCount.should.equal 1
  65. @Project.findPopulatedById.calledWith(@projectId).should.equal true
  66. done()
  67. it 'should make a request to references service', (done) ->
  68. @call (err, data) =>
  69. @request.post.callCount.should.equal 1
  70. arg = @request.post.firstCall.args[0]
  71. expect(arg.json).to.have.all.keys 'docUrls', 'fullIndex'
  72. expect(arg.json.docUrls.length).to.equal 2
  73. expect(arg.json.fullIndex).to.equal true
  74. done()
  75. it 'should not produce an error', (done) ->
  76. @call (err, data) =>
  77. expect(err).to.equal null
  78. done()
  79. it 'should return data', (done) ->
  80. @call (err, data) =>
  81. expect(data).to.not.equal null
  82. expect(data).to.not.equal undefined
  83. expect(data).to.equal @fakeResponseData
  84. done()
  85. describe 'with docIds as "ALL"', ->
  86. beforeEach ->
  87. @docIds = 'ALL'
  88. @handler._findBibDocIds.returns(['aaa', 'ccc'])
  89. it 'should call _findBibDocIds', (done) ->
  90. @call (err, data) =>
  91. @handler._findBibDocIds.callCount.should.equal 1
  92. @handler._findBibDocIds.calledWith(@fakeProject).should.equal true
  93. done()
  94. it 'should not produce an error', (done) ->
  95. @call (err, data) =>
  96. expect(err).to.equal null
  97. done()
  98. it 'should return data', (done) ->
  99. @call (err, data) =>
  100. expect(data).to.not.equal null
  101. expect(data).to.not.equal undefined
  102. expect(data).to.equal @fakeResponseData
  103. done()
  104. describe 'when Project.findPopulatedById produces an error', ->
  105. beforeEach ->
  106. @Project.findPopulatedById.callsArgWith(1, new Error('woops'))
  107. it 'should produce an error', (done) ->
  108. @call (err, data) =>
  109. expect(err).to.not.equal null
  110. expect(err).to.be.instanceof Error
  111. expect(data).to.equal undefined
  112. done()
  113. it 'should not send request', (done) ->
  114. @call (err, data) =>
  115. @request.post.callCount.should.equal 0
  116. done()
  117. describe 'when _isFullIndex produces an error', ->
  118. beforeEach ->
  119. @Project.findPopulatedById.callsArgWith(1, null, @fakeProject)
  120. @handler._isFullIndex.callsArgWith(1, new Error('woops'))
  121. it 'should produce an error', (done) ->
  122. @call (err, data) =>
  123. expect(err).to.not.equal null
  124. expect(err).to.be.instanceof Error
  125. expect(data).to.equal undefined
  126. done()
  127. it 'should not send request', (done) ->
  128. @call (err, data) =>
  129. @request.post.callCount.should.equal 0
  130. done()
  131. describe 'when request produces an error', ->
  132. beforeEach ->
  133. @Project.findPopulatedById.callsArgWith(1, null, @fakeProject)
  134. @handler._isFullIndex.callsArgWith(1, null, false)
  135. @request.post.callsArgWith(1, new Error('woops'))
  136. it 'should produce an error', (done) ->
  137. @call (err, data) =>
  138. expect(err).to.not.equal null
  139. expect(err).to.be.instanceof Error
  140. expect(data).to.equal undefined
  141. done()
  142. describe 'when request responds with error status', ->
  143. beforeEach ->
  144. @Project.findPopulatedById.callsArgWith(1, null, @fakeProject)
  145. @handler._isFullIndex.callsArgWith(1, null, false)
  146. @request.post.callsArgWith(1, null, {statusCode: 500}, null)
  147. it 'should produce an error', (done) ->
  148. @call (err, data) =>
  149. expect(err).to.not.equal null
  150. expect(err).to.be.instanceof Error
  151. expect(data).to.equal undefined
  152. done()
  153. describe '_findBibDocIds', ->
  154. beforeEach ->
  155. @fakeProject =
  156. rootFolder: [
  157. docs: [
  158. {name: 'one.bib', _id: 'aaa'},
  159. {name: 'two.txt', _id: 'bbb'},
  160. ]
  161. folders: [
  162. {docs: [{name: 'three.bib', _id: 'ccc'}], folders: []}
  163. ]
  164. ]
  165. @expectedIds = ['aaa', 'ccc']
  166. it 'should select the correct docIds', ->
  167. result = @handler._findBibDocIds(@fakeProject)
  168. expect(result).to.deep.equal @expectedIds
  169. describe '_isFullIndex', ->
  170. beforeEach ->
  171. @fakeProject =
  172. owner_ref:
  173. features:
  174. references: false
  175. @call = (callback) =>
  176. @handler._isFullIndex @fakeProject, callback
  177. describe 'with references feature on', ->
  178. beforeEach ->
  179. @fakeProject.owner_ref.features.references = true
  180. it 'should return true', ->
  181. @call (err, isFullIndex) =>
  182. expect(err).to.equal null
  183. expect(isFullIndex).to.equal true
  184. describe 'with references feature off', ->
  185. beforeEach ->
  186. @fakeProject.owner_ref.features.references = false
  187. it 'should return false', ->
  188. @call (err, isFullIndex) =>
  189. expect(err).to.equal null
  190. expect(isFullIndex).to.equal false