|
|
@@ -3,7 +3,6 @@ chai = require('chai')
|
|
|
should = chai.should()
|
|
|
modulePath = "../../../../app/js/PersistenceManager.js"
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
-Errors = require "../../../../app/js/Errors"
|
|
|
|
|
|
describe "PersistenceManager.setDoc", ->
|
|
|
beforeEach ->
|
|
|
@@ -13,76 +12,28 @@ describe "PersistenceManager.setDoc", ->
|
|
|
"./Metrics": @Metrics =
|
|
|
Timer: class Timer
|
|
|
done: sinon.stub()
|
|
|
- @project_id = "project-id-123"
|
|
|
- @doc_id = "doc-id-123"
|
|
|
- @lines = ["one", "two", "three"]
|
|
|
- @version = 42
|
|
|
- @callback = sinon.stub()
|
|
|
- @Settings.apis =
|
|
|
- web:
|
|
|
- url: @url = "www.example.com"
|
|
|
- user: @user = "sharelatex"
|
|
|
- pass: @pass = "password"
|
|
|
-
|
|
|
- describe "with a successful response from the web api", ->
|
|
|
- beforeEach ->
|
|
|
- @request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify(lines: @lines, version: @version))
|
|
|
- @PersistenceManager.setDoc(@project_id, @doc_id, @lines, @version, @callback)
|
|
|
-
|
|
|
- it "should call the web api", ->
|
|
|
- @request
|
|
|
- .calledWith({
|
|
|
- url: "#{@url}/project/#{@project_id}/doc/#{@doc_id}"
|
|
|
- body: JSON.stringify
|
|
|
- lines: @lines
|
|
|
- version: @version
|
|
|
- method: "POST"
|
|
|
- headers:
|
|
|
- "content-type": "application/json"
|
|
|
- auth:
|
|
|
- user: @user
|
|
|
- pass: @pass
|
|
|
- sendImmediately: true
|
|
|
- jar: false
|
|
|
- })
|
|
|
- .should.equal true
|
|
|
-
|
|
|
- it "should call the callback without error", ->
|
|
|
- @callback.calledWith(null).should.equal true
|
|
|
+ "logger-sharelatex": @logger = {warn: sinon.stub()}
|
|
|
|
|
|
- it "should time the execution", ->
|
|
|
- @Metrics.Timer::done.called.should.equal true
|
|
|
-
|
|
|
- describe "when request returns an error", ->
|
|
|
- beforeEach ->
|
|
|
- @request.callsArgWith(1, @error = new Error("oops"), null, null)
|
|
|
- @PersistenceManager.setDoc(@project_id, @doc_id, @lines, @version, @callback)
|
|
|
-
|
|
|
- it "should return the error", ->
|
|
|
- @callback.calledWith(@error).should.equal true
|
|
|
-
|
|
|
- it "should time the execution", ->
|
|
|
- @Metrics.Timer::done.called.should.equal true
|
|
|
+ @project_id = "mock-project-id"
|
|
|
+ @doc_id = "mock-doc-id"
|
|
|
+ @callback = sinon.stub()
|
|
|
+ @lines = ["mock", "doc", "lines"]
|
|
|
+ @version = 42
|
|
|
|
|
|
- describe "when the request returns 404", ->
|
|
|
- beforeEach ->
|
|
|
- @request.callsArgWith(1, null, {statusCode: 404}, "")
|
|
|
- @PersistenceManager.setDoc(@project_id, @doc_id, @lines, @version, @callback)
|
|
|
-
|
|
|
- it "should return a NotFoundError", ->
|
|
|
- @callback.calledWith(new Errors.NotFoundError("not found")).should.equal true
|
|
|
+ @PersistenceManager.setDocInWeb = sinon.stub().callsArg(4)
|
|
|
+ @PersistenceManager.setDocVersionInMongo = sinon.stub().callsArg(2)
|
|
|
|
|
|
- it "should time the execution", ->
|
|
|
- @Metrics.Timer::done.called.should.equal true
|
|
|
+ @PersistenceManager.setDoc @project_id, @doc_id, @lines, @version, @callback
|
|
|
|
|
|
- describe "when the request returns an error status code", ->
|
|
|
- beforeEach ->
|
|
|
- @request.callsArgWith(1, null, {statusCode: 500}, "")
|
|
|
- @PersistenceManager.setDoc(@project_id, @doc_id, @lines, @version, @callback)
|
|
|
-
|
|
|
- it "should return an error", ->
|
|
|
- @callback.calledWith(new Error("web api error")).should.equal true
|
|
|
+ it "should set the doc in the web api", ->
|
|
|
+ @PersistenceManager.setDocInWeb
|
|
|
+ .calledWith(@project_id, @doc_id, @lines, @version)
|
|
|
+ .should.equal true
|
|
|
|
|
|
- it "should time the execution", ->
|
|
|
- @Metrics.Timer::done.called.should.equal true
|
|
|
+ it "should set the doc version in mongo", ->
|
|
|
+ @PersistenceManager.setDocVersionInMongo
|
|
|
+ .calledWith(@doc_id, @version)
|
|
|
+ .should.equal true
|
|
|
|
|
|
+ it "should call the callback", ->
|
|
|
+ @callback.called.should.equal true
|