ProjectEntityMongoUpdateHandlerTests.coffee 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. chai = require('chai')
  2. expect = chai.expect
  3. assert = require('chai').assert
  4. should = chai.should()
  5. sinon = require 'sinon'
  6. tk = require("timekeeper")
  7. modulePath = "../../../../app/js/Features/Project/ProjectEntityMongoUpdateHandler"
  8. Errors = require "../../../../app/js/Features/Errors/Errors"
  9. ObjectId = require("mongoose").Types.ObjectId
  10. SandboxedModule = require('sandboxed-module')
  11. describe 'ProjectEntityMongoUpdateHandler', ->
  12. project_id = '4eecb1c1bffa66588e0000a1'
  13. doc_id = '4eecb1c1bffa66588e0000a2'
  14. file_id = '4eecb1c1bffa66588e0000a3'
  15. folder_id = "4eecaffcbffa66588e000008"
  16. beforeEach ->
  17. @FolderModel = class Folder
  18. constructor:(options)->
  19. {@name} = options
  20. @._id = "folder_id"
  21. @docName = "doc-name"
  22. @fileName = "something.jpg"
  23. @project = _id: project_id, name: 'project name'
  24. @callback = sinon.stub()
  25. tk.freeze(Date.now())
  26. @subject = SandboxedModule.require modulePath, requires:
  27. 'logger-sharelatex': @logger = {log:sinon.stub(), error: sinon.stub(), err:->}
  28. "settings-sharelatex":@settings = {
  29. maxEntitiesPerProject: 100
  30. }
  31. "../Cooldown/CooldownManager": @CooldownManager = {}
  32. '../../models/Folder': Folder:@FolderModel
  33. "../../infrastructure/LockManager":@LockManager =
  34. runWithLock:
  35. sinon.spy((namespace, id, runner, callback) -> runner(callback))
  36. '../../models/Project': Project:@ProjectModel = {}
  37. './ProjectEntityHandler': @ProjectEntityHandler = {}
  38. './ProjectLocator': @ProjectLocator = {}
  39. "./ProjectGetter": @ProjectGetter =
  40. getProjectWithoutLock: sinon.stub().yields(null, @project)
  41. afterEach ->
  42. tk.reset()
  43. describe 'addDoc', ->
  44. beforeEach ->
  45. @subject._confirmFolder = sinon.stub().yields(folder_id)
  46. @subject._putElement = sinon.stub()
  47. @doc = _id: doc_id
  48. @subject.addDoc project_id, folder_id, @doc, @callback
  49. it 'gets the project', ->
  50. @ProjectGetter.getProjectWithoutLock
  51. .calledWith(project_id, {rootFolder:true, name:true, overleaf:true})
  52. .should.equal true
  53. it 'checks the folder exists', ->
  54. @subject._confirmFolder
  55. .calledWith(@project, folder_id)
  56. .should.equal true
  57. it 'puts the element in mongo', ->
  58. @subject._putElement
  59. .calledWith(@project, folder_id, @doc, 'doc', @callback)
  60. .should.equal true
  61. describe 'addFile', ->
  62. beforeEach ->
  63. @subject._confirmFolder = sinon.stub().yields(folder_id)
  64. @subject._putElement = sinon.stub()
  65. @file = _id: file_id
  66. @subject.addFile project_id, folder_id, @file, @callback
  67. it 'gets the project', ->
  68. @ProjectGetter.getProjectWithoutLock
  69. .calledWith(project_id, {rootFolder:true, name:true, overleaf:true})
  70. .should.equal true
  71. it 'checks the folder exists', ->
  72. @subject._confirmFolder
  73. .calledWith(@project, folder_id)
  74. .should.equal true
  75. it 'puts the element in mongo', ->
  76. @subject._putElement
  77. .calledWith(@project, folder_id, @file, 'file', @callback)
  78. .should.equal true
  79. describe 'replaceFileWithNew', ->
  80. beforeEach ->
  81. @file = _id: file_id
  82. @path = mongo: 'file.png'
  83. @newFile = _id: 'new-file-id'
  84. @newFile.linkedFileData = @linkedFileData = {provider: 'url'}
  85. @newProject = "new-project"
  86. @ProjectLocator.findElement = sinon.stub().yields(null, @file, @path)
  87. @ProjectModel.findOneAndUpdate = sinon.stub().yields(null, @newProject)
  88. @ProjectModel.update = sinon.stub().yields()
  89. @subject.replaceFileWithNew project_id, file_id, @newFile, @callback
  90. it 'gets the project', ->
  91. @ProjectGetter.getProjectWithoutLock
  92. .calledWith(project_id, {rootFolder:true, name:true, overleaf:true})
  93. .should.equal true
  94. it 'finds the existing element', ->
  95. @ProjectLocator.findElement
  96. .calledWith({ @project, element_id: file_id, type: 'file' })
  97. .should.equal true
  98. it 'inserts a deletedFile reference for the old file', ->
  99. @ProjectModel.update
  100. .calledWith({ _id: project_id },
  101. {
  102. $push: {
  103. deletedFiles: {
  104. _id: file_id
  105. name: @file.name
  106. linkedFileData: @file.linkedFileData
  107. deletedAt: new Date()
  108. }
  109. }
  110. }
  111. )
  112. .should.equal true
  113. it 'increments the project version and sets the rev and created_at', ->
  114. @ProjectModel.findOneAndUpdate
  115. .calledWith(
  116. { _id: project_id },
  117. {
  118. '$inc': { 'version': 1, 'file.png.rev': 1 }
  119. '$set': { 'file.png._id': @newFile._id, 'file.png.created': new Date(), 'file.png.linkedFileData': @linkedFileData }
  120. },
  121. {new: true}
  122. )
  123. .should.equal true
  124. it 'calls the callback', ->
  125. @callback.calledWith(null, @file, @project, @path, @newProject).should.equal true
  126. describe 'mkdirp', ->
  127. beforeEach ->
  128. @parentFolder_id = "1jnjknjk"
  129. @newFolder = {_id:"newFolder_id_here"}
  130. @lastFolder = {_id:"123das", folders:[]}
  131. @rootFolder = {_id: "rootFolderId" }
  132. @project = _id: project_id, rootFolder: [@rootFolder]
  133. @ProjectGetter.getProjectWithOnlyFolders = sinon.stub().yields(null, @project)
  134. @ProjectLocator.findElementByPath = ->
  135. sinon.stub @ProjectLocator, "findElementByPath", (options, cb) =>
  136. {path} = options
  137. @parentFolder = {_id:"parentFolder_id_here"}
  138. lastFolder = path.substring(path.lastIndexOf("/"))
  139. if lastFolder.indexOf("level1") == -1
  140. cb "level1 is not the last foler "
  141. else
  142. cb null, @parentFolder
  143. @subject.addFolder =
  144. withoutLock: (project_id, parentFolder_id, folderName, callback) =>
  145. callback null, {name:folderName}, @parentFolder_id
  146. it 'should return the root folder if the path is just a slash', (done)->
  147. path = "/"
  148. @subject.mkdirp project_id, path, {}, (err, folders, lastFolder)=>
  149. lastFolder.should.deep.equal @rootFolder
  150. assert.equal lastFolder.parentFolder_id, undefined
  151. done()
  152. it 'should make just one folder', (done)->
  153. path = "/differentFolder/"
  154. @subject.mkdirp project_id, path, {}, (err, folders, lastFolder)=>
  155. folders.length.should.equal 1
  156. lastFolder.name.should.equal "differentFolder"
  157. lastFolder.parentFolder_id.should.equal @parentFolder_id
  158. done()
  159. it 'should make the final folder in path if it doesnt exist with one level', (done)->
  160. path = "level1/level2"
  161. @subject.mkdirp project_id, path, {}, (err, folders, lastFolder)=>
  162. folders.length.should.equal 1
  163. lastFolder.name.should.equal "level2"
  164. lastFolder.parentFolder_id.should.equal @parentFolder_id
  165. done()
  166. it 'should make the final folder in path if it doesnt exist with mutliple levels', (done)->
  167. path = "level1/level2/level3"
  168. @subject.mkdirp project_id, path, {}, (err, folders, lastFolder) =>
  169. folders.length.should.equal 2
  170. folders[0].name.should.equal "level2"
  171. folders[0].parentFolder_id.should.equal @parentFolder_id
  172. lastFolder.name.should.equal "level3"
  173. lastFolder.parentFolder_id.should.equal @parentFolder_id
  174. done()
  175. it 'should work with slashes either side', (done)->
  176. path = "/level1/level2/level3/"
  177. @subject.mkdirp project_id, path, {}, (err, folders, lastFolder)=>
  178. folders.length.should.equal 2
  179. folders[0].name.should.equal "level2"
  180. folders[0].parentFolder_id.should.equal @parentFolder_id
  181. lastFolder.name.should.equal "level3"
  182. lastFolder.parentFolder_id.should.equal @parentFolder_id
  183. done()
  184. it 'should use a case-insensitive match by default', (done)->
  185. path = "/differentFolder/"
  186. @subject.mkdirp project_id, path, {}, (err, folders, lastFolder)=>
  187. @ProjectLocator.findElementByPath.calledWithMatch({exactCaseMatch:undefined})
  188. .should.equal true
  189. done()
  190. it 'should use a case-sensitive match if exactCaseMatch option is set', (done)->
  191. path = "/differentFolder/"
  192. @subject.mkdirp project_id, path, {exactCaseMatch:true}, (err, folders, lastFolder)=>
  193. @ProjectLocator.findElementByPath.calledWithMatch({exactCaseMatch:true})
  194. .should.equal true
  195. done()
  196. describe 'moveEntity', ->
  197. beforeEach ->
  198. @pathAfterMove = {
  199. fileSystem: "/somewhere/else.txt"
  200. }
  201. @ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
  202. @ProjectEntityHandler.getAllEntitiesFromProject
  203. .onFirstCall()
  204. .yields(null, @oldDocs = ['old-doc'], @oldFiles = ['old-file'])
  205. @ProjectEntityHandler.getAllEntitiesFromProject
  206. .onSecondCall()
  207. .yields(null, @newDocs = ['new-doc'], @newFiles = ['new-file'])
  208. @doc = {lines:["1234","312343d"], rev: "1234"}
  209. @path = { mongo:"folders[0]", fileSystem:"/old_folder/somewhere.txt" }
  210. @newProject = "new-project"
  211. @ProjectLocator.findElement = sinon.stub()
  212. .withArgs({@project, element_id: @docId, type: 'docs'})
  213. .yields(null, @doc, @path)
  214. @subject._checkValidMove = sinon.stub().yields()
  215. @subject._removeElementFromMongoArray = sinon.stub().yields(null, @newProject)
  216. @subject._putElement = sinon.stub().yields(null, path: @pathAfterMove, @newProject)
  217. @subject.moveEntity project_id, doc_id, folder_id, "docs", @callback
  218. it 'should get the project', ->
  219. @ProjectGetter.getProjectWithoutLock
  220. .calledWith(project_id, {rootFolder:true, name:true, overleaf:true})
  221. .should.equal true
  222. it 'should find the doc to move', ->
  223. @ProjectLocator.findElement
  224. .calledWith({element_id: doc_id, type: "docs", project: @project })
  225. .should.equal true
  226. it 'should check this is a valid move', ->
  227. @subject._checkValidMove
  228. .calledWith(@project, 'docs', @doc, @path, folder_id)
  229. .should.equal true
  230. it "should put the element in the new folder", ->
  231. @subject._putElement
  232. .calledWith(@project, folder_id, @doc, "docs")
  233. .should.equal true
  234. it 'should remove the element from its current position', ->
  235. @subject._removeElementFromMongoArray
  236. .calledWith(@ProjectModel, project_id, @path.mongo)
  237. .should.equal true
  238. it 'should remove the element from its current position after putting the element in the new folder', ->
  239. @subject._removeElementFromMongoArray
  240. .calledAfter(@subject._putElement)
  241. .should.equal true
  242. it "calls the callback", ->
  243. changes = { @oldDocs, @newDocs, @oldFiles, @newFiles, @newProject }
  244. @callback.calledWith(
  245. null, @project, @path.fileSystem, @pathAfterMove.fileSystem, @doc.rev, changes
  246. ).should.equal true
  247. describe 'moveEntity must refuse to move the folder to a subfolder of itself', ->
  248. beforeEach ->
  249. @pathAfterMove = {
  250. fileSystem: "/somewhere/else.txt"
  251. }
  252. @doc = {lines:["1234","312343d"], rev: "1234"}
  253. @path = { mongo:"folders[0]", fileSystem:"/old_folder/somewhere.txt" }
  254. @newProject = "new-project"
  255. @ProjectLocator.findElement = sinon.stub()
  256. .withArgs({@project, element_id: @docId, type: 'docs'})
  257. .yields(null, @doc, @path)
  258. # return an error when moving a folder to a subfolder of itself
  259. @subject._checkValidMove = sinon.stub().yields(new Error())
  260. @subject._removeElementFromMongoArray = sinon.stub().yields(null, @project)
  261. @subject._putElement = sinon.stub().yields(null, path: @pathAfterMove, @newProject)
  262. @subject.moveEntity project_id, doc_id, folder_id, "docs", @callback
  263. it 'should get the project', ->
  264. @ProjectGetter.getProjectWithoutLock
  265. .calledWith(project_id, {rootFolder:true, name:true, overleaf:true})
  266. .should.equal true
  267. it 'should find the doc to move', ->
  268. @ProjectLocator.findElement
  269. .calledWith({element_id: doc_id, type: "docs", project: @project })
  270. .should.equal true
  271. it 'should check this is an invalid move', ->
  272. @subject._checkValidMove
  273. .calledWith(@project, 'docs', @doc, @path, folder_id)
  274. .should.equal true
  275. it "should not put the element in the new folder", ->
  276. @subject._putElement.called
  277. .should.equal false
  278. it 'should not remove the element from its current position', ->
  279. @subject._removeElementFromMongoArray.called
  280. .should.equal false
  281. it "calls the callback with an error", ->
  282. @callback.calledWith(
  283. new Error()
  284. ).should.equal true
  285. describe 'deleteEntity', ->
  286. beforeEach ->
  287. @path = mongo: "mongo.path", fileSystem: "/file/system/path"
  288. @doc = _id: doc_id
  289. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, @path)
  290. @subject._removeElementFromMongoArray = sinon.stub().yields()
  291. @subject.deleteEntity project_id, doc_id, 'doc', @callback
  292. it "should get the project", ->
  293. @ProjectGetter.getProjectWithoutLock
  294. .calledWith(project_id, {rootFolder:true, name:true, overleaf:true})
  295. .should.equal true
  296. it "should find the element", ->
  297. @ProjectLocator.findElement
  298. .calledWith({@project, element_id: @doc._id, type: 'doc'})
  299. .should.equal true
  300. it "should remove the element from the database", ->
  301. @subject._removeElementFromMongoArray
  302. .calledWith(@ProjectModel, project_id, @path.mongo)
  303. .should.equal true
  304. it "calls the callbck", ->
  305. @callback.calledWith(null, @doc, @path, @project).should.equal true
  306. describe "renameEntity", ->
  307. beforeEach ->
  308. @newName = "new.tex"
  309. @path = mongo: "mongo.path", fileSystem: "/old.tex"
  310. @project =
  311. _id: ObjectId(project_id)
  312. rootFolder: [_id:ObjectId()]
  313. @doc = _id: doc_id, name: "old.tex", rev: 1
  314. @folder = _id: folder_id
  315. @newProject = "new-project"
  316. @ProjectGetter.getProjectWithoutLock = sinon.stub().yields(null, @project)
  317. @ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
  318. @ProjectEntityHandler.getAllEntitiesFromProject
  319. .onFirstCall()
  320. .yields( null, @oldDocs = ['old-doc'], @oldFiles = ['old-file'])
  321. @ProjectEntityHandler.getAllEntitiesFromProject
  322. .onSecondCall()
  323. .yields( null, @newDocs = ['new-doc'], @newFiles = ['new-file'])
  324. @ProjectLocator.findElement = sinon.stub().yields(null, @doc, @path, @folder)
  325. @subject._checkValidElementName = sinon.stub().yields()
  326. @ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @newProject)
  327. @subject.renameEntity project_id, doc_id, 'doc', @newName, @callback
  328. it 'should get the project', ->
  329. @ProjectGetter.getProjectWithoutLock
  330. .calledWith(project_id, {rootFolder:true, name:true, overleaf:true})
  331. .should.equal true
  332. it 'should find the doc', ->
  333. @ProjectLocator.findElement
  334. .calledWith({element_id: doc_id, type: 'doc', project: @project })
  335. .should.equal true
  336. it 'should check the new name is valid', ->
  337. @subject._checkValidElementName
  338. .calledWith(@folder, @newName)
  339. .should.equal true
  340. it 'should update the doc name', ->
  341. @ProjectModel.findOneAndUpdate
  342. .calledWith(
  343. { _id: project_id },
  344. { $set: { "mongo.path.name": @newName }, $inc: {"version": 1} },
  345. { new: true }
  346. ).should.equal true
  347. it 'calls the callback', ->
  348. changes = { @oldDocs, @newDocs, @oldFiles, @newFiles, @newProject }
  349. @callback.calledWith(
  350. null, @project, '/old.tex', '/new.tex', @doc.rev, changes
  351. ).should.equal true
  352. describe 'addFolder', ->
  353. beforeEach ->
  354. @folderName = "folder1234"
  355. @ProjectGetter.getProjectWithOnlyFolders = sinon.stub().callsArgWith(1, null, @project)
  356. @subject._confirmFolder = sinon.stub().yields(folder_id)
  357. @subject._putElement = sinon.stub().yields()
  358. @subject.addFolder project_id, folder_id, @folderName, @callback
  359. it 'gets the project', ->
  360. @ProjectGetter.getProjectWithoutLock
  361. .calledWith(project_id, {rootFolder:true, name:true, overleaf:true})
  362. .should.equal true
  363. it 'checks the parent folder exists', ->
  364. @subject._confirmFolder
  365. .calledWith(@project, folder_id)
  366. .should.equal true
  367. it 'puts the element in mongo', ->
  368. folderMatcher = sinon.match (folder) =>
  369. folder.name == @folderName
  370. @subject._putElement
  371. .calledWithMatch(@project, folder_id, folderMatcher, 'folder')
  372. .should.equal true
  373. it 'calls the callback', ->
  374. folderMatcher = sinon.match (folder) =>
  375. folder.name == @folderName
  376. @callback.calledWithMatch(null, folderMatcher, folder_id).should.equal true
  377. describe '_removeElementFromMongoArray ', ->
  378. beforeEach ->
  379. @mongoPath = "folders[0].folders[5]"
  380. @id = "12344"
  381. @ProjectModel.update = sinon.stub().yields()
  382. @ProjectModel.findOneAndUpdate = sinon.stub().yields(null, @project)
  383. @subject._removeElementFromMongoArray @ProjectModel, @id, @mongoPath, @callback
  384. it 'should unset', ->
  385. update = { '$unset': { } }
  386. update['$unset'][@mongoPath] = 1
  387. @ProjectModel.update
  388. .calledWith({ _id: @id }, update, {})
  389. .should.equal true
  390. it 'should pull', ->
  391. @ProjectModel.findOneAndUpdate
  392. .calledWith({ _id: @id }, { '$pull': { 'folders[0]': null }, '$inc': {'version': 1} }, {'new': true})
  393. .should.equal true
  394. it 'should call the callback', ->
  395. @callback.calledWith(null, @project).should.equal true
  396. describe "_countElements", ->
  397. beforeEach ->
  398. @project =
  399. _id: project_id,
  400. rootFolder: [
  401. docs: [{_id:123}, {_id:345}]
  402. fileRefs: [{_id:123}, {_id:345}, {_id:456}]
  403. folders: [
  404. {
  405. docs: [{_id:123}, {_id:345}, {_id:456}]
  406. fileRefs:{}
  407. folders: [
  408. {
  409. docs:[_id:1234],
  410. fileRefs:[{_id:23123}, {_id:123213}, {_id:2312}]
  411. folders:[
  412. {
  413. docs:[{_id:321321}, {_id:123213}]
  414. fileRefs:[{_id:312321}]
  415. folders:[]
  416. }
  417. ]
  418. }
  419. ]
  420. },{
  421. docs:[{_id:123}, {_id:32131}]
  422. fileRefs:[]
  423. folders:[
  424. {
  425. docs:[{_id:3123}]
  426. fileRefs:[{_id:321321}, {_id:321321}, {_id:313122}]
  427. folders:0
  428. }
  429. ]
  430. }
  431. ]
  432. ]
  433. it "should return the correct number", ->
  434. expect(@subject._countElements @project).to.equal(26)
  435. it "should deal with null folders", ->
  436. @project.rootFolder[0].folders[0].folders = undefined
  437. expect(@subject._countElements @project).to.equal(17)
  438. it "should deal with null docs", ->
  439. @project.rootFolder[0].folders[0].docs = undefined
  440. expect(@subject._countElements @project).to.equal(23)
  441. it "should deal with null fileRefs", ->
  442. @project.rootFolder[0].folders[0].folders[0].fileRefs = undefined
  443. expect(@subject._countElements @project).to.equal(23)
  444. describe "_putElement", ->
  445. beforeEach ->
  446. @project =
  447. _id: project_id
  448. rootFolder: [_id:ObjectId()]
  449. @folder =
  450. _id: ObjectId()
  451. name: "someFolder"
  452. docs: [ {name: "another-doc.tex"} ]
  453. fileRefs: [ {name: "another-file.tex"} ]
  454. folders: [ {name: "another-folder"} ]
  455. @doc =
  456. _id: ObjectId()
  457. name: "new.tex"
  458. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  459. @ProjectLocator.findElement = sinon.stub().yields(null, @folder, @path)
  460. @ProjectModel.findOneAndUpdate = sinon.stub().yields(null, @project)
  461. describe "updating the project", ->
  462. it "should use the correct mongo path", (done)->
  463. @subject._putElement @project, @folder._id, @doc, "docs", (err)=>
  464. @ProjectModel.findOneAndUpdate.args[0][0]._id.should.equal @project._id
  465. assert.deepEqual @ProjectModel.findOneAndUpdate.args[0][1].$push[@path.mongo+".docs"], @doc
  466. done()
  467. it "should return the project in the callback", (done)->
  468. @subject._putElement @project, @folder._id, @doc, "docs", (err, path, project)=>
  469. assert.equal project, @project
  470. done()
  471. it "should add an s onto the type if not included", (done)->
  472. @subject._putElement @project, @folder._id, @doc, "doc", (err)=>
  473. assert.deepEqual @ProjectModel.findOneAndUpdate.args[0][1].$push[@path.mongo+".docs"], @doc
  474. done()
  475. it "should not call update if element is null", (done)->
  476. @subject._putElement @project, @folder._id, null, "doc", (err)=>
  477. @ProjectModel.findOneAndUpdate.called.should.equal false
  478. done()
  479. it "should default to root folder insert", (done)->
  480. @subject._putElement @project, null, @doc, "doc", (err)=>
  481. @ProjectLocator.findElement.args[0][0].element_id.should.equal @project.rootFolder[0]._id
  482. done()
  483. it "should error if the element has no _id", (done)->
  484. doc =
  485. name:"something"
  486. @subject._putElement @project, @folder._id, doc, "doc", (err)=>
  487. @ProjectModel.findOneAndUpdate.called.should.equal false
  488. done()
  489. it "should error if element name contains invalid characters", (done)->
  490. doc =
  491. _id: ObjectId()
  492. name: "something*bad"
  493. @subject._putElement @project, @folder._id, doc, "doc", (err)=>
  494. @ProjectModel.findOneAndUpdate.called.should.equal false
  495. err.should.deep.equal new Errors.InvalidNameError("invalid element name")
  496. done()
  497. it "should error if element name is too long", (done)->
  498. doc =
  499. _id: ObjectId()
  500. name: new Array(200).join("long-") + "something"
  501. @subject._putElement @project, @folder._id, doc, "doc", (err)=>
  502. @ProjectModel.findOneAndUpdate.called.should.equal false
  503. err.should.deep.equal new Errors.InvalidNameError("invalid element name")
  504. done()
  505. it "should error if the folder name is too long", (done)->
  506. @path =
  507. mongo: "mongo.path",
  508. fileSystem: new Array(200).join("subdir/") + "foo"
  509. @ProjectLocator.findElement.callsArgWith(1, null, @folder, @path)
  510. doc =
  511. _id: ObjectId()
  512. name: "something"
  513. @subject._putElement @project, @folder._id, doc, "doc", (err)=>
  514. @ProjectModel.findOneAndUpdate.called.should.equal false
  515. err.should.deep.equal new Errors.InvalidNameError("path too long")
  516. done()
  517. it "should error if a document already exists with the same name", (done)->
  518. doc =
  519. _id: ObjectId()
  520. name: "another-doc.tex"
  521. @subject._putElement @project, @folder, doc, "doc", (err)=>
  522. @ProjectModel.findOneAndUpdate.called.should.equal false
  523. err.should.deep.equal new Errors.InvalidNameError("file already exists")
  524. done()
  525. it "should error if a file already exists with the same name", (done)->
  526. doc =
  527. _id: ObjectId()
  528. name: "another-file.tex"
  529. @subject._putElement @project, @folder, doc, "doc", (err)=>
  530. @ProjectModel.findOneAndUpdate.called.should.equal false
  531. err.should.deep.equal new Errors.InvalidNameError("file already exists")
  532. done()
  533. it "should error if a folder already exists with the same name", (done)->
  534. doc =
  535. _id: ObjectId()
  536. name: "another-folder"
  537. @subject._putElement @project, @folder, doc, "doc", (err)=>
  538. @ProjectModel.findOneAndUpdate.called.should.equal false
  539. err.should.deep.equal new Errors.InvalidNameError("file already exists")
  540. done()
  541. describe '_checkValidElementName', ->
  542. beforeEach ->
  543. @folder =
  544. docs: [ name: 'doc_name' ]
  545. fileRefs: [ name: 'file_name' ]
  546. folders: [ name: 'folder_name' ]
  547. it 'returns an error if name matches any doc name', ->
  548. @subject._checkValidElementName @folder, 'doc_name', (err) ->
  549. expect(err).to.deep.equal new Errors.InvalidNameError("file already exists")
  550. it 'returns an error if name matches any file name', ->
  551. @subject._checkValidElementName @folder, 'file_name', (err) ->
  552. expect(err).to.deep.equal new Errors.InvalidNameError("file already exists")
  553. it 'returns an error if name matches any folder name', ->
  554. @subject._checkValidElementName @folder, 'folder_name', (err) ->
  555. expect(err).to.deep.equal new Errors.InvalidNameError("file already exists")
  556. it 'returns nothing if name is valid', ->
  557. @subject._checkValidElementName @folder, 'unique_name', (err) ->
  558. expect(err).to.be.undefined
  559. describe '_checkValidMove', ->
  560. beforeEach ->
  561. @destFolder = _id: folder_id
  562. @destFolderPath = fileSystem: '/foo/bar'
  563. @ProjectLocator.findElement = sinon.stub().yields(null, @destFolder, @destFolderPath)
  564. @subject._checkValidElementName = sinon.stub().yields()
  565. it 'checks the element name is valid', ->
  566. @doc = _id: doc_id, name: 'doc_name'
  567. @subject._checkValidMove @project, 'doc', @doc, fileSystem: '/main.tex', @destFolder._id, (err) =>
  568. expect(err).to.be.undefined
  569. @subject._checkValidElementName
  570. .calledWith(@destFolder, @doc.name)
  571. .should.equal true
  572. it 'returns an error if trying to move a folder inside itself', ->
  573. folder = name: 'folder_name'
  574. @subject._checkValidMove @project, 'folder', folder, fileSystem: '/foo', @destFolder._id, (err) =>
  575. expect(err).to.deep.equal new Errors.InvalidNameError("destination folder is a child folder of me")
  576. describe "_insertDeletedDocReference", ->
  577. beforeEach ->
  578. @doc =
  579. _id: ObjectId()
  580. name: "test.tex"
  581. @callback = sinon.stub()
  582. @ProjectModel.update = sinon.stub().yields()
  583. @subject._insertDeletedDocReference project_id, @doc, @callback
  584. it "should insert the doc into deletedDocs", ->
  585. @ProjectModel.update
  586. .calledWith({
  587. _id: project_id
  588. }, {
  589. $push: {
  590. deletedDocs: {
  591. _id: @doc._id
  592. name: @doc.name
  593. }
  594. }
  595. })
  596. .should.equal true
  597. it "should call the callback", ->
  598. @callback.called.should.equal true