ReferencesHandlerTests.coffee 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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/References/ReferencesHandler"
  7. describe 'ReferencesHandler', ->
  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 not call _findBibDocIds', (done) ->
  71. @call (err, data) =>
  72. @handler._findBibDocIds.callCount.should.equal 0
  73. done()
  74. it 'should call DocumentUpdaterHandler.flushDocToMongo', (done) ->
  75. @call (err, data) =>
  76. @DocumentUpdaterHandler.flushDocToMongo.callCount.should.equal 2
  77. @docIds.forEach (docId) =>
  78. @DocumentUpdaterHandler.flushDocToMongo.calledWith(@projectId, docId).should.equal true
  79. done()
  80. it 'should make a request to references service', (done) ->
  81. @call (err, data) =>
  82. @request.post.callCount.should.equal 1
  83. arg = @request.post.firstCall.args[0]
  84. expect(arg.json).to.have.all.keys 'docUrls', 'fullIndex'
  85. expect(arg.json.docUrls.length).to.equal 2
  86. expect(arg.json.fullIndex).to.equal true
  87. done()
  88. it 'should not produce an error', (done) ->
  89. @call (err, data) =>
  90. expect(err).to.equal null
  91. done()
  92. it 'should return data', (done) ->
  93. @call (err, data) =>
  94. expect(data).to.not.equal null
  95. expect(data).to.not.equal undefined
  96. expect(data).to.equal @fakeResponseData
  97. done()
  98. describe 'when Project.findPopulatedById produces an error', ->
  99. beforeEach ->
  100. @Project.findPopulatedById.callsArgWith(1, new Error('woops'))
  101. it 'should produce an error', (done) ->
  102. @call (err, data) =>
  103. expect(err).to.not.equal null
  104. expect(err).to.be.instanceof Error
  105. expect(data).to.equal undefined
  106. done()
  107. it 'should not send request', (done) ->
  108. @call (err, data) =>
  109. @request.post.callCount.should.equal 0
  110. done()
  111. describe 'when _isFullIndex produces an error', ->
  112. beforeEach ->
  113. @Project.findPopulatedById.callsArgWith(1, null, @fakeProject)
  114. @handler._isFullIndex.callsArgWith(1, new Error('woops'))
  115. it 'should produce an error', (done) ->
  116. @call (err, data) =>
  117. expect(err).to.not.equal null
  118. expect(err).to.be.instanceof Error
  119. expect(data).to.equal undefined
  120. done()
  121. it 'should not send request', (done) ->
  122. @call (err, data) =>
  123. @request.post.callCount.should.equal 0
  124. done()
  125. describe 'when flushDocToMongo produces an error', ->
  126. beforeEach ->
  127. @Project.findPopulatedById.callsArgWith(1, null, @fakeProject)
  128. @handler._isFullIndex.callsArgWith(1, false)
  129. @DocumentUpdaterHandler.flushDocToMongo.callsArgWith(2, new Error('woops'))
  130. it 'should produce an error', (done) ->
  131. @call (err, data) =>
  132. expect(err).to.not.equal null
  133. expect(err).to.be.instanceof Error
  134. expect(data).to.equal undefined
  135. done()
  136. it 'should not send request', (done) ->
  137. @call (err, data) =>
  138. @request.post.callCount.should.equal 0
  139. done()
  140. describe 'when request produces an error', ->
  141. beforeEach ->
  142. @Project.findPopulatedById.callsArgWith(1, null, @fakeProject)
  143. @handler._isFullIndex.callsArgWith(1, null, false)
  144. @DocumentUpdaterHandler.flushDocToMongo.callsArgWith(2, null)
  145. @request.post.callsArgWith(1, new Error('woops'))
  146. it 'should produce an error', (done) ->
  147. @call (err, data) =>
  148. expect(err).to.not.equal null
  149. expect(err).to.be.instanceof Error
  150. expect(data).to.equal undefined
  151. done()
  152. describe 'when request responds with error status', ->
  153. beforeEach ->
  154. @Project.findPopulatedById.callsArgWith(1, null, @fakeProject)
  155. @handler._isFullIndex.callsArgWith(1, null, false)
  156. @request.post.callsArgWith(1, null, {statusCode: 500}, null)
  157. it 'should produce an error', (done) ->
  158. @call (err, data) =>
  159. expect(err).to.not.equal null
  160. expect(err).to.be.instanceof Error
  161. expect(data).to.equal undefined
  162. done()
  163. describe 'indexAll', ->
  164. beforeEach ->
  165. sinon.stub(@handler, '_findBibDocIds').returns(['aaa', 'ccc'])
  166. sinon.stub(@handler, '_isFullIndex').callsArgWith(1, null, true)
  167. @request.post.callsArgWith(1, null, {statusCode: 200}, @fakeResponseData)
  168. @call = (callback) =>
  169. @handler.indexAll @projectId, callback
  170. it 'should call _findBibDocIds', (done) ->
  171. @call (err, data) =>
  172. @handler._findBibDocIds.callCount.should.equal 1
  173. @handler._findBibDocIds.calledWith(@fakeProject).should.equal true
  174. done()
  175. it 'should call DocumentUpdaterHandler.flushDocToMongo', (done) ->
  176. @call (err, data) =>
  177. @DocumentUpdaterHandler.flushDocToMongo.callCount.should.equal 2
  178. done()
  179. it 'should make a request to references service', (done) ->
  180. @call (err, data) =>
  181. @request.post.callCount.should.equal 1
  182. arg = @request.post.firstCall.args[0]
  183. expect(arg.json).to.have.all.keys 'docUrls', 'fullIndex'
  184. expect(arg.json.docUrls.length).to.equal 2
  185. expect(arg.json.fullIndex).to.equal true
  186. done()
  187. it 'should not produce an error', (done) ->
  188. @call (err, data) =>
  189. expect(err).to.equal null
  190. done()
  191. it 'should return data', (done) ->
  192. @call (err, data) =>
  193. expect(data).to.not.equal null
  194. expect(data).to.not.equal undefined
  195. expect(data).to.equal @fakeResponseData
  196. done()
  197. describe 'when Project.findPopulatedById produces an error', ->
  198. beforeEach ->
  199. @Project.findPopulatedById.callsArgWith(1, new Error('woops'))
  200. it 'should produce an error', (done) ->
  201. @call (err, data) =>
  202. expect(err).to.not.equal null
  203. expect(err).to.be.instanceof Error
  204. expect(data).to.equal undefined
  205. done()
  206. it 'should not send request', (done) ->
  207. @call (err, data) =>
  208. @request.post.callCount.should.equal 0
  209. done()
  210. describe 'when _isFullIndex produces an error', ->
  211. beforeEach ->
  212. @Project.findPopulatedById.callsArgWith(1, null, @fakeProject)
  213. @handler._isFullIndex.callsArgWith(1, new Error('woops'))
  214. it 'should produce an error', (done) ->
  215. @call (err, data) =>
  216. expect(err).to.not.equal null
  217. expect(err).to.be.instanceof Error
  218. expect(data).to.equal undefined
  219. done()
  220. it 'should not send request', (done) ->
  221. @call (err, data) =>
  222. @request.post.callCount.should.equal 0
  223. done()
  224. describe 'when flushDocToMongo produces an error', ->
  225. beforeEach ->
  226. @Project.findPopulatedById.callsArgWith(1, null, @fakeProject)
  227. @handler._isFullIndex.callsArgWith(1, false)
  228. @DocumentUpdaterHandler.flushDocToMongo.callsArgWith(2, new Error('woops'))
  229. it 'should produce an error', (done) ->
  230. @call (err, data) =>
  231. expect(err).to.not.equal null
  232. expect(err).to.be.instanceof Error
  233. expect(data).to.equal undefined
  234. done()
  235. it 'should not send request', (done) ->
  236. @call (err, data) =>
  237. @request.post.callCount.should.equal 0
  238. done()
  239. describe '_findBibDocIds', ->
  240. beforeEach ->
  241. @fakeProject =
  242. rootFolder: [
  243. docs: [
  244. {name: 'one.bib', _id: 'aaa'},
  245. {name: 'two.txt', _id: 'bbb'},
  246. ]
  247. folders: [
  248. {docs: [{name: 'three.bib', _id: 'ccc'}], folders: []}
  249. ]
  250. ]
  251. @expectedIds = ['aaa', 'ccc']
  252. it 'should select the correct docIds', ->
  253. result = @handler._findBibDocIds(@fakeProject)
  254. expect(result).to.deep.equal @expectedIds
  255. describe '_isFullIndex', ->
  256. beforeEach ->
  257. @fakeProject =
  258. owner_ref:
  259. features:
  260. references: false
  261. @call = (callback) =>
  262. @handler._isFullIndex @fakeProject, callback
  263. describe 'with references feature on', ->
  264. beforeEach ->
  265. @fakeProject.owner_ref.features.references = true
  266. it 'should return true', ->
  267. @call (err, isFullIndex) =>
  268. expect(err).to.equal null
  269. expect(isFullIndex).to.equal true
  270. describe 'with references feature off', ->
  271. beforeEach ->
  272. @fakeProject.owner_ref.features.references = false
  273. it 'should return false', ->
  274. @call (err, isFullIndex) =>
  275. expect(err).to.equal null
  276. expect(isFullIndex).to.equal false