ProjectEntityMongoUpdateHandlerTests.coffee 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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, @newProject)
  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 put the element in the new folder", ->
  231. @subject._putElement
  232. .calledWith(@project, folder_id, @doc, "docs")
  233. .should.equal true
  234. it 'should remove the element from its current position', ->
  235. @subject._removeElementFromMongoArray
  236. .calledWith(@ProjectModel, project_id, @path.mongo, doc_id)
  237. .should.equal true
  238. it 'should remove the element from its current position after putting the element in the new folder', ->
  239. @subject._removeElementFromMongoArray
  240. .calledAfter(@subject._putElement)
  241. .should.equal true
  242. it "calls the callback", ->
  243. changes = { @oldDocs, @newDocs, @oldFiles, @newFiles, @newProject }
  244. @callback.calledWith(
  245. null, @project, @path.fileSystem, @pathAfterMove.fileSystem, @doc.rev, changes
  246. ).should.equal true
  247. describe 'moveEntity must refuse to move the folder to a subfolder of itself', ->
  248. beforeEach ->
  249. @pathAfterMove = {
  250. fileSystem: "/somewhere/else.txt"
  251. }
  252. @doc = {lines:["1234","312343d"], rev: "1234"}
  253. @path = { mongo:"folders[0]", fileSystem:"/old_folder/somewhere.txt" }
  254. @newProject = "new-project"
  255. @ProjectLocator.findElement = sinon.stub()
  256. .withArgs({@project, element_id: @docId, type: 'docs'})
  257. .yields(null, @doc, @path)
  258. # return an error when moving a folder to a subfolder of itself
  259. @subject._checkValidMove = sinon.stub().yields(new Error())
  260. @subject._removeElementFromMongoArray = sinon.stub().yields(null, @project)
  261. @subject._putElement = sinon.stub().yields(null, path: @pathAfterMove, @newProject)
  262. @subject.moveEntity project_id, doc_id, folder_id, "docs", @callback
  263. it 'should get the project', ->
  264. @ProjectGetter.getProjectWithoutLock
  265. .calledWith(project_id, {rootFolder:true, name:true, overleaf:true})
  266. .should.equal true
  267. it 'should find the doc to move', ->
  268. @ProjectLocator.findElement
  269. .calledWith({element_id: doc_id, type: "docs", project: @project })
  270. .should.equal true
  271. it 'should check this is an invalid move', ->
  272. @subject._checkValidMove
  273. .calledWith(@project, 'docs', @doc, @path, folder_id)
  274. .should.equal true
  275. it "should not put the element in the new folder", ->
  276. @subject._putElement.called
  277. .should.equal false
  278. it 'should not remove the element from its current position', ->
  279. @subject._removeElementFromMongoArray.called
  280. .should.equal false
  281. it "calls the callback with an error", ->
  282. @callback.calledWith(
  283. new Error()
  284. ).should.equal true
  285. describe 'deleteEntity', ->
  286. beforeEach ->
  287. @path = mongo: "mongo.path", fileSystem: "/file/system/path"
  288. @doc = _id: doc_id
  289. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, @path)
  290. @subject._removeElementFromMongoArray = sinon.stub().yields()
  291. @subject.deleteEntity project_id, doc_id, 'doc', @callback
  292. it "should get the project", ->
  293. @ProjectGetter.getProjectWithoutLock
  294. .calledWith(project_id, {rootFolder:true, name:true, overleaf:true})
  295. .should.equal true
  296. it "should find the element", ->
  297. @ProjectLocator.findElement
  298. .calledWith({@project, element_id: @doc._id, type: 'doc'})
  299. .should.equal true
  300. it "should remove the element from the database", ->
  301. @subject._removeElementFromMongoArray
  302. .calledWith(@ProjectModel, project_id, @path.mongo, @doc._id)
  303. .should.equal true
  304. it "calls the callbck", ->
  305. @callback.calledWith(null, @doc, @path, @project).should.equal true
  306. describe "renameEntity", ->
  307. beforeEach ->
  308. @newName = "new.tex"
  309. @path = mongo: "mongo.path", fileSystem: "/old.tex"
  310. @project =
  311. _id: ObjectId(project_id)
  312. rootFolder: [_id:ObjectId()]
  313. @doc = _id: doc_id, name: "old.tex", rev: 1
  314. @folder = _id: folder_id
  315. @newProject = "new-project"
  316. @ProjectGetter.getProjectWithoutLock = sinon.stub().yields(null, @project)
  317. @ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
  318. @ProjectEntityHandler.getAllEntitiesFromProject
  319. .onFirstCall()
  320. .yields( null, @oldDocs = ['old-doc'], @oldFiles = ['old-file'])
  321. @ProjectEntityHandler.getAllEntitiesFromProject
  322. .onSecondCall()
  323. .yields( null, @newDocs = ['new-doc'], @newFiles = ['new-file'])
  324. @ProjectLocator.findElement = sinon.stub().yields(null, @doc, @path, @folder)
  325. @subject._checkValidElementName = sinon.stub().yields()
  326. @ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @newProject)
  327. @subject.renameEntity project_id, doc_id, 'doc', @newName, @callback
  328. it 'should get the project', ->
  329. @ProjectGetter.getProjectWithoutLock
  330. .calledWith(project_id, {rootFolder:true, name:true, overleaf:true})
  331. .should.equal true
  332. it 'should find the doc', ->
  333. @ProjectLocator.findElement
  334. .calledWith({element_id: doc_id, type: 'doc', project: @project })
  335. .should.equal true
  336. it 'should check the new name is valid', ->
  337. @subject._checkValidElementName
  338. .calledWith(@folder, @newName)
  339. .should.equal true
  340. it 'should update the doc name', ->
  341. @ProjectModel.findOneAndUpdate
  342. .calledWith(
  343. { _id: project_id },
  344. { $set: { "mongo.path.name": @newName }, $inc: {"version": 1} },
  345. { new: true }
  346. ).should.equal true
  347. it 'calls the callback', ->
  348. changes = { @oldDocs, @newDocs, @oldFiles, @newFiles, @newProject }
  349. @callback.calledWith(
  350. null, @project, '/old.tex', '/new.tex', @doc.rev, changes
  351. ).should.equal true
  352. describe 'addFolder', ->
  353. beforeEach ->
  354. @folderName = "folder1234"
  355. @ProjectGetter.getProjectWithOnlyFolders = sinon.stub().callsArgWith(1, null, @project)
  356. @subject._confirmFolder = sinon.stub().yields(folder_id)
  357. @subject._putElement = sinon.stub().yields()
  358. @subject.addFolder project_id, folder_id, @folderName, @callback
  359. it 'gets the project', ->
  360. @ProjectGetter.getProjectWithoutLock
  361. .calledWith(project_id, {rootFolder:true, name:true, overleaf:true})
  362. .should.equal true
  363. it 'checks the parent folder exists', ->
  364. @subject._confirmFolder
  365. .calledWith(@project, folder_id)
  366. .should.equal true
  367. it 'puts the element in mongo', ->
  368. folderMatcher = sinon.match (folder) =>
  369. folder.name == @folderName
  370. @subject._putElement
  371. .calledWithMatch(@project, folder_id, folderMatcher, 'folder')
  372. .should.equal true
  373. it 'calls the callback', ->
  374. folderMatcher = sinon.match (folder) =>
  375. folder.name == @folderName
  376. @callback.calledWithMatch(null, folderMatcher, folder_id).should.equal true
  377. describe '_removeElementFromMongoArray ', ->
  378. beforeEach ->
  379. @mongoPath = "folders[0].folders[5]"
  380. @id = "12344"
  381. @entityId = "5678"
  382. @ProjectModel.update = sinon.stub().yields()
  383. @ProjectModel.findOneAndUpdate = sinon.stub().yields(null, @project)
  384. @subject._removeElementFromMongoArray @ProjectModel, @id, @mongoPath, @entityId, @callback
  385. it 'should pull', ->
  386. @ProjectModel.findOneAndUpdate
  387. .calledWith({ _id: @id }, { '$pull': { 'folders[0]': {_id: @entityId } }, '$inc': {'version': 1} }, {'new': true})
  388. .should.equal true
  389. it 'should call the callback', ->
  390. @callback.calledWith(null, @project).should.equal true
  391. describe "_countElements", ->
  392. beforeEach ->
  393. @project =
  394. _id: project_id,
  395. rootFolder: [
  396. docs: [{_id:123}, {_id:345}]
  397. fileRefs: [{_id:123}, {_id:345}, {_id:456}]
  398. folders: [
  399. {
  400. docs: [{_id:123}, {_id:345}, {_id:456}]
  401. fileRefs:{}
  402. folders: [
  403. {
  404. docs:[_id:1234],
  405. fileRefs:[{_id:23123}, {_id:123213}, {_id:2312}]
  406. folders:[
  407. {
  408. docs:[{_id:321321}, {_id:123213}]
  409. fileRefs:[{_id:312321}]
  410. folders:[]
  411. }
  412. ]
  413. }
  414. ]
  415. },{
  416. docs:[{_id:123}, {_id:32131}]
  417. fileRefs:[]
  418. folders:[
  419. {
  420. docs:[{_id:3123}]
  421. fileRefs:[{_id:321321}, {_id:321321}, {_id:313122}]
  422. folders:0
  423. }
  424. ]
  425. }
  426. ]
  427. ]
  428. it "should return the correct number", ->
  429. expect(@subject._countElements @project).to.equal(26)
  430. it "should deal with null folders", ->
  431. @project.rootFolder[0].folders[0].folders = undefined
  432. expect(@subject._countElements @project).to.equal(17)
  433. it "should deal with null docs", ->
  434. @project.rootFolder[0].folders[0].docs = undefined
  435. expect(@subject._countElements @project).to.equal(23)
  436. it "should deal with null fileRefs", ->
  437. @project.rootFolder[0].folders[0].folders[0].fileRefs = undefined
  438. expect(@subject._countElements @project).to.equal(23)
  439. describe "_putElement", ->
  440. beforeEach ->
  441. @project =
  442. _id: project_id
  443. rootFolder: [_id:ObjectId()]
  444. @folder =
  445. _id: ObjectId()
  446. name: "someFolder"
  447. docs: [ {name: "another-doc.tex"} ]
  448. fileRefs: [ {name: "another-file.tex"} ]
  449. folders: [ {name: "another-folder"} ]
  450. @doc =
  451. _id: ObjectId()
  452. name: "new.tex"
  453. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  454. @ProjectLocator.findElement = sinon.stub().yields(null, @folder, @path)
  455. @ProjectModel.findOneAndUpdate = sinon.stub().yields(null, @project)
  456. describe "updating the project", ->
  457. it "should use the correct mongo path", (done)->
  458. @subject._putElement @project, @folder._id, @doc, "docs", (err)=>
  459. @ProjectModel.findOneAndUpdate.args[0][0]._id.should.equal @project._id
  460. assert.deepEqual @ProjectModel.findOneAndUpdate.args[0][1].$push[@path.mongo+".docs"], @doc
  461. done()
  462. it "should return the project in the callback", (done)->
  463. @subject._putElement @project, @folder._id, @doc, "docs", (err, path, project)=>
  464. assert.equal project, @project
  465. done()
  466. it "should add an s onto the type if not included", (done)->
  467. @subject._putElement @project, @folder._id, @doc, "doc", (err)=>
  468. assert.deepEqual @ProjectModel.findOneAndUpdate.args[0][1].$push[@path.mongo+".docs"], @doc
  469. done()
  470. it "should not call update if element is null", (done)->
  471. @subject._putElement @project, @folder._id, null, "doc", (err)=>
  472. @ProjectModel.findOneAndUpdate.called.should.equal false
  473. done()
  474. it "should default to root folder insert", (done)->
  475. @subject._putElement @project, null, @doc, "doc", (err)=>
  476. @ProjectLocator.findElement.args[0][0].element_id.should.equal @project.rootFolder[0]._id
  477. done()
  478. it "should error if the element has no _id", (done)->
  479. doc =
  480. name:"something"
  481. @subject._putElement @project, @folder._id, doc, "doc", (err)=>
  482. @ProjectModel.findOneAndUpdate.called.should.equal false
  483. done()
  484. it "should error if element name contains invalid characters", (done)->
  485. doc =
  486. _id: ObjectId()
  487. name: "something*bad"
  488. @subject._putElement @project, @folder._id, doc, "doc", (err)=>
  489. @ProjectModel.findOneAndUpdate.called.should.equal false
  490. err.should.deep.equal new Errors.InvalidNameError("invalid element name")
  491. done()
  492. it "should error if element name is too long", (done)->
  493. doc =
  494. _id: ObjectId()
  495. name: new Array(200).join("long-") + "something"
  496. @subject._putElement @project, @folder._id, doc, "doc", (err)=>
  497. @ProjectModel.findOneAndUpdate.called.should.equal false
  498. err.should.deep.equal new Errors.InvalidNameError("invalid element name")
  499. done()
  500. it "should error if the folder name is too long", (done)->
  501. @path =
  502. mongo: "mongo.path",
  503. fileSystem: new Array(200).join("subdir/") + "foo"
  504. @ProjectLocator.findElement.callsArgWith(1, null, @folder, @path)
  505. doc =
  506. _id: ObjectId()
  507. name: "something"
  508. @subject._putElement @project, @folder._id, doc, "doc", (err)=>
  509. @ProjectModel.findOneAndUpdate.called.should.equal false
  510. err.should.deep.equal new Errors.InvalidNameError("path too long")
  511. done()
  512. it "should error if a document already exists with the same name", (done)->
  513. doc =
  514. _id: ObjectId()
  515. name: "another-doc.tex"
  516. @subject._putElement @project, @folder, doc, "doc", (err)=>
  517. @ProjectModel.findOneAndUpdate.called.should.equal false
  518. err.should.deep.equal new Errors.InvalidNameError("file already exists")
  519. done()
  520. it "should error if a file already exists with the same name", (done)->
  521. doc =
  522. _id: ObjectId()
  523. name: "another-file.tex"
  524. @subject._putElement @project, @folder, doc, "doc", (err)=>
  525. @ProjectModel.findOneAndUpdate.called.should.equal false
  526. err.should.deep.equal new Errors.InvalidNameError("file already exists")
  527. done()
  528. it "should error if a folder already exists with the same name", (done)->
  529. doc =
  530. _id: ObjectId()
  531. name: "another-folder"
  532. @subject._putElement @project, @folder, doc, "doc", (err)=>
  533. @ProjectModel.findOneAndUpdate.called.should.equal false
  534. err.should.deep.equal new Errors.InvalidNameError("file already exists")
  535. done()
  536. describe '_checkValidElementName', ->
  537. beforeEach ->
  538. @folder =
  539. docs: [ name: 'doc_name' ]
  540. fileRefs: [ name: 'file_name' ]
  541. folders: [ name: 'folder_name' ]
  542. it 'returns an error if name matches any doc name', ->
  543. @subject._checkValidElementName @folder, 'doc_name', (err) ->
  544. expect(err).to.deep.equal new Errors.InvalidNameError("file already exists")
  545. it 'returns an error if name matches any file name', ->
  546. @subject._checkValidElementName @folder, 'file_name', (err) ->
  547. expect(err).to.deep.equal new Errors.InvalidNameError("file already exists")
  548. it 'returns an error if name matches any folder name', ->
  549. @subject._checkValidElementName @folder, 'folder_name', (err) ->
  550. expect(err).to.deep.equal new Errors.InvalidNameError("file already exists")
  551. it 'returns nothing if name is valid', ->
  552. @subject._checkValidElementName @folder, 'unique_name', (err) ->
  553. expect(err).to.be.undefined
  554. describe '_checkValidMove', ->
  555. beforeEach ->
  556. @destFolder = _id: folder_id
  557. @destFolderPath = fileSystem: '/foo/bar'
  558. @ProjectLocator.findElement = sinon.stub().yields(null, @destFolder, @destFolderPath)
  559. @subject._checkValidElementName = sinon.stub().yields()
  560. it 'checks the element name is valid', ->
  561. @doc = _id: doc_id, name: 'doc_name'
  562. @subject._checkValidMove @project, 'doc', @doc, fileSystem: '/main.tex', @destFolder._id, (err) =>
  563. expect(err).to.be.undefined
  564. @subject._checkValidElementName
  565. .calledWith(@destFolder, @doc.name)
  566. .should.equal true
  567. it 'returns an error if trying to move a folder inside itself', ->
  568. folder = name: 'folder_name'
  569. @subject._checkValidMove @project, 'folder', folder, fileSystem: '/foo', @destFolder._id, (err) =>
  570. expect(err).to.deep.equal new Errors.InvalidNameError("destination folder is a child folder of me")
  571. describe "_insertDeletedDocReference", ->
  572. beforeEach ->
  573. @doc =
  574. _id: ObjectId()
  575. name: "test.tex"
  576. @callback = sinon.stub()
  577. @ProjectModel.update = sinon.stub().yields()
  578. @subject._insertDeletedDocReference project_id, @doc, @callback
  579. it "should insert the doc into deletedDocs", ->
  580. @ProjectModel.update
  581. .calledWith({
  582. _id: project_id
  583. }, {
  584. $push: {
  585. deletedDocs: {
  586. _id: @doc._id
  587. name: @doc.name
  588. }
  589. }
  590. })
  591. .should.equal true
  592. it "should call the callback", ->
  593. @callback.called.should.equal true