ProjectEntityMongoUpdateHandlerTests.coffee 21 KB

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