ProjectEntityHandlerTests.coffee 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. chai = require('chai')
  2. assert = require('chai').assert
  3. should = chai.should()
  4. sinon = require 'sinon'
  5. modulePath = "../../../../app/js/Features/Project/ProjectEntityHandler"
  6. SandboxedModule = require('sandboxed-module')
  7. ObjectId = require("mongoose").Types.ObjectId
  8. tk = require 'timekeeper'
  9. describe 'project entity handler', ->
  10. project_id = '4eecb1c1bffa66588e0000a1'
  11. folder_id = "4eecaffcbffa66588e000008"
  12. rootFolderId = "4eecaffcbffa66588e000007"
  13. beforeEach ->
  14. @FileStoreHandler =
  15. uploadFileFromDisk:(project_id, fileRef, localImagePath, callback)->callback()
  16. copyFile: sinon.stub().callsArgWith(4, null)
  17. @tpdsUpdateSender =
  18. addDoc:sinon.stub().callsArg(2)
  19. addFile:sinon.stub().callsArg(2)
  20. addFolder:sinon.stub().callsArg(2)
  21. @rootFolder =
  22. _id:rootFolderId,
  23. folders:[
  24. {name:"level1", folders:[]}
  25. ]
  26. @ProjectModel = class Project
  27. constructor:(options)->
  28. @._id = project_id
  29. @name = "project_name_here"
  30. @rev = 0
  31. save:(callback)->callback()
  32. rootFolder:[@rootFolder]
  33. @DocModel = class Doc
  34. constructor:(options)->
  35. {@name, @lines} = options
  36. @rev = 0
  37. @FileModel = class File
  38. constructor:(options)->
  39. {@name} = options
  40. @._id = "file_id"
  41. @rev = 0
  42. @FolderModel = class Folder
  43. constructor:(options)->
  44. {@name} = options
  45. @project = new @ProjectModel()
  46. @project.rootFolder = [@rootFolder]
  47. @ProjectModel.findById = (project_id, callback)=> callback(null, @project)
  48. @ProjectModel.getProject = (project_id, fields, callback)=> callback(null, @project)
  49. @ProjectModel.putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:"somehintg"}})
  50. @projectUpdater = markAsUpdated:sinon.stub()
  51. @ProjectEntityHandler = SandboxedModule.require modulePath, requires:
  52. '../../models/Project': Project:@ProjectModel
  53. '../../models/Doc': Doc:@DocModel
  54. '../../models/Folder': Folder:@FolderModel
  55. '../../models/File': File:@FileModel
  56. '../FileStore/FileStoreHandler':@FileStoreHandler
  57. '../ThirdPartyDataStore/TpdsUpdateSender':@tpdsUpdateSender
  58. './ProjectLocator':@projectLocator = {}
  59. '../../Features/DocumentUpdater/DocumentUpdaterHandler':@documentUpdaterHandler = {}
  60. '../Docstore/DocstoreManager': @DocstoreManager = {}
  61. 'logger-sharelatex':{log:->}
  62. './ProjectUpdateHandler': @projectUpdater
  63. describe 'mkdirp', ->
  64. beforeEach ->
  65. @parentFolder_id = "1jnjknjk"
  66. @newFolder = {_id:"newFolder_id_here"}
  67. @lastFolder = {_id:"123das", folders:[]}
  68. @projectLocator.findElementByPath = (project_id, path, cb)=>
  69. @parentFolder = {_id:"parentFolder_id_here"}
  70. lastFolder = path.substring(path.lastIndexOf("/"))
  71. if lastFolder.indexOf("level1") == -1
  72. cb "level1 is not the last foler "
  73. else
  74. cb null, @parentFolder
  75. @ProjectEntityHandler.addFolder = (project_id, parentFolder_id, folderName, sl_req_id, callback)=>
  76. callback null, {name:folderName}, @parentFolder_id
  77. it 'should return the root folder if the path is just a slash', (done)->
  78. path = "/"
  79. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  80. lastFolder.should.deep.equal @rootFolder
  81. assert.equal lastFolder.parentFolder_id, undefined
  82. done()
  83. it 'should make just one folder', (done)->
  84. path = "/differentFolder/"
  85. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  86. folders.length.should.equal 1
  87. lastFolder.name.should.equal "differentFolder"
  88. lastFolder.parentFolder_id.should.equal @parentFolder_id
  89. done()
  90. it 'should make the final folder in path if it doesnt exist with one level', (done)->
  91. path = "level1/level2"
  92. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  93. folders.length.should.equal 1
  94. lastFolder.name.should.equal "level2"
  95. lastFolder.parentFolder_id.should.equal @parentFolder_id
  96. done()
  97. it 'should make the final folder in path if it doesnt exist with mutliple levels', (done)->
  98. path = "level1/level2/level3"
  99. @ProjectEntityHandler.mkdirp project_id, path,(err, folders, lastFolder) =>
  100. folders.length.should.equal 2
  101. folders[0].name.should.equal "level2"
  102. folders[0].parentFolder_id.should.equal @parentFolder_id
  103. lastFolder.name.should.equal "level3"
  104. lastFolder.parentFolder_id.should.equal @parentFolder_id
  105. done()
  106. it 'should work with slashes either side', (done)->
  107. path = "/level1/level2/level3/"
  108. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  109. folders.length.should.equal 2
  110. folders[0].name.should.equal "level2"
  111. folders[0].parentFolder_id.should.equal @parentFolder_id
  112. lastFolder.name.should.equal "level3"
  113. lastFolder.parentFolder_id.should.equal @parentFolder_id
  114. done()
  115. describe 'deleting an element', ->
  116. entity_id = "4eecaffcbffa66588e000009"
  117. beforeEach ->
  118. @tpdsUpdateSender.deleteEntity = sinon.stub().callsArg(2)
  119. @ProjectEntityHandler._removeElementFromMongoArray = sinon.stub().callsArg(3)
  120. @ProjectEntityHandler._cleanUpEntity = sinon.stub().callsArg(3)
  121. @path = mongo: "mongo.path", fileSystem: "/file/system/path"
  122. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: entity_id }, @path)
  123. describe "deleting from Mongo", ->
  124. beforeEach (done) ->
  125. @ProjectEntityHandler.deleteEntity project_id, entity_id, @type = 'file', done
  126. it "should retreive the path", ->
  127. @projectLocator.findElement.called.should.equal true
  128. options = @projectLocator.findElement.args[0][0]
  129. options.type.should.equal @type
  130. options.project.should.equal @project
  131. options.element_id.should.equal entity_id
  132. it "should remove the element from the database", ->
  133. @ProjectEntityHandler._removeElementFromMongoArray.calledWith(@ProjectModel, project_id, @path.mongo).should.equal true
  134. it "should call the third party data store", ->
  135. options = @tpdsUpdateSender.deleteEntity.args[0][0]
  136. options.project_id.should.equal project_id
  137. options.path.should.equal @path.fileSystem
  138. it "should clean up the entity from the rest of the system", ->
  139. @ProjectEntityHandler._cleanUpEntity
  140. .calledWith(@project, @entity, @type)
  141. .should.equal true
  142. describe "_cleanUpEntity", ->
  143. beforeEach ->
  144. @entity_id = "4eecaffcbffa66588e000009"
  145. @documentUpdaterHandler.deleteDoc = sinon.stub().callsArg(2)
  146. @FileStoreHandler.deleteFile = sinon.stub().callsArg(2)
  147. @ProjectEntityHandler.unsetRootDoc = sinon.stub().callsArg(1)
  148. describe "a file", ->
  149. beforeEach (done) ->
  150. @ProjectEntityHandler._cleanUpEntity @project, _id: @entity_id, 'file', done
  151. it "should delete the file from FileStoreHandler", ->
  152. @FileStoreHandler.deleteFile.calledWith(project_id, @entity_id).should.equal true
  153. it "should not attempt to delete from the document updater", ->
  154. @documentUpdaterHandler.deleteDoc.called.should.equal false
  155. describe "a doc", ->
  156. beforeEach (done) ->
  157. @ProjectEntityHandler._cleanUpEntity @project, _id: @entity_id, 'doc', done
  158. it "should not attempted to delete from FileStoreHandler", ->
  159. @FileStoreHandler.deleteFile.called.should.equal false
  160. it "should delete the doc from the document updater", ->
  161. @documentUpdaterHandler.deleteDoc.calledWith(project_id, @entity_id).should.equal true
  162. describe "when the entity is the root document", ->
  163. beforeEach (done) ->
  164. @project.rootDoc_id = new ObjectId(@entity_id)
  165. @ProjectEntityHandler._cleanUpEntity @project, _id: @entity_id, 'doc', done
  166. it "should unset the root doc id", ->
  167. @ProjectEntityHandler.unsetRootDoc.calledWith(project_id).should.equal true
  168. describe "a folder", ->
  169. beforeEach (done) ->
  170. @folder =
  171. folders: [
  172. fileRefs: [ @file1 = {_id: "file-id-1" } ]
  173. docs: [ @doc1 = { _id: "doc-id-1" } ]
  174. folders: []
  175. ]
  176. fileRefs: [ @file2 = { _id: "file-id-2" } ]
  177. docs: [ @doc2 = { _id: "doc-id-2" } ]
  178. @ProjectEntityHandler._cleanUpDoc = sinon.stub().callsArg(3)
  179. @ProjectEntityHandler._cleanUpFile = sinon.stub().callsArg(3)
  180. @ProjectEntityHandler._cleanUpEntity @project, @folder, "folder", done
  181. it "should clean up all sub files", ->
  182. @ProjectEntityHandler._cleanUpFile.calledWith(@project, @file1).should.equal true
  183. @ProjectEntityHandler._cleanUpFile.calledWith(@project, @file2).should.equal true
  184. it "should clean up all sub docs", ->
  185. @ProjectEntityHandler._cleanUpDoc.calledWith(@project, @doc1).should.equal true
  186. @ProjectEntityHandler._cleanUpDoc.calledWith(@project, @doc2).should.equal true
  187. describe 'moving an element', ->
  188. beforeEach ->
  189. @docId = "4eecaffcbffa66588e000009"
  190. @doc = {lines:["1234","312343d"], rev: "1234"}
  191. it 'should find the project then element', (done)->
  192. @projectLocator.findElement = (options, callback)=>
  193. options.element_id.should.equal @docId
  194. options.type.should.equal 'docs'
  195. done()
  196. @ProjectEntityHandler.moveEntity project_id, @docId, folder_id, "docs", ->
  197. it 'should remove the element then add it back in', (done)->
  198. path = {mongo:"folders[0]"}
  199. @projectLocator.findElement = (opts, callback)=>
  200. callback(null, @doc, path)
  201. @ProjectEntityHandler._removeElementFromMongoArray = (model, model_id, path, callback)-> callback()
  202. @ProjectModel.putElement = (passedProject_id, destinationFolder_id, entity, entityType, callback)=>
  203. passedProject_id.should.equal project_id
  204. destinationFolder_id.should.equal folder_id
  205. entity.should.deep.equal @doc
  206. entityType.should.equal 'docs'
  207. done()
  208. @ProjectEntityHandler.moveEntity project_id, @docId, folder_id, "docs", ->
  209. it 'should tell the third party data store', (done)->
  210. startPath = {fileSystem:"/somewhere.txt"}
  211. endPath = {fileSystem:"/somewhere.txt"}
  212. @projectLocator.findElement = (opts, callback)=>
  213. callback(null, @doc, startPath)
  214. @ProjectEntityHandler._removeElementFromMongoArray = (model, model_id, path, callback)-> callback()
  215. @ProjectModel.putElement = (passedProject_id, destinationFolder_id, entity, entityType, callback)->
  216. callback null, path:endPath
  217. @tpdsUpdateSender.moveEntity = (opts)=>
  218. opts.project_id.should.equal project_id
  219. opts.startPath.should.equal startPath.fileSystem
  220. opts.endPath.should.equal endPath.fileSystem
  221. opts.project_name.should.equal @project.name
  222. opts.rev.should.equal @doc.rev
  223. done()
  224. @ProjectEntityHandler.moveEntity project_id, @docId, folder_id, "docs", ->
  225. describe 'removing element from mongo array', ->
  226. it 'should call update with log the path', (done)->
  227. mongoPath = "folders[0].folders[5]"
  228. id = "12344"
  229. firstUpdate = true
  230. model =
  231. update: (conditions, update, opts, callback)->
  232. if firstUpdate
  233. conditions._id.should.equal id
  234. update.$unset[mongoPath].should.equal 1
  235. firstUpdate = false
  236. callback()
  237. else
  238. conditions._id.should.equal id
  239. assert.deepEqual update, { '$pull': { 'folders[0]': null } }
  240. done()
  241. @ProjectEntityHandler._removeElementFromMongoArray model, id, mongoPath, ->
  242. describe 'adding doc', ->
  243. docName = "some new doc"
  244. docLines = ['1234','abc']
  245. it 'should call put element', (done)->
  246. @ProjectModel.putElement = (passedProject_id, passedFolder_id, passedDoc, passedType, callback)->
  247. passedProject_id.should.equal project_id
  248. passedFolder_id.should.equal folder_id
  249. passedDoc.name.should.equal docName
  250. passedDoc.lines[0].should.equal docLines[0]
  251. passedDoc.lines[1].should.equal docLines[1]
  252. passedType.should.equal 'doc'
  253. done()
  254. @ProjectEntityHandler.addDoc project_id, folder_id, docName, docLines, "", (err, doc, parentFolder)->
  255. it 'should return doc and parent folder', (done)->
  256. @ProjectEntityHandler.addDoc project_id, folder_id, docName, docLines, "", (err, doc, parentFolder)->
  257. parentFolder.should.equal folder_id
  258. doc.name.should.equal docName
  259. done()
  260. it 'should call third party data store', (done)->
  261. fileSystemPath = "/somehwere/#{docName}"
  262. @ProjectModel.putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:fileSystemPath}})
  263. @tpdsUpdateSender.addDoc = (options)=>
  264. options.project_id.should.equal project_id
  265. options.docLines.should.equal docLines
  266. options.path.should.equal fileSystemPath
  267. options.project_name.should.equal @project.name
  268. options.rev.should.equal 0
  269. done()
  270. @ProjectEntityHandler.addDoc project_id, folder_id, docName, docLines, "",->
  271. describe 'adding file', ->
  272. fileName = "something.jpg"
  273. beforeEach ->
  274. @filePath = "somewhere"
  275. it 'should upload it via the FileStoreHandler', (done)->
  276. @FileStoreHandler.uploadFileFromDisk = (passedProject_id, file_id, filePath, callback)=>
  277. file_id.should.equal "file_id"
  278. passedProject_id.should.equal project_id
  279. filePath.should.equal @filePath
  280. done()
  281. @ProjectEntityHandler.addFile project_id, folder_id, fileName, @filePath, (err, fileRef, parentFolder)->
  282. it 'should put file into folder by calling put element', (done)->
  283. @ProjectModel.putElement = (passedProject_id, passedFolder_id, passedFileRef, passedType, callback)->
  284. passedProject_id.should.equal project_id
  285. passedFolder_id.should.equal folder_id
  286. passedFileRef.name.should.equal fileName
  287. passedType.should.equal 'file'
  288. done()
  289. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, (err, fileRef, parentFolder)->
  290. it 'should return doc and parent folder', (done)->
  291. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, (err, fileRef, parentFolder)->
  292. parentFolder.should.equal folder_id
  293. fileRef.name.should.equal fileName
  294. done()
  295. it 'should call third party data store', (done)->
  296. @project.existsInVersioningApi = true
  297. opts =
  298. path : "/somehwere/idsadsds"
  299. project_id : project_id
  300. @ProjectModel.putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
  301. @tpdsUpdateSender.addFile = (options)=>
  302. options.project_id.should.equal project_id
  303. options.path.should.equal opts.path
  304. options.project_name.should.equal @project.name
  305. options.file_id.should.not.be.null
  306. options.rev.should.equal 0
  307. done()
  308. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, (err, fileRef, parentFolder)->
  309. describe 'replacing a file', ->
  310. beforeEach ->
  311. @projectLocator
  312. @file_id = "file_id_here"
  313. @fsPath = "fs_path_here.png"
  314. @fileRef = {rev:3, _id:@file_id}
  315. @filePaths = {fileSystem:"/folder1/file.png", mongo:"folder.1.files.somewhere"}
  316. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @fileRef, @filePaths)
  317. @ProjectModel.update = (_, __, ___, cb)-> cb()
  318. it 'should find the file', (done)->
  319. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  320. @projectLocator.findElement.calledWith({element_id:@file_id, type:"file", project_id:project_id}).should.equal true
  321. done()
  322. it 'should tell the file store handler to upload the file from disk', (done)->
  323. @FileStoreHandler.uploadFileFromDisk = sinon.stub().callsArgWith(3)
  324. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  325. @FileStoreHandler.uploadFileFromDisk.calledWith(project_id, @file_id, @fsPath).should.equal true
  326. done()
  327. it 'should send the file to the tpds with an incremented rev', (done)->
  328. @tpdsUpdateSender.addFile = (options)=>
  329. options.project_id.should.equal project_id
  330. options.path.should.equal @filePaths.fileSystem
  331. options.project_name.should.equal @project.name
  332. options.file_id.should.equal @file_id
  333. options.rev.should.equal @fileRef.rev + 1
  334. done()
  335. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  336. it 'should inc the rev id', (done)->
  337. @ProjectModel.update = (conditions, update, options, callback)=>
  338. conditions._id.should.equal project_id
  339. update.$inc["#{@filePaths.mongo}.rev"].should.equal 1
  340. done()
  341. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  342. it 'should update the created at date', (done)->
  343. d = new Date()
  344. @ProjectModel.update = (conditions, update, options, callback)=>
  345. conditions._id.should.equal project_id
  346. differenceInMs = update.$set["#{@filePaths.mongo}.created"].getTime() - d.getTime()
  347. differenceInMs.should.be.below(10)
  348. done()
  349. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  350. describe 'adding a folder', ->
  351. folderName = "folder1234"
  352. it 'should call put element', (done)->
  353. @ProjectModel.putElement = (passedProject_id, passedFolder_id, passedFolder, passedType, callback)->
  354. passedProject_id.should.equal project_id
  355. passedFolder_id.should.equal folder_id
  356. passedFolder.name.should.equal folderName
  357. passedType.should.equal 'folder'
  358. done()
  359. @ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
  360. it 'should return the folder and parent folder', (done)->
  361. @ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
  362. parentFolder.should.equal folder_id
  363. folder.name.should.equal folderName
  364. done()
  365. describe 'updating document lines', ->
  366. docId = "123456"
  367. docLines = ['1234','abc', '543543']
  368. mongoPath = "folders[0].folders[5]"
  369. fileSystemPath = "/somehwere/something.tex"
  370. it 'should find project via getProject', (done)->
  371. @ProjectModel.getProject = (passedProject_id, callback)->
  372. passedProject_id.should.equal project_id
  373. done()
  374. @ProjectEntityHandler.updateDocLines project_id, "", [], ->
  375. it 'should find the doc', (done)->
  376. @projectLocator.findElement = (options, callback)->
  377. options.element_id.should.equal docId
  378. options.type.should.equal 'docs'
  379. done()
  380. @ProjectEntityHandler.updateDocLines project_id, docId, "", ->
  381. it 'should build mongo update statment', (done)->
  382. @projectLocator.findElement = (opts, callback)->
  383. callback(null, {lines:[], rev:0}, {mongo:mongoPath})
  384. @ProjectModel.update = (conditions, update, options, callback)->
  385. conditions._id.should.equal project_id
  386. update.$set["#{mongoPath}.lines"].should.equal docLines
  387. update.$inc["#{mongoPath}.rev"].should.equal 1
  388. done()
  389. @ProjectEntityHandler.updateDocLines project_id, docId, docLines, ->
  390. it 'should call third party data store ', (done)->
  391. rev = 3
  392. @projectLocator.findElement = (opts, callback)->
  393. callback(null, {lines:[],rev:rev}, {fileSystem:fileSystemPath})
  394. @ProjectModel.update = (conditions, update, options, callback)-> callback()
  395. @tpdsUpdateSender.addDoc = (options, _, callback)=>
  396. options.project_id.should.equal project_id
  397. options.docLines.should.equal docLines
  398. options.path.should.equal fileSystemPath
  399. options.project_name.should.equal @project.name
  400. options.rev.should.equal (rev+1)
  401. callback()
  402. @projectUpdater.markAsUpdated.calledWith(project_id).should.equal true
  403. @ProjectEntityHandler.updateDocLines project_id, docId, docLines, done
  404. describe "flushing folders, docs and files", ->
  405. beforeEach ->
  406. @project.rootFolder = [
  407. folders: [{
  408. name : "folder1"
  409. folders : [{
  410. name : "folder2"
  411. folders : [{
  412. name:"folder4",
  413. docs:[{name:"doc3", rev:3,lines:["doc3"]}],
  414. fileRefs:[{_id:"file3_id", rev:3,name:"file3"}],
  415. folders:[]
  416. }]
  417. docs : [{
  418. rev:2
  419. name: "doc2"
  420. lines: ["doc2", "lines"]
  421. }]
  422. fileRefs : []
  423. }]
  424. docs : []
  425. fileRefs : [{
  426. rev:2
  427. name : "file2"
  428. _id : "file2_id"
  429. }]
  430. }, {
  431. name : "folder3"
  432. folders : []
  433. docs : []
  434. fileRefs : []
  435. }]
  436. docs: [{
  437. rev:1
  438. name: "doc1"
  439. lines: ["doc1", "lines"]
  440. }]
  441. fileRefs: [{
  442. rev:1
  443. _id : "file1_id"
  444. name : "file1"
  445. }]
  446. ]
  447. it "should work for a very small project", (done)->
  448. @project.rootFolder[0].folders = []
  449. @ProjectEntityHandler.getAllDocs project_id, (err, docs) =>
  450. docs["/doc1"].name.should.equal "doc1"
  451. @ProjectEntityHandler.getAllFiles project_id, (err, files) =>
  452. files["/file1"].name.should.equal "file1"
  453. done()
  454. it "should be able to get all folders", (done) ->
  455. @ProjectEntityHandler.getAllFolders project_id, (err, folders) ->
  456. should.exist folders["/"]
  457. should.exist folders["/folder1"]
  458. should.exist folders["/folder1/folder2"]
  459. should.exist folders["/folder1/folder2/folder4"]
  460. folders["/folder1/folder2/folder4"].name.should.equal "folder4"
  461. should.exist folders["/folder3"]
  462. done()
  463. it "should be able to get all docs", (done) ->
  464. @ProjectEntityHandler.getAllDocs project_id, (err, docs) ->
  465. docs["/doc1"].name.should.equal "doc1"
  466. docs["/folder1/folder2/doc2"].name.should.equal "doc2"
  467. docs["/folder1/folder2/folder4/doc3"].lines.should.deep.equal ["doc3"]
  468. done()
  469. it "should be able to get all files", (done) ->
  470. @ProjectEntityHandler.getAllFiles project_id, (err, files) ->
  471. files["/file1"].name.should.equal "file1"
  472. files["/folder1/file2"].name.should.equal "file2"
  473. files["/folder1/folder2/folder4/file3"].name.should.equal "file3"
  474. done()
  475. describe "flushProjectToThirdPartyDataStore", ->
  476. beforeEach (done) ->
  477. @addedDocs = {}
  478. @addedFiles = {}
  479. @tpdsUpdateSender.addDoc = (options, _, callback) =>
  480. callback()
  481. sinon.spy @tpdsUpdateSender, "addDoc"
  482. @tpdsUpdateSender.addFile = (options, _, callback) =>
  483. callback()
  484. sinon.spy @tpdsUpdateSender, "addFile"
  485. @documentUpdaterHandler.flushProjectToMongo = (project_id, sl_req_id, callback) ->
  486. callback()
  487. sinon.spy @documentUpdaterHandler, "flushProjectToMongo"
  488. @ProjectEntityHandler.flushProjectToThirdPartyDataStore project_id, (err) -> done()
  489. it "should flush the documents from the document updater", ->
  490. @documentUpdaterHandler.flushProjectToMongo
  491. .calledWith(@project._id).should.equal true
  492. @documentUpdaterHandler.flushProjectToMongo
  493. .calledBefore(@tpdsUpdateSender.addDoc).should.equal true
  494. @documentUpdaterHandler.flushProjectToMongo
  495. .calledBefore(@tpdsUpdateSender.addFile).should.equal true
  496. it "should call addDoc for each doc", ->
  497. @tpdsUpdateSender.addDoc.calledWith(
  498. project_id : @project._id
  499. path : "/doc1"
  500. docLines: ["doc1", "lines"]
  501. project_name: @project.name
  502. rev:1
  503. ).should.equal true
  504. @tpdsUpdateSender.addDoc.calledWith(
  505. project_id : @project._id
  506. path : "/folder1/folder2/doc2"
  507. docLines: ["doc2", "lines"]
  508. project_name: @project.name
  509. rev:2
  510. ).should.equal true
  511. it "should call addFile for each file", ->
  512. @tpdsUpdateSender.addFile.calledWith(
  513. project_id : @project._id
  514. file_id : "file1_id"
  515. path : "/file1"
  516. project_name: @project.name
  517. rev:1
  518. ).should.equal true
  519. @tpdsUpdateSender.addFile.calledWith(
  520. project_id : @project._id
  521. file_id : "file2_id"
  522. path : "/folder1/file2"
  523. project_name: @project.name
  524. rev:2
  525. ).should.equal true
  526. describe "setRootDoc", ->
  527. it "should call Project.update", ->
  528. @project_id = "project-id-123234adfs"
  529. @rootDoc_id = "root-doc-id-123123"
  530. @ProjectModel.update = sinon.stub()
  531. @ProjectEntityHandler.setRootDoc @project_id, @rootDoc_id
  532. @ProjectModel.update.calledWith({_id : @project_id}, {rootDoc_id: @rootDoc_id})
  533. .should.equal true
  534. describe "unsetRootDoc", ->
  535. it "should call Project.update", ->
  536. @project_id = "project-id-123234adfs"
  537. @rootDoc_id = "root-doc-id-123123"
  538. @ProjectModel.update = sinon.stub()
  539. @ProjectEntityHandler.unsetRootDoc @project_id
  540. @ProjectModel.update.calledWith({_id : @project_id}, {$unset : {rootDoc_id: true}})
  541. .should.equal true
  542. describe 'copying file', ->
  543. fileName = "something.jpg"
  544. filePath = "dumpFolder/somewhere/image.jpeg"
  545. oldProject_id = "123kljadas"
  546. oldFileRef = {name:fileName, _id:"oldFileRef"}
  547. it 'should copy the file in FileStoreHandler', (done)->
  548. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)=>
  549. @FileStoreHandler.copyFile.calledWith(oldProject_id, oldFileRef._id, project_id, fileRef._id).should.equal true
  550. done()
  551. it 'should put file into folder by calling put element', (done)->
  552. @ProjectModel.putElement = (passedProject_id, passedFolder_id, passedFileRef, passedType, callback)->
  553. passedProject_id.should.equal project_id
  554. passedFolder_id.should.equal folder_id
  555. passedFileRef.name.should.equal fileName
  556. passedType.should.equal 'file'
  557. done()
  558. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  559. it 'should return doc and parent folder', (done)->
  560. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  561. parentFolder.should.equal folder_id
  562. fileRef.name.should.equal fileName
  563. done()
  564. it 'should call third party data store if versioning is enabled', (done)->
  565. @project.existsInVersioningApi = true
  566. opts =
  567. path : "/somehwere/idsadsds"
  568. project_id : project_id
  569. @ProjectModel.putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
  570. @tpdsUpdateSender.addFile = (options)=>
  571. options.project_id.should.equal project_id
  572. options.project_name.should.equal @project.name
  573. options.path.should.equal opts.path
  574. options.file_id.should.not.be.null
  575. options.rev.should.equal 0
  576. done()
  577. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  578. describe "renameEntity", ->
  579. beforeEach ->
  580. @entity_id = "4eecaffcbffa66588e000009"
  581. @entityType = "doc"
  582. @newName = "new.tex"
  583. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  584. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: @entity_id, name:"old.tex", rev:4 }, @path)
  585. @ProjectModel.update = sinon.stub().callsArgWith(3)
  586. @tpdsUpdateSender.moveEntity = sinon.stub()
  587. it "should update the name in mongo", (done)->
  588. @ProjectEntityHandler.renameEntity @project_id, @entity_id, @entityType, @newName, =>
  589. @ProjectModel.update.calledWith({_id : @project_id}, {"$set":{"mongo.path.name":@newName}}).should.equal true
  590. done()
  591. it "should send the update to the tpds", (done)->
  592. @ProjectEntityHandler.renameEntity @project_id, @entity_id, @entityType, @newName, =>
  593. @tpdsUpdateSender.moveEntity.calledWith({project_id:@project_id, startPath:@path.fileSystem, endPath:"/file/system/new.tex", project_name:@project.name, rev:4}).should.equal true
  594. done()
  595. describe "_insertDeletedDocReference", ->
  596. beforeEach ->
  597. @doc =
  598. _id: ObjectId()
  599. name: "test.tex"
  600. @callback = sinon.stub()
  601. @ProjectModel.update = sinon.stub().callsArgWith(3)
  602. @ProjectEntityHandler._insertDeletedDocReference project_id, @doc, @callback
  603. it "should insert the doc into deletedDocs", ->
  604. @ProjectModel.update
  605. .calledWith({
  606. _id: project_id
  607. }, {
  608. $push: {
  609. deletedDocs: {
  610. _id: @doc._id
  611. name: @doc.name
  612. }
  613. }
  614. })
  615. .should.equal true
  616. it "should call the callback", ->
  617. @callback.called.should.equal true
  618. describe "_cleanUpDoc", ->
  619. beforeEach ->
  620. @project =
  621. _id: ObjectId(project_id)
  622. @doc =
  623. _id: ObjectId()
  624. name: "test.tex"
  625. @ProjectEntityHandler.unsetRootDoc = sinon.stub().callsArg(1)
  626. @ProjectEntityHandler._insertDeletedDocReference = sinon.stub().callsArg(2)
  627. @documentUpdaterHandler.deleteDoc = sinon.stub().callsArg(2)
  628. @DocstoreManager.deleteDoc = sinon.stub().callsArg(2)
  629. @callback = sinon.stub()
  630. describe "when the doc is the root doc", ->
  631. beforeEach ->
  632. @project.rootDoc_id = @doc._id
  633. @ProjectEntityHandler._cleanUpDoc @project, @doc, @callback
  634. it "should unset the root doc", ->
  635. @ProjectEntityHandler.unsetRootDoc
  636. .calledWith(project_id)
  637. .should.equal true
  638. it "should delete the doc in the doc updater", ->
  639. @documentUpdaterHandler.deleteDoc
  640. .calledWith(project_id, @doc._id.toString())
  641. it "should insert the doc into the deletedDocs array", ->
  642. @ProjectEntityHandler._insertDeletedDocReference
  643. .calledWith(@project._id, @doc)
  644. .should.equal true
  645. it "should delete the doc in the doc store", ->
  646. @DocstoreManager.deleteDoc
  647. .calledWith(project_id, @doc._id.toString())
  648. .should.equal true
  649. it "should call the callback", ->
  650. @callback.called.should.equal true
  651. describe "when the doc is not the root doc", ->
  652. beforeEach ->
  653. @project.rootDoc_id = ObjectId()
  654. @ProjectEntityHandler._cleanUpDoc @project, @doc, @callback
  655. it "should not unset the root doc", ->
  656. @ProjectEntityHandler.unsetRootDoc.called.should.equal false
  657. it "should call the callback", ->
  658. @callback.called.should.equal true