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

118 lines
3.4 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:40:42 -05:00
sinon = require "sinon"
chai = require("chai")
chai.should()
{db, ObjectId} = require "../../../app/js/mongojs"
2014-02-12 05:40:42 -05:00
MockTrackChangesApi = require "./helpers/MockTrackChangesApi"
2014-02-12 05:40:42 -05:00
MockWebApi = require "./helpers/MockWebApi"
DocUpdaterClient = require "./helpers/DocUpdaterClient"
describe "Deleting a document", ->
before ->
@lines = ["one", "two", "three"]
@version = 42
2014-02-12 05:40:42 -05:00
@update =
doc: @doc_id
op: [{
i: "one and a half\n"
p: 4
}]
v: @version
2014-02-12 05:40:42 -05:00
@result = ["one", "one and a half", "two", "three"]
sinon.spy MockTrackChangesApi, "flushDoc"
after ->
MockTrackChangesApi.flushDoc.restore()
2014-02-12 05:40:42 -05:00
describe "when the updated doc exists in the doc updater", ->
before (done) ->
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
sinon.spy MockWebApi, "setDocumentLines"
sinon.spy MockWebApi, "getDocument"
MockWebApi.insertDoc @project_id, @doc_id, lines: @lines
db.docOps.insert {
doc_id: ObjectId(@doc_id)
version: @version
}, (error) =>
2014-02-12 05:40:42 -05:00
throw error if error?
DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) =>
2014-02-12 05:40:42 -05:00
throw error if error?
DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) =>
throw error if error?
setTimeout () =>
DocUpdaterClient.deleteDoc @project_id, @doc_id, (error, res, body) =>
@statusCode = res.statusCode
setTimeout done, 200
, 200
2014-02-12 05:40:42 -05:00
after ->
MockWebApi.setDocumentLines.restore()
MockWebApi.getDocument.restore()
it "should return a 204 status code", ->
@statusCode.should.equal 204
it "should send the updated document to the web api", ->
MockWebApi.setDocumentLines
.calledWith(@project_id, @doc_id, @result)
.should.equal true
it "should write the version to mongo", (done) ->
db.docOps.find {
doc_id: ObjectId(@doc_id)
}, {
version: 1
}, (error, docs) =>
doc = docs[0]
doc.version.should.equal @version + 1
done()
2014-02-12 05:40:42 -05:00
it "should need to reload the doc if read again", (done) ->
MockWebApi.getDocument.called.should.equal.false
DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
MockWebApi.getDocument
.calledWith(@project_id, @doc_id)
.should.equal true
done()
it "should flush track changes", ->
MockTrackChangesApi.flushDoc.calledWith(@doc_id).should.equal true
2014-02-12 05:40:42 -05:00
describe "when the doc is not in the doc updater", ->
before (done) ->
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
MockWebApi.insertDoc @project_id, @doc_id, {
lines: @lines
}
sinon.spy MockWebApi, "setDocumentLines"
sinon.spy MockWebApi, "getDocument"
DocUpdaterClient.deleteDoc @project_id, @doc_id, (error, res, body) =>
@statusCode = res.statusCode
setTimeout done, 200
2014-02-12 05:40:42 -05:00
after ->
MockWebApi.setDocumentLines.restore()
MockWebApi.getDocument.restore()
it "should return a 204 status code", ->
@statusCode.should.equal 204
it "should not need to send the updated document to the web api", ->
MockWebApi.setDocumentLines.called.should.equal false
it "should need to reload the doc if read again", (done) ->
MockWebApi.getDocument.called.should.equal.false
DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
MockWebApi.getDocument
.calledWith(@project_id, @doc_id)
.should.equal true
done()
it "should flush track changes", ->
MockTrackChangesApi.flushDoc.calledWith(@doc_id).should.equal true
2014-02-12 05:40:42 -05:00