ProjectEntityMongoUpdateHandlerTests.coffee 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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. @ProjectLocator.findElement = sinon.stub().yields(null, @file, @path)
  86. @ProjectModel.update = sinon.stub().yields()
  87. @subject.replaceFileWithNew project_id, file_id, @newFile, @callback
  88. it 'gets the project', ->
  89. @ProjectGetter.getProjectWithoutLock
  90. .calledWith(project_id, {rootFolder:true, name:true, overleaf:true})
  91. .should.equal true
  92. it 'finds the existing element', ->
  93. @ProjectLocator.findElement
  94. .calledWith({ @project, element_id: file_id, type: 'file' })
  95. .should.equal true
  96. it 'inserts a deletedFile reference for the old file', ->
  97. @ProjectModel.update
  98. .calledWith({ _id: project_id },
  99. {
  100. $push: {
  101. deletedFiles: {
  102. _id: file_id
  103. name: @file.name
  104. linkedFileData: @file.linkedFileData
  105. deletedAt: new Date()
  106. }
  107. }
  108. }
  109. )
  110. .should.equal true
  111. it 'increments the project version and sets the rev and created_at', ->
  112. @ProjectModel.update
  113. .calledWith(
  114. { _id: project_id },
  115. {
  116. '$inc': { 'version': 1, 'file.png.rev': 1 }
  117. '$set': { 'file.png._id': @newFile._id, 'file.png.created': new Date(), 'file.png.linkedFileData': @linkedFileData }
  118. }
  119. {}
  120. )
  121. .should.equal true
  122. it 'calls the callback', ->
  123. @callback.calledWith(null, @file, @project, @path).should.equal true
  124. describe 'mkdirp', ->
  125. beforeEach ->
  126. @parentFolder_id = "1jnjknjk"
  127. @newFolder = {_id:"newFolder_id_here"}
  128. @lastFolder = {_id:"123das", folders:[]}
  129. @rootFolder = {_id: "rootFolderId" }
  130. @project = _id: project_id, rootFolder: [@rootFolder]
  131. @ProjectGetter.getProjectWithOnlyFolders = sinon.stub().yields(null, @project)
  132. @ProjectLocator.findElementByPath = ->
  133. sinon.stub @ProjectLocator, "findElementByPath", (options, cb) =>
  134. {path} = options
  135. @parentFolder = {_id:"parentFolder_id_here"}
  136. lastFolder = path.substring(path.lastIndexOf("/"))
  137. if lastFolder.indexOf("level1") == -1
  138. cb "level1 is not the last foler "
  139. else
  140. cb null, @parentFolder
  141. @subject.addFolder =
  142. withoutLock: (project_id, parentFolder_id, folderName, callback) =>
  143. callback null, {name:folderName}, @parentFolder_id
  144. it 'should return the root folder if the path is just a slash', (done)->
  145. path = "/"
  146. @subject.mkdirp project_id, path, {}, (err, folders, lastFolder)=>
  147. lastFolder.should.deep.equal @rootFolder
  148. assert.equal lastFolder.parentFolder_id, undefined
  149. done()
  150. it 'should make just one folder', (done)->
  151. path = "/differentFolder/"
  152. @subject.mkdirp project_id, path, {}, (err, folders, lastFolder)=>
  153. folders.length.should.equal 1
  154. lastFolder.name.should.equal "differentFolder"
  155. lastFolder.parentFolder_id.should.equal @parentFolder_id
  156. done()
  157. it 'should make the final folder in path if it doesnt exist with one level', (done)->
  158. path = "level1/level2"
  159. @subject.mkdirp project_id, path, {}, (err, folders, lastFolder)=>
  160. folders.length.should.equal 1
  161. lastFolder.name.should.equal "level2"
  162. lastFolder.parentFolder_id.should.equal @parentFolder_id
  163. done()
  164. it 'should make the final folder in path if it doesnt exist with mutliple levels', (done)->
  165. path = "level1/level2/level3"
  166. @subject.mkdirp project_id, path, {}, (err, folders, lastFolder) =>
  167. folders.length.should.equal 2
  168. folders[0].name.should.equal "level2"
  169. folders[0].parentFolder_id.should.equal @parentFolder_id
  170. lastFolder.name.should.equal "level3"
  171. lastFolder.parentFolder_id.should.equal @parentFolder_id
  172. done()
  173. it 'should work with slashes either side', (done)->
  174. path = "/level1/level2/level3/"
  175. @subject.mkdirp project_id, path, {}, (err, folders, lastFolder)=>
  176. folders.length.should.equal 2
  177. folders[0].name.should.equal "level2"
  178. folders[0].parentFolder_id.should.equal @parentFolder_id
  179. lastFolder.name.should.equal "level3"
  180. lastFolder.parentFolder_id.should.equal @parentFolder_id
  181. done()
  182. it 'should use a case-insensitive match by default', (done)->
  183. path = "/differentFolder/"
  184. @subject.mkdirp project_id, path, {}, (err, folders, lastFolder)=>
  185. @ProjectLocator.findElementByPath.calledWithMatch({exactCaseMatch:undefined})
  186. .should.equal true
  187. done()
  188. it 'should use a case-sensitive match if exactCaseMatch option is set', (done)->
  189. path = "/differentFolder/"
  190. @subject.mkdirp project_id, path, {exactCaseMatch:true}, (err, folders, lastFolder)=>
  191. @ProjectLocator.findElementByPath.calledWithMatch({exactCaseMatch:true})
  192. .should.equal true
  193. done()
  194. describe 'moveEntity', ->
  195. beforeEach ->
  196. @pathAfterMove = {
  197. fileSystem: "/somewhere/else.txt"
  198. }
  199. @ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
  200. @ProjectEntityHandler.getAllEntitiesFromProject
  201. .onFirstCall()
  202. .yields(null, @oldDocs = ['old-doc'], @oldFiles = ['old-file'])
  203. @ProjectEntityHandler.getAllEntitiesFromProject
  204. .onSecondCall()
  205. .yields(null, @newDocs = ['new-doc'], @newFiles = ['new-file'])
  206. @doc = {lines:["1234","312343d"], rev: "1234"}
  207. @path = { mongo:"folders[0]", fileSystem:"/old_folder/somewhere.txt" }
  208. @ProjectLocator.findElement = sinon.stub()
  209. .withArgs({@project, element_id: @docId, type: 'docs'})
  210. .yields(null, @doc, @path)
  211. @subject._checkValidMove = sinon.stub().yields()
  212. @subject._removeElementFromMongoArray = sinon.stub().yields(null, @project)
  213. @subject._putElement = sinon.stub().yields(null, path: @pathAfterMove)
  214. @subject.moveEntity project_id, doc_id, folder_id, "docs", @callback
  215. it 'should get the project', ->
  216. @ProjectGetter.getProjectWithoutLock
  217. .calledWith(project_id, {rootFolder:true, name:true, overleaf:true})
  218. .should.equal true
  219. it 'should find the doc to move', ->
  220. @ProjectLocator.findElement
  221. .calledWith({element_id: doc_id, type: "docs", project: @project })
  222. .should.equal true
  223. it 'should check this is a valid move', ->
  224. @subject._checkValidMove
  225. .calledWith(@project, 'docs', @doc, @path, folder_id)
  226. .should.equal true
  227. it 'should remove the element from its current position', ->
  228. @subject._removeElementFromMongoArray
  229. .calledWith(@ProjectModel, project_id, @path.mongo)
  230. .should.equal true
  231. it "should put the element back in the new folder", ->
  232. @subject._putElement
  233. .calledWith(@project, folder_id, @doc, "docs")
  234. .should.equal true
  235. it "calls the callback", ->
  236. changes = { @oldDocs, @newDocs, @oldFiles, @newFiles }
  237. @callback.calledWith(
  238. null, @project, @path.fileSystem, @pathAfterMove.fileSystem, @doc.rev, changes
  239. ).should.equal true
  240. describe 'deleteEntity', ->
  241. beforeEach ->
  242. @path = mongo: "mongo.path", fileSystem: "/file/system/path"
  243. @doc = _id: doc_id
  244. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, @path)
  245. @subject._removeElementFromMongoArray = sinon.stub().yields()
  246. @subject.deleteEntity project_id, doc_id, 'doc', @callback
  247. it "should get the project", ->
  248. @ProjectGetter.getProjectWithoutLock
  249. .calledWith(project_id, {rootFolder:true, name:true, overleaf:true})
  250. .should.equal true
  251. it "should find the element", ->
  252. @ProjectLocator.findElement
  253. .calledWith({@project, element_id: @doc._id, type: 'doc'})
  254. .should.equal true
  255. it "should remove the element from the database", ->
  256. @subject._removeElementFromMongoArray
  257. .calledWith(@ProjectModel, project_id, @path.mongo)
  258. .should.equal true
  259. it "calls the callbck", ->
  260. @callback.calledWith(null, @doc, @path, @project).should.equal true
  261. describe "renameEntity", ->
  262. beforeEach ->
  263. @newName = "new.tex"
  264. @path = mongo: "mongo.path", fileSystem: "/old.tex"
  265. @project =
  266. _id: ObjectId(project_id)
  267. rootFolder: [_id:ObjectId()]
  268. @doc = _id: doc_id, name: "old.tex", rev: 1
  269. @folder = _id: folder_id
  270. @ProjectGetter.getProjectWithoutLock = sinon.stub().yields(null, @project)
  271. @ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
  272. @ProjectEntityHandler.getAllEntitiesFromProject
  273. .onFirstCall()
  274. .yields( null, @oldDocs = ['old-doc'], @oldFiles = ['old-file'])
  275. @ProjectEntityHandler.getAllEntitiesFromProject
  276. .onSecondCall()
  277. .yields( null, @newDocs = ['new-doc'], @newFiles = ['new-file'])
  278. @ProjectLocator.findElement = sinon.stub().yields(null, @doc, @path, @folder)
  279. @subject._checkValidElementName = sinon.stub().yields()
  280. @ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
  281. @subject.renameEntity project_id, doc_id, 'doc', @newName, @callback
  282. it 'should get the project', ->
  283. @ProjectGetter.getProjectWithoutLock
  284. .calledWith(project_id, {rootFolder:true, name:true, overleaf:true})
  285. .should.equal true
  286. it 'should find the doc', ->
  287. @ProjectLocator.findElement
  288. .calledWith({element_id: doc_id, type: 'doc', project: @project })
  289. .should.equal true
  290. it 'should check the new name is valid', ->
  291. @subject._checkValidElementName
  292. .calledWith(@folder, @newName)
  293. .should.equal true
  294. it 'should update the doc name', ->
  295. @ProjectModel.findOneAndUpdate
  296. .calledWith(
  297. { _id: project_id },
  298. { $set: { "mongo.path.name": @newName }, $inc: {"version": 1} },
  299. { new: true }
  300. ).should.equal true
  301. it 'calls the callback', ->
  302. changes = { @oldDocs, @newDocs, @oldFiles, @newFiles }
  303. @callback.calledWith(
  304. null, @project, '/old.tex', '/new.tex', @doc.rev, changes
  305. ).should.equal true
  306. describe 'addFolder', ->
  307. beforeEach ->
  308. @folderName = "folder1234"
  309. @ProjectGetter.getProjectWithOnlyFolders = sinon.stub().callsArgWith(1, null, @project)
  310. @subject._confirmFolder = sinon.stub().yields(folder_id)
  311. @subject._putElement = sinon.stub().yields()
  312. @subject.addFolder project_id, folder_id, @folderName, @callback
  313. it 'gets the project', ->
  314. @ProjectGetter.getProjectWithoutLock
  315. .calledWith(project_id, {rootFolder:true, name:true, overleaf:true})
  316. .should.equal true
  317. it 'checks the parent folder exists', ->
  318. @subject._confirmFolder
  319. .calledWith(@project, folder_id)
  320. .should.equal true
  321. it 'puts the element in mongo', ->
  322. folderMatcher = sinon.match (folder) =>
  323. folder.name == @folderName
  324. @subject._putElement
  325. .calledWithMatch(@project, folder_id, folderMatcher, 'folder')
  326. .should.equal true
  327. it 'calls the callback', ->
  328. folderMatcher = sinon.match (folder) =>
  329. folder.name == @folderName
  330. @callback.calledWithMatch(null, folderMatcher, folder_id).should.equal true
  331. describe '_removeElementFromMongoArray ', ->
  332. beforeEach ->
  333. @mongoPath = "folders[0].folders[5]"
  334. @id = "12344"
  335. @ProjectModel.update = sinon.stub().yields()
  336. @ProjectModel.findOneAndUpdate = sinon.stub().yields(null, @project)
  337. @subject._removeElementFromMongoArray @ProjectModel, @id, @mongoPath, @callback
  338. it 'should unset', ->
  339. update = { '$unset': { } }
  340. update['$unset'][@mongoPath] = 1
  341. @ProjectModel.update
  342. .calledWith({ _id: @id }, update, {})
  343. .should.equal true
  344. it 'should pull', ->
  345. @ProjectModel.findOneAndUpdate
  346. .calledWith({ _id: @id }, { '$pull': { 'folders[0]': null }, '$inc': {'version': 1} }, {'new': true})
  347. .should.equal true
  348. it 'should call the callback', ->
  349. @callback.calledWith(null, @project).should.equal true
  350. describe "_countElements", ->
  351. beforeEach ->
  352. @project =
  353. _id: project_id,
  354. rootFolder: [
  355. docs: [{_id:123}, {_id:345}]
  356. fileRefs: [{_id:123}, {_id:345}, {_id:456}]
  357. folders: [
  358. {
  359. docs: [{_id:123}, {_id:345}, {_id:456}]
  360. fileRefs:{}
  361. folders: [
  362. {
  363. docs:[_id:1234],
  364. fileRefs:[{_id:23123}, {_id:123213}, {_id:2312}]
  365. folders:[
  366. {
  367. docs:[{_id:321321}, {_id:123213}]
  368. fileRefs:[{_id:312321}]
  369. folders:[]
  370. }
  371. ]
  372. }
  373. ]
  374. },{
  375. docs:[{_id:123}, {_id:32131}]
  376. fileRefs:[]
  377. folders:[
  378. {
  379. docs:[{_id:3123}]
  380. fileRefs:[{_id:321321}, {_id:321321}, {_id:313122}]
  381. folders:0
  382. }
  383. ]
  384. }
  385. ]
  386. ]
  387. it "should return the correct number", ->
  388. expect(@subject._countElements @project).to.equal(26)
  389. it "should deal with null folders", ->
  390. @project.rootFolder[0].folders[0].folders = undefined
  391. expect(@subject._countElements @project).to.equal(17)
  392. it "should deal with null docs", ->
  393. @project.rootFolder[0].folders[0].docs = undefined
  394. expect(@subject._countElements @project).to.equal(23)
  395. it "should deal with null fileRefs", ->
  396. @project.rootFolder[0].folders[0].folders[0].fileRefs = undefined
  397. expect(@subject._countElements @project).to.equal(23)
  398. describe "_putElement", ->
  399. beforeEach ->
  400. @project =
  401. _id: project_id
  402. rootFolder: [_id:ObjectId()]
  403. @folder =
  404. _id: ObjectId()
  405. name: "someFolder"
  406. docs: [ {name: "another-doc.tex"} ]
  407. fileRefs: [ {name: "another-file.tex"} ]
  408. folders: [ {name: "another-folder"} ]
  409. @doc =
  410. _id: ObjectId()
  411. name: "new.tex"
  412. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  413. @ProjectLocator.findElement = sinon.stub().yields(null, @folder, @path)
  414. @ProjectModel.findOneAndUpdate = sinon.stub().yields(null, @project)
  415. describe "updating the project", ->
  416. it "should use the correct mongo path", (done)->
  417. @subject._putElement @project, @folder._id, @doc, "docs", (err)=>
  418. @ProjectModel.findOneAndUpdate.args[0][0]._id.should.equal @project._id
  419. assert.deepEqual @ProjectModel.findOneAndUpdate.args[0][1].$push[@path.mongo+".docs"], @doc
  420. done()
  421. it "should return the project in the callback", (done)->
  422. @subject._putElement @project, @folder._id, @doc, "docs", (err, path, project)=>
  423. assert.equal project, @project
  424. done()
  425. it "should add an s onto the type if not included", (done)->
  426. @subject._putElement @project, @folder._id, @doc, "doc", (err)=>
  427. assert.deepEqual @ProjectModel.findOneAndUpdate.args[0][1].$push[@path.mongo+".docs"], @doc
  428. done()
  429. it "should not call update if element is null", (done)->
  430. @subject._putElement @project, @folder._id, null, "doc", (err)=>
  431. @ProjectModel.findOneAndUpdate.called.should.equal false
  432. done()
  433. it "should default to root folder insert", (done)->
  434. @subject._putElement @project, null, @doc, "doc", (err)=>
  435. @ProjectLocator.findElement.args[0][0].element_id.should.equal @project.rootFolder[0]._id
  436. done()
  437. it "should error if the element has no _id", (done)->
  438. doc =
  439. name:"something"
  440. @subject._putElement @project, @folder._id, doc, "doc", (err)=>
  441. @ProjectModel.findOneAndUpdate.called.should.equal false
  442. done()
  443. it "should error if element name contains invalid characters", (done)->
  444. doc =
  445. _id: ObjectId()
  446. name: "something*bad"
  447. @subject._putElement @project, @folder._id, doc, "doc", (err)=>
  448. @ProjectModel.findOneAndUpdate.called.should.equal false
  449. err.should.deep.equal new Errors.InvalidNameError("invalid element name")
  450. done()
  451. it "should error if element name is too long", (done)->
  452. doc =
  453. _id: ObjectId()
  454. name: new Array(200).join("long-") + "something"
  455. @subject._putElement @project, @folder._id, doc, "doc", (err)=>
  456. @ProjectModel.findOneAndUpdate.called.should.equal false
  457. err.should.deep.equal new Errors.InvalidNameError("invalid element name")
  458. done()
  459. it "should error if the folder name is too long", (done)->
  460. @path =
  461. mongo: "mongo.path",
  462. fileSystem: new Array(200).join("subdir/") + "foo"
  463. @ProjectLocator.findElement.callsArgWith(1, null, @folder, @path)
  464. doc =
  465. _id: ObjectId()
  466. name: "something"
  467. @subject._putElement @project, @folder._id, doc, "doc", (err)=>
  468. @ProjectModel.findOneAndUpdate.called.should.equal false
  469. err.should.deep.equal new Errors.InvalidNameError("path too long")
  470. done()
  471. it "should error if a document already exists with the same name", (done)->
  472. doc =
  473. _id: ObjectId()
  474. name: "another-doc.tex"
  475. @subject._putElement @project, @folder, doc, "doc", (err)=>
  476. @ProjectModel.findOneAndUpdate.called.should.equal false
  477. err.should.deep.equal new Errors.InvalidNameError("file already exists")
  478. done()
  479. it "should error if a file already exists with the same name", (done)->
  480. doc =
  481. _id: ObjectId()
  482. name: "another-file.tex"
  483. @subject._putElement @project, @folder, doc, "doc", (err)=>
  484. @ProjectModel.findOneAndUpdate.called.should.equal false
  485. err.should.deep.equal new Errors.InvalidNameError("file already exists")
  486. done()
  487. it "should error if a folder already exists with the same name", (done)->
  488. doc =
  489. _id: ObjectId()
  490. name: "another-folder"
  491. @subject._putElement @project, @folder, doc, "doc", (err)=>
  492. @ProjectModel.findOneAndUpdate.called.should.equal false
  493. err.should.deep.equal new Errors.InvalidNameError("file already exists")
  494. done()
  495. describe '_checkValidElementName', ->
  496. beforeEach ->
  497. @folder =
  498. docs: [ name: 'doc_name' ]
  499. fileRefs: [ name: 'file_name' ]
  500. folders: [ name: 'folder_name' ]
  501. it 'returns an error if name matches any doc name', ->
  502. @subject._checkValidElementName @folder, 'doc_name', (err) ->
  503. expect(err).to.deep.equal new Errors.InvalidNameError("file already exists")
  504. it 'returns an error if name matches any file name', ->
  505. @subject._checkValidElementName @folder, 'file_name', (err) ->
  506. expect(err).to.deep.equal new Errors.InvalidNameError("file already exists")
  507. it 'returns an error if name matches any folder name', ->
  508. @subject._checkValidElementName @folder, 'folder_name', (err) ->
  509. expect(err).to.deep.equal new Errors.InvalidNameError("file already exists")
  510. it 'returns nothing if name is valid', ->
  511. @subject._checkValidElementName @folder, 'unique_name', (err) ->
  512. expect(err).to.be.undefined
  513. describe '_checkValidMove', ->
  514. beforeEach ->
  515. @destFolder = _id: folder_id
  516. @destFolderPath = fileSystem: '/foo/bar'
  517. @ProjectLocator.findElement = sinon.stub().yields(null, @destFolder, @destFolderPath)
  518. @subject._checkValidElementName = sinon.stub().yields()
  519. it 'checks the element name is valid', ->
  520. @doc = _id: doc_id, name: 'doc_name'
  521. @subject._checkValidMove @project, 'doc', @doc, fileSystem: '/main.tex', @destFolder._id, (err) =>
  522. expect(err).to.be.undefined
  523. @subject._checkValidElementName
  524. .calledWith(@destFolder, @doc.name)
  525. .should.equal true
  526. it 'returns an error if trying to move a folder inside itself', ->
  527. folder = name: 'folder_name'
  528. @subject._checkValidMove @project, 'folder', folder, fileSystem: '/foo', @destFolder._id, (err) =>
  529. expect(err).to.deep.equal new Errors.InvalidNameError("destination folder is a child folder of me")
  530. describe "_insertDeletedDocReference", ->
  531. beforeEach ->
  532. @doc =
  533. _id: ObjectId()
  534. name: "test.tex"
  535. @callback = sinon.stub()
  536. @ProjectModel.update = sinon.stub().yields()
  537. @subject._insertDeletedDocReference project_id, @doc, @callback
  538. it "should insert the doc into deletedDocs", ->
  539. @ProjectModel.update
  540. .calledWith({
  541. _id: project_id
  542. }, {
  543. $push: {
  544. deletedDocs: {
  545. _id: @doc._id
  546. name: @doc.name
  547. }
  548. }
  549. })
  550. .should.equal true
  551. it "should call the callback", ->
  552. @callback.called.should.equal true