ReferencesSearchHandlerTests.coffee 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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/ReferencesSearch/ReferencesSearchHandler"
  7. describe 'ReferencesSearchHandler', ->
  8. beforeEach ->
  9. @project_id = '222'
  10. @file_id = '111111'
  11. @handler = SandboxedModule.require modulePath, requires:
  12. 'logger-sharelatex': {
  13. log: ->
  14. err: ->
  15. }
  16. 'settings-sharelatex': @settings = {
  17. apis:
  18. references: {url: 'http://some.url'}
  19. web: {url: 'http://some.url'}
  20. }
  21. 'request': @request = {
  22. get: sinon.stub()
  23. post: sinon.stub()
  24. }
  25. '../../models/Project': @Project = {
  26. Project: {
  27. findById: sinon.stub().callsArgWith(2, null, {owner_ref: '111'})
  28. }
  29. }
  30. '../User/UserGetter': @UserGetter = {
  31. getUser: sinon.stub().callsArgWith(2, null, {features: {references: false}})
  32. }
  33. describe '_findBibDocIds', ->
  34. beforeEach ->
  35. @fakeProject =
  36. rootFolder: [
  37. docs: [
  38. {name: 'one.bib', _id: 'aaa'},
  39. {name: 'two.txt', _id: 'bbb'},
  40. ]
  41. folders: [
  42. {docs: [{name: 'three.bib', _id: 'ccc'}], folders: []}
  43. ]
  44. ]
  45. @expectedIds = ['aaa', 'ccc']
  46. it 'should select the correct docIds', ->
  47. result = @handler._findBibDocIds(@fakeProject)
  48. expect(result).to.deep.equal @expectedIds
  49. describe '_isFullIndex', ->
  50. beforeEach ->
  51. @fakeProject =
  52. owner_ref:
  53. features:
  54. references: false
  55. @call = (callback) =>
  56. @handler._isFullIndex @fakeProject, callback
  57. describe 'with references feature on', ->
  58. beforeEach ->
  59. @fakeProject.owner_ref.features.references = true
  60. it 'should return true', ->
  61. @call (err, isFullIndex) =>
  62. expect(err).to.equal null
  63. expect(isFullIndex).to.equal true
  64. describe 'with references feature off', ->
  65. beforeEach ->
  66. @fakeProject.owner_ref.features.references = false
  67. it 'should return false', ->
  68. @call (err, isFullIndex) =>
  69. expect(err).to.equal null
  70. expect(isFullIndex).to.equal false
  71. describe 'index', ->
  72. # describe 'indexFile', ->
  73. # describe 'full index or not', ->
  74. # beforeEach ->
  75. # @request.post.callsArgWith(1, null, {statusCode: 200}, {})
  76. # describe 'when full index is not required', ->
  77. # beforeEach ->
  78. # @UserGetter.getUser.callsArgWith(2, null, {features: {references: false}})
  79. # it 'should set fullIndex to true', (done) ->
  80. # @handler.indexFile @project_id, @file_id, (err) =>
  81. # @request.post.calledOnce.should.equal true
  82. # options = @request.post.firstCall.args[0]
  83. # options.json.fullIndex.should.equal false
  84. # done()
  85. # describe 'when full index is required', ->
  86. # beforeEach ->
  87. # @UserGetter.getUser.callsArgWith(2, null, {features: {references: true}})
  88. # it 'should set fullIndex to true', (done) ->
  89. # @handler.indexFile @project_id, @file_id, (err) =>
  90. # @request.post.calledOnce.should.equal true
  91. # options = @request.post.firstCall.args[0]
  92. # options.json.fullIndex.should.equal true
  93. # done()
  94. # describe 'when index operation is successful', ->
  95. # beforeEach ->
  96. # @request.post.callsArgWith(1, null, {statusCode: 201}, {})
  97. # it 'should not produce an error', (done) ->
  98. # @handler.indexFile @project_id, @file_id, (err) =>
  99. # expect(err).to.equal null
  100. # @request.post.calledOnce.should.equal true
  101. # options = @request.post.firstCall.args[0]
  102. # options.json.fullIndex.should.equal false
  103. # options.json.referencesUrl.should.not.be.undefined
  104. # options.url.should.not.be.undefined
  105. # done()
  106. # describe 'when index operation fails', ->
  107. # beforeEach ->
  108. # @request.post.callsArgWith(1, null, {statusCode: 500}, {})
  109. # it 'should produce an error', (done) ->
  110. # @handler.indexFile @project_id, @file_id, (err) =>
  111. # expect(err).to.not.equal null
  112. # done()
  113. # describe 'getKeys', ->
  114. # describe 'when request is successful', ->
  115. # beforeEach ->
  116. # @data =
  117. # projectId: @projectId
  118. # keys: ['a', 'b', 'c']
  119. # @request.get.callsArgWith(1, null, {statusCode: 200}, @data)
  120. # it 'should not produce an error', ->
  121. # @handler.getKeys @project_id, (err, result) =>
  122. # expect(err).to.equal null
  123. # it 'should produce a result object', ->
  124. # @handler.getKeys @project_id, (err, result) =>
  125. # expect(result).to.not.equal null
  126. # expect(result).to.deep.equal @data
  127. # describe 'when request fails', ->
  128. # beforeEach ->
  129. # @data =
  130. # projectId: @project_Id
  131. # keys: ['a', 'b', 'c']
  132. # @request.get.callsArgWith(1, null, {statusCode: 500}, null)
  133. # it 'should produce an error', ->
  134. # @handler.getKeys @project_id, (err, result) =>
  135. # expect(err).to.not.equal null
  136. # it 'should not produce a result', ->
  137. # @handler.getKeys @project_id, (err, result) =>
  138. # expect(result).to.not.equal null