ProjectEntityMongoUpdateHandlerTests.coffee 20 KB

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