ProjectEntityHandlerTests.coffee 28 KB

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