ProjectEntityHandlerTests.coffee 28 KB

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