overleaf/services/document-updater/test/acceptance/coffee/SettingADocumentTests.coffee

97 lines
3.2 KiB
CoffeeScript
Raw Normal View History

2014-02-12 10:40:42 +00:00
sinon = require "sinon"
chai = require("chai")
chai.should()
expect = require("chai").expect
2016-11-29 15:09:11 +00:00
Settings = require('settings-sharelatex')
rclient = require("redis-sharelatex").createClient(Settings.redis.web)
2014-02-12 10:40:42 +00:00
MockTrackChangesApi = require "./helpers/MockTrackChangesApi"
2014-02-12 10:40:42 +00:00
MockWebApi = require "./helpers/MockWebApi"
DocUpdaterClient = require "./helpers/DocUpdaterClient"
describe "Setting a document", ->
before ->
2014-02-12 10:40:42 +00:00
@lines = ["one", "two", "three"]
@version = 42
2014-02-12 10:40:42 +00:00
@update =
doc: @doc_id
op: [{
i: "one and a half\n"
p: 4
}]
v: @version
2014-02-12 10:40:42 +00:00
@result = ["one", "one and a half", "two", "three"]
@newLines = ["these", "are", "the", "new", "lines"]
@source = "dropbox"
@user_id = "user-id-123"
sinon.spy MockTrackChangesApi, "flushDoc"
sinon.spy MockWebApi, "setDocument"
after ->
MockWebApi.setDocument.restore()
MockTrackChangesApi.flushDoc.restore()
2014-02-12 10:40:42 +00:00
describe "when the updated doc exists in the doc updater", ->
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) =>
2014-02-12 10:40:42 +00:00
throw error if error?
DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) =>
2014-02-12 10:40:42 +00:00
throw error if error?
setTimeout () =>
DocUpdaterClient.setDocLines @project_id, @doc_id, @newLines, @source, @user_id, (error, res, body) =>
@statusCode = res.statusCode
done()
, 200
2014-02-12 10:40:42 +00:00
it "should return a 204 status code", ->
@statusCode.should.equal 204
it "should send the updated doc lines and version to the web api", ->
MockWebApi.setDocument
2014-02-12 10:40:42 +00: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) =>
doc.version.should.equal @version + 2
2014-02-12 10:40:42 +00:00
done()
it "should leave the document in redis", (done) ->
rclient.get "doclines:#{@doc_id}", (error, lines) =>
throw error if error?
expect(JSON.parse(lines)).to.deep.equal @newLines
done()
describe "when the updated doc does not exist in the doc updater", ->
before (done) ->
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version}
DocUpdaterClient.setDocLines @project_id, @doc_id, @newLines, @source, @user_id, (error, res, body) =>
@statusCode = res.statusCode
setTimeout done, 200
it "should return a 204 status code", ->
@statusCode.should.equal 204
2014-02-12 10:40:42 +00:00
it "should send the updated doc lines to the web api", ->
MockWebApi.setDocument
.calledWith(@project_id, @doc_id, @newLines)
.should.equal true
it "should flush track changes", ->
MockTrackChangesApi.flushDoc.calledWith(@doc_id).should.equal true
it "should remove the document from redis", (done) ->
rclient.get "doclines:#{@doc_id}", (error, lines) =>
throw error if error?
expect(lines).to.not.exist
done()