DocManagerTests.coffee 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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", ->
  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 does not exist", ->
  48. beforeEach ->
  49. @project = { name: "mock-project" }
  50. @MongoManager.findProject = sinon.stub().callsArgWith(1, null, @project)
  51. @DocManager.findDocInProject = sinon.stub().callsArgWith(2, null, null, null)
  52. @DocManager.getDoc @project_id, @doc_id, @callback
  53. it "should try to find the doc in the project", ->
  54. @DocManager.findDocInProject
  55. .calledWith(@project, @doc_id)
  56. .should.equal true
  57. it "should return a NotFoundError", ->
  58. @callback
  59. .calledWith(new Errors.NotFoundError("No such doc: #{@doc_id}"))
  60. .should.equal true
  61. describe "getAllDocs", ->
  62. describe "when the project exists", ->
  63. beforeEach ->
  64. @project = { name: "mock-project" }
  65. @docs = [{ _id: @doc_id, lines: ["mock-lines"] }]
  66. @mongoPath = "mock.mongo.path"
  67. @MongoManager.findProject = sinon.stub().callsArgWith(1, null, @project)
  68. @DocManager.findAllDocsInProject = sinon.stub().callsArgWith(1, null, @docs)
  69. @DocManager.getAllDocs @project_id, @callback
  70. it "should get the project from the database", ->
  71. @MongoManager.findProject
  72. .calledWith(@project_id)
  73. .should.equal true
  74. it "should find all the docs in the project", ->
  75. @DocManager.findAllDocsInProject
  76. .calledWith(@project)
  77. .should.equal true
  78. it "should return the docs", ->
  79. @callback.calledWith(null, @docs).should.equal true
  80. describe "when the project does not exist", ->
  81. beforeEach ->
  82. @MongoManager.findProject = sinon.stub().callsArgWith(1, null, null)
  83. @DocManager.findAllDocsInProject = sinon.stub()
  84. @DocManager.getDoc @project_id, @doc_id, @callback
  85. it "should not try to find the doc in the project", ->
  86. @DocManager.findAllDocsInProject.called.should.equal false
  87. it "should return a NotFoundError", ->
  88. @callback
  89. .calledWith(new Errors.NotFoundError("No such project: #{@project_id}"))
  90. .should.equal true
  91. describe "deleteDoc", ->
  92. describe "when the doc exists", ->
  93. beforeEach ->
  94. @lines = ["mock", "doc", "lines"]
  95. @DocManager.getDoc = sinon.stub().callsArgWith(2, null, lines: @lines)
  96. @MongoManager.insertDoc = sinon.stub().callsArg(3)
  97. @DocManager.deleteDoc @project_id, @doc_id, @callback
  98. it "should get the doc", ->
  99. @DocManager.getDoc
  100. .calledWith(@project_id, @doc_id)
  101. .should.equal true
  102. it "insert the doc as a deleted doc", ->
  103. @MongoManager.insertDoc
  104. .calledWith(@project_id, @doc_id, {
  105. lines: @lines
  106. deleted: true
  107. })
  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.insertDoc = sinon.stub().callsArg(3)
  115. @DocManager.deleteDoc @project_id, @doc_id, @callback
  116. it "should not try to insert a deleted doc", ->
  117. @MongoManager.insertDoc.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: 42 }
  127. @mongoPath = "mock.mongo.path"
  128. @MongoManager.updateDoc = sinon.stub().callsArg(3)
  129. describe "when the doc lines have changed", ->
  130. beforeEach ->
  131. @DocManager.getDoc = sinon.stub().callsArgWith(2, null, @doc, @mongoPath)
  132. @DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback
  133. it "should get the existing doc", ->
  134. @DocManager.getDoc
  135. .calledWith(@project_id, @doc_id)
  136. .should.equal true
  137. it "should update the doc with the new doc lines", ->
  138. @MongoManager.updateDoc
  139. .calledWith(@project_id, @mongoPath, @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", ->
  153. @callback.calledWith(null, true).should.equal true
  154. describe "when the doc lines have not changed", ->
  155. beforeEach ->
  156. @DocManager.getDoc = sinon.stub().callsArgWith(2, null, @doc, @mongoPath)
  157. @DocManager.updateDoc @project_id, @doc_id, @oldDocLines.slice(), @callback
  158. it "should not update the doc", ->
  159. @MongoManager.updateDoc.called.should.equal false
  160. it "should return the callback", ->
  161. @callback.calledWith(null, false).should.equal true
  162. describe "when the doc does not exist", ->
  163. beforeEach ->
  164. @DocManager.getDoc = sinon.stub().callsArgWith(2, null, null, null)
  165. @DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback
  166. it "should not try to update the doc", ->
  167. @MongoManager.updateDoc.called.should.equal false
  168. it "should return a NotFoundError", ->
  169. @callback
  170. .calledWith(new Errors.NotFoundError("No such project/doc: #{@project_id}/#{@doc_id}"))
  171. .should.equal true
  172. describe "findDocInProject", ->
  173. it "should find the doc when it is in the root folder", (done) ->
  174. @DocManager.findDocInProject {
  175. rootFolder: [{
  176. docs: [{
  177. _id: ObjectId(@doc_id)
  178. }]
  179. }]
  180. }, @doc_id, (error, doc, mongoPath) =>
  181. expect(doc).to.deep.equal { _id: ObjectId(@doc_id) }
  182. mongoPath.should.equal "rootFolder.0.docs.0"
  183. done()
  184. it "should find the doc when it is in a sub folder", (done) ->
  185. @DocManager.findDocInProject {
  186. rootFolder: [{
  187. folders: [{
  188. docs: [{
  189. _id: ObjectId(@doc_id)
  190. }]
  191. }]
  192. }]
  193. }, @doc_id, (error, doc, mongoPath) =>
  194. expect(doc).to.deep.equal { _id: ObjectId(@doc_id) }
  195. mongoPath.should.equal "rootFolder.0.folders.0.docs.0"
  196. done()
  197. it "should find the doc when it there are other docs", (done) ->
  198. @DocManager.findDocInProject {
  199. rootFolder: [{
  200. folders: [{
  201. docs: [{
  202. _id: ObjectId()
  203. }]
  204. }, {
  205. docs: [{
  206. _id: ObjectId()
  207. }, {
  208. _id: ObjectId(@doc_id)
  209. }]
  210. }],
  211. docs: [{
  212. _id: ObjectId()
  213. }]
  214. }]
  215. }, @doc_id, (error, doc, mongoPath) =>
  216. expect(doc).to.deep.equal { _id: ObjectId(@doc_id) }
  217. mongoPath.should.equal "rootFolder.0.folders.1.docs.1"
  218. done()
  219. it "should return null when the doc doesn't exist", (done) ->
  220. @DocManager.findDocInProject {
  221. rootFolder: [{
  222. folders: [{
  223. docs: []
  224. }]
  225. }]
  226. }, @doc_id, (error, doc, mongoPath) =>
  227. expect(doc).to.be.null
  228. expect(mongoPath).to.be.null
  229. done()
  230. describe "findAllDocsInProject", ->
  231. it "should return all the docs", (done) ->
  232. @DocManager.findAllDocsInProject {
  233. rootFolder: [{
  234. docs: [
  235. @doc1 = { _id: ObjectId() }
  236. ],
  237. folders: [{
  238. docs: [
  239. @doc2 = { _id: ObjectId() }
  240. ]
  241. }, {
  242. docs: [
  243. @doc3 = { _id: ObjectId() }
  244. @doc4 = { _id: ObjectId() }
  245. ]
  246. }]
  247. }]
  248. }, (error, docs) =>
  249. expect(docs).to.deep.equal [@doc1, @doc2, @doc3, @doc4]
  250. done()