2015-12-30 10:08:12 -05:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
should = require('chai').should()
|
|
|
|
sinon = require 'sinon'
|
|
|
|
assert = require("chai").assert
|
2016-02-08 12:04:27 -05:00
|
|
|
modulePath = "../../../../app/js/Features/References/ReferencesController"
|
2015-12-30 10:08:12 -05:00
|
|
|
MockRequest = require "../helpers/MockRequest"
|
|
|
|
MockResponse = require "../helpers/MockResponse"
|
|
|
|
|
2016-02-08 12:04:27 -05:00
|
|
|
describe "ReferencesController", ->
|
2015-12-30 10:08:12 -05:00
|
|
|
|
|
|
|
beforeEach ->
|
2016-01-26 09:29:23 -05:00
|
|
|
@projectId = '2222'
|
2015-12-30 10:08:12 -05:00
|
|
|
@controller = SandboxedModule.require modulePath, requires:
|
2015-12-30 11:15:31 -05:00
|
|
|
'logger-sharelatex': {
|
|
|
|
log: ->
|
|
|
|
err: ->
|
2016-01-26 09:29:23 -05:00
|
|
|
},
|
2015-12-30 10:08:12 -05:00
|
|
|
'settings-sharelatex': @settings = {
|
|
|
|
apis: {web: {url: 'http://some.url'}}
|
2016-01-26 09:29:23 -05:00
|
|
|
},
|
2016-02-08 12:04:27 -05:00
|
|
|
'./ReferencesHandler': @ReferencesHandler = {
|
2016-01-26 09:29:23 -05:00
|
|
|
index: sinon.stub()
|
2016-01-27 11:00:53 -05:00
|
|
|
indexAll: sinon.stub()
|
2016-01-26 09:29:23 -05:00
|
|
|
},
|
|
|
|
'../Editor/EditorRealTimeController': @EditorRealTimeController = {
|
|
|
|
emitToRoom: sinon.stub()
|
2015-12-31 04:39:48 -05:00
|
|
|
}
|
2016-01-26 09:29:23 -05:00
|
|
|
@req = new MockRequest()
|
|
|
|
@req.params.Project_id = @projectId
|
|
|
|
@req.body =
|
|
|
|
docIds: @docIds = ['aaa', 'bbb']
|
|
|
|
shouldBroadcast: false
|
|
|
|
@res = new MockResponse()
|
|
|
|
@res.json = sinon.stub()
|
2016-06-24 10:46:37 -04:00
|
|
|
@res.send = sinon.stub()
|
2016-02-16 07:28:02 -05:00
|
|
|
@res.sendStatus = sinon.stub()
|
2016-01-26 09:29:23 -05:00
|
|
|
@fakeResponseData =
|
|
|
|
projectId: @projectId,
|
|
|
|
keys: ['one', 'two', 'three']
|
|
|
|
|
2016-01-27 11:00:53 -05:00
|
|
|
describe 'indexAll', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@req.body = {shouldBroadcast: false}
|
2016-02-08 12:04:27 -05:00
|
|
|
@ReferencesHandler.indexAll.callsArgWith(1, null, @fakeResponseData)
|
2016-01-27 11:00:53 -05:00
|
|
|
@call = (callback) =>
|
|
|
|
@controller.indexAll @req, @res
|
|
|
|
callback()
|
|
|
|
|
|
|
|
it 'should not produce an error', (done) ->
|
|
|
|
@call () =>
|
2016-02-16 07:28:02 -05:00
|
|
|
@res.sendStatus.callCount.should.equal 0
|
|
|
|
@res.sendStatus.calledWith(500).should.equal false
|
|
|
|
@res.sendStatus.calledWith(400).should.equal false
|
2016-01-27 11:00:53 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should return data', (done) ->
|
|
|
|
@call () =>
|
|
|
|
@res.json.callCount.should.equal 1
|
|
|
|
@res.json.calledWith(@fakeResponseData).should.equal true
|
|
|
|
done()
|
|
|
|
|
2016-02-08 12:04:27 -05:00
|
|
|
it 'should call ReferencesHandler.indexAll', (done) ->
|
2016-01-27 11:00:53 -05:00
|
|
|
@call () =>
|
2016-02-08 12:04:27 -05:00
|
|
|
@ReferencesHandler.indexAll.callCount.should.equal 1
|
|
|
|
@ReferencesHandler.indexAll.calledWith(@projectId).should.equal true
|
2016-01-27 11:00:53 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'when shouldBroadcast is true', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
2016-02-08 12:04:27 -05:00
|
|
|
@ReferencesHandler.index.callsArgWith(2, null, @fakeResponseData)
|
2016-01-27 11:00:53 -05:00
|
|
|
@req.body.shouldBroadcast = true
|
|
|
|
|
|
|
|
it 'should call EditorRealTimeController.emitToRoom', (done) ->
|
|
|
|
@call () =>
|
|
|
|
@EditorRealTimeController.emitToRoom.callCount.should.equal 1
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should not produce an error', (done) ->
|
|
|
|
@call () =>
|
2016-02-16 07:28:02 -05:00
|
|
|
@res.sendStatus.callCount.should.equal 0
|
|
|
|
@res.sendStatus.calledWith(500).should.equal false
|
|
|
|
@res.sendStatus.calledWith(400).should.equal false
|
2016-01-27 11:00:53 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should still return data', (done) ->
|
|
|
|
@call () =>
|
|
|
|
@res.json.callCount.should.equal 1
|
|
|
|
@res.json.calledWith(@fakeResponseData).should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'when shouldBroadcast is false', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
2016-02-08 12:04:27 -05:00
|
|
|
@ReferencesHandler.index.callsArgWith(2, null, @fakeResponseData)
|
2016-01-27 11:00:53 -05:00
|
|
|
@req.body.shouldBroadcast = false
|
|
|
|
|
|
|
|
it 'should not call EditorRealTimeController.emitToRoom', (done) ->
|
|
|
|
@call () =>
|
|
|
|
@EditorRealTimeController.emitToRoom.callCount.should.equal 0
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should not produce an error', (done) ->
|
|
|
|
@call () =>
|
2016-02-16 07:28:02 -05:00
|
|
|
@res.sendStatus.callCount.should.equal 0
|
|
|
|
@res.sendStatus.calledWith(500).should.equal false
|
|
|
|
@res.sendStatus.calledWith(400).should.equal false
|
2016-01-27 11:00:53 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should still return data', (done) ->
|
|
|
|
@call () =>
|
|
|
|
@res.json.callCount.should.equal 1
|
|
|
|
@res.json.calledWith(@fakeResponseData).should.equal true
|
|
|
|
done()
|
|
|
|
|
2016-06-24 10:46:37 -04:00
|
|
|
describe 'there is no dataaaaaaa', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@ReferencesHandler.indexAll.callsArgWith(1)
|
|
|
|
@call = (callback) =>
|
|
|
|
@controller.indexAll @req, @res
|
|
|
|
callback()
|
|
|
|
|
|
|
|
it 'should not call EditorRealTimeController.emitToRoom', (done) ->
|
|
|
|
@call () =>
|
|
|
|
@EditorRealTimeController.emitToRoom.callCount.should.equal 0
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should not produce an error', (done) ->
|
|
|
|
@call () =>
|
|
|
|
@res.sendStatus.callCount.should.equal 0
|
|
|
|
@res.sendStatus.calledWith(500).should.equal false
|
|
|
|
@res.sendStatus.calledWith(400).should.equal false
|
|
|
|
done()
|
|
|
|
|
2016-07-26 09:23:00 -04:00
|
|
|
it 'should send a response with an empty keys list', (done) ->
|
2016-06-24 10:46:37 -04:00
|
|
|
@call () =>
|
2016-07-26 09:23:00 -04:00
|
|
|
@res.json.called.should.equal true
|
|
|
|
@res.json.calledWith({projectId: @projectId, keys: []}).should.equal true
|
2016-06-24 10:46:37 -04:00
|
|
|
done()
|
2016-01-27 11:00:53 -05:00
|
|
|
|
2016-01-26 09:29:23 -05:00
|
|
|
describe 'index', ->
|
|
|
|
|
2018-02-27 10:03:21 -05:00
|
|
|
beforeEach ->
|
|
|
|
@call = (callback) =>
|
|
|
|
@controller.index @req, @res
|
|
|
|
callback()
|
|
|
|
|
2016-01-26 09:29:23 -05:00
|
|
|
describe 'with docIds as an array and shouldBroadcast as false', ->
|
2015-12-31 04:39:48 -05:00
|
|
|
|
2016-01-26 09:29:23 -05:00
|
|
|
beforeEach ->
|
2016-02-08 12:04:27 -05:00
|
|
|
@ReferencesHandler.index.callsArgWith(2, null, @fakeResponseData)
|
2016-01-26 09:29:23 -05:00
|
|
|
|
2016-02-08 12:04:27 -05:00
|
|
|
it 'should call ReferencesHandler.index', (done) ->
|
2016-01-26 09:29:23 -05:00
|
|
|
@call () =>
|
2016-02-08 12:04:27 -05:00
|
|
|
@ReferencesHandler.index.callCount.should.equal 1
|
|
|
|
@ReferencesHandler.index.calledWith(@projectId, @docIds).should.equal true
|
2016-01-26 09:29:23 -05:00
|
|
|
done()
|
2015-12-31 04:39:48 -05:00
|
|
|
|
2016-01-26 09:29:23 -05:00
|
|
|
it 'should return data', (done) ->
|
|
|
|
@call () =>
|
|
|
|
@res.json.callCount.should.equal 1
|
|
|
|
@res.json.calledWith(@fakeResponseData).should.equal true
|
|
|
|
done()
|
2015-12-31 04:39:48 -05:00
|
|
|
|
2016-01-26 09:29:23 -05:00
|
|
|
it 'should not produce an error', (done) ->
|
|
|
|
@call () =>
|
2016-02-16 07:28:02 -05:00
|
|
|
@res.sendStatus.callCount.should.equal 0
|
|
|
|
@res.sendStatus.calledWith(500).should.equal false
|
|
|
|
@res.sendStatus.calledWith(400).should.equal false
|
2015-12-31 04:39:48 -05:00
|
|
|
done()
|
2016-01-26 09:29:23 -05:00
|
|
|
|
|
|
|
it 'should not call EditorRealTimController.emitToRoom', (done) ->
|
|
|
|
@call () =>
|
|
|
|
@EditorRealTimeController.emitToRoom.callCount.should.equal 0
|
2015-12-31 04:39:48 -05:00
|
|
|
done()
|
|
|
|
|
2016-02-08 12:04:27 -05:00
|
|
|
describe 'when ReferencesHandler.index produces an error', ->
|
2015-12-30 10:08:12 -05:00
|
|
|
|
2016-01-26 09:29:23 -05:00
|
|
|
beforeEach ->
|
2016-02-08 12:04:27 -05:00
|
|
|
@ReferencesHandler.index.callsArgWith(2, new Error('woops'), null)
|
2015-12-30 10:08:12 -05:00
|
|
|
|
2016-01-26 09:29:23 -05:00
|
|
|
it 'should produce an error response', (done) ->
|
|
|
|
@call () =>
|
2016-02-16 07:28:02 -05:00
|
|
|
@res.sendStatus.callCount.should.equal 1
|
|
|
|
@res.sendStatus.calledWith(500).should.equal true
|
2016-01-26 09:29:23 -05:00
|
|
|
done()
|
2015-12-30 10:08:12 -05:00
|
|
|
|
2016-01-26 09:29:23 -05:00
|
|
|
describe 'when shouldBroadcast is true', ->
|
2015-12-30 10:24:57 -05:00
|
|
|
|
|
|
|
beforeEach ->
|
2016-02-08 12:04:27 -05:00
|
|
|
@ReferencesHandler.index.callsArgWith(2, null, @fakeResponseData)
|
2016-01-26 09:29:23 -05:00
|
|
|
@req.body.shouldBroadcast = true
|
2015-12-30 10:24:57 -05:00
|
|
|
|
2016-01-26 09:29:23 -05:00
|
|
|
it 'should call EditorRealTimeController.emitToRoom', (done) ->
|
|
|
|
@call () =>
|
|
|
|
@EditorRealTimeController.emitToRoom.callCount.should.equal 1
|
2015-12-30 10:24:57 -05:00
|
|
|
done()
|
|
|
|
|
2016-01-26 09:29:23 -05:00
|
|
|
it 'should not produce an error', (done) ->
|
|
|
|
@call () =>
|
2016-02-16 07:28:02 -05:00
|
|
|
@res.sendStatus.callCount.should.equal 0
|
|
|
|
@res.sendStatus.calledWith(500).should.equal false
|
|
|
|
@res.sendStatus.calledWith(400).should.equal false
|
2015-12-30 10:24:57 -05:00
|
|
|
done()
|
2015-12-30 11:15:31 -05:00
|
|
|
|
2016-01-26 09:29:23 -05:00
|
|
|
it 'should still return data', (done) ->
|
|
|
|
@call () =>
|
|
|
|
@res.json.callCount.should.equal 1
|
|
|
|
@res.json.calledWith(@fakeResponseData).should.equal true
|
2015-12-30 11:15:31 -05:00
|
|
|
done()
|
|
|
|
|
2016-01-26 09:29:23 -05:00
|
|
|
describe 'with missing docIds', ->
|
2015-12-30 11:15:31 -05:00
|
|
|
|
|
|
|
beforeEach ->
|
2016-01-26 09:29:23 -05:00
|
|
|
delete @req.body.docIds
|
|
|
|
|
|
|
|
it 'should produce an error response', (done) ->
|
|
|
|
@call () =>
|
2016-02-16 07:28:02 -05:00
|
|
|
@res.sendStatus.callCount.should.equal 1
|
|
|
|
@res.sendStatus.calledWith(400).should.equal true
|
2015-12-30 11:15:31 -05:00
|
|
|
done()
|
|
|
|
|
2016-02-08 12:04:27 -05:00
|
|
|
it 'should not call ReferencesHandler.index', (done) ->
|
2016-01-26 09:29:23 -05:00
|
|
|
@call () =>
|
2016-02-08 12:04:27 -05:00
|
|
|
@ReferencesHandler.index.callCount.should.equal 0
|
2015-12-30 11:15:31 -05:00
|
|
|
done()
|
2015-12-30 11:20:14 -05:00
|
|
|
|
2016-01-26 09:29:23 -05:00
|
|
|
describe 'with invalid docIds', ->
|
2015-12-30 11:20:14 -05:00
|
|
|
|
|
|
|
beforeEach ->
|
2016-01-26 09:29:23 -05:00
|
|
|
@req.body.docIds = 42
|
|
|
|
|
|
|
|
it 'should produce an error response', (done) ->
|
|
|
|
@call () =>
|
2016-02-16 07:28:02 -05:00
|
|
|
@res.sendStatus.callCount.should.equal 1
|
|
|
|
@res.sendStatus.calledWith(400).should.equal true
|
2015-12-30 11:20:14 -05:00
|
|
|
done()
|
|
|
|
|
2016-02-08 12:04:27 -05:00
|
|
|
it 'should not call ReferencesHandler.index', (done) ->
|
2016-01-26 09:29:23 -05:00
|
|
|
@call () =>
|
2016-02-08 12:04:27 -05:00
|
|
|
@ReferencesHandler.index.callCount.should.equal 0
|
2015-12-30 11:20:14 -05:00
|
|
|
done()
|