DocManagerTests.coffee 12 KB

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