ReferencesSearchControllerTests.coffee 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. SandboxedModule = require('sandboxed-module')
  2. should = require('chai').should()
  3. sinon = require 'sinon'
  4. assert = require("chai").assert
  5. modulePath = "../../../../app/js/Features/ReferencesSearch/ReferencesSearchController"
  6. MockRequest = require "../helpers/MockRequest"
  7. MockResponse = require "../helpers/MockResponse"
  8. describe "ReferencesSearchController", ->
  9. beforeEach ->
  10. @projectId = '2222'
  11. @controller = SandboxedModule.require modulePath, requires:
  12. 'logger-sharelatex': {
  13. log: ->
  14. err: ->
  15. },
  16. 'settings-sharelatex': @settings = {
  17. apis: {web: {url: 'http://some.url'}}
  18. },
  19. './ReferencesSearchHandler': @ReferencesSearchHandler = {
  20. index: sinon.stub()
  21. indexAll: sinon.stub()
  22. },
  23. '../Editor/EditorRealTimeController': @EditorRealTimeController = {
  24. emitToRoom: sinon.stub()
  25. }
  26. @req = new MockRequest()
  27. @req.params.Project_id = @projectId
  28. @req.body =
  29. docIds: @docIds = ['aaa', 'bbb']
  30. shouldBroadcast: false
  31. @res = new MockResponse()
  32. @res.json = sinon.stub()
  33. @res.send = sinon.stub()
  34. @fakeResponseData =
  35. projectId: @projectId,
  36. keys: ['one', 'two', 'three']
  37. describe 'indexAll', ->
  38. beforeEach ->
  39. @req.body = {shouldBroadcast: false}
  40. @ReferencesSearchHandler.indexAll.callsArgWith(1, null, @fakeResponseData)
  41. @call = (callback) =>
  42. @controller.indexAll @req, @res
  43. callback()
  44. it 'should not produce an error', (done) ->
  45. @call () =>
  46. @res.send.callCount.should.equal 0
  47. @res.send.calledWith(500).should.equal false
  48. @res.send.calledWith(400).should.equal false
  49. done()
  50. it 'should return data', (done) ->
  51. @call () =>
  52. @res.json.callCount.should.equal 1
  53. @res.json.calledWith(@fakeResponseData).should.equal true
  54. done()
  55. it 'should call ReferencesSearchHandler.indexAll', (done) ->
  56. @call () =>
  57. @ReferencesSearchHandler.indexAll.callCount.should.equal 1
  58. @ReferencesSearchHandler.indexAll.calledWith(@projectId).should.equal true
  59. done()
  60. describe 'when shouldBroadcast is true', ->
  61. beforeEach ->
  62. @ReferencesSearchHandler.index.callsArgWith(2, null, @fakeResponseData)
  63. @req.body.shouldBroadcast = true
  64. it 'should call EditorRealTimeController.emitToRoom', (done) ->
  65. @call () =>
  66. @EditorRealTimeController.emitToRoom.callCount.should.equal 1
  67. done()
  68. it 'should not produce an error', (done) ->
  69. @call () =>
  70. @res.send.callCount.should.equal 0
  71. @res.send.calledWith(500).should.equal false
  72. @res.send.calledWith(400).should.equal false
  73. done()
  74. it 'should still return data', (done) ->
  75. @call () =>
  76. @res.json.callCount.should.equal 1
  77. @res.json.calledWith(@fakeResponseData).should.equal true
  78. done()
  79. describe 'when shouldBroadcast is false', ->
  80. beforeEach ->
  81. @ReferencesSearchHandler.index.callsArgWith(2, null, @fakeResponseData)
  82. @req.body.shouldBroadcast = false
  83. it 'should not call EditorRealTimeController.emitToRoom', (done) ->
  84. @call () =>
  85. @EditorRealTimeController.emitToRoom.callCount.should.equal 0
  86. done()
  87. it 'should not produce an error', (done) ->
  88. @call () =>
  89. @res.send.callCount.should.equal 0
  90. @res.send.calledWith(500).should.equal false
  91. @res.send.calledWith(400).should.equal false
  92. done()
  93. it 'should still return data', (done) ->
  94. @call () =>
  95. @res.json.callCount.should.equal 1
  96. @res.json.calledWith(@fakeResponseData).should.equal true
  97. done()
  98. describe 'index', ->
  99. describe 'with docIds as an array and shouldBroadcast as false', ->
  100. beforeEach ->
  101. @ReferencesSearchHandler.index.callsArgWith(2, null, @fakeResponseData)
  102. @call = (callback) =>
  103. @controller.index @req, @res
  104. callback()
  105. it 'should call ReferencesSearchHandler.index', (done) ->
  106. @call () =>
  107. @ReferencesSearchHandler.index.callCount.should.equal 1
  108. @ReferencesSearchHandler.index.calledWith(@projectId, @docIds).should.equal true
  109. done()
  110. it 'should return data', (done) ->
  111. @call () =>
  112. @res.json.callCount.should.equal 1
  113. @res.json.calledWith(@fakeResponseData).should.equal true
  114. done()
  115. it 'should not produce an error', (done) ->
  116. @call () =>
  117. @res.send.callCount.should.equal 0
  118. @res.send.calledWith(500).should.equal false
  119. @res.send.calledWith(400).should.equal false
  120. done()
  121. it 'should not call EditorRealTimController.emitToRoom', (done) ->
  122. @call () =>
  123. @EditorRealTimeController.emitToRoom.callCount.should.equal 0
  124. done()
  125. describe 'when ReferencesSearchHandler.index produces an error', ->
  126. beforeEach ->
  127. @ReferencesSearchHandler.index.callsArgWith(2, new Error('woops'), null)
  128. it 'should produce an error response', (done) ->
  129. @call () =>
  130. @res.send.callCount.should.equal 1
  131. @res.send.calledWith(500).should.equal true
  132. done()
  133. describe 'when shouldBroadcast is true', ->
  134. beforeEach ->
  135. @ReferencesSearchHandler.index.callsArgWith(2, null, @fakeResponseData)
  136. @req.body.shouldBroadcast = true
  137. it 'should call EditorRealTimeController.emitToRoom', (done) ->
  138. @call () =>
  139. @EditorRealTimeController.emitToRoom.callCount.should.equal 1
  140. done()
  141. it 'should not produce an error', (done) ->
  142. @call () =>
  143. @res.send.callCount.should.equal 0
  144. @res.send.calledWith(500).should.equal false
  145. @res.send.calledWith(400).should.equal false
  146. done()
  147. it 'should still return data', (done) ->
  148. @call () =>
  149. @res.json.callCount.should.equal 1
  150. @res.json.calledWith(@fakeResponseData).should.equal true
  151. done()
  152. describe 'with missing docIds', ->
  153. beforeEach ->
  154. delete @req.body.docIds
  155. it 'should produce an error response', (done) ->
  156. @call () =>
  157. @res.send.callCount.should.equal 1
  158. @res.send.calledWith(400).should.equal true
  159. done()
  160. it 'should not call ReferencesSearchHandler.index', (done) ->
  161. @call () =>
  162. @ReferencesSearchHandler.index.callCount.should.equal 0
  163. done()
  164. describe 'with invalid docIds', ->
  165. beforeEach ->
  166. @req.body.docIds = 42
  167. it 'should produce an error response', (done) ->
  168. @call () =>
  169. @res.send.callCount.should.equal 1
  170. @res.send.calledWith(400).should.equal true
  171. done()
  172. it 'should not call ReferencesSearchHandler.index', (done) ->
  173. @call () =>
  174. @ReferencesSearchHandler.index.callCount.should.equal 0
  175. done()