| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- chai = require('chai')
- chai.should()
- sinon = require("sinon")
- modulePath = "../../../../app/js/Features/Docstore/DocstoreManager"
- SandboxedModule = require('sandboxed-module')
- describe "DocstoreManager", ->
- beforeEach ->
- @requestDefaults = sinon.stub().returns(@request = sinon.stub())
- @DocstoreManager = SandboxedModule.require modulePath, requires:
- "request" : defaults: @requestDefaults
- "settings-sharelatex": @settings =
- apis:
- docstore:
- url: "docstore.sharelatex.com"
- "logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub(), err:->}
- @requestDefaults.calledWith(jar: false).should.equal true
- @project_id = "project-id-123"
- @doc_id = "doc-id-123"
- @callback = sinon.stub()
- describe "deleteDoc", ->
- describe "with a successful response code", ->
- beforeEach ->
- @request.del = sinon.stub().callsArgWith(1, null, statusCode: 204, "")
- @DocstoreManager.deleteDoc @project_id, @doc_id, @callback
- it "should delete the doc in the docstore api", ->
- @request.del
- .calledWith("#{@settings.apis.docstore.url}/project/#{@project_id}/doc/#{@doc_id}")
- .should.equal true
- it "should call the callback without an error", ->
- @callback.calledWith(null).should.equal true
- describe "with a failed response code", ->
- beforeEach ->
- @request.del = sinon.stub().callsArgWith(1, null, statusCode: 500, "")
- @DocstoreManager.deleteDoc @project_id, @doc_id, @callback
- it "should call the callback with an error", ->
- @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
- it "should log the error", ->
- @logger.error
- .calledWith({
- err: new Error("docstore api responded with a non-success code: 500")
- project_id: @project_id
- doc_id: @doc_id
- }, "error deleting doc in docstore")
- .should.equal true
- describe "updateDoc", ->
- beforeEach ->
- @lines = ["mock", "doc", "lines"]
- @rev = 5
- @modified = true
- describe "with a successful response code", ->
- beforeEach ->
- @request.post = sinon.stub().callsArgWith(1, null, statusCode: 204, { modified: @modified, rev: @rev })
- @DocstoreManager.updateDoc @project_id, @doc_id, @lines, @callback
- it "should update the doc in the docstore api", ->
- @request.post
- .calledWith({
- url: "#{@settings.apis.docstore.url}/project/#{@project_id}/doc/#{@doc_id}"
- json:
- lines: @lines
- })
- .should.equal true
- it "should call the callback with the modified status and revision", ->
- @callback.calledWith(null, @modified, @rev).should.equal true
- describe "with a failed response code", ->
- beforeEach ->
- @request.post = sinon.stub().callsArgWith(1, null, statusCode: 500, "")
- @DocstoreManager.updateDoc @project_id, @doc_id, @lines, @callback
- it "should call the callback with an error", ->
- @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
- it "should log the error", ->
- @logger.error
- .calledWith({
- err: new Error("docstore api responded with a non-success code: 500")
- project_id: @project_id
- doc_id: @doc_id
- }, "error updating doc in docstore")
- .should.equal true
- describe "getDoc", ->
- beforeEach ->
- @doc =
- lines: @lines = ["mock", "doc", "lines"]
- rev: @rev = 5
- describe "with a successful response code", ->
- beforeEach ->
- @request.get = sinon.stub().callsArgWith(1, null, statusCode: 204, @doc)
- @DocstoreManager.getDoc @project_id, @doc_id, @callback
- it "should get the doc from the docstore api", ->
- @request.get
- .calledWith({
- url: "#{@settings.apis.docstore.url}/project/#{@project_id}/doc/#{@doc_id}"
- json: true
- })
- .should.equal true
- it "should call the callback with the lines, version and rev", ->
- @callback.calledWith(null, @lines, @rev).should.equal true
- describe "with a failed response code", ->
- beforeEach ->
- @request.get = sinon.stub().callsArgWith(1, null, statusCode: 500, "")
- @DocstoreManager.getDoc @project_id, @doc_id, @callback
- it "should call the callback with an error", ->
- @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
- it "should log the error", ->
- @logger.error
- .calledWith({
- err: new Error("docstore api responded with a non-success code: 500")
- project_id: @project_id
- doc_id: @doc_id
- }, "error getting doc from docstore")
- .should.equal true
- describe "with include_deleted=true", ->
- beforeEach ->
- @request.get = sinon.stub().callsArgWith(1, null, statusCode: 204, @doc)
- @DocstoreManager.getDoc @project_id, @doc_id, include_deleted: true, @callback
- it "should get the doc from the docstore api (including deleted)", ->
- @request.get
- .calledWith({
- url: "#{@settings.apis.docstore.url}/project/#{@project_id}/doc/#{@doc_id}?include_deleted=true"
- json: true
- })
- .should.equal true
- it "should call the callback with the lines, version and rev", ->
- @callback.calledWith(null, @lines, @rev).should.equal true
- describe "getAllDocs", ->
- describe "with a successful response code", ->
- beforeEach ->
- @request.get = sinon.stub().callsArgWith(1, null, statusCode: 204, @docs = [{ _id: "mock-doc-id" }])
- @DocstoreManager.getAllDocs @project_id, @callback
- it "should get all the project docs in the docstore api", ->
- @request.get
- .calledWith({
- url: "#{@settings.apis.docstore.url}/project/#{@project_id}/doc"
- json: true
- })
- .should.equal true
- it "should call the callback with the docs", ->
- @callback.calledWith(null, @docs).should.equal true
- describe "with a failed response code", ->
- beforeEach ->
- @request.get = sinon.stub().callsArgWith(1, null, statusCode: 500, "")
- @DocstoreManager.getAllDocs @project_id, @callback
- it "should call the callback with an error", ->
- @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
- it "should log the error", ->
- @logger.error
- .calledWith({
- err: new Error("docstore api responded with a non-success code: 500")
- project_id: @project_id
- }, "error getting all docs from docstore")
- .should.equal true
- describe "archiveProject", ->
- describe "with a successful response code", ->
- beforeEach ->
- @request.post = sinon.stub().callsArgWith(1, null, statusCode: 204)
- @DocstoreManager.archiveProject @project_id, @callback
- it "should call the callback", ->
- @callback.called.should.equal true
- describe "with a failed response code", ->
- beforeEach ->
- @request.post = sinon.stub().callsArgWith(1, null, statusCode: 500)
- @DocstoreManager.archiveProject @project_id, @callback
- it "should call the callback with an error", ->
- @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
- describe "unarchiveProject", ->
- describe "with a successful response code", ->
- beforeEach ->
- @request.post = sinon.stub().callsArgWith(1, null, statusCode: 204)
- @DocstoreManager.unarchiveProject @project_id, @callback
- it "should call the callback", ->
- @callback.called.should.equal true
- describe "with a failed response code", ->
- beforeEach ->
- @request.post = sinon.stub().callsArgWith(1, null, statusCode: 500)
- @DocstoreManager.unarchiveProject @project_id, @callback
- it "should call the callback with an error", ->
- @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
|