DocManagerTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. chai = require('chai')
  4. assert = require("chai").assert
  5. chai.should()
  6. expect = chai.expect
  7. modulePath = require('path').join __dirname, '../../../app/js/DocManager'
  8. ObjectId = require("mongojs").ObjectId
  9. Errors = require "../../../app/js/Errors"
  10. describe "DocManager", ->
  11. beforeEach ->
  12. @DocManager = SandboxedModule.require modulePath, requires:
  13. "./MongoManager": @MongoManager = {}
  14. "./DocArchiveManager": @DocArchiveManager = {}
  15. "./RangeManager": @RangeManager = {
  16. jsonRangesToMongo: (r) -> r
  17. shouldUpdateRanges: sinon.stub().returns false
  18. }
  19. "logger-sharelatex": @logger =
  20. log: sinon.stub()
  21. warn:->
  22. err:->
  23. @doc_id = ObjectId().toString()
  24. @project_id = ObjectId().toString()
  25. @another_project_id = ObjectId().toString()
  26. @callback = sinon.stub()
  27. @stubbedError = new Error("blew up")
  28. describe "checkDocExists", ->
  29. beforeEach ->
  30. @DocManager._getDoc = sinon.stub()
  31. it "should call get doc with a quick filter", (done)->
  32. @DocManager._getDoc.callsArgWith(3, null, {_id:@doc_id})
  33. @DocManager.checkDocExists @project_id, @doc_id, (err, exist)=>
  34. exist.should.equal true
  35. @DocManager._getDoc.calledWith(@project_id, @doc_id, {_id:1, inS3:true}).should.equal true
  36. done()
  37. it "should return false when doc is not there", (done)->
  38. @DocManager._getDoc.callsArgWith(3, null)
  39. @DocManager.checkDocExists @project_id, @doc_id, (err, exist)=>
  40. exist.should.equal false
  41. done()
  42. it "should return error when get doc errors", (done)->
  43. @DocManager._getDoc.callsArgWith(3, "error")
  44. @DocManager.checkDocExists @project_id, @doc_id, (err, exist)=>
  45. err.should.equal "error"
  46. done()
  47. describe "getFullDoc", ->
  48. beforeEach ->
  49. @DocManager._getDoc = sinon.stub()
  50. @doc =
  51. _id: @doc_id
  52. lines:["2134"]
  53. it "should call get doc with a quick filter", (done)->
  54. @DocManager._getDoc.callsArgWith(3, null, @doc)
  55. @DocManager.getFullDoc @project_id, @doc_id, (err, doc)=>
  56. doc.should.equal @doc
  57. @DocManager._getDoc.calledWith(@project_id, @doc_id, {lines: true, rev: true, deleted: true, version: true, ranges: true, inS3:true}).should.equal true
  58. done()
  59. it "should return error when get doc errors", (done)->
  60. @DocManager._getDoc.callsArgWith(3, "error")
  61. @DocManager.getFullDoc @project_id, @doc_id, (err, exist)=>
  62. err.should.equal "error"
  63. done()
  64. describe "getRawDoc", ->
  65. beforeEach ->
  66. @DocManager._getDoc = sinon.stub()
  67. @doc =
  68. lines:["2134"]
  69. it "should call get doc with a quick filter", (done)->
  70. @DocManager._getDoc.callsArgWith(3, null, @doc)
  71. @DocManager.getDocLines @project_id, @doc_id, (err, doc)=>
  72. doc.should.equal @doc
  73. @DocManager._getDoc.calledWith(@project_id, @doc_id, {lines: true, inS3:true}).should.equal true
  74. done()
  75. it "should return error when get doc errors", (done)->
  76. @DocManager._getDoc.callsArgWith(3, "error")
  77. @DocManager.getDocLines @project_id, @doc_id, (err, exist)=>
  78. err.should.equal "error"
  79. done()
  80. describe "getDoc", ->
  81. beforeEach ->
  82. @project = { name: "mock-project" }
  83. @doc = { _id: @doc_id, project_id: @project_id, lines: ["mock-lines"] }
  84. @version = 42
  85. @MongoManager.findDoc = sinon.stub()
  86. @MongoManager.getDocVersion = sinon.stub().yields(null, @version)
  87. describe "when using a filter", ->
  88. beforeEach ->
  89. @MongoManager.findDoc.yields(null, @doc)
  90. it "should error if inS3 is not set to true", (done)->
  91. @DocManager._getDoc @project_id, @doc_id, {inS3: false}, (err)->
  92. expect(err).to.exist
  93. done()
  94. it "should always get inS3 even when no filter is passed", (done)->
  95. @DocManager._getDoc @project_id, @doc_id, undefined, (err)=>
  96. @MongoManager.findDoc.called.should.equal false
  97. expect(err).to.exist
  98. done()
  99. it "should not error if inS3 is set to true", (done)->
  100. @DocManager._getDoc @project_id, @doc_id, {inS3: true}, (err)->
  101. expect(err).to.not.exist
  102. done()
  103. describe "when the doc is in the doc collection", ->
  104. beforeEach ->
  105. @MongoManager.findDoc.yields(null, @doc)
  106. @DocManager._getDoc @project_id, @doc_id, {version: true, inS3:true}, @callback
  107. it "should get the doc from the doc collection", ->
  108. @MongoManager.findDoc
  109. .calledWith(@project_id, @doc_id)
  110. .should.equal true
  111. it "should get the doc version from the docOps collection", ->
  112. @MongoManager.getDocVersion
  113. .calledWith(@doc_id)
  114. .should.equal true
  115. it "should return the callback with the doc with the version", ->
  116. @callback.called.should.equal true
  117. doc = @callback.args[0][1]
  118. doc.lines.should.equal @doc.lines
  119. doc.version.should.equal @version
  120. describe "without the version filter", ->
  121. beforeEach ->
  122. @MongoManager.findDoc.yields(null, @doc)
  123. @DocManager._getDoc @project_id, @doc_id, {version: false, inS3:true}, @callback
  124. it "should not get the doc version from the docOps collection", ->
  125. @MongoManager.getDocVersion.called.should.equal false
  126. describe "when MongoManager.findDoc errors", ->
  127. beforeEach ->
  128. @MongoManager.findDoc.yields(@stubbedError)
  129. @DocManager._getDoc @project_id, @doc_id, {version: true, inS3:true}, @callback
  130. it "should return the error", ->
  131. @callback.calledWith(@stubbedError).should.equal true
  132. describe "when the doc is archived", ->
  133. beforeEach ->
  134. @doc = { _id: @doc_id, project_id: @project_id, lines: ["mock-lines"], inS3: true }
  135. @MongoManager.findDoc.yields(null, @doc)
  136. @DocArchiveManager.unarchiveDoc = (project_id, doc_id, callback) =>
  137. @doc.inS3 = false
  138. callback()
  139. sinon.spy @DocArchiveManager, "unarchiveDoc"
  140. @DocManager._getDoc @project_id, @doc_id, {version: true, inS3:true}, @callback
  141. it "should call the DocArchive to unarchive the doc", ->
  142. @DocArchiveManager.unarchiveDoc.calledWith(@project_id, @doc_id).should.equal true
  143. it "should look up the doc twice", ->
  144. @MongoManager.findDoc.calledTwice.should.equal true
  145. it "should return the doc", ->
  146. @callback.calledWith(null, @doc).should.equal true
  147. describe "when the doc does not exist in the docs collection", ->
  148. beforeEach ->
  149. @MongoManager.findDoc = sinon.stub().yields(null, null)
  150. @DocManager._getDoc @project_id, @doc_id, {version: true, inS3:true}, @callback
  151. it "should return a NotFoundError", ->
  152. @callback
  153. .calledWith(sinon.match.has('message', "No such doc: #{@doc_id} in project #{@project_id}"))
  154. .should.equal true
  155. describe "getAllNonDeletedDocs", ->
  156. describe "when the project exists", ->
  157. beforeEach ->
  158. @docs = [{ _id: @doc_id, project_id: @project_id, lines: ["mock-lines"] }]
  159. @MongoManager.getProjectsDocs = sinon.stub().callsArgWith(3, null, @docs)
  160. @DocArchiveManager.unArchiveAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  161. @filter = { lines: true }
  162. @DocManager.getAllNonDeletedDocs @project_id, @filter, @callback
  163. it "should get the project from the database", ->
  164. @MongoManager.getProjectsDocs
  165. .calledWith(@project_id, {include_deleted: false}, @filter)
  166. .should.equal true
  167. it "should return the docs", ->
  168. @callback.calledWith(null, @docs).should.equal true
  169. describe "when there are no docs for the project", ->
  170. beforeEach ->
  171. @MongoManager.getProjectsDocs = sinon.stub().callsArgWith(3, null, null)
  172. @DocArchiveManager.unArchiveAllDocs = sinon.stub().callsArgWith(1, null)
  173. @DocManager.getAllNonDeletedDocs @project_id, @filter, @callback
  174. it "should return a NotFoundError", ->
  175. @callback
  176. .calledWith(sinon.match.has('message', "No docs for project #{@project_id}"))
  177. .should.equal true
  178. describe "deleteDoc", ->
  179. describe "when the doc exists", ->
  180. beforeEach ->
  181. @lines = ["mock", "doc", "lines"]
  182. @rev = 77
  183. @DocManager.checkDocExists = sinon.stub().callsArgWith(2, null, true)
  184. @MongoManager.markDocAsDeleted = sinon.stub().callsArg(2)
  185. @DocManager.deleteDoc @project_id, @doc_id, @callback
  186. it "should get the doc", ->
  187. @DocManager.checkDocExists
  188. .calledWith(@project_id, @doc_id)
  189. .should.equal true
  190. it "should mark doc as deleted", ->
  191. @MongoManager.markDocAsDeleted
  192. .calledWith(@project_id, @doc_id)
  193. .should.equal true
  194. it "should return the callback", ->
  195. @callback.called.should.equal true
  196. describe "when the doc does not exist", ->
  197. beforeEach ->
  198. @DocManager.checkDocExists = sinon.stub().callsArgWith(2, null, false)
  199. @DocManager.deleteDoc @project_id, @doc_id, @callback
  200. it "should return a NotFoundError", ->
  201. @callback
  202. .calledWith(sinon.match.has('message', "No such project/doc to delete: #{@project_id}/#{@doc_id}"))
  203. .should.equal true
  204. describe "updateDoc", ->
  205. beforeEach ->
  206. @oldDocLines = ["old", "doc", "lines"]
  207. @newDocLines = ["new", "doc", "lines"]
  208. @originalRanges = {
  209. changes: [{
  210. id: ObjectId().toString()
  211. op: { i: "foo", p: 3 }
  212. meta:
  213. user_id: ObjectId().toString()
  214. ts: new Date().toString()
  215. }]
  216. }
  217. @newRanges = {
  218. changes: [{
  219. id: ObjectId().toString()
  220. op: { i: "bar", p: 6 }
  221. meta:
  222. user_id: ObjectId().toString()
  223. ts: new Date().toString()
  224. }]
  225. }
  226. @version = 42
  227. @doc = { _id: @doc_id, project_id: @project_id, lines: @oldDocLines, rev: @rev = 5, version: @version, ranges: @originalRanges }
  228. @MongoManager.upsertIntoDocCollection = sinon.stub().callsArg(3)
  229. @MongoManager.setDocVersion = sinon.stub().yields()
  230. @DocManager._getDoc = sinon.stub()
  231. describe "when only the doc lines have changed", ->
  232. beforeEach ->
  233. @DocManager._getDoc = sinon.stub().callsArgWith(3, null, @doc)
  234. @DocManager.updateDoc @project_id, @doc_id, @newDocLines, @version, @originalRanges, @callback
  235. it "should get the existing doc", ->
  236. @DocManager._getDoc
  237. .calledWith(@project_id, @doc_id, {version: true, rev: true, lines: true, version: true, ranges: true, inS3:true})
  238. .should.equal true
  239. it "should upsert the document to the doc collection", ->
  240. @MongoManager.upsertIntoDocCollection
  241. .calledWith(@project_id, @doc_id, {lines: @newDocLines})
  242. .should.equal true
  243. it "should not update the version", ->
  244. @MongoManager.setDocVersion.called.should.equal false
  245. it "should return the callback with the new rev", ->
  246. @callback.calledWith(null, true, @rev + 1).should.equal true
  247. describe "when the doc ranges have changed", ->
  248. beforeEach ->
  249. @DocManager._getDoc = sinon.stub().callsArgWith(3, null, @doc)
  250. @RangeManager.shouldUpdateRanges.returns true
  251. @DocManager.updateDoc @project_id, @doc_id, @oldDocLines, @version, @newRanges, @callback
  252. it "should upsert the ranges", ->
  253. @MongoManager.upsertIntoDocCollection
  254. .calledWith(@project_id, @doc_id, {ranges: @newRanges})
  255. .should.equal true
  256. it "should not update the version", ->
  257. @MongoManager.setDocVersion.called.should.equal false
  258. it "should return the callback with the new rev", ->
  259. @callback.calledWith(null, true, @rev + 1).should.equal true
  260. describe "when only the version has changed", ->
  261. beforeEach ->
  262. @DocManager._getDoc = sinon.stub().callsArgWith(3, null, @doc)
  263. @DocManager.updateDoc @project_id, @doc_id, @oldDocLines, @version + 1, @originalRanges, @callback
  264. it "should not change the lines or ranges", ->
  265. @MongoManager.upsertIntoDocCollection.called.should.equal false
  266. it "should update the version", ->
  267. @MongoManager.setDocVersion
  268. .calledWith(@doc_id, @version + 1)
  269. .should.equal true
  270. it "should return the callback with the old rev", ->
  271. @callback.calledWith(null, true, @rev).should.equal true
  272. describe "when the doc has not changed at all", ->
  273. beforeEach ->
  274. @DocManager._getDoc = sinon.stub().callsArgWith(3, null, @doc)
  275. @DocManager.updateDoc @project_id, @doc_id, @oldDocLines, @version, @originalRanges, @callback
  276. it "should not update the ranges or lines", ->
  277. @MongoManager.upsertIntoDocCollection.called.should.equal false
  278. it "should not update the version", ->
  279. @MongoManager.setDocVersion.called.should.equal false
  280. it "should return the callback with the old rev and modified == false", ->
  281. @callback.calledWith(null, false, @rev).should.equal true
  282. describe "when the version is null", ->
  283. beforeEach ->
  284. @DocManager.updateDoc @project_id, @doc_id, @newDocLines, null, @originalRanges, @callback
  285. it "should return an error", ->
  286. @callback.calledWith(sinon.match.has('message', "no lines, version or ranges provided")).should.equal true
  287. describe "when the lines are null", ->
  288. beforeEach ->
  289. @DocManager.updateDoc @project_id, @doc_id, null, @version, @originalRanges, @callback
  290. it "should return an error", ->
  291. @callback.calledWith(sinon.match.has('message', "no lines, version or ranges provided")).should.equal true
  292. describe "when the ranges are null", ->
  293. beforeEach ->
  294. @DocManager.updateDoc @project_id, @doc_id, @newDocLines, @version, null, @callback
  295. it "should return an error", ->
  296. @callback.calledWith(sinon.match.has('message', "no lines, version or ranges provided")).should.equal true
  297. describe "when there is a generic error getting the doc", ->
  298. beforeEach ->
  299. @error = new Error("doc could not be found")
  300. @DocManager._getDoc = sinon.stub().callsArgWith(3, @error, null, null)
  301. @DocManager.updateDoc @project_id, @doc_id, @newDocLines, @version, @originalRanges, @callback
  302. it "should not upsert the document to the doc collection", ->
  303. @MongoManager.upsertIntoDocCollection.called.should.equal false
  304. it "should return the callback with the error", ->
  305. @callback.calledWith(@error).should.equal true
  306. describe "when the doc lines have not changed", ->
  307. beforeEach ->
  308. @DocManager._getDoc = sinon.stub().callsArgWith(3, null, @doc)
  309. @DocManager.updateDoc @project_id, @doc_id, @oldDocLines.slice(), @version, @originalRanges, @callback
  310. it "should not update the doc", ->
  311. @MongoManager.upsertIntoDocCollection.called.should.equal false
  312. it "should return the callback with the existing rev", ->
  313. @callback.calledWith(null, false, @rev).should.equal true
  314. describe "when the doc does not exist", ->
  315. beforeEach ->
  316. @DocManager._getDoc = sinon.stub().callsArgWith(3, null, null, null)
  317. @DocManager.updateDoc @project_id, @doc_id, @newDocLines, @version, @originalRanges, @callback
  318. it "should upsert the document to the doc collection", ->
  319. @MongoManager.upsertIntoDocCollection
  320. .calledWith(@project_id, @doc_id, {lines: @newDocLines, ranges: @originalRanges})
  321. .should.equal true
  322. it "should set the version", ->
  323. @MongoManager.setDocVersion
  324. .calledWith(@doc_id, @version)
  325. .should.equal true
  326. it "should return the callback with the new rev", ->
  327. @callback.calledWith(null, true, 1).should.equal true