ProjectEntityHandlerTests.coffee 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. chai = require('chai')
  2. assert = require('chai').assert
  3. should = chai.should()
  4. expect = chai.expect
  5. sinon = require 'sinon'
  6. modulePath = "../../../../app/js/Features/Project/ProjectEntityHandler"
  7. SandboxedModule = require('sandboxed-module')
  8. ObjectId = require("mongoose").Types.ObjectId
  9. Errors = require "../../../../app/js/Features/Errors/Errors"
  10. describe 'ProjectEntityHandler', ->
  11. project_id = '4eecb1c1bffa66588e0000a1'
  12. doc_id = '4eecb1c1bffa66588e0000a2'
  13. folder_id = "4eecaffcbffa66588e000008"
  14. rootFolderId = "4eecaffcbffa66588e000007"
  15. userId = 1234
  16. beforeEach ->
  17. @TpdsUpdateSender =
  18. addDoc:sinon.stub().callsArg(1)
  19. addFile:sinon.stub().callsArg(1)
  20. @ProjectModel = class Project
  21. constructor:(options)->
  22. @._id = project_id
  23. @name = "project_name_here"
  24. @rev = 0
  25. rootFolder:[@rootFolder]
  26. @project = new @ProjectModel()
  27. @ProjectLocator =
  28. findElement : sinon.stub()
  29. @DocumentUpdaterHandler =
  30. updateProjectStructure: sinon.stub().yields()
  31. @callback = sinon.stub()
  32. @ProjectEntityHandler = SandboxedModule.require modulePath, requires:
  33. 'logger-sharelatex': @logger = {log:sinon.stub(), error: sinon.stub(), err:->}
  34. '../Docstore/DocstoreManager': @DocstoreManager = {}
  35. '../../Features/DocumentUpdater/DocumentUpdaterHandler':@DocumentUpdaterHandler
  36. '../../models/Project': Project:@ProjectModel
  37. './ProjectLocator': @ProjectLocator
  38. "./ProjectGetter": @ProjectGetter = {}
  39. '../ThirdPartyDataStore/TpdsUpdateSender':@TpdsUpdateSender
  40. describe "getting folders, docs and files", ->
  41. beforeEach ->
  42. @project.rootFolder = [
  43. docs: [@doc1 = {
  44. name : "doc1"
  45. _id : "doc1_id"
  46. }]
  47. fileRefs: [@file1 = {
  48. rev : 1
  49. _id : "file1_id"
  50. name : "file1"
  51. }]
  52. folders: [@folder1 = {
  53. name : "folder1"
  54. docs : [@doc2 = {
  55. name : "doc2"
  56. _id : "doc2_id"
  57. }]
  58. fileRefs : [@file2 = {
  59. rev : 2
  60. name : "file2"
  61. _id : "file2_id"
  62. }]
  63. folders : []
  64. }]
  65. ]
  66. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().yields(null, @project)
  67. describe "getAllDocs", ->
  68. beforeEach ->
  69. @docs = [{
  70. _id: @doc1._id
  71. lines: @lines1 = ["one"]
  72. rev: @rev1 = 1
  73. }, {
  74. _id: @doc2._id
  75. lines: @lines2 = ["two"]
  76. rev: @rev2 = 2
  77. }]
  78. @DocstoreManager.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  79. @ProjectEntityHandler.getAllDocs project_id, @callback
  80. it "should get the doc lines and rev from the docstore", ->
  81. @DocstoreManager.getAllDocs
  82. .calledWith(project_id)
  83. .should.equal true
  84. it "should call the callback with the docs with the lines and rev included", ->
  85. @callback
  86. .calledWith(null, {
  87. "/doc1": {
  88. _id: @doc1._id
  89. lines: @lines1
  90. name: @doc1.name
  91. rev: @rev1
  92. }
  93. "/folder1/doc2": {
  94. _id: @doc2._id
  95. lines: @lines2
  96. name: @doc2.name
  97. rev: @rev2
  98. }
  99. })
  100. .should.equal true
  101. describe "getAllFiles", ->
  102. beforeEach ->
  103. @callback = sinon.stub()
  104. @ProjectEntityHandler.getAllFiles project_id, @callback
  105. it "should call the callback with the files", ->
  106. @callback
  107. .calledWith(null, {
  108. "/file1": @file1
  109. "/folder1/file2": @file2
  110. })
  111. .should.equal true
  112. describe "getAllDocPathsFromProject", ->
  113. beforeEach ->
  114. @docs = [{
  115. _id: @doc1._id
  116. lines: @lines1 = ["one"]
  117. rev: @rev1 = 1
  118. }, {
  119. _id: @doc2._id
  120. lines: @lines2 = ["two"]
  121. rev: @rev2 = 2
  122. }]
  123. @callback = sinon.stub()
  124. @ProjectEntityHandler.getAllDocPathsFromProject @project, @callback
  125. it "should call the callback with the path for each doc_id", ->
  126. @expected = {}
  127. @expected[@doc1._id] = "/#{@doc1.name}"
  128. @expected[@doc2._id] = "/folder1/#{@doc2.name}"
  129. @callback
  130. .calledWith(null, @expected)
  131. .should.equal true
  132. describe "_getAllFolders", ->
  133. beforeEach ->
  134. @callback = sinon.stub()
  135. @ProjectEntityHandler._getAllFolders project_id, @callback
  136. it "should get the project without the docs lines", ->
  137. @ProjectGetter.getProjectWithoutDocLines
  138. .calledWith(project_id)
  139. .should.equal true
  140. it "should call the callback with the folders", ->
  141. @callback
  142. .calledWith(null, {
  143. "/": @project.rootFolder[0]
  144. "/folder1": @folder1
  145. })
  146. .should.equal true
  147. describe "_getAllFoldersFromProject", ->
  148. beforeEach ->
  149. @callback = sinon.stub()
  150. @ProjectEntityHandler._getAllFoldersFromProject @project, @callback
  151. it "should call the callback with the folders", ->
  152. @callback
  153. .calledWith(null, {
  154. "/": @project.rootFolder[0]
  155. "/folder1": @folder1
  156. })
  157. .should.equal true
  158. describe "flushProjectToThirdPartyDataStore", ->
  159. beforeEach (done) ->
  160. @project = {
  161. _id: project_id
  162. name: "Mock project name"
  163. }
  164. @DocumentUpdaterHandler.flushProjectToMongo = sinon.stub().yields()
  165. @docs = {
  166. "/doc/one": @doc1 = { _id: "mock-doc-1", lines: ["one"], rev: 5 }
  167. "/doc/two": @doc2 = { _id: "mock-doc-2", lines: ["two"], rev: 6 }
  168. }
  169. @files = {
  170. "/file/one": @file1 = { _id: "mock-file-1", rev: 7 }
  171. "/file/two": @file2 = { _id: "mock-file-2", rev: 8 }
  172. }
  173. @ProjectEntityHandler.getAllDocs = sinon.stub().yields(null, @docs)
  174. @ProjectEntityHandler.getAllFiles = sinon.stub().yields(null, @files)
  175. @ProjectGetter.getProject = sinon.stub().yields(null, @project)
  176. @ProjectEntityHandler.flushProjectToThirdPartyDataStore project_id, () -> done()
  177. it "should flush the project from the doc updater", ->
  178. @DocumentUpdaterHandler.flushProjectToMongo.calledWith(project_id).should.equal true
  179. it "should look up the project in mongo", ->
  180. @ProjectGetter.getProject.calledWith(project_id).should.equal true
  181. it "should get all the docs in the project", ->
  182. @ProjectEntityHandler.getAllDocs.calledWith(project_id).should.equal true
  183. it "should get all the files in the project", ->
  184. @ProjectEntityHandler.getAllFiles.calledWith(project_id).should.equal true
  185. it "should flush each doc to the TPDS", ->
  186. for path, doc of @docs
  187. @TpdsUpdateSender.addDoc
  188. .calledWith({
  189. project_id: project_id,
  190. doc_id: doc._id
  191. project_name: @project.name
  192. rev: doc.rev
  193. path: path
  194. })
  195. .should.equal true
  196. it "should flush each file to the TPDS", ->
  197. for path, file of @files
  198. @TpdsUpdateSender.addFile
  199. .calledWith({
  200. project_id: project_id,
  201. file_id: file._id
  202. project_name: @project.name
  203. rev: file.rev
  204. path: path
  205. })
  206. .should.equal true
  207. describe 'getDoc', ->
  208. beforeEach ->
  209. @lines = ["mock", "doc", "lines"]
  210. @rev = 5
  211. @version = 42
  212. @ranges = {"mock": "ranges"}
  213. @DocstoreManager.getDoc = sinon.stub().callsArgWith(3, null, @lines, @rev, @version, @ranges)
  214. @ProjectEntityHandler.getDoc project_id, doc_id, @callback
  215. it "should call the docstore", ->
  216. @DocstoreManager.getDoc
  217. .calledWith(project_id, doc_id)
  218. .should.equal true
  219. it "should call the callback with the lines, version and rev", ->
  220. @callback.calledWith(null, @lines, @rev, @version, @ranges).should.equal true