ReferencesSearchControllerTests.coffee 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 = {findElement: sinon.stub()}
  21. './ReferencesSearchHandler': @ReferencesSearchHandler = {indexFile: sinon.stub(), getKeys: sinon.stub()}
  22. describe 'indexFile', ->
  23. beforeEach ->
  24. @req = new MockRequest()
  25. @req.params.Project_id = @project_id
  26. @res = new MockResponse()
  27. @ProjectLocator.findElement.callsArgWith(1, null, {})
  28. @ReferencesSearchHandler.indexFile.callsArgWith(2, null)
  29. describe 'with a valid doc_id', ->
  30. beforeEach ->
  31. @req.body = {docId: @doc_id}
  32. it 'should produce a 200 response', (done) ->
  33. @res.send = (status) =>
  34. status.should.equal 200
  35. done()
  36. @controller.indexFile(@req, @res)
  37. it 'should call ProjectLocator.findElement', (done) ->
  38. @res.send = (status) =>
  39. @ProjectLocator.findElement.calledOnce.should.equal true
  40. arg =
  41. project_id: @project_id
  42. element_id: @doc_id,
  43. type: 'doc'
  44. @ProjectLocator.findElement.calledWith(arg).should.equal true
  45. done()
  46. @controller.indexFile(@req, @res)
  47. it 'should call ReferencesSearchHandler.indexFile', (done) ->
  48. @res.send = (status) =>
  49. @ReferencesSearchHandler.indexFile.calledOnce.should.equal true
  50. expected_url = "http://some.url/project/2222/doc/3333"
  51. @ReferencesSearchHandler.indexFile
  52. .calledWith(@project_id, expected_url).should.equal true
  53. done()
  54. @controller.indexFile(@req, @res)
  55. describe 'without a doc_id', ->
  56. beforeEach ->
  57. @req.body = {bad: true}
  58. it 'should produce a 400 response', (done) ->
  59. @res.send = (status) =>
  60. status.should.equal 400
  61. done()
  62. @controller.indexFile(@req, @res)
  63. describe 'when the ProjectLocator cannot find the doc', ->
  64. beforeEach ->
  65. @req.body = {docId: 'some_weird_id'}
  66. @ProjectLocator.findElement.callsArgWith(1, new Error('not found'), null)
  67. @ReferencesSearchHandler.indexFile.callsArgWith(2, null)
  68. it 'should call ProjectLocator.findElement', (done) ->
  69. @res.send = (status) =>
  70. @ProjectLocator.findElement.calledOnce.should.equal true
  71. arg =
  72. project_id: @project_id
  73. element_id: 'some_weird_id',
  74. type: 'doc'
  75. @ProjectLocator.findElement.calledWith(arg).should.equal true
  76. done()
  77. @controller.indexFile(@req, @res)
  78. it 'should produce a 500 response', (done) ->
  79. @res.send = (status) =>
  80. status.should.equal 500
  81. done()
  82. @controller.indexFile(@req, @res)
  83. describe 'when the ReferencesSearchHandler produces an error', ->
  84. beforeEach ->
  85. @req.body = {docId: @doc_id}
  86. @ProjectLocator.findElement.callsArgWith(1, null, {})
  87. @ReferencesSearchHandler.indexFile.callsArgWith(2, new Error('something went wrong'))
  88. it 'should call ReferencesSearchHandler.indexFile', (done) ->
  89. @res.send = (status) =>
  90. @ReferencesSearchHandler.indexFile.calledOnce.should.equal true
  91. expected_url = "http://some.url/project/2222/doc/3333"
  92. @ReferencesSearchHandler.indexFile.calledWith(@project_id, expected_url).should.equal true
  93. done()
  94. @controller.indexFile(@req, @res)
  95. it 'should produce a 500 response', (done) ->
  96. @res.send = (status) =>
  97. status.should.equal 500
  98. done()
  99. @controller.indexFile(@req, @res)