ProjectEntityMongoUpdateHandlerTests.coffee 20 KB

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