Add in methods for retrieving updates and doc version

This commit is contained in:
James Allen
2014-03-04 13:02:48 +00:00
parent 20d70859aa
commit 1d1dcdfa2f
6 changed files with 128 additions and 1 deletions
@@ -0,0 +1,52 @@
sinon = require('sinon')
chai = require('chai')
should = chai.should()
expect = chai.expect
modulePath = "../../../../app/js/DocumentUpdaterManager.js"
SandboxedModule = require('sandboxed-module')
describe "DocumentUpdaterManager", ->
beforeEach ->
@DocumentUpdaterManager = SandboxedModule.require modulePath, requires:
"request": @request = {}
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
'settings-sharelatex': @settings =
apis : documentupdater: url : "http://example.com"
@callback = sinon.stub()
@lines = ["one", "two", "three"]
@version = 42
describe "getDocument", ->
describe "successfully", ->
beforeEach ->
@body = JSON.stringify
lines: @lines
version: @version
ops: []
@request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
@DocumentUpdaterManager.getDocument @project_id, @doc_id, @callback
it 'should get the document from the document updater', ->
url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}"
@request.get.calledWith(url).should.equal true
it "should call the callback with the lines and version", ->
@callback.calledWith(null, @lines, @version, @ops).should.equal true
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, @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.get = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
@DocumentUpdaterManager.getDocument @project_id, @doc_id, @callback
it "should return the callback with an error", ->
@callback
.calledWith(new Error("doc updater returned failure status code: 500"))
.should.equal true
@@ -131,3 +131,33 @@ describe "MongoManager", ->
it "should call the callback", ->
@callback.called.should.equal true
describe "getUpdatesBetweenDates", ->
beforeEach ->
@updates = ["mock-update"]
@db.docHistory = {}
@db.docHistory.find = sinon.stub().returns @db.docHistory
@db.docHistory.sort = sinon.stub().returns @db.docHistory
@db.docHistory.toArray = sinon.stub().callsArgWith(0, null, @updates)
@from = new Date(Date.now())
@to = new Date(Date.now() + 100000)
@MongoManager.getUpdatesBetweenDates @doc_id, @from, @to, @callback
it "should find the updates for the doc", ->
@db.docHistory.find
.calledWith({
doc_id: ObjectId(@doc_id)
"meta.start_ts": { $gte: @from }
"meta.end_ts": { $lte: @to }
})
.should.equal true
it "should sort in descending timestamp order", ->
@db.docHistory.sort
.calledWith("meta.end_ts": -1)
.should.equal true
it "should call the call back with the updates", ->
@callback.calledWith(null, @updates).should.equal true