DocManagerTests.coffee 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 = {log: sinon.stub()}
  14. @doc_id = ObjectId().toString()
  15. @project_id = ObjectId().toString()
  16. @callback = sinon.stub()
  17. describe "getDoc", ->
  18. describe "when the project exists and the doc is in it", ->
  19. beforeEach ->
  20. @project = { name: "mock-project" }
  21. @doc = { _id: @doc_id, lines: ["mock-lines"] }
  22. @mongoPath = "mock.mongo.path"
  23. @MongoManager.findProject = sinon.stub().callsArgWith(1, null, @project)
  24. @DocManager.findDocInProject = sinon.stub().callsArgWith(2, null, @doc, @mongoPath)
  25. @DocManager.getDoc @project_id, @doc_id, @callback
  26. it "should get the project from the database", ->
  27. @MongoManager.findProject
  28. .calledWith(@project_id)
  29. .should.equal true
  30. it "should find the doc in the project", ->
  31. @DocManager.findDocInProject
  32. .calledWith(@project, @doc_id)
  33. .should.equal true
  34. it "should return the doc", ->
  35. @callback.calledWith(null, @doc, @mongoPath).should.equal true
  36. describe "when the project does not exist", ->
  37. beforeEach ->
  38. @MongoManager.findProject = sinon.stub().callsArgWith(1, null, null)
  39. @DocManager.findDocInProject = sinon.stub()
  40. @DocManager.getDoc @project_id, @doc_id, @callback
  41. it "should not try to find the doc in the project", ->
  42. @DocManager.findDocInProject.called.should.equal false
  43. it "should return a NotFoundError", ->
  44. @callback
  45. .calledWith(new Errors.NotFoundError("No such project: #{@project_id}"))
  46. .should.equal true
  47. describe "when the doc is deleted", ->
  48. beforeEach ->
  49. @MongoManager.findProject = sinon.stub().callsArgWith(1, null, @project)
  50. @DocManager.findDocInProject = sinon.stub().callsArgWith(2, null, null, null)
  51. @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, @doc)
  52. describe "when include_deleted = true", ->
  53. beforeEach ->
  54. @DocManager.getDoc @project_id, @doc_id, include_deleted: true, @callback
  55. it "should try to find the doc in the docs collection", ->
  56. @MongoManager.findDoc
  57. .calledWith(@doc_id)
  58. .should.equal true
  59. it "should return the doc", ->
  60. @callback
  61. .calledWith(null, @doc)
  62. .should.equal true
  63. describe "when include_deleted is not set", ->
  64. beforeEach ->
  65. @DocManager.getDoc @project_id, @doc_id, @callback
  66. it "should not try to find the doc in the docs collection", ->
  67. @MongoManager.findDoc.called.should.equal false
  68. it "should return a NotFoundError", ->
  69. @callback
  70. .calledWith(new Errors.NotFoundError("No such doc: #{@doc_id}"))
  71. .should.equal true
  72. describe "when the doc does not exist anywhere", ->
  73. beforeEach ->
  74. @MongoManager.findProject = sinon.stub().callsArgWith(1, null, @project)
  75. @DocManager.findDocInProject = sinon.stub().callsArgWith(2, null, null, null)
  76. @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, null)
  77. @DocManager.getDoc @project_id, @doc_id, @callback
  78. it "should return a NotFoundError", ->
  79. @callback
  80. .calledWith(new Errors.NotFoundError("No such doc: #{@doc_id}"))
  81. .should.equal true
  82. describe "getAllDocs", ->
  83. describe "when the project exists", ->
  84. beforeEach ->
  85. @project = { name: "mock-project" }
  86. @docs = [{ _id: @doc_id, lines: ["mock-lines"] }]
  87. @mongoPath = "mock.mongo.path"
  88. @MongoManager.findProject = sinon.stub().callsArgWith(1, null, @project)
  89. @DocManager.findAllDocsInProject = sinon.stub().callsArgWith(1, null, @docs)
  90. @DocManager.getAllDocs @project_id, @callback
  91. it "should get the project from the database", ->
  92. @MongoManager.findProject
  93. .calledWith(@project_id)
  94. .should.equal true
  95. it "should find all the docs in the project", ->
  96. @DocManager.findAllDocsInProject
  97. .calledWith(@project)
  98. .should.equal true
  99. it "should return the docs", ->
  100. @callback.calledWith(null, @docs).should.equal true
  101. describe "when the project does not exist", ->
  102. beforeEach ->
  103. @MongoManager.findProject = sinon.stub().callsArgWith(1, null, null)
  104. @DocManager.findAllDocsInProject = sinon.stub()
  105. @DocManager.getDoc @project_id, @doc_id, @callback
  106. it "should not try to find the doc in the project", ->
  107. @DocManager.findAllDocsInProject.called.should.equal false
  108. it "should return a NotFoundError", ->
  109. @callback
  110. .calledWith(new Errors.NotFoundError("No such project: #{@project_id}"))
  111. .should.equal true
  112. describe "deleteDoc", ->
  113. describe "when the doc exists", ->
  114. beforeEach ->
  115. @lines = ["mock", "doc", "lines"]
  116. @DocManager.getDoc = sinon.stub().callsArgWith(2, null, lines: @lines)
  117. @MongoManager.insertDoc = sinon.stub().callsArg(3)
  118. @DocManager.deleteDoc @project_id, @doc_id, @callback
  119. it "should get the doc", ->
  120. @DocManager.getDoc
  121. .calledWith(@project_id, @doc_id)
  122. .should.equal true
  123. it "insert the doc as a deleted doc", ->
  124. @MongoManager.insertDoc
  125. .calledWith(@project_id, @doc_id, {
  126. lines: @lines
  127. deleted: true
  128. })
  129. .should.equal true
  130. it "should return the callback", ->
  131. @callback.called.should.equal true
  132. describe "when the doc does not exist", ->
  133. beforeEach ->
  134. @DocManager.getDoc = sinon.stub().callsArgWith(2, null, null)
  135. @MongoManager.insertDoc = sinon.stub().callsArg(3)
  136. @DocManager.deleteDoc @project_id, @doc_id, @callback
  137. it "should not try to insert a deleted doc", ->
  138. @MongoManager.insertDoc.called.should.equal false
  139. it "should return a NotFoundError", ->
  140. @callback
  141. .calledWith(new Errors.NotFoundError("No such doc: #{@doc_id}"))
  142. .should.equal true
  143. describe "updateDoc", ->
  144. beforeEach ->
  145. @oldDocLines = ["old", "doc", "lines"]
  146. @newDocLines = ["new", "doc", "lines"]
  147. @doc = { _id: @doc_id, lines: @oldDocLines, rev: @rev = 5 }
  148. @mongoPath = "mock.mongo.path"
  149. @MongoManager.updateDoc = sinon.stub().callsArg(3)
  150. describe "when the doc lines have changed", ->
  151. beforeEach ->
  152. @DocManager.getDoc = sinon.stub().callsArgWith(2, null, @doc, @mongoPath)
  153. @DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback
  154. it "should get the existing doc", ->
  155. @DocManager.getDoc
  156. .calledWith(@project_id, @doc_id)
  157. .should.equal true
  158. it "should update the doc with the new doc lines", ->
  159. @MongoManager.updateDoc
  160. .calledWith(@project_id, @mongoPath, @newDocLines)
  161. .should.equal true
  162. it "should log out the old and new doc lines", ->
  163. @logger.log
  164. .calledWith(
  165. project_id: @project_id
  166. doc_id: @doc_id
  167. oldDocLines: @oldDocLines
  168. newDocLines: @newDocLines
  169. rev: @doc.rev
  170. "updating doc lines"
  171. )
  172. .should.equal true
  173. it "should return the callback with the new rev", ->
  174. @callback.calledWith(null, true, @rev + 1).should.equal true
  175. describe "when the doc lines have not changed", ->
  176. beforeEach ->
  177. @DocManager.getDoc = sinon.stub().callsArgWith(2, null, @doc, @mongoPath)
  178. @DocManager.updateDoc @project_id, @doc_id, @oldDocLines.slice(), @callback
  179. it "should not update the doc", ->
  180. @MongoManager.updateDoc.called.should.equal false
  181. it "should return the callback with the existing rev", ->
  182. @callback.calledWith(null, false, @rev).should.equal true
  183. describe "when the doc does not exist", ->
  184. beforeEach ->
  185. @DocManager.getDoc = sinon.stub().callsArgWith(2, null, null, null)
  186. @DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback
  187. it "should not try to update the doc", ->
  188. @MongoManager.updateDoc.called.should.equal false
  189. it "should return a NotFoundError", ->
  190. @callback
  191. .calledWith(new Errors.NotFoundError("No such project/doc: #{@project_id}/#{@doc_id}"))
  192. .should.equal true
  193. describe "findDocInProject", ->
  194. it "should find the doc when it is in the root folder", (done) ->
  195. @DocManager.findDocInProject {
  196. rootFolder: [{
  197. docs: [{
  198. _id: ObjectId(@doc_id)
  199. }]
  200. }]
  201. }, @doc_id, (error, doc, mongoPath) =>
  202. expect(doc).to.deep.equal { _id: ObjectId(@doc_id) }
  203. mongoPath.should.equal "rootFolder.0.docs.0"
  204. done()
  205. it "should find the doc when it is in a sub folder", (done) ->
  206. @DocManager.findDocInProject {
  207. rootFolder: [{
  208. folders: [{
  209. docs: [{
  210. _id: ObjectId(@doc_id)
  211. }]
  212. }]
  213. }]
  214. }, @doc_id, (error, doc, mongoPath) =>
  215. expect(doc).to.deep.equal { _id: ObjectId(@doc_id) }
  216. mongoPath.should.equal "rootFolder.0.folders.0.docs.0"
  217. done()
  218. it "should find the doc when it there are other docs", (done) ->
  219. @DocManager.findDocInProject {
  220. rootFolder: [{
  221. folders: [{
  222. docs: [{
  223. _id: ObjectId()
  224. }]
  225. }, {
  226. docs: [{
  227. _id: ObjectId()
  228. }, {
  229. _id: ObjectId(@doc_id)
  230. }]
  231. }],
  232. docs: [{
  233. _id: ObjectId()
  234. }]
  235. }]
  236. }, @doc_id, (error, doc, mongoPath) =>
  237. expect(doc).to.deep.equal { _id: ObjectId(@doc_id) }
  238. mongoPath.should.equal "rootFolder.0.folders.1.docs.1"
  239. done()
  240. it "should return null when the doc doesn't exist", (done) ->
  241. @DocManager.findDocInProject {
  242. rootFolder: [{
  243. folders: [{
  244. docs: []
  245. }]
  246. }]
  247. }, @doc_id, (error, doc, mongoPath) =>
  248. expect(doc).to.be.null
  249. expect(mongoPath).to.be.null
  250. done()
  251. describe "findAllDocsInProject", ->
  252. it "should return all the docs", (done) ->
  253. @DocManager.findAllDocsInProject {
  254. rootFolder: [{
  255. docs: [
  256. @doc1 = { _id: ObjectId() }
  257. ],
  258. folders: [{
  259. docs: [
  260. @doc2 = { _id: ObjectId() }
  261. ]
  262. }, {
  263. docs: [
  264. @doc3 = { _id: ObjectId() }
  265. @doc4 = { _id: ObjectId() }
  266. ]
  267. }]
  268. }]
  269. }, (error, docs) =>
  270. expect(docs).to.deep.equal [@doc1, @doc2, @doc3, @doc4]
  271. done()