DocManagerTests.coffee 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. chai = require('chai')
  4. chai.should()
  5. expect = chai.expect
  6. modulePath = require('path').join __dirname, '../../../app/js/DocManager'
  7. ObjectId = require("mongojs").ObjectId
  8. Errors = require "../../../app/js/Errors"
  9. describe "DocManager", ->
  10. beforeEach ->
  11. @DocManager = SandboxedModule.require modulePath, requires:
  12. "./MongoManager": @MongoManager = {}
  13. "logger-sharelatex": @logger =
  14. log: sinon.stub()
  15. warn:->
  16. err:->
  17. @doc_id = ObjectId().toString()
  18. @project_id = ObjectId().toString()
  19. @callback = sinon.stub()
  20. @stubbedError = new Error("blew up")
  21. describe "getDoc", ->
  22. beforeEach ->
  23. @project = { name: "mock-project" }
  24. @doc = { _id: @doc_id, lines: ["mock-lines"] }
  25. @docCollectionDoc = { _id: @doc_id, lines: ["mock-lines"] }
  26. describe "when the doc is in the doc collection not projects collection", ->
  27. beforeEach ->
  28. @MongoManager.findDoc = sinon.stub()
  29. it "should get the doc from the doc collection when it is present there", (done)->
  30. @MongoManager.findDoc.callsArgWith(1, null, @docCollectionDoc)
  31. @DocManager.getDoc @project_id, @doc_id, (err, doc)=>
  32. doc.should.equal @docCollectionDoc
  33. done()
  34. it "should return the error from find doc", (done)->
  35. @MongoManager.findDoc.callsArgWith(1, @stubbedError)
  36. @DocManager.getDoc @project_id, @doc_id, (err, doc)=>
  37. err.should.equal @stubbedError
  38. done()
  39. describe "when the doc is deleted", ->
  40. beforeEach ->
  41. @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, @doc)
  42. @DocManager.getDoc @project_id, @doc_id, @callback
  43. it "should try to find the doc in the docs collection", ->
  44. @MongoManager.findDoc
  45. .calledWith(@doc_id)
  46. .should.equal true
  47. it "should return the doc", ->
  48. @callback
  49. .calledWith(null, @doc)
  50. .should.equal true
  51. describe "when the doc does not exist anywhere", ->
  52. beforeEach ->
  53. @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, null)
  54. @DocManager.getDoc @project_id, @doc_id, @callback
  55. it "should return a NotFoundError", ->
  56. @callback
  57. .calledWith(new Errors.NotFoundError("No such doc: #{@doc_id} in project #{@project_id}"))
  58. .should.equal true
  59. describe "getAllDocs", ->
  60. describe "when the project exists", ->
  61. beforeEach ->
  62. @docs = [{ _id: @doc_id, lines: ["mock-lines"] }]
  63. @MongoManager.getProjectsDocs = sinon.stub().callsArgWith(1, null, @docs)
  64. @DocManager.getAllDocs @project_id, @callback
  65. it "should get the project from the database", ->
  66. @MongoManager.getProjectsDocs
  67. .calledWith(@project_id)
  68. .should.equal true
  69. it "should return the docs", ->
  70. @callback.calledWith(null, @docs).should.equal true
  71. describe "when there are no docs for the project", ->
  72. beforeEach ->
  73. @MongoManager.getProjectsDocs = sinon.stub().callsArgWith(1, null, null)
  74. @DocManager.getAllDocs @project_id, @callback
  75. it "should return a NotFoundError", ->
  76. @callback
  77. .calledWith(new Errors.NotFoundError("No such docs for project #{@project_id}"))
  78. .should.equal true
  79. describe "deleteDoc", ->
  80. describe "when the doc exists", ->
  81. beforeEach ->
  82. @lines = ["mock", "doc", "lines"]
  83. @rev = 77
  84. @DocManager.getDoc = sinon.stub().callsArgWith(2, null, {lines: @lines, rev:@rev})
  85. @MongoManager.upsertIntoDocCollection = sinon.stub().callsArg(3)
  86. @MongoManager.markDocAsDeleted = sinon.stub().callsArg(1)
  87. @DocManager.deleteDoc @project_id, @doc_id, @callback
  88. it "should get the doc", ->
  89. @DocManager.getDoc
  90. .calledWith(@project_id, @doc_id)
  91. .should.equal true
  92. it "should update the doc lines", ->
  93. @MongoManager.upsertIntoDocCollection
  94. .calledWith(@project_id, @doc_id, @lines)
  95. .should.equal true
  96. it "should mark doc as deleted", ->
  97. @MongoManager.markDocAsDeleted
  98. .calledWith(@doc_id)
  99. .should.equal true
  100. it "should return the callback", ->
  101. @callback.called.should.equal true
  102. describe "when the doc does not exist", ->
  103. beforeEach ->
  104. @DocManager.getDoc = sinon.stub().callsArgWith(2, null, null)
  105. @MongoManager.upsertIntoDocCollection = sinon.stub()
  106. @DocManager.deleteDoc @project_id, @doc_id, @callback
  107. it "should not try to insert a deleted doc", ->
  108. @MongoManager.upsertIntoDocCollection.called.should.equal false
  109. it "should return a NotFoundError", ->
  110. @callback
  111. .calledWith(new Errors.NotFoundError("No such doc: #{@doc_id}"))
  112. .should.equal true
  113. describe "updateDoc", ->
  114. beforeEach ->
  115. @oldDocLines = ["old", "doc", "lines"]
  116. @newDocLines = ["new", "doc", "lines"]
  117. @doc = { _id: @doc_id, lines: @oldDocLines, rev: @rev = 5 }
  118. @MongoManager.upsertIntoDocCollection = sinon.stub().callsArg(3)
  119. @MongoManager.findDoc = sinon.stub()
  120. describe "when the doc lines have changed", ->
  121. beforeEach ->
  122. @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, @doc)
  123. @DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback
  124. it "should get the existing doc", ->
  125. @MongoManager.findDoc
  126. .calledWith(@doc_id)
  127. .should.equal true
  128. it "should upsert the document to the doc collection", ->
  129. @MongoManager.upsertIntoDocCollection
  130. .calledWith(@project_id, @doc_id, @newDocLines)
  131. .should.equal true
  132. it "should log out the old and new doc lines", ->
  133. @logger.log
  134. .calledWith(
  135. project_id: @project_id
  136. doc_id: @doc_id
  137. oldDocLines: @oldDocLines
  138. newDocLines: @newDocLines
  139. rev: @doc.rev
  140. "updating doc lines"
  141. )
  142. .should.equal true
  143. it "should return the callback with the new rev", ->
  144. @callback.calledWith(null, true, @rev + 1).should.equal true
  145. describe "when there is a generic error getting the doc", ->
  146. beforeEach ->
  147. @error = new Error("doc could not be found")
  148. @MongoManager.findDoc = sinon.stub().callsArgWith(1, @error, null, null)
  149. @DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback
  150. it "should not upsert the document to the doc collection", ->
  151. @MongoManager.upsertIntoDocCollection.called.should.equal false
  152. it "should return the callback with the error", ->
  153. @callback.calledWith(@error).should.equal true
  154. describe "when the doc lines have not changed", ->
  155. beforeEach ->
  156. @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, @doc)
  157. @DocManager.updateDoc @project_id, @doc_id, @oldDocLines.slice(), @callback
  158. it "should not update the doc", ->
  159. @MongoManager.upsertIntoDocCollection.called.should.equal false
  160. it "should return the callback with the existing rev", ->
  161. @callback.calledWith(null, false, @rev).should.equal true
  162. describe "when the doc lines are an empty array", ->
  163. beforeEach ->
  164. @doc.lines = []
  165. @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, @doc)
  166. @DocManager.updateDoc @project_id, @doc_id, @doc.lines, @callback
  167. it "should upsert the document to the doc collection", ->
  168. @MongoManager.upsertIntoDocCollection
  169. .calledWith(@project_id, @doc_id, @doc.lines)
  170. .should.equal true
  171. describe "when the doc does not exist", ->
  172. beforeEach ->
  173. @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, null, null)
  174. @DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback
  175. it "should upsert the document to the doc collection", ->
  176. @MongoManager.upsertIntoDocCollection
  177. .calledWith(@project_id, @doc_id, @newDocLines)
  178. .should.equal true
  179. it "should return the callback with the new rev", ->
  180. @callback.calledWith(null, true, 1).should.equal true