2015-12-30 10:08:12 -05:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
should = require('chai').should()
|
|
|
|
sinon = require 'sinon'
|
|
|
|
assert = require("chai").assert
|
|
|
|
modulePath = "../../../../app/js/Features/ReferencesSearch/ReferencesSearchController"
|
|
|
|
MockRequest = require "../helpers/MockRequest"
|
|
|
|
MockResponse = require "../helpers/MockResponse"
|
|
|
|
|
|
|
|
describe "ReferencesSearchController", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@project_id = '2222'
|
|
|
|
@doc_id = '3333'
|
|
|
|
@controller = SandboxedModule.require modulePath, requires:
|
2015-12-30 11:15:31 -05:00
|
|
|
'logger-sharelatex': {
|
|
|
|
log: ->
|
|
|
|
err: ->
|
|
|
|
}
|
2015-12-30 10:08:12 -05:00
|
|
|
'settings-sharelatex': @settings = {
|
|
|
|
apis: {web: {url: 'http://some.url'}}
|
|
|
|
}
|
2015-12-31 04:39:48 -05:00
|
|
|
'../Project/ProjectLocator': @ProjectLocator = {
|
|
|
|
findElement: sinon.stub()
|
|
|
|
}
|
|
|
|
'./ReferencesSearchHandler': @ReferencesSearchHandler = {
|
|
|
|
indexFile: sinon.stub()
|
|
|
|
getKeys: sinon.stub()
|
|
|
|
}
|
|
|
|
|
|
|
|
describe 'getKeys', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@req = new MockRequest()
|
|
|
|
@req.params.Project_id = @project_id
|
|
|
|
@res = new MockResponse()
|
|
|
|
@data =
|
|
|
|
projectId: @project_id,
|
|
|
|
keys: ['one', 'two', 'three']
|
|
|
|
@ReferencesSearchHandler.getKeys.callsArgWith(1, null, @data)
|
|
|
|
|
|
|
|
describe 'when remote service works', ->
|
|
|
|
|
|
|
|
it 'should produce a json response', (done) ->
|
|
|
|
@res.json = (data) =>
|
|
|
|
data.should.not.equal null
|
|
|
|
data.should.deep.equal @data
|
|
|
|
done()
|
|
|
|
@controller.getKeys(@req, @res)
|
|
|
|
|
|
|
|
it 'should call getKeys on ReferencesSearchHandler', (done) ->
|
|
|
|
@res.json = (data) =>
|
|
|
|
@ReferencesSearchHandler.getKeys
|
|
|
|
.calledOnce.should.equal true
|
|
|
|
@ReferencesSearchHandler.getKeys
|
|
|
|
.calledWith(@project_id).should.equal true
|
|
|
|
done()
|
|
|
|
@controller.getKeys(@req, @res)
|
|
|
|
|
|
|
|
describe 'when remote service produces an error', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@ReferencesSearchHandler.getKeys.callsArgWith(1, new Error('nope'))
|
|
|
|
|
|
|
|
it 'should produce a 500 response', (done) ->
|
|
|
|
@res.send = (status) =>
|
|
|
|
status.should.equal 500
|
|
|
|
done()
|
|
|
|
@controller.getKeys(@req, @res)
|
|
|
|
|
|
|
|
it 'should call getKeys on ReferencesSearchHandler', (done) ->
|
|
|
|
@res.send = (status) =>
|
|
|
|
@ReferencesSearchHandler.getKeys
|
|
|
|
.calledOnce.should.equal true
|
|
|
|
@ReferencesSearchHandler.getKeys
|
|
|
|
.calledWith(@project_id).should.equal true
|
|
|
|
done()
|
|
|
|
@controller.getKeys(@req, @res)
|
2015-12-30 10:08:12 -05:00
|
|
|
|
|
|
|
describe 'indexFile', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@req = new MockRequest()
|
2015-12-30 10:24:57 -05:00
|
|
|
@req.params.Project_id = @project_id
|
2015-12-30 10:08:12 -05:00
|
|
|
@res = new MockResponse()
|
|
|
|
@ProjectLocator.findElement.callsArgWith(1, null, {})
|
|
|
|
@ReferencesSearchHandler.indexFile.callsArgWith(2, null)
|
|
|
|
|
2015-12-30 10:24:57 -05:00
|
|
|
describe 'with a valid doc_id', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@req.body = {docId: @doc_id}
|
|
|
|
|
|
|
|
it 'should produce a 200 response', (done) ->
|
|
|
|
@res.send = (status) =>
|
|
|
|
status.should.equal 200
|
|
|
|
done()
|
|
|
|
@controller.indexFile(@req, @res)
|
|
|
|
|
|
|
|
it 'should call ProjectLocator.findElement', (done) ->
|
|
|
|
@res.send = (status) =>
|
|
|
|
@ProjectLocator.findElement.calledOnce.should.equal true
|
|
|
|
arg =
|
|
|
|
project_id: @project_id
|
|
|
|
element_id: @doc_id,
|
|
|
|
type: 'doc'
|
|
|
|
@ProjectLocator.findElement.calledWith(arg).should.equal true
|
|
|
|
done()
|
|
|
|
@controller.indexFile(@req, @res)
|
|
|
|
|
|
|
|
it 'should call ReferencesSearchHandler.indexFile', (done) ->
|
|
|
|
@res.send = (status) =>
|
|
|
|
@ReferencesSearchHandler.indexFile.calledOnce.should.equal true
|
|
|
|
expected_url = "http://some.url/project/2222/doc/3333"
|
2015-12-31 04:22:35 -05:00
|
|
|
@ReferencesSearchHandler.indexFile
|
|
|
|
.calledWith(@project_id, expected_url).should.equal true
|
2015-12-30 10:24:57 -05:00
|
|
|
done()
|
|
|
|
@controller.indexFile(@req, @res)
|
2015-12-30 11:15:31 -05:00
|
|
|
|
|
|
|
describe 'without a doc_id', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@req.body = {bad: true}
|
|
|
|
|
|
|
|
it 'should produce a 400 response', (done) ->
|
|
|
|
@res.send = (status) =>
|
|
|
|
status.should.equal 400
|
|
|
|
done()
|
|
|
|
@controller.indexFile(@req, @res)
|
|
|
|
|
|
|
|
describe 'when the ProjectLocator cannot find the doc', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@req.body = {docId: 'some_weird_id'}
|
|
|
|
@ProjectLocator.findElement.callsArgWith(1, new Error('not found'), null)
|
2015-12-30 11:20:14 -05:00
|
|
|
@ReferencesSearchHandler.indexFile.callsArgWith(2, null)
|
2015-12-30 11:15:31 -05:00
|
|
|
|
|
|
|
it 'should call ProjectLocator.findElement', (done) ->
|
|
|
|
@res.send = (status) =>
|
|
|
|
@ProjectLocator.findElement.calledOnce.should.equal true
|
|
|
|
arg =
|
|
|
|
project_id: @project_id
|
2015-12-30 11:20:14 -05:00
|
|
|
element_id: 'some_weird_id',
|
2015-12-30 11:15:31 -05:00
|
|
|
type: 'doc'
|
|
|
|
@ProjectLocator.findElement.calledWith(arg).should.equal true
|
|
|
|
done()
|
|
|
|
@controller.indexFile(@req, @res)
|
|
|
|
|
|
|
|
it 'should produce a 500 response', (done) ->
|
|
|
|
@res.send = (status) =>
|
|
|
|
status.should.equal 500
|
|
|
|
done()
|
|
|
|
@controller.indexFile(@req, @res)
|
2015-12-30 11:20:14 -05:00
|
|
|
|
|
|
|
describe 'when the ReferencesSearchHandler produces an error', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@req.body = {docId: @doc_id}
|
|
|
|
@ProjectLocator.findElement.callsArgWith(1, null, {})
|
2015-12-31 04:39:48 -05:00
|
|
|
@ReferencesSearchHandler.indexFile
|
|
|
|
.callsArgWith(2, new Error('something went wrong'))
|
2015-12-30 11:20:14 -05:00
|
|
|
|
|
|
|
it 'should call ReferencesSearchHandler.indexFile', (done) ->
|
|
|
|
@res.send = (status) =>
|
|
|
|
@ReferencesSearchHandler.indexFile.calledOnce.should.equal true
|
|
|
|
expected_url = "http://some.url/project/2222/doc/3333"
|
2015-12-31 04:39:48 -05:00
|
|
|
@ReferencesSearchHandler.indexFile
|
|
|
|
.calledWith(@project_id, expected_url).should.equal true
|
2015-12-30 11:20:14 -05:00
|
|
|
done()
|
|
|
|
@controller.indexFile(@req, @res)
|
|
|
|
|
|
|
|
it 'should produce a 500 response', (done) ->
|
|
|
|
@res.send = (status) =>
|
|
|
|
status.should.equal 500
|
|
|
|
done()
|
|
|
|
@controller.indexFile(@req, @res)
|