2014-11-12 10:54:55 -05:00
|
|
|
require('chai').should()
|
|
|
|
sinon = require("sinon")
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
path = require "path"
|
|
|
|
modulePath = '../../../app/js/DocumentUpdaterManager'
|
|
|
|
|
|
|
|
describe 'DocumentUpdaterManager', ->
|
|
|
|
beforeEach ->
|
|
|
|
@project_id = "project-id-923"
|
|
|
|
@doc_id = "doc-id-394"
|
|
|
|
@lines = ["one", "two", "three"]
|
|
|
|
@version = 42
|
2019-05-31 17:27:05 -04:00
|
|
|
@settings =
|
2014-11-12 10:54:55 -05:00
|
|
|
apis: documentupdater: url: "http://doc-updater.example.com"
|
2017-05-02 10:51:17 -04:00
|
|
|
redis: documentupdater:
|
|
|
|
key_schema:
|
|
|
|
pendingUpdates: ({doc_id}) -> "PendingUpdates:#{doc_id}"
|
2014-11-13 12:07:05 -05:00
|
|
|
@rclient = {auth:->}
|
2019-05-31 17:27:05 -04:00
|
|
|
|
2017-06-01 06:27:56 -04:00
|
|
|
@DocumentUpdaterManager = SandboxedModule.require modulePath,
|
|
|
|
requires:
|
|
|
|
'settings-sharelatex':@settings
|
|
|
|
'logger-sharelatex': @logger = {log: sinon.stub(), error: sinon.stub(), warn: sinon.stub()}
|
|
|
|
'request': @request = {}
|
|
|
|
'redis-sharelatex' : createClient: () => @rclient
|
|
|
|
'metrics-sharelatex': @Metrics =
|
|
|
|
Timer: class Timer
|
|
|
|
done: () ->
|
|
|
|
globals:
|
|
|
|
JSON: @JSON = Object.create(JSON) # avoid modifying JSON object directly
|
2014-11-12 10:54:55 -05:00
|
|
|
|
|
|
|
describe "getDocument", ->
|
|
|
|
beforeEach ->
|
|
|
|
@callback = sinon.stub()
|
|
|
|
|
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
|
|
|
@body = JSON.stringify
|
|
|
|
lines: @lines
|
|
|
|
version: @version
|
|
|
|
ops: @ops = ["mock-op-1", "mock-op-2"]
|
2016-12-08 06:37:31 -05:00
|
|
|
ranges: @ranges = {"mock": "ranges"}
|
2014-11-12 10:54:55 -05:00
|
|
|
@fromVersion = 2
|
|
|
|
@request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
|
|
|
|
@DocumentUpdaterManager.getDocument @project_id, @doc_id, @fromVersion, @callback
|
|
|
|
|
|
|
|
it 'should get the document from the document updater', ->
|
|
|
|
url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}?fromVersion=#{@fromVersion}"
|
|
|
|
@request.get.calledWith(url).should.equal true
|
|
|
|
|
2016-12-08 06:37:31 -05:00
|
|
|
it "should call the callback with the lines, version, ranges and ops", ->
|
|
|
|
@callback.calledWith(null, @lines, @version, @ranges, @ops).should.equal true
|
2014-11-12 10:54:55 -05:00
|
|
|
|
|
|
|
describe "when the document updater API returns an error", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.get = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
|
|
|
|
@DocumentUpdaterManager.getDocument @project_id, @doc_id, @fromVersion, @callback
|
|
|
|
|
|
|
|
it "should return an error to the callback", ->
|
|
|
|
@callback.calledWith(@error).should.equal true
|
|
|
|
|
2019-05-31 17:27:05 -04:00
|
|
|
[404, 422].forEach (statusCode) ->
|
|
|
|
describe "when the document updater returns a #{statusCode} status code", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.get = sinon.stub().callsArgWith(1, null, { statusCode }, "")
|
|
|
|
@DocumentUpdaterManager.getDocument @project_id, @doc_id, @fromVersion, @callback
|
2016-05-31 09:21:23 -04:00
|
|
|
|
2019-05-31 17:27:05 -04:00
|
|
|
it "should return the callback with an error", ->
|
|
|
|
@callback.called.should.equal(true)
|
|
|
|
err = @callback.getCall(0).args[0]
|
|
|
|
err.should.have.property('statusCode', statusCode)
|
|
|
|
err.should.have.property('message', "doc updater could not load requested ops")
|
|
|
|
@logger.error.called.should.equal(false)
|
|
|
|
@logger.warn.called.should.equal(true)
|
2016-05-31 09:21:23 -04:00
|
|
|
|
2014-11-12 10:54:55 -05:00
|
|
|
describe "when the document updater returns a failure error code", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.get = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
|
|
|
|
@DocumentUpdaterManager.getDocument @project_id, @doc_id, @fromVersion, @callback
|
|
|
|
|
|
|
|
it "should return the callback with an error", ->
|
2019-05-31 17:27:05 -04:00
|
|
|
@callback.called.should.equal(true)
|
|
|
|
err = @callback.getCall(0).args[0]
|
|
|
|
err.should.have.property('statusCode', 500)
|
|
|
|
err.should.have.property('message', "doc updater returned a non-success status code: 500")
|
|
|
|
@logger.error.called.should.equal(true)
|
2014-11-13 12:07:05 -05:00
|
|
|
|
2014-11-14 10:53:59 -05:00
|
|
|
describe 'flushProjectToMongoAndDelete', ->
|
|
|
|
beforeEach ->
|
|
|
|
@callback = sinon.stub()
|
|
|
|
|
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.del = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
|
|
|
|
@DocumentUpdaterManager.flushProjectToMongoAndDelete @project_id, @callback
|
|
|
|
|
|
|
|
it 'should delete the project from the document updater', ->
|
2019-06-03 05:34:59 -04:00
|
|
|
url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}?background=true"
|
2014-11-14 10:53:59 -05:00
|
|
|
@request.del.calledWith(url).should.equal true
|
|
|
|
|
|
|
|
it "should call the callback with no error", ->
|
|
|
|
@callback.calledWith(null).should.equal true
|
|
|
|
|
|
|
|
describe "when the document updater API returns an error", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.del = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
|
|
|
|
@DocumentUpdaterManager.flushProjectToMongoAndDelete @project_id, @callback
|
|
|
|
|
|
|
|
it "should return an error to the callback", ->
|
|
|
|
@callback.calledWith(@error).should.equal true
|
|
|
|
|
|
|
|
describe "when the document updater returns a failure error code", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.del = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
|
|
|
|
@DocumentUpdaterManager.flushProjectToMongoAndDelete @project_id, @callback
|
|
|
|
|
|
|
|
it "should return the callback with an error", ->
|
2019-05-31 17:27:05 -04:00
|
|
|
@callback.called.should.equal(true)
|
|
|
|
err = @callback.getCall(0).args[0]
|
|
|
|
err.should.have.property('statusCode', 500)
|
|
|
|
err.should.have.property('message', "document updater returned a failure status code: 500")
|
2014-11-14 10:53:59 -05:00
|
|
|
|
2014-11-13 12:07:05 -05:00
|
|
|
describe 'queueChange', ->
|
|
|
|
beforeEach ->
|
|
|
|
@change = {
|
2019-05-24 05:19:02 -04:00
|
|
|
"doc":"1234567890",
|
|
|
|
"op":["d":"test", "p":345]
|
|
|
|
"v": 789
|
2014-11-13 12:07:05 -05:00
|
|
|
}
|
2017-05-11 12:27:28 -04:00
|
|
|
@rclient.rpush = sinon.stub().yields()
|
2014-11-13 12:07:05 -05:00
|
|
|
@callback = sinon.stub()
|
|
|
|
|
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
|
|
|
@DocumentUpdaterManager.queueChange(@project_id, @doc_id, @change, @callback)
|
|
|
|
|
|
|
|
it "should push the change", ->
|
|
|
|
@rclient.rpush
|
|
|
|
.calledWith("PendingUpdates:#{@doc_id}", JSON.stringify(@change))
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should notify the doc updater of the change via the pending-updates-list queue", ->
|
|
|
|
@rclient.rpush
|
|
|
|
.calledWith("pending-updates-list", "#{@project_id}:#{@doc_id}")
|
|
|
|
.should.equal true
|
|
|
|
|
2017-05-11 12:27:28 -04:00
|
|
|
describe "with error talking to redis during rpush", ->
|
2014-11-13 12:07:05 -05:00
|
|
|
beforeEach ->
|
2017-05-11 12:27:28 -04:00
|
|
|
@rclient.rpush = sinon.stub().yields(new Error("something went wrong"))
|
2014-11-13 12:07:05 -05:00
|
|
|
@DocumentUpdaterManager.queueChange(@project_id, @doc_id, @change, @callback)
|
|
|
|
|
|
|
|
it "should return an error", ->
|
|
|
|
@callback.calledWithExactly(sinon.match(Error)).should.equal true
|
2017-06-01 06:27:56 -04:00
|
|
|
|
|
|
|
describe "with null byte corruption", ->
|
|
|
|
beforeEach ->
|
|
|
|
@JSON.stringify = () -> return '["bad bytes! \u0000 <- here"]'
|
|
|
|
@DocumentUpdaterManager.queueChange(@project_id, @doc_id, @change, @callback)
|
|
|
|
|
|
|
|
it "should return an error", ->
|
|
|
|
@callback.calledWithExactly(sinon.match(Error)).should.equal true
|
|
|
|
|
|
|
|
it "should not push the change onto the pending-updates-list queue", ->
|
|
|
|
@rclient.rpush.called.should.equal false
|
2019-05-24 05:19:02 -04:00
|
|
|
|
|
|
|
describe "with invalid keys", ->
|
|
|
|
beforeEach ->
|
|
|
|
@change = {
|
|
|
|
"op":["d":"test", "p":345]
|
|
|
|
"version": 789 # not a valid key
|
|
|
|
}
|
|
|
|
@DocumentUpdaterManager.queueChange(@project_id, @doc_id, @change, @callback)
|
|
|
|
|
|
|
|
it "should remove the invalid keys from the change", ->
|
|
|
|
@rclient.rpush
|
|
|
|
.calledWith("PendingUpdates:#{@doc_id}", JSON.stringify({op:@change.op}))
|
|
|
|
.should.equal true
|