ProjectEntityMongoUpdateHandlerTests.coffee 24 KB

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