ReferencesHandlerTests.coffee 9.7 KB

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