DocManagerTests.coffee 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. @another_project_id = ObjectId().toString()
  21. @callback = sinon.stub()
  22. @stubbedError = new Error("blew up")
  23. describe "getDoc", ->
  24. beforeEach ->
  25. @project = { name: "mock-project" }
  26. @doc = { _id: @doc_id, project_id: @project_id, lines: ["mock-lines"] }
  27. @version = 42
  28. @MongoManager.findDoc = sinon.stub()
  29. @MongoManager.getDocVersion = sinon.stub().yields(null, @version)
  30. describe "when the doc is in the doc collection", ->
  31. beforeEach ->
  32. @MongoManager.findDoc.yields(null, @doc)
  33. @DocManager.getDoc @project_id, @doc_id, {version: true}, @callback
  34. it "should get the doc from the doc collection", ->
  35. @MongoManager.findDoc
  36. .calledWith(@project_id, @doc_id)
  37. .should.equal true
  38. it "should get the doc version from the docOps collection", ->
  39. @MongoManager.getDocVersion
  40. .calledWith(@doc_id)
  41. .should.equal true
  42. it "should return the callback with the doc with the version", ->
  43. @callback.called.should.equal true
  44. doc = @callback.args[0][1]
  45. doc.lines.should.equal @doc.lines
  46. doc.version.should.equal @version
  47. describe "without the version filter", ->
  48. beforeEach ->
  49. @MongoManager.findDoc.yields(null, @doc)
  50. @DocManager.getDoc @project_id, @doc_id, {version: false}, @callback
  51. it "should not get the doc version from the docOps collection", ->
  52. @MongoManager.getDocVersion.called.should.equal false
  53. describe "when MongoManager.findDoc errors", ->
  54. beforeEach ->
  55. @MongoManager.findDoc.yields(@stubbedError)
  56. @DocManager.getDoc @project_id, @doc_id, {version: true}, @callback
  57. it "should return the error", ->
  58. @callback.calledWith(@stubbedError).should.equal true
  59. describe "when the doc is archived", ->
  60. beforeEach ->
  61. @doc = { _id: @doc_id, project_id: @project_id, lines: ["mock-lines"], inS3: true }
  62. @MongoManager.findDoc.yields(null, @doc)
  63. @DocArchiveManager.unarchiveDoc = (project_id, doc_id, callback) =>
  64. @doc.inS3 = false
  65. callback()
  66. sinon.spy @DocArchiveManager, "unarchiveDoc"
  67. @DocManager.getDoc @project_id, @doc_id, {version: true}, @callback
  68. it "should call the DocArchive to unarchive the doc", ->
  69. @DocArchiveManager.unarchiveDoc.calledWith(@project_id, @doc_id).should.equal true
  70. it "should look up the doc twice", ->
  71. @MongoManager.findDoc.calledTwice.should.equal true
  72. it "should return the doc", ->
  73. @callback.calledWith(null, @doc).should.equal true
  74. describe "when the doc does not exist in the docs collection", ->
  75. beforeEach ->
  76. @MongoManager.findDoc = sinon.stub().callsArgWith(2, null, null)
  77. @DocManager.getDoc @project_id, @doc_id, {version: true}, @callback
  78. it "should return a NotFoundError", ->
  79. @callback
  80. .calledWith(new Errors.NotFoundError("No such doc: #{@doc_id} in project #{@project_id}"))
  81. .should.equal true
  82. describe "getAllDocs", ->
  83. describe "when the project exists", ->
  84. beforeEach ->
  85. @docs = [{ _id: @doc_id, project_id: @project_id, lines: ["mock-lines"] }]
  86. @MongoManager.getProjectsDocs = sinon.stub().callsArgWith(1, null, @docs)
  87. @DocArchiveManager.unArchiveAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  88. @DocManager.getAllDocs @project_id, @callback
  89. it "should get the project from the database", ->
  90. @MongoManager.getProjectsDocs
  91. .calledWith(@project_id)
  92. .should.equal true
  93. it "should return the docs", ->
  94. @callback.calledWith(null, @docs).should.equal true
  95. describe "when there are no docs for the project", ->
  96. beforeEach ->
  97. @MongoManager.getProjectsDocs = sinon.stub().callsArgWith(1, null, null)
  98. @DocArchiveManager.unArchiveAllDocs = sinon.stub().callsArgWith(1, null, null)
  99. @DocManager.getAllDocs @project_id, @callback
  100. it "should return a NotFoundError", ->
  101. @callback
  102. .calledWith(new Errors.NotFoundError("No such docs for project #{@project_id}"))
  103. .should.equal true
  104. describe "deleteDoc", ->
  105. describe "when the doc exists", ->
  106. beforeEach ->
  107. @lines = ["mock", "doc", "lines"]
  108. @rev = 77
  109. @DocManager.getDoc = sinon.stub().callsArgWith(3, null, {lines: @lines, rev:@rev})
  110. @MongoManager.markDocAsDeleted = sinon.stub().callsArg(2)
  111. @DocManager.deleteDoc @project_id, @doc_id, @callback
  112. it "should get the doc", ->
  113. @DocManager.getDoc
  114. .calledWith(@project_id, @doc_id)
  115. .should.equal true
  116. it "should mark doc as deleted", ->
  117. @MongoManager.markDocAsDeleted
  118. .calledWith(@project_id, @doc_id)
  119. .should.equal true
  120. it "should return the callback", ->
  121. @callback.called.should.equal true
  122. describe "when the doc does not exist", ->
  123. beforeEach ->
  124. @DocManager.getDoc = sinon.stub().callsArgWith(3, null, null)
  125. @DocManager.deleteDoc @project_id, @doc_id, @callback
  126. it "should return a NotFoundError", ->
  127. @callback
  128. .calledWith(new Errors.NotFoundError("No such doc: #{@doc_id}"))
  129. .should.equal true
  130. describe "updateDoc", ->
  131. beforeEach ->
  132. @oldDocLines = ["old", "doc", "lines"]
  133. @newDocLines = ["new", "doc", "lines"]
  134. @version = 42
  135. @doc = { _id: @doc_id, project_id: @project_id, lines: @oldDocLines, rev: @rev = 5, version: @version }
  136. @MongoManager.upsertIntoDocCollection = sinon.stub().callsArg(3)
  137. @MongoManager.setDocVersion = sinon.stub().yields()
  138. @DocManager.getDoc = sinon.stub()
  139. describe "when the doc lines have changed", ->
  140. beforeEach ->
  141. @DocManager.getDoc = sinon.stub().callsArgWith(3, null, @doc)
  142. @DocManager.updateDoc @project_id, @doc_id, @newDocLines, @version, @callback
  143. it "should get the existing doc", ->
  144. @DocManager.getDoc
  145. .calledWith(@project_id, @doc_id)
  146. .should.equal true
  147. it "should upsert the document to the doc collection", ->
  148. @MongoManager.upsertIntoDocCollection
  149. .calledWith(@project_id, @doc_id, {lines: @newDocLines})
  150. .should.equal true
  151. it "should update the version", ->
  152. @MongoManager.setDocVersion
  153. .calledWith(@doc_id, @version)
  154. .should.equal true
  155. it "should log out the old and new doc lines", ->
  156. @logger.log
  157. .calledWith(
  158. project_id: @project_id
  159. doc_id: @doc_id
  160. oldDocLines: @oldDocLines
  161. newDocLines: @newDocLines
  162. rev: @doc.rev
  163. oldVersion: @version
  164. newVersion: @version
  165. "updating doc lines"
  166. )
  167. .should.equal true
  168. it "should return the callback with the new rev", ->
  169. @callback.calledWith(null, true, @rev + 1).should.equal true
  170. describe "when the version has changed", ->
  171. beforeEach ->
  172. @DocManager.getDoc = sinon.stub().callsArgWith(3, null, @doc)
  173. @DocManager.updateDoc @project_id, @doc_id, @oldDocLines, @version + 1, @callback
  174. it "should get the existing doc with the version", ->
  175. @DocManager.getDoc
  176. .calledWith(@project_id, @doc_id, {version: true})
  177. .should.equal true
  178. it "should upsert the document to the doc collection", ->
  179. @MongoManager.upsertIntoDocCollection
  180. .calledWith(@project_id, @doc_id, {lines: @oldDocLines})
  181. .should.equal true
  182. it "should update the version", ->
  183. @MongoManager.setDocVersion
  184. .calledWith(@doc_id, @version + 1)
  185. .should.equal true
  186. it "should return the callback with the new rev", ->
  187. @callback.calledWith(null, true, @rev + 1).should.equal true
  188. describe "when the version is null", ->
  189. beforeEach ->
  190. @DocManager.updateDoc @project_id, @doc_id, @newDocLines, null, @callback
  191. it "should return an error", ->
  192. @callback.calledWith(new Error("no lines or version provided")).should.equal true
  193. describe "when the lines are null", ->
  194. beforeEach ->
  195. @DocManager.updateDoc @project_id, @doc_id, null, @version, @callback
  196. it "should return an error", ->
  197. @callback.calledWith(new Error("no lines or version provided")).should.equal true
  198. describe "when there is a generic error getting the doc", ->
  199. beforeEach ->
  200. @error = new Error("doc could not be found")
  201. @DocManager.getDoc = sinon.stub().callsArgWith(3, @error, null, null)
  202. @DocManager.updateDoc @project_id, @doc_id, @newDocLines, @version, @callback
  203. it "should not upsert the document to the doc collection", ->
  204. @MongoManager.upsertIntoDocCollection.called.should.equal false
  205. it "should return the callback with the error", ->
  206. @callback.calledWith(@error).should.equal true
  207. describe "when the doc lines have not changed", ->
  208. beforeEach ->
  209. @DocManager.getDoc = sinon.stub().callsArgWith(3, null, @doc)
  210. @DocManager.updateDoc @project_id, @doc_id, @oldDocLines.slice(), @version, @callback
  211. it "should not update the doc", ->
  212. @MongoManager.upsertIntoDocCollection.called.should.equal false
  213. it "should return the callback with the existing rev", ->
  214. @callback.calledWith(null, false, @rev).should.equal true
  215. describe "when the doc lines are an empty array", ->
  216. beforeEach ->
  217. @doc.lines = []
  218. @DocManager.getDoc = sinon.stub().callsArgWith(3, null, @doc)
  219. @DocManager.updateDoc @project_id, @doc_id, @doc.lines, @callback
  220. it "should upsert the document to the doc collection", ->
  221. @MongoManager.upsertIntoDocCollection
  222. .calledWith(@project_id, @doc_id, {lines: @doc.lines})
  223. .should.equal true
  224. describe "when the doc does not exist", ->
  225. beforeEach ->
  226. @DocManager.getDoc = sinon.stub().callsArgWith(3, null, null, null)
  227. @DocManager.updateDoc @project_id, @doc_id, @newDocLines, @version, @callback
  228. it "should upsert the document to the doc collection", ->
  229. @MongoManager.upsertIntoDocCollection
  230. .calledWith(@project_id, @doc_id, {lines: @newDocLines})
  231. .should.equal true
  232. it "should return the callback with the new rev", ->
  233. @callback.calledWith(null, true, 1).should.equal true