2016-12-01 11:40:15 -05:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
|
|
|
modulePath = "../../../../app/js/PersistenceManager.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
Errors = require "../../../../app/js/Errors"
|
|
|
|
|
|
|
|
describe "PersistenceManager", ->
|
|
|
|
beforeEach ->
|
2017-07-18 10:28:18 -04:00
|
|
|
@request = sinon.stub()
|
|
|
|
@request.defaults = () => @request
|
2016-12-01 11:40:15 -05:00
|
|
|
@PersistenceManager = SandboxedModule.require modulePath, requires:
|
2017-07-18 10:28:18 -04:00
|
|
|
"requestretry": @request
|
2016-12-01 11:40:15 -05:00
|
|
|
"settings-sharelatex": @Settings = {}
|
|
|
|
"./Metrics": @Metrics =
|
|
|
|
Timer: class Timer
|
|
|
|
done: sinon.stub()
|
2020-01-14 08:53:50 -05:00
|
|
|
inc: sinon.stub()
|
2016-12-01 11:40:15 -05:00
|
|
|
"logger-sharelatex": @logger = {log: sinon.stub(), err: sinon.stub()}
|
|
|
|
@project_id = "project-id-123"
|
2018-04-23 07:08:04 -04:00
|
|
|
@projectHistoryId = "history-id-123"
|
2016-12-01 11:40:15 -05:00
|
|
|
@doc_id = "doc-id-123"
|
|
|
|
@lines = ["one", "two", "three"]
|
|
|
|
@version = 42
|
|
|
|
@callback = sinon.stub()
|
2016-12-08 07:31:43 -05:00
|
|
|
@ranges = { comments: "mock", entries: "mock" }
|
2017-09-29 05:34:28 -04:00
|
|
|
@pathname = '/a/b/c.tex'
|
2019-04-22 20:02:48 -04:00
|
|
|
@lastUpdatedAt = Date.now()
|
|
|
|
@lastUpdatedBy = 'last-author-id'
|
2016-12-01 11:40:15 -05:00
|
|
|
@Settings.apis =
|
|
|
|
web:
|
|
|
|
url: @url = "www.example.com"
|
|
|
|
user: @user = "sharelatex"
|
|
|
|
pass: @pass = "password"
|
|
|
|
|
|
|
|
describe "getDoc", ->
|
2018-03-02 05:02:49 -05:00
|
|
|
beforeEach ->
|
|
|
|
@webResponse = {
|
|
|
|
lines: @lines,
|
|
|
|
version: @version,
|
|
|
|
ranges: @ranges
|
|
|
|
pathname: @pathname,
|
2018-04-23 07:08:04 -04:00
|
|
|
projectHistoryId: @projectHistoryId
|
2018-03-02 05:02:49 -05:00
|
|
|
}
|
2017-09-29 05:34:28 -04:00
|
|
|
|
2016-12-01 11:40:15 -05:00
|
|
|
describe "with a successful response from the web api", ->
|
|
|
|
beforeEach ->
|
2018-03-02 05:02:49 -05:00
|
|
|
@request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify(@webResponse))
|
2016-12-01 11:40:15 -05:00
|
|
|
@PersistenceManager.getDoc(@project_id, @doc_id, @callback)
|
|
|
|
|
|
|
|
it "should call the web api", ->
|
|
|
|
@request
|
|
|
|
.calledWith({
|
|
|
|
url: "#{@url}/project/#{@project_id}/doc/#{@doc_id}"
|
|
|
|
method: "GET"
|
|
|
|
headers:
|
|
|
|
"accept": "application/json"
|
|
|
|
auth:
|
|
|
|
user: @user
|
|
|
|
pass: @pass
|
|
|
|
sendImmediately: true
|
|
|
|
jar: false
|
|
|
|
timeout: 5000
|
|
|
|
})
|
|
|
|
.should.equal true
|
|
|
|
|
2016-12-08 07:31:43 -05:00
|
|
|
it "should call the callback with the doc lines, version and ranges", ->
|
2018-04-23 07:08:04 -04:00
|
|
|
@callback
|
|
|
|
.calledWith(null, @lines, @version, @ranges, @pathname, @projectHistoryId)
|
|
|
|
.should.equal true
|
2016-12-01 11:40:15 -05:00
|
|
|
|
|
|
|
it "should time the execution", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
2020-01-14 08:53:50 -05:00
|
|
|
it "should increment the metric", ->
|
|
|
|
@Metrics.inc.calledWith("getDoc", {status: 200}).should.equal true
|
|
|
|
|
2016-12-01 11:40:15 -05:00
|
|
|
describe "when request returns an error", ->
|
|
|
|
beforeEach ->
|
2020-01-14 08:53:50 -05:00
|
|
|
@error = new Error("oops")
|
|
|
|
@error.code = "EOOPS"
|
|
|
|
@request.callsArgWith(1, @error, null, null)
|
2016-12-01 11:40:15 -05:00
|
|
|
@PersistenceManager.getDoc(@project_id, @doc_id, @callback)
|
|
|
|
|
|
|
|
it "should return the error", ->
|
|
|
|
@callback.calledWith(@error).should.equal true
|
|
|
|
|
|
|
|
it "should time the execution", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
2020-01-14 08:53:50 -05:00
|
|
|
it "should increment the metric", ->
|
|
|
|
@Metrics.inc.calledWith("getDoc", {status: "EOOPS"}).should.equal true
|
|
|
|
|
2016-12-01 11:40:15 -05:00
|
|
|
describe "when the request returns 404", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.callsArgWith(1, null, {statusCode: 404}, "")
|
|
|
|
@PersistenceManager.getDoc(@project_id, @doc_id, @callback)
|
2017-09-29 05:34:28 -04:00
|
|
|
|
2016-12-01 11:40:15 -05:00
|
|
|
it "should return a NotFoundError", ->
|
|
|
|
@callback.calledWith(new Errors.NotFoundError("not found")).should.equal true
|
|
|
|
|
|
|
|
it "should time the execution", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
2020-01-14 08:53:50 -05:00
|
|
|
it "should increment the metric", ->
|
|
|
|
@Metrics.inc.calledWith("getDoc", {status: 404}).should.equal true
|
|
|
|
|
2016-12-01 11:40:15 -05:00
|
|
|
describe "when the request returns an error status code", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.callsArgWith(1, null, {statusCode: 500}, "")
|
|
|
|
@PersistenceManager.getDoc(@project_id, @doc_id, @callback)
|
2017-09-29 05:34:28 -04:00
|
|
|
|
2016-12-01 11:40:15 -05:00
|
|
|
it "should return an error", ->
|
|
|
|
@callback.calledWith(new Error("web api error")).should.equal true
|
|
|
|
|
|
|
|
it "should time the execution", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
2020-01-14 08:53:50 -05:00
|
|
|
it "should increment the metric", ->
|
|
|
|
@Metrics.inc.calledWith("getDoc", {status: 500}).should.equal true
|
|
|
|
|
2016-12-01 11:40:15 -05:00
|
|
|
describe "when request returns an doc without lines", ->
|
|
|
|
beforeEach ->
|
2018-03-02 05:02:49 -05:00
|
|
|
delete @webResponse.lines
|
|
|
|
@request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify(@webResponse))
|
2016-12-01 11:40:15 -05:00
|
|
|
@PersistenceManager.getDoc(@project_id, @doc_id, @callback)
|
|
|
|
|
|
|
|
it "should return and error", ->
|
|
|
|
@callback.calledWith(new Error("web API response had no doc lines")).should.equal true
|
|
|
|
|
|
|
|
describe "when request returns an doc without a version", ->
|
|
|
|
beforeEach ->
|
2018-03-02 05:02:49 -05:00
|
|
|
delete @webResponse.version
|
|
|
|
@request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify(@webResponse))
|
2016-12-01 11:40:15 -05:00
|
|
|
@PersistenceManager.getDoc(@project_id, @doc_id, @callback)
|
|
|
|
|
|
|
|
it "should return and error", ->
|
|
|
|
@callback.calledWith(new Error("web API response had no valid doc version")).should.equal true
|
|
|
|
|
2018-03-02 05:02:49 -05:00
|
|
|
describe "when request returns an doc without a pathname", ->
|
|
|
|
beforeEach ->
|
|
|
|
delete @webResponse.pathname
|
|
|
|
@request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify(@webResponse))
|
|
|
|
@PersistenceManager.getDoc(@project_id, @doc_id, @callback)
|
|
|
|
|
|
|
|
it "should return and error", ->
|
|
|
|
@callback.calledWith(new Error("web API response had no valid doc pathname")).should.equal true
|
|
|
|
|
2016-12-01 11:40:15 -05:00
|
|
|
describe "setDoc", ->
|
|
|
|
describe "with a successful response from the web api", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.callsArgWith(1, null, {statusCode: 200})
|
2019-04-22 20:02:48 -04:00
|
|
|
@PersistenceManager.setDoc(@project_id, @doc_id, @lines, @version, @ranges, @lastUpdatedAt, @lastUpdatedBy, @callback)
|
2016-12-01 11:40:15 -05:00
|
|
|
|
|
|
|
it "should call the web api", ->
|
|
|
|
@request
|
|
|
|
.calledWith({
|
|
|
|
url: "#{@url}/project/#{@project_id}/doc/#{@doc_id}"
|
|
|
|
json:
|
|
|
|
lines: @lines
|
|
|
|
version: @version
|
2016-12-08 07:31:43 -05:00
|
|
|
ranges: @ranges
|
2019-04-22 20:02:48 -04:00
|
|
|
lastUpdatedAt: @lastUpdatedAt
|
|
|
|
lastUpdatedBy: @lastUpdatedBy
|
2016-12-01 11:40:15 -05:00
|
|
|
method: "POST"
|
|
|
|
auth:
|
|
|
|
user: @user
|
|
|
|
pass: @pass
|
|
|
|
sendImmediately: true
|
|
|
|
jar: false
|
|
|
|
timeout: 5000
|
|
|
|
})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback without error", ->
|
|
|
|
@callback.calledWith(null).should.equal true
|
|
|
|
|
|
|
|
it "should time the execution", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
2020-01-14 08:53:50 -05:00
|
|
|
it "should increment the metric", ->
|
|
|
|
@Metrics.inc.calledWith("setDoc", {status: 200}).should.equal true
|
|
|
|
|
2016-12-01 11:40:15 -05:00
|
|
|
describe "when request returns an error", ->
|
|
|
|
beforeEach ->
|
2020-01-14 08:53:50 -05:00
|
|
|
@error = new Error("oops")
|
|
|
|
@error.code = "EOOPS"
|
|
|
|
@request.callsArgWith(1, @error, null, null)
|
2019-04-22 20:02:48 -04:00
|
|
|
@PersistenceManager.setDoc(@project_id, @doc_id, @lines, @version, @ranges, @lastUpdatedAt, @lastUpdatedBy, @callback)
|
2016-12-01 11:40:15 -05:00
|
|
|
|
|
|
|
it "should return the error", ->
|
|
|
|
@callback.calledWith(@error).should.equal true
|
|
|
|
|
|
|
|
it "should time the execution", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
2020-01-14 08:53:50 -05:00
|
|
|
it "should increment the metric", ->
|
|
|
|
@Metrics.inc.calledWith("setDoc", {status: "EOOPS"}).should.equal true
|
|
|
|
|
2016-12-01 11:40:15 -05:00
|
|
|
describe "when the request returns 404", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.callsArgWith(1, null, {statusCode: 404}, "")
|
2019-04-22 20:02:48 -04:00
|
|
|
@PersistenceManager.setDoc(@project_id, @doc_id, @lines, @version, @ranges, @lastUpdatedAt, @lastUpdatedBy, @callback)
|
2017-09-29 05:34:28 -04:00
|
|
|
|
2016-12-01 11:40:15 -05:00
|
|
|
it "should return a NotFoundError", ->
|
|
|
|
@callback.calledWith(new Errors.NotFoundError("not found")).should.equal true
|
|
|
|
|
|
|
|
it "should time the execution", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
2020-01-14 08:53:50 -05:00
|
|
|
it "should increment the metric", ->
|
|
|
|
@Metrics.inc.calledWith("setDoc", {status: 404}).should.equal true
|
|
|
|
|
2016-12-01 11:40:15 -05:00
|
|
|
describe "when the request returns an error status code", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.callsArgWith(1, null, {statusCode: 500}, "")
|
2019-04-22 20:02:48 -04:00
|
|
|
@PersistenceManager.setDoc(@project_id, @doc_id, @lines, @version, @ranges, @lastUpdatedAt, @lastUpdatedBy, @callback)
|
2017-09-29 05:34:28 -04:00
|
|
|
|
2016-12-01 11:40:15 -05:00
|
|
|
it "should return an error", ->
|
|
|
|
@callback.calledWith(new Error("web api error")).should.equal true
|
|
|
|
|
|
|
|
it "should time the execution", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
2020-01-14 08:53:50 -05:00
|
|
|
it "should increment the metric", ->
|
|
|
|
@Metrics.inc.calledWith("setDoc", {status: 500}).should.equal true
|