2014-02-12 05:40:42 -05:00
|
|
|
sinon = require "sinon"
|
|
|
|
chai = require("chai")
|
|
|
|
chai.should()
|
2016-01-20 09:31:25 -05:00
|
|
|
expect = require("chai").expect
|
2016-11-29 10:09:11 -05:00
|
|
|
Settings = require('settings-sharelatex')
|
2017-04-12 09:53:03 -04:00
|
|
|
rclient_du = require("redis-sharelatex").createClient(Settings.redis.documentupdater)
|
|
|
|
Keys = Settings.redis.documentupdater.key_schema
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2016-01-20 09:31:25 -05:00
|
|
|
MockTrackChangesApi = require "./helpers/MockTrackChangesApi"
|
2017-10-05 10:14:35 -04:00
|
|
|
MockProjectHistoryApi = require "./helpers/MockProjectHistoryApi"
|
2014-02-12 05:40:42 -05:00
|
|
|
MockWebApi = require "./helpers/MockWebApi"
|
|
|
|
DocUpdaterClient = require "./helpers/DocUpdaterClient"
|
2018-02-15 11:28:40 -05:00
|
|
|
DocUpdaterApp = require "./helpers/DocUpdaterApp"
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
describe "Setting a document", ->
|
2018-02-15 11:28:40 -05:00
|
|
|
before (done) ->
|
2014-02-12 05:40:42 -05:00
|
|
|
@lines = ["one", "two", "three"]
|
2014-02-10 10:17:08 -05:00
|
|
|
@version = 42
|
2014-02-12 05:40:42 -05:00
|
|
|
@update =
|
|
|
|
doc: @doc_id
|
|
|
|
op: [{
|
|
|
|
i: "one and a half\n"
|
|
|
|
p: 4
|
|
|
|
}]
|
2014-02-10 10:17:08 -05:00
|
|
|
v: @version
|
2014-02-12 05:40:42 -05:00
|
|
|
@result = ["one", "one and a half", "two", "three"]
|
|
|
|
@newLines = ["these", "are", "the", "new", "lines"]
|
2014-03-11 08:47:26 -04:00
|
|
|
@source = "dropbox"
|
|
|
|
@user_id = "user-id-123"
|
2017-10-05 10:14:35 -04:00
|
|
|
|
2016-01-20 09:31:25 -05:00
|
|
|
sinon.spy MockTrackChangesApi, "flushDoc"
|
2017-10-05 10:14:35 -04:00
|
|
|
sinon.spy MockProjectHistoryApi, "flushProject"
|
2016-11-29 12:06:23 -05:00
|
|
|
sinon.spy MockWebApi, "setDocument"
|
2018-02-15 11:28:40 -05:00
|
|
|
DocUpdaterApp.ensureRunning(done)
|
2017-10-05 10:14:35 -04:00
|
|
|
|
2016-01-20 09:31:25 -05:00
|
|
|
after ->
|
|
|
|
MockTrackChangesApi.flushDoc.restore()
|
2017-10-05 10:14:35 -04:00
|
|
|
MockProjectHistoryApi.flushProject.restore()
|
|
|
|
MockWebApi.setDocument.restore()
|
|
|
|
|
2014-02-12 05:40:42 -05:00
|
|
|
describe "when the updated doc exists in the doc updater", ->
|
|
|
|
before (done) ->
|
2016-01-20 09:31:25 -05:00
|
|
|
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
|
2016-11-29 12:06:23 -05:00
|
|
|
MockWebApi.insertDoc @project_id, @doc_id, lines: @lines, version: @version
|
|
|
|
DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) =>
|
2014-02-12 05:40:42 -05:00
|
|
|
throw error if error?
|
2016-11-29 12:06:23 -05:00
|
|
|
DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) =>
|
2014-02-12 05:40:42 -05:00
|
|
|
throw error if error?
|
2016-11-29 12:06:23 -05:00
|
|
|
setTimeout () =>
|
2017-03-21 07:20:38 -04:00
|
|
|
DocUpdaterClient.setDocLines @project_id, @doc_id, @newLines, @source, @user_id, false, (error, res, body) =>
|
2016-11-29 12:06:23 -05:00
|
|
|
@statusCode = res.statusCode
|
|
|
|
done()
|
|
|
|
, 200
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
it "should return a 204 status code", ->
|
|
|
|
@statusCode.should.equal 204
|
|
|
|
|
2016-11-29 12:06:23 -05:00
|
|
|
it "should send the updated doc lines and version to the web api", ->
|
|
|
|
MockWebApi.setDocument
|
2014-02-12 05:40:42 -05:00
|
|
|
.calledWith(@project_id, @doc_id, @newLines)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should update the lines in the doc updater", (done) ->
|
|
|
|
DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
|
|
|
|
doc.lines.should.deep.equal @newLines
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should bump the version in the doc updater", (done) ->
|
|
|
|
DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
|
2014-02-10 10:17:08 -05:00
|
|
|
doc.version.should.equal @version + 2
|
2014-02-12 05:40:42 -05:00
|
|
|
done()
|
2017-10-05 10:14:35 -04:00
|
|
|
|
2016-01-20 09:31:25 -05:00
|
|
|
it "should leave the document in redis", (done) ->
|
2017-04-12 09:53:03 -04:00
|
|
|
rclient_du.get Keys.docLines({doc_id: @doc_id}), (error, lines) =>
|
2016-01-20 09:31:25 -05:00
|
|
|
throw error if error?
|
|
|
|
expect(JSON.parse(lines)).to.deep.equal @newLines
|
|
|
|
done()
|
2017-10-05 10:14:35 -04:00
|
|
|
|
2016-01-20 09:31:25 -05:00
|
|
|
describe "when the updated doc does not exist in the doc updater", ->
|
|
|
|
before (done) ->
|
|
|
|
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
|
2016-11-29 12:06:23 -05:00
|
|
|
MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version}
|
2017-03-21 07:20:38 -04:00
|
|
|
DocUpdaterClient.setDocLines @project_id, @doc_id, @newLines, @source, @user_id, false, (error, res, body) =>
|
2016-11-29 12:06:23 -05:00
|
|
|
@statusCode = res.statusCode
|
|
|
|
setTimeout done, 200
|
2017-10-05 10:14:35 -04:00
|
|
|
|
2016-01-20 09:31:25 -05:00
|
|
|
it "should return a 204 status code", ->
|
|
|
|
@statusCode.should.equal 204
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2016-01-20 09:31:25 -05:00
|
|
|
it "should send the updated doc lines to the web api", ->
|
2016-11-29 12:06:23 -05:00
|
|
|
MockWebApi.setDocument
|
2016-01-20 09:31:25 -05:00
|
|
|
.calledWith(@project_id, @doc_id, @newLines)
|
|
|
|
.should.equal true
|
2017-10-05 10:14:35 -04:00
|
|
|
|
2016-01-20 10:05:31 -05:00
|
|
|
it "should flush track changes", ->
|
|
|
|
MockTrackChangesApi.flushDoc.calledWith(@doc_id).should.equal true
|
2017-10-05 10:14:35 -04:00
|
|
|
|
|
|
|
it "should flush project history", ->
|
|
|
|
MockProjectHistoryApi.flushProject.calledWith(@project_id).should.equal true
|
|
|
|
|
2016-01-20 09:31:25 -05:00
|
|
|
it "should remove the document from redis", (done) ->
|
2017-04-12 09:53:03 -04:00
|
|
|
rclient_du.get Keys.docLines({doc_id: @doc_id}), (error, lines) =>
|
2016-01-20 09:31:25 -05:00
|
|
|
throw error if error?
|
|
|
|
expect(lines).to.not.exist
|
|
|
|
done()
|
2017-10-05 10:14:35 -04:00
|
|
|
|
2017-03-21 07:20:38 -04:00
|
|
|
describe "with track changes", ->
|
|
|
|
before ->
|
|
|
|
@lines = ["one", "one and a half", "two", "three"]
|
|
|
|
@id_seed = "587357bd35e64f6157"
|
|
|
|
@update =
|
|
|
|
doc: @doc_id
|
|
|
|
op: [{
|
|
|
|
d: "one and a half\n"
|
|
|
|
p: 4
|
|
|
|
}]
|
|
|
|
meta:
|
|
|
|
tc: @id_seed
|
|
|
|
user_id: @user_id
|
|
|
|
v: @version
|
|
|
|
|
|
|
|
describe "with the undo flag", ->
|
|
|
|
before (done) ->
|
|
|
|
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
|
|
|
|
MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version}
|
|
|
|
DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) =>
|
|
|
|
throw error if error?
|
|
|
|
DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) =>
|
|
|
|
throw error if error?
|
|
|
|
# Go back to old lines, with undo flag
|
|
|
|
DocUpdaterClient.setDocLines @project_id, @doc_id, @lines, @source, @user_id, true, (error, res, body) =>
|
|
|
|
@statusCode = res.statusCode
|
|
|
|
setTimeout done, 200
|
2017-10-05 10:14:35 -04:00
|
|
|
|
2017-03-21 07:20:38 -04:00
|
|
|
it "should undo the tracked changes", (done) ->
|
|
|
|
DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, data) =>
|
|
|
|
throw error if error?
|
|
|
|
ranges = data.ranges
|
|
|
|
expect(ranges.changes).to.be.undefined
|
|
|
|
done()
|
2017-10-05 10:14:35 -04:00
|
|
|
|
2017-03-21 07:20:38 -04:00
|
|
|
describe "without the undo flag", ->
|
|
|
|
before (done) ->
|
|
|
|
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
|
|
|
|
MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version}
|
|
|
|
DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) =>
|
|
|
|
throw error if error?
|
|
|
|
DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) =>
|
|
|
|
throw error if error?
|
|
|
|
# Go back to old lines, without undo flag
|
|
|
|
DocUpdaterClient.setDocLines @project_id, @doc_id, @lines, @source, @user_id, false, (error, res, body) =>
|
|
|
|
@statusCode = res.statusCode
|
|
|
|
setTimeout done, 200
|
2017-10-05 10:14:35 -04:00
|
|
|
|
2017-03-21 07:20:38 -04:00
|
|
|
it "should not undo the tracked changes", (done) ->
|
|
|
|
DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, data) =>
|
|
|
|
throw error if error?
|
|
|
|
ranges = data.ranges
|
|
|
|
expect(ranges.changes.length).to.equal 1
|
|
|
|
done()
|
|
|
|
|
2017-10-05 10:14:35 -04:00
|
|
|
|