ProjectEntityHandlerTests.coffee 28 KB

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