ProjectEntityMongoUpdateHandlerTests.coffee 22 KB

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