ReferencesHandlerTests.coffee 9.6 KB

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