| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- should = require('chai').should()
- spies = require('chai-spies')
- chai = require('chai').use(spies)
- sinon = require("sinon")
- SandboxedModule = require('sandboxed-module')
- assert = require('chai').assert
- path = require 'path'
- _ = require 'underscore'
- ObjectId = require("mongojs").ObjectId;
- modulePath = path.join __dirname, '../../../../app/js/Features/DocumentUpdater/DocumentUpdaterHandler'
- describe 'DocumentUpdaterHandler', ->
- beforeEach ->
- @project_id = "project-id-923"
- @doc_id = "doc-id-394"
- @lines = ["one", "two", "three"]
- @version = 42
- @user_id = "mock-user-id-123"
- @project =
- _id: @project_id
- @request = {}
- @projectEntityHandler = {}
- @settings =
- apis:
- documentupdater:
- url : "http://document_updater.example.com"
- project_history:
- url: "http://project_history.example.com"
- @callback = sinon.stub()
- @handler = SandboxedModule.require modulePath, requires:
- 'request': defaults:=> return @request
- 'settings-sharelatex':@settings
- 'logger-sharelatex':{log:(->), error:(->)}
- '../Project/ProjectEntityHandler':@projectEntityHandler
- "../../models/Project": Project: @Project={}
- '../../Features/Project/ProjectLocator':{}
- "metrics-sharelatex":
- Timer:->
- done:->
- describe 'flushProjectToMongo', ->
- describe "successfully", ->
- beforeEach ->
- @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
- @handler.flushProjectToMongo @project_id, @callback
- it 'should flush the document from the document updater', ->
- url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/flush"
- @request.post.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.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
- @handler.flushProjectToMongo @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.post = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
- @handler.flushProjectToMongo @project_id, @callback
- it "should return the callback with an error", ->
- @callback
- .calledWith(new Error("doc updater returned failure status code: 500"))
- .should.equal true
- describe 'flushProjectToMongoAndDelete', ->
- describe "successfully", ->
- beforeEach ->
- @request.del = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
- @handler.flushProjectToMongoAndDelete @project_id, @callback
- it 'should delete the project from the document updater', ->
- url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}"
- @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)
- @handler.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 }, "")
- @handler.flushProjectToMongoAndDelete @project_id, @callback
- it "should return the callback with an error", ->
- @callback
- .calledWith(new Error("doc updater returned failure status code: 500"))
- .should.equal true
- describe 'flushDocToMongo', ->
- describe "successfully", ->
- beforeEach ->
- @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
- @handler.flushDocToMongo @project_id, @doc_id, @callback
- it 'should flush the document from the document updater', ->
- url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}/flush"
- @request.post.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.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
- @handler.flushDocToMongo @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.post = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
- @handler.flushDocToMongo @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
- describe "deleteDoc", ->
- describe "successfully", ->
- beforeEach ->
- @request.del = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
- @handler.deleteDoc @project_id, @doc_id, @callback
- it 'should delete the document from the document updater', ->
- url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}"
- @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)
- @handler.deleteDoc @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.del = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
- @handler.deleteDoc @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
- describe "setDocument", ->
- beforeEach ->
- @source = "dropbox"
- describe "successfully", ->
- beforeEach ->
- @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
- @handler.setDocument @project_id, @doc_id, @user_id, @lines, @source, @callback
- it 'should set the document in the document updater', ->
- url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}"
- @request.post
- .calledWith({
- url: url
- json:
- lines: @lines
- source: @source
- user_id: @user_id
- })
- .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.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
- @handler.setDocument @project_id, @doc_id, @user_id, @lines, @source, @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.post = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
- @handler.setDocument @project_id, @doc_id, @user_id, @lines, @source, @callback
- it "should return the callback with an error", ->
- @callback
- .calledWith(new Error("doc updater returned failure status code: 500"))
- .should.equal true
- describe "getDocument", ->
- describe "successfully", ->
- beforeEach ->
- @body = JSON.stringify
- lines: @lines
- version: @version
- ops: @ops = ["mock-op-1", "mock-op-2"]
- ranges: @ranges = {"mock":"ranges"}
- @fromVersion = 2
- @request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
- @handler.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
- it "should call the callback with the lines and version", ->
- @callback.calledWith(null, @lines, @version, @ranges, @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)
- @handler.getDocument @project_id, @doc_id, @fromVersion, @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 }, "")
- @handler.getDocument @project_id, @doc_id, @fromVersion, @callback
- it "should return the callback with an error", ->
- @callback
- .calledWith(new Error("doc updater returned failure status code: 500"))
- .should.equal true
- describe "getProjectDocsIfMatch", ->
- beforeEach ->
- @project_state_hash = "1234567890abcdef"
- describe "successfully", ->
- beforeEach ->
- @doc0 =
- _id: @doc_id
- lines: @lines
- v: @version
- @docs = [ @doc0, @doc0, @doc0 ]
- @body = JSON.stringify @docs
- @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
- @handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
- it 'should get the documenst from the document updater', ->
- url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/get_and_flush_if_old?state=#{@project_state_hash}"
- @request.post.calledWith(url).should.equal true
- it "should call the callback with the documents", ->
- @callback.calledWithExactly(null, @docs).should.equal true
- describe "when the document updater API returns an error", ->
- beforeEach ->
- @request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
- @handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
- it "should return an error to the callback", ->
- @callback.calledWith(@error).should.equal true
- describe "when the document updater returns a conflict error code", ->
- beforeEach ->
- @request.post = sinon.stub().callsArgWith(1, null, { statusCode: 409 }, "Conflict")
- @handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
- it "should return the callback with no documents", ->
- @callback
- .alwaysCalledWithExactly()
- .should.equal true
- describe "clearProjectState", ->
- describe "successfully", ->
- beforeEach ->
- @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 200})
- @handler.clearProjectState @project_id, @callback
- it 'should clear the project state from the document updater', ->
- url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/clearState"
- @request.post.calledWith(url).should.equal true
- it "should call the callback", ->
- @callback.calledWithExactly().should.equal true
- describe "when the document updater API returns an error", ->
- beforeEach ->
- @request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
- @handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
- it "should return an error to the callback", ->
- @callback.calledWith(@error).should.equal true
- describe "when the document updater returns a conflict error code", ->
- beforeEach ->
- @request.post = sinon.stub().callsArgWith(1, null, { statusCode: 409 }, "Conflict")
- @handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
- it "should return the callback with no documents", ->
- @callback
- .alwaysCalledWithExactly()
- .should.equal true
- describe "acceptChanges", ->
- beforeEach ->
- @change_id = "mock-change-id-1"
- describe "successfully", ->
- beforeEach ->
- @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
- @handler.acceptChanges @project_id, @doc_id, [ @change_id ], @callback
- it 'should accept the change in the document updater', ->
- req =
- url: "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}/change/accept"
- json:
- change_ids: [ @change_id ]
- @request.post.calledWith(req).should.equal true
- it "should call the callback", ->
- @callback.calledWith(null).should.equal true
- describe "when the document updater API returns an error", ->
- beforeEach ->
- @request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
- @handler.acceptChanges @project_id, @doc_id, [ @change_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.post = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
- @handler.acceptChanges @project_id, @doc_id, [ @change_id ], @callback
- it "should return the callback with an error", ->
- @callback
- .calledWith(new Error("doc updater returned failure status code: 500"))
- .should.equal true
- describe "deleteThread", ->
- beforeEach ->
- @thread_id = "mock-thread-id-1"
- describe "successfully", ->
- beforeEach ->
- @request.del = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
- @handler.deleteThread @project_id, @doc_id, @thread_id, @callback
- it 'should delete the thread in the document updater', ->
- url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}/comment/#{@thread_id}"
- @request.del.calledWith(url).should.equal true
- it "should call the callback", ->
- @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)
- @handler.deleteThread @project_id, @doc_id, @thread_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 }, "")
- @handler.deleteThread @project_id, @doc_id, @thread_id, @callback
- it "should return the callback with an error", ->
- @callback
- .calledWith(new Error("doc updater returned failure status code: 500"))
- .should.equal true
- describe "updateProjectStructure ", ->
- beforeEach ->
- @user_id = 1234
- @docIdA = new ObjectId()
- @docIdB = new ObjectId()
- @oldDocs = [
- { path: '/old_a', doc: _id: @docIdA }
- { path: '/old_b', doc: _id: @docIdB }
- ]
- # create new instances of the same ObjectIds so that == doens't pass
- @newDocs = [
- { path: '/old_a', doc: _id: new ObjectId(@docIdA.toString()) }
- { path: '/new_b', doc: _id: new ObjectId(@docIdB.toString()) }
- ]
- @oldFiles = []
- @newFiles = []
- describe "with project history disabled", ->
- beforeEach ->
- @settings.apis.project_history.enabled = false
- @request.post = sinon.stub()
- @handler.updateProjectStructure @project_id, @user_id, @oldDocs, @newDocs, @oldFiles, @newFiles, @callback
- it 'does not make a web request', ->
- @request.post.called.should.equal false
- it 'calls the callback', ->
- @callback.called.should.equal true
- describe "with project history enabled", ->
- beforeEach ->
- @settings.apis.project_history.enabled = true
- @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
- @handler.updateProjectStructure @project_id, @user_id, @oldDocs, @newDocs, @oldFiles, @newFiles, @callback
- it 'should send the structure update to the document updater', ->
- docUpdates = [
- id: @docIdB,
- pathname: "/old_b"
- newPathname: "/new_b"
- ]
- url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}"
- @request.post
- .calledWith(url: url, json: {docUpdates, fileUpdates: [], userId: @user_id})
- .should.equal true
- it "should call the callback with no error", ->
- @callback.calledWith(null).should.equal true
|