DocManagerTests.coffee 8.1 KB

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