2014-02-12 10:23:40 +00:00
|
|
|
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'
|
2017-11-01 17:54:58 +00:00
|
|
|
ObjectId = require("mongojs").ObjectId;
|
2014-02-12 10:23:40 +00:00
|
|
|
modulePath = path.join __dirname, '../../../../app/js/Features/DocumentUpdater/DocumentUpdaterHandler'
|
|
|
|
|
2017-01-09 14:25:27 +00:00
|
|
|
describe 'DocumentUpdaterHandler', ->
|
2014-02-12 10:23:40 +00:00
|
|
|
beforeEach ->
|
|
|
|
@project_id = "project-id-923"
|
|
|
|
@doc_id = "doc-id-394"
|
|
|
|
@lines = ["one", "two", "three"]
|
|
|
|
@version = 42
|
2016-02-04 14:26:50 +00:00
|
|
|
@user_id = "mock-user-id-123"
|
2014-02-12 10:23:40 +00:00
|
|
|
@project =
|
|
|
|
_id: @project_id
|
|
|
|
|
|
|
|
@request = {}
|
|
|
|
@projectEntityHandler = {}
|
2017-11-01 17:54:58 +00:00
|
|
|
@settings =
|
|
|
|
apis:
|
|
|
|
documentupdater:
|
|
|
|
url : "http://document_updater.example.com"
|
|
|
|
project_history:
|
|
|
|
url: "http://project_history.example.com"
|
|
|
|
|
|
|
|
@callback = sinon.stub()
|
2014-02-12 10:23:40 +00:00
|
|
|
@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':{}
|
2017-11-01 17:54:58 +00:00
|
|
|
"metrics-sharelatex":
|
2016-03-09 12:51:19 +00:00
|
|
|
Timer:->
|
|
|
|
done:->
|
2014-02-12 10:23:40 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-05-08 14:47:50 +00:00
|
|
|
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
|
|
|
|
|
2014-02-12 10:23:40 +00:00
|
|
|
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 ->
|
2014-10-15 14:36:08 +00:00
|
|
|
@source = "dropbox"
|
2014-02-12 10:23:40 +00:00
|
|
|
|
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
|
2016-02-04 14:26:50 +00:00
|
|
|
@handler.setDocument @project_id, @doc_id, @user_id, @lines, @source, @callback
|
2014-02-12 10:23:40 +00:00
|
|
|
|
|
|
|
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
|
2014-10-15 15:37:14 +00:00
|
|
|
source: @source
|
2016-02-04 14:26:50 +00:00
|
|
|
user_id: @user_id
|
2014-02-12 10:23:40 +00:00
|
|
|
})
|
|
|
|
.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)
|
2016-02-04 14:26:50 +00:00
|
|
|
@handler.setDocument @project_id, @doc_id, @user_id, @lines, @source, @callback
|
2014-02-12 10:23:40 +00:00
|
|
|
|
|
|
|
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 }, "")
|
2016-02-04 14:26:50 +00:00
|
|
|
@handler.setDocument @project_id, @doc_id, @user_id, @lines, @source, @callback
|
2014-02-12 10:23:40 +00:00
|
|
|
|
|
|
|
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"]
|
2016-12-08 14:09:06 +00:00
|
|
|
ranges: @ranges = {"mock":"ranges"}
|
2014-02-12 10:23:40 +00:00
|
|
|
@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", ->
|
2016-12-08 14:09:06 +00:00
|
|
|
@callback.calledWith(null, @lines, @version, @ranges, @ops).should.equal true
|
2014-02-12 10:23:40 +00:00
|
|
|
|
|
|
|
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
|
2017-01-09 14:25:27 +00:00
|
|
|
|
2017-08-14 13:33:34 +00:00
|
|
|
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
|
2017-10-12 14:01:11 +00:00
|
|
|
@request.post = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
|
2017-08-14 13:33:34 +00:00
|
|
|
@handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
|
|
|
|
|
|
|
|
it 'should get the documenst from the document updater', ->
|
2017-10-12 14:01:11 +00:00
|
|
|
url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/get_and_flush_if_old?state=#{@project_state_hash}"
|
|
|
|
@request.post.calledWith(url).should.equal true
|
2017-08-14 13:33:34 +00:00
|
|
|
|
|
|
|
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 ->
|
2017-10-12 14:01:11 +00:00
|
|
|
@request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
|
2017-08-14 13:33:34 +00:00
|
|
|
@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 ->
|
2017-10-12 14:01:11 +00:00
|
|
|
@request.post = sinon.stub().callsArgWith(1, null, { statusCode: 409 }, "Conflict")
|
2017-08-14 13:33:34 +00:00
|
|
|
@handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
|
|
|
|
|
|
|
|
it "should return the callback with no documents", ->
|
|
|
|
@callback
|
|
|
|
.alwaysCalledWithExactly()
|
|
|
|
.should.equal true
|
|
|
|
|
2017-09-08 14:57:29 +00:00
|
|
|
|
|
|
|
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', ->
|
2017-09-15 08:20:53 +00:00
|
|
|
url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/clearState"
|
2017-09-08 14:57:29 +00:00
|
|
|
@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 ->
|
2017-10-12 14:01:11 +00:00
|
|
|
@request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
|
2017-09-08 14:57:29 +00:00
|
|
|
@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 ->
|
2017-10-12 14:01:11 +00:00
|
|
|
@request.post = sinon.stub().callsArgWith(1, null, { statusCode: 409 }, "Conflict")
|
2017-09-08 14:57:29 +00:00
|
|
|
@handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
|
|
|
|
|
|
|
|
it "should return the callback with no documents", ->
|
|
|
|
@callback
|
|
|
|
.alwaysCalledWithExactly()
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
|
2017-05-18 14:04:12 +00:00
|
|
|
describe "acceptChanges", ->
|
2017-01-09 14:25:27 +00:00
|
|
|
beforeEach ->
|
|
|
|
@change_id = "mock-change-id-1"
|
|
|
|
|
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.post = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
|
2017-05-18 14:04:12 +00:00
|
|
|
@handler.acceptChanges @project_id, @doc_id, [ @change_id ], @callback
|
2017-01-09 14:25:27 +00:00
|
|
|
|
|
|
|
it 'should accept the change in the document updater', ->
|
2017-05-18 14:04:12 +00:00
|
|
|
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
|
2017-01-09 14:25:27 +00:00
|
|
|
|
|
|
|
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)
|
2017-05-18 14:04:12 +00:00
|
|
|
@handler.acceptChanges @project_id, @doc_id, [ @change_id ], @callback
|
2017-01-09 14:25:27 +00:00
|
|
|
|
|
|
|
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 }, "")
|
2017-05-18 14:04:12 +00:00
|
|
|
@handler.acceptChanges @project_id, @doc_id, [ @change_id ], @callback
|
2017-01-09 14:25:27 +00:00
|
|
|
|
|
|
|
it "should return the callback with an error", ->
|
|
|
|
@callback
|
|
|
|
.calledWith(new Error("doc updater returned failure status code: 500"))
|
|
|
|
.should.equal true
|
2017-01-24 15:18:49 +00:00
|
|
|
|
|
|
|
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"))
|
2017-09-08 14:57:29 +00:00
|
|
|
.should.equal true
|
2017-11-01 17:54:58 +00:00
|
|
|
|
|
|
|
describe "updateProjectStructure ", ->
|
|
|
|
beforeEach ->
|
|
|
|
@user_id = 1234
|
|
|
|
|
|
|
|
describe "with project history disabled", ->
|
|
|
|
beforeEach ->
|
2017-12-15 16:11:16 +00:00
|
|
|
@settings.apis.project_history.sendProjectStructureOps = false
|
2017-11-01 17:54:58 +00:00
|
|
|
@request.post = sinon.stub()
|
|
|
|
|
2017-11-30 15:46:37 +00:00
|
|
|
@handler.updateProjectStructure @project_id, @user_id, {}, @callback
|
2017-11-01 17:54:58 +00:00
|
|
|
|
|
|
|
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 ->
|
2017-12-15 16:11:16 +00:00
|
|
|
@settings.apis.project_history.sendProjectStructureOps = true
|
2017-11-10 15:47:12 +00:00
|
|
|
@url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}"
|
2017-11-01 17:54:58 +00:00
|
|
|
@request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
|
|
|
|
|
2017-11-10 15:47:12 +00:00
|
|
|
describe "when an entity has changed name", ->
|
|
|
|
it 'should send the structure update to the document updater', (done) ->
|
|
|
|
@docIdA = new ObjectId()
|
|
|
|
@docIdB = new ObjectId()
|
2017-11-30 15:46:37 +00:00
|
|
|
@changes = {
|
|
|
|
oldDocs: [
|
|
|
|
{ path: '/old_a', doc: _id: @docIdA }
|
|
|
|
{ path: '/old_b', doc: _id: @docIdB }
|
|
|
|
]
|
|
|
|
# create new instances of the same ObjectIds so that == doesn't pass
|
|
|
|
newDocs: [
|
|
|
|
{ path: '/old_a', doc: _id: new ObjectId(@docIdA.toString()) }
|
|
|
|
{ path: '/new_b', doc: _id: new ObjectId(@docIdB.toString()) }
|
|
|
|
]
|
|
|
|
}
|
2017-11-10 15:47:12 +00:00
|
|
|
|
|
|
|
docUpdates = [
|
2017-11-14 16:47:15 +00:00
|
|
|
id: @docIdB.toString(),
|
2017-11-10 15:47:12 +00:00
|
|
|
pathname: "/old_b"
|
|
|
|
newPathname: "/new_b"
|
|
|
|
]
|
|
|
|
|
2017-11-30 15:46:37 +00:00
|
|
|
@handler.updateProjectStructure @project_id, @user_id, @changes, () =>
|
2017-11-10 15:47:12 +00:00
|
|
|
@request.post
|
|
|
|
.calledWith(url: @url, json: {docUpdates, fileUpdates: [], userId: @user_id})
|
|
|
|
.should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe "when a doc has been added", ->
|
|
|
|
it 'should send the structure update to the document updater', (done) ->
|
|
|
|
@docId = new ObjectId()
|
2017-11-30 15:46:37 +00:00
|
|
|
@changes = newDocs: [
|
2017-11-10 15:47:12 +00:00
|
|
|
{ path: '/foo', docLines: 'a\nb', doc: _id: @docId }
|
|
|
|
]
|
|
|
|
|
|
|
|
docUpdates = [
|
2017-11-14 16:47:15 +00:00
|
|
|
id: @docId.toString(),
|
2017-11-10 15:47:12 +00:00
|
|
|
pathname: "/foo"
|
|
|
|
docLines: 'a\nb'
|
|
|
|
url: undefined
|
|
|
|
]
|
|
|
|
|
2017-11-30 15:46:37 +00:00
|
|
|
@handler.updateProjectStructure @project_id, @user_id, @changes, () =>
|
2017-11-10 15:47:12 +00:00
|
|
|
@request.post
|
|
|
|
.calledWith(url: @url, json: {docUpdates, fileUpdates: [], userId: @user_id})
|
|
|
|
.should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe "when a file has been added", ->
|
|
|
|
it 'should send the structure update to the document updater', (done) ->
|
|
|
|
@fileId = new ObjectId()
|
2017-11-30 15:46:37 +00:00
|
|
|
@changes = newFiles: [
|
2017-11-10 15:47:12 +00:00
|
|
|
{ path: '/bar', url: 'filestore.example.com/file', file: _id: @fileId }
|
|
|
|
]
|
|
|
|
|
|
|
|
fileUpdates = [
|
2017-11-14 16:47:15 +00:00
|
|
|
id: @fileId.toString(),
|
2017-11-10 15:47:12 +00:00
|
|
|
pathname: "/bar"
|
|
|
|
url: 'filestore.example.com/file'
|
|
|
|
docLines: undefined
|
|
|
|
]
|
|
|
|
|
2017-11-30 15:46:37 +00:00
|
|
|
@handler.updateProjectStructure @project_id, @user_id, @changes, () =>
|
2017-11-10 15:47:12 +00:00
|
|
|
@request.post
|
|
|
|
.calledWith(url: @url, json: {docUpdates: [], fileUpdates, userId: @user_id})
|
|
|
|
.should.equal true
|
|
|
|
done()
|
2017-11-15 15:12:59 +00:00
|
|
|
|
2017-12-14 13:06:27 +00:00
|
|
|
describe "when an entity has been deleted", ->
|
|
|
|
it 'should end the structure update to the document updater', (done) ->
|
2017-11-15 15:12:59 +00:00
|
|
|
@docId = new ObjectId()
|
2017-11-30 15:46:37 +00:00
|
|
|
@changes = oldDocs: [
|
2017-11-15 15:12:59 +00:00
|
|
|
{ path: '/foo', docLines: 'a\nb', doc: _id: @docId }
|
|
|
|
]
|
|
|
|
|
2017-12-14 13:06:27 +00:00
|
|
|
docUpdates = [
|
|
|
|
id: @docId.toString(),
|
2017-12-14 14:39:27 +00:00
|
|
|
pathname: '/foo',
|
|
|
|
newPathname: ''
|
2017-12-14 13:06:27 +00:00
|
|
|
]
|
|
|
|
|
2017-11-30 15:46:37 +00:00
|
|
|
@handler.updateProjectStructure @project_id, @user_id, @changes, () =>
|
2017-12-14 13:06:27 +00:00
|
|
|
@request.post
|
|
|
|
.calledWith(url: @url, json: {docUpdates, fileUpdates: [], userId: @user_id})
|
|
|
|
.should.equal true
|
2017-11-15 15:12:59 +00:00
|
|
|
done()
|
|
|
|
|