ReferencesSearchControllerTests.coffee 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. @project_id = '2222'
  11. @doc_id = '3333'
  12. @controller = SandboxedModule.require modulePath, requires:
  13. 'logger-sharelatex': {
  14. log: ->
  15. err: ->
  16. }
  17. 'settings-sharelatex': @settings = {
  18. apis: {web: {url: 'http://some.url'}}
  19. }
  20. '../Project/ProjectLocator': @ProjectLocator = {
  21. findElement: sinon.stub()
  22. }
  23. './ReferencesSearchHandler': @ReferencesSearchHandler = {
  24. indexFile: sinon.stub()
  25. getKeys: sinon.stub()
  26. }
  27. describe 'getKeys', ->
  28. beforeEach ->
  29. @req = new MockRequest()
  30. @req.params.Project_id = @project_id
  31. @res = new MockResponse()
  32. @data =
  33. projectId: @project_id,
  34. keys: ['one', 'two', 'three']
  35. @ReferencesSearchHandler.getKeys.callsArgWith(1, null, @data)
  36. describe 'when remote service works', ->
  37. it 'should produce a json response', (done) ->
  38. @res.json = (data) =>
  39. data.should.not.equal null
  40. data.should.deep.equal @data
  41. done()
  42. @controller.getKeys(@req, @res)
  43. it 'should call getKeys on ReferencesSearchHandler', (done) ->
  44. @res.json = (data) =>
  45. @ReferencesSearchHandler.getKeys
  46. .calledOnce.should.equal true
  47. @ReferencesSearchHandler.getKeys
  48. .calledWith(@project_id).should.equal true
  49. done()
  50. @controller.getKeys(@req, @res)
  51. describe 'when remote service produces an error', ->
  52. beforeEach ->
  53. @ReferencesSearchHandler.getKeys.callsArgWith(1, new Error('nope'))
  54. it 'should produce a 500 response', (done) ->
  55. @res.send = (status) =>
  56. status.should.equal 500
  57. done()
  58. @controller.getKeys(@req, @res)
  59. it 'should call getKeys on ReferencesSearchHandler', (done) ->
  60. @res.send = (status) =>
  61. @ReferencesSearchHandler.getKeys
  62. .calledOnce.should.equal true
  63. @ReferencesSearchHandler.getKeys
  64. .calledWith(@project_id).should.equal true
  65. done()
  66. @controller.getKeys(@req, @res)
  67. describe 'indexFile', ->
  68. beforeEach ->
  69. @req = new MockRequest()
  70. @req.params.Project_id = @project_id
  71. @res = new MockResponse()
  72. @ProjectLocator.findElement.callsArgWith(1, null, {})
  73. @ReferencesSearchHandler.indexFile.callsArgWith(2, null)
  74. describe 'with a valid doc_id', ->
  75. beforeEach ->
  76. @req.body = {docId: @doc_id}
  77. it 'should produce a 200 response', (done) ->
  78. @res.send = (status) =>
  79. status.should.equal 200
  80. done()
  81. @controller.indexFile(@req, @res)
  82. it 'should call ProjectLocator.findElement', (done) ->
  83. @res.send = (status) =>
  84. @ProjectLocator.findElement.calledOnce.should.equal true
  85. arg =
  86. project_id: @project_id
  87. element_id: @doc_id,
  88. type: 'doc'
  89. @ProjectLocator.findElement.calledWith(arg).should.equal true
  90. done()
  91. @controller.indexFile(@req, @res)
  92. it 'should call ReferencesSearchHandler.indexFile', (done) ->
  93. @res.send = (status) =>
  94. @ReferencesSearchHandler.indexFile.calledOnce.should.equal true
  95. expected_url = "http://some.url/project/2222/doc/3333"
  96. @ReferencesSearchHandler.indexFile
  97. .calledWith(@project_id, expected_url).should.equal true
  98. done()
  99. @controller.indexFile(@req, @res)
  100. describe 'without a doc_id', ->
  101. beforeEach ->
  102. @req.body = {bad: true}
  103. it 'should produce a 400 response', (done) ->
  104. @res.send = (status) =>
  105. status.should.equal 400
  106. done()
  107. @controller.indexFile(@req, @res)
  108. describe 'when the ProjectLocator cannot find the doc', ->
  109. beforeEach ->
  110. @req.body = {docId: 'some_weird_id'}
  111. @ProjectLocator.findElement.callsArgWith(1, new Error('not found'), null)
  112. @ReferencesSearchHandler.indexFile.callsArgWith(2, null)
  113. it 'should call ProjectLocator.findElement', (done) ->
  114. @res.send = (status) =>
  115. @ProjectLocator.findElement.calledOnce.should.equal true
  116. arg =
  117. project_id: @project_id
  118. element_id: 'some_weird_id',
  119. type: 'doc'
  120. @ProjectLocator.findElement.calledWith(arg).should.equal true
  121. done()
  122. @controller.indexFile(@req, @res)
  123. it 'should produce a 500 response', (done) ->
  124. @res.send = (status) =>
  125. status.should.equal 500
  126. done()
  127. @controller.indexFile(@req, @res)
  128. describe 'when the ReferencesSearchHandler produces an error', ->
  129. beforeEach ->
  130. @req.body = {docId: @doc_id}
  131. @ProjectLocator.findElement.callsArgWith(1, null, {})
  132. @ReferencesSearchHandler.indexFile
  133. .callsArgWith(2, new Error('something went wrong'))
  134. it 'should call ReferencesSearchHandler.indexFile', (done) ->
  135. @res.send = (status) =>
  136. @ReferencesSearchHandler.indexFile.calledOnce.should.equal true
  137. expected_url = "http://some.url/project/2222/doc/3333"
  138. @ReferencesSearchHandler.indexFile
  139. .calledWith(@project_id, expected_url).should.equal true
  140. done()
  141. @controller.indexFile(@req, @res)
  142. it 'should produce a 500 response', (done) ->
  143. @res.send = (status) =>
  144. status.should.equal 500
  145. done()
  146. @controller.indexFile(@req, @res)