ReferencesHandlerTests.coffee 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. {
  23. docs: [{name: 'three.bib', _id: 'ccc'}],
  24. fileRefs: [{name: 'four.bib', _id: 'ghg'}],
  25. folders: []
  26. }
  27. ]
  28. ]
  29. @docIds = ['aaa', 'ccc']
  30. @handler = SandboxedModule.require modulePath, requires:
  31. 'logger-sharelatex': {
  32. log: ->
  33. err: ->
  34. }
  35. 'settings-sharelatex': @settings = {
  36. apis:
  37. references: {url: 'http://some.url/references'}
  38. docstore: {url: 'http://some.url/docstore'}
  39. filestore: {url: 'http://some.url/filestore'}
  40. }
  41. 'request': @request = {
  42. get: sinon.stub()
  43. post: sinon.stub()
  44. }
  45. '../Project/ProjectGetter': @ProjectGetter = {
  46. getProject: sinon.stub().callsArgWith(2, null, @fakeProject)
  47. }
  48. '../User/UserGetter': @UserGetter = {
  49. getUser: sinon.stub()
  50. }
  51. '../DocumentUpdater/DocumentUpdaterHandler': @DocumentUpdaterHandler = {
  52. flushDocToMongo: sinon.stub().callsArgWith(2, null)
  53. }
  54. @fakeResponseData =
  55. projectId: @projectId
  56. keys: ['k1', 'k2']
  57. describe 'index', ->
  58. beforeEach ->
  59. sinon.stub(@handler, '_findBibDocIds')
  60. sinon.stub(@handler, '_findBibFileIds')
  61. sinon.stub(@handler, '_isFullIndex').callsArgWith(1, null, true)
  62. @request.post.callsArgWith(1, null, {statusCode: 200}, @fakeResponseData)
  63. @call = (callback) =>
  64. @handler.index @projectId, @docIds, callback
  65. describe 'with docIds as an array', ->
  66. beforeEach ->
  67. @docIds = ['aaa', 'ccc']
  68. it 'should not call _findBibDocIds', (done) ->
  69. @call (err, data) =>
  70. @handler._findBibDocIds.callCount.should.equal 0
  71. done()
  72. it 'should call ProjectGetter.getProject', (done) ->
  73. @call (err, data) =>
  74. @ProjectGetter.getProject.callCount.should.equal 1
  75. @ProjectGetter.getProject.calledWith(@projectId).should.equal true
  76. done()
  77. it 'should not call _findBibDocIds', (done) ->
  78. @call (err, data) =>
  79. @handler._findBibDocIds.callCount.should.equal 0
  80. done()
  81. it 'should call DocumentUpdaterHandler.flushDocToMongo', (done) ->
  82. @call (err, data) =>
  83. @DocumentUpdaterHandler.flushDocToMongo.callCount.should.equal 2
  84. @docIds.forEach (docId) =>
  85. @DocumentUpdaterHandler.flushDocToMongo.calledWith(@projectId, docId).should.equal true
  86. done()
  87. it 'should make a request to references service', (done) ->
  88. @call (err, data) =>
  89. @request.post.callCount.should.equal 1
  90. arg = @request.post.firstCall.args[0]
  91. expect(arg.json).to.have.all.keys 'docUrls', 'fullIndex'
  92. expect(arg.json.docUrls.length).to.equal 2
  93. expect(arg.json.fullIndex).to.equal true
  94. done()
  95. it 'should not produce an error', (done) ->
  96. @call (err, data) =>
  97. expect(err).to.equal null
  98. done()
  99. it 'should return data', (done) ->
  100. @call (err, data) =>
  101. expect(data).to.not.equal null
  102. expect(data).to.not.equal undefined
  103. expect(data).to.equal @fakeResponseData
  104. done()
  105. describe 'when ProjectGetter.getProject produces an error', ->
  106. beforeEach ->
  107. @ProjectGetter.getProject.callsArgWith(2, new Error('woops'))
  108. it 'should produce an error', (done) ->
  109. @call (err, data) =>
  110. expect(err).to.not.equal null
  111. expect(err).to.be.instanceof Error
  112. expect(data).to.equal undefined
  113. done()
  114. it 'should not send request', (done) ->
  115. @call (err, data) =>
  116. @request.post.callCount.should.equal 0
  117. done()
  118. describe 'when _isFullIndex produces an error', ->
  119. beforeEach ->
  120. @ProjectGetter.getProject.callsArgWith(2, null, @fakeProject)
  121. @handler._isFullIndex.callsArgWith(1, new Error('woops'))
  122. it 'should produce an error', (done) ->
  123. @call (err, data) =>
  124. expect(err).to.not.equal null
  125. expect(err).to.be.instanceof Error
  126. expect(data).to.equal undefined
  127. done()
  128. it 'should not send request', (done) ->
  129. @call (err, data) =>
  130. @request.post.callCount.should.equal 0
  131. done()
  132. describe 'when flushDocToMongo produces an error', ->
  133. beforeEach ->
  134. @ProjectGetter.getProject.callsArgWith(2, null, @fakeProject)
  135. @handler._isFullIndex.callsArgWith(1, false)
  136. @DocumentUpdaterHandler.flushDocToMongo.callsArgWith(2, new Error('woops'))
  137. it 'should produce an error', (done) ->
  138. @call (err, data) =>
  139. expect(err).to.not.equal null
  140. expect(err).to.be.instanceof Error
  141. expect(data).to.equal undefined
  142. done()
  143. it 'should not send request', (done) ->
  144. @call (err, data) =>
  145. @request.post.callCount.should.equal 0
  146. done()
  147. describe 'when request produces an error', ->
  148. beforeEach ->
  149. @ProjectGetter.getProject.callsArgWith(2, null, @fakeProject)
  150. @handler._isFullIndex.callsArgWith(1, null, false)
  151. @DocumentUpdaterHandler.flushDocToMongo.callsArgWith(2, null)
  152. @request.post.callsArgWith(1, new Error('woops'))
  153. it 'should produce an error', (done) ->
  154. @call (err, data) =>
  155. expect(err).to.not.equal null
  156. expect(err).to.be.instanceof Error
  157. expect(data).to.equal undefined
  158. done()
  159. describe 'when request responds with error status', ->
  160. beforeEach ->
  161. @ProjectGetter.getProject.callsArgWith(2, null, @fakeProject)
  162. @handler._isFullIndex.callsArgWith(1, null, false)
  163. @request.post.callsArgWith(1, null, {statusCode: 500}, null)
  164. it 'should produce an error', (done) ->
  165. @call (err, data) =>
  166. expect(err).to.not.equal null
  167. expect(err).to.be.instanceof Error
  168. expect(data).to.equal undefined
  169. done()
  170. describe 'indexAll', ->
  171. beforeEach ->
  172. sinon.stub(@handler, '_findBibDocIds').returns(['aaa', 'ccc'])
  173. sinon.stub(@handler, '_findBibFileIds').returns(['fff', 'ggg'])
  174. sinon.stub(@handler, '_isFullIndex').callsArgWith(1, null, true)
  175. @request.post.callsArgWith(1, null, {statusCode: 200}, @fakeResponseData)
  176. @call = (callback) =>
  177. @handler.indexAll @projectId, callback
  178. it 'should call _findBibDocIds', (done) ->
  179. @call (err, data) =>
  180. @handler._findBibDocIds.callCount.should.equal 1
  181. @handler._findBibDocIds.calledWith(@fakeProject).should.equal true
  182. done()
  183. it 'should call _findBibFileIds', (done) ->
  184. @call (err, data) =>
  185. @handler._findBibDocIds.callCount.should.equal 1
  186. @handler._findBibDocIds.calledWith(@fakeProject).should.equal true
  187. done()
  188. it 'should call DocumentUpdaterHandler.flushDocToMongo', (done) ->
  189. @call (err, data) =>
  190. @DocumentUpdaterHandler.flushDocToMongo.callCount.should.equal 2
  191. done()
  192. it 'should make a request to references service', (done) ->
  193. @call (err, data) =>
  194. @request.post.callCount.should.equal 1
  195. arg = @request.post.firstCall.args[0]
  196. expect(arg.json).to.have.all.keys 'docUrls', 'fullIndex'
  197. expect(arg.json.docUrls.length).to.equal 4
  198. expect(arg.json.fullIndex).to.equal true
  199. done()
  200. it 'should not produce an error', (done) ->
  201. @call (err, data) =>
  202. expect(err).to.equal null
  203. done()
  204. it 'should return data', (done) ->
  205. @call (err, data) =>
  206. expect(data).to.not.equal null
  207. expect(data).to.not.equal undefined
  208. expect(data).to.equal @fakeResponseData
  209. done()
  210. describe 'when ProjectGetter.getProject produces an error', ->
  211. beforeEach ->
  212. @ProjectGetter.getProject.callsArgWith(2, new Error('woops'))
  213. it 'should produce an error', (done) ->
  214. @call (err, data) =>
  215. expect(err).to.not.equal null
  216. expect(err).to.be.instanceof Error
  217. expect(data).to.equal undefined
  218. done()
  219. it 'should not send request', (done) ->
  220. @call (err, data) =>
  221. @request.post.callCount.should.equal 0
  222. done()
  223. describe 'when _isFullIndex produces an error', ->
  224. beforeEach ->
  225. @ProjectGetter.getProject.callsArgWith(2, null, @fakeProject)
  226. @handler._isFullIndex.callsArgWith(1, new Error('woops'))
  227. it 'should produce an error', (done) ->
  228. @call (err, data) =>
  229. expect(err).to.not.equal null
  230. expect(err).to.be.instanceof Error
  231. expect(data).to.equal undefined
  232. done()
  233. it 'should not send request', (done) ->
  234. @call (err, data) =>
  235. @request.post.callCount.should.equal 0
  236. done()
  237. describe 'when flushDocToMongo produces an error', ->
  238. beforeEach ->
  239. @ProjectGetter.getProject.callsArgWith(2, null, @fakeProject)
  240. @handler._isFullIndex.callsArgWith(1, false)
  241. @DocumentUpdaterHandler.flushDocToMongo.callsArgWith(2, new Error('woops'))
  242. it 'should produce an error', (done) ->
  243. @call (err, data) =>
  244. expect(err).to.not.equal null
  245. expect(err).to.be.instanceof Error
  246. expect(data).to.equal undefined
  247. done()
  248. it 'should not send request', (done) ->
  249. @call (err, data) =>
  250. @request.post.callCount.should.equal 0
  251. done()
  252. describe '_findBibDocIds', ->
  253. beforeEach ->
  254. @fakeProject =
  255. rootFolder: [
  256. docs: [
  257. {name: 'one.bib', _id: 'aaa'},
  258. {name: 'two.txt', _id: 'bbb'},
  259. ]
  260. folders: [
  261. {docs: [{name: 'three.bib', _id: 'ccc'}], folders: []}
  262. ]
  263. ]
  264. @expectedIds = ['aaa', 'ccc']
  265. it 'should select the correct docIds', ->
  266. result = @handler._findBibDocIds(@fakeProject)
  267. expect(result).to.deep.equal @expectedIds
  268. it 'should not error with a non array of folders from dirty data', ->
  269. @fakeProject.rootFolder[0].folders[0].folders = {}
  270. result = @handler._findBibDocIds(@fakeProject)
  271. expect(result).to.deep.equal @expectedIds
  272. describe '_findBibFileIds', ->
  273. beforeEach ->
  274. @fakeProject =
  275. rootFolder: [
  276. docs: [
  277. {name: 'one.bib', _id: 'aaa'},
  278. {name: 'two.txt', _id: 'bbb'},
  279. ]
  280. fileRefs: [
  281. {name: 'other.bib', _id: 'ddd'}
  282. ],
  283. folders: [
  284. {
  285. docs: [{name: 'three.bib', _id: 'ccc'}],
  286. fileRefs: [{name: 'four.bib', _id: 'ghg'}],
  287. folders: []
  288. }
  289. ]
  290. ]
  291. @expectedIds = ['ddd', 'ghg']
  292. it 'should select the correct docIds', ->
  293. result = @handler._findBibFileIds(@fakeProject)
  294. expect(result).to.deep.equal @expectedIds
  295. describe '_isFullIndex', ->
  296. beforeEach ->
  297. @fakeProject =
  298. owner_ref: @owner_ref = "owner-ref-123"
  299. @owner =
  300. features:
  301. references: false
  302. @UserGetter.getUser = sinon.stub()
  303. @UserGetter.getUser.withArgs(@owner_ref, {features: true}).yields(null, @owner)
  304. @call = (callback) =>
  305. @handler._isFullIndex @fakeProject, callback
  306. describe 'with references feature on', ->
  307. beforeEach ->
  308. @owner.features.references = true
  309. it 'should return true', ->
  310. @call (err, isFullIndex) =>
  311. expect(err).to.equal null
  312. expect(isFullIndex).to.equal true
  313. describe 'with references feature off', ->
  314. beforeEach ->
  315. @owner.features.references = false
  316. it 'should return false', ->
  317. @call (err, isFullIndex) =>
  318. expect(err).to.equal null
  319. expect(isFullIndex).to.equal false
  320. describe 'with referencesSearch', ->
  321. beforeEach ->
  322. @owner.features = {referencesSearch: true, references: false}
  323. it 'should return true', ->
  324. @call (err, isFullIndex) =>
  325. expect(err).to.equal null
  326. expect(isFullIndex).to.equal true