ProjectEntityMongoUpdateHandlerTests.js 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. max-len,
  5. no-return-assign,
  6. no-unused-vars,
  7. standard/no-callback-literal,
  8. */
  9. // TODO: This file was created by bulk-decaffeinate.
  10. // Fix any style issues and re-enable lint.
  11. /*
  12. * decaffeinate suggestions:
  13. * DS102: Remove unnecessary code created because of implicit returns
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. const chai = require('chai')
  17. const { expect } = chai
  18. const { assert } = require('chai')
  19. const should = chai.should()
  20. const sinon = require('sinon')
  21. const tk = require('timekeeper')
  22. const modulePath =
  23. '../../../../app/src/Features/Project/ProjectEntityMongoUpdateHandler'
  24. const Errors = require('../../../../app/src/Features/Errors/Errors')
  25. const { ObjectId } = require('mongoose').Types
  26. const SandboxedModule = require('sandboxed-module')
  27. describe('ProjectEntityMongoUpdateHandler', function() {
  28. const project_id = '4eecb1c1bffa66588e0000a1'
  29. const doc_id = '4eecb1c1bffa66588e0000a2'
  30. const file_id = '4eecb1c1bffa66588e0000a3'
  31. const folder_id = '4eecaffcbffa66588e000008'
  32. beforeEach(function() {
  33. let Folder
  34. this.FolderModel = Folder = class Folder {
  35. constructor(options) {
  36. ;({ name: this.name } = options)
  37. this._id = 'folder_id'
  38. }
  39. }
  40. this.docName = 'doc-name'
  41. this.fileName = 'something.jpg'
  42. this.project = { _id: project_id, name: 'project name' }
  43. this.callback = sinon.stub()
  44. tk.freeze(Date.now())
  45. return (this.subject = SandboxedModule.require(modulePath, {
  46. globals: {
  47. console: console
  48. },
  49. requires: {
  50. 'logger-sharelatex': (this.logger = {
  51. log: sinon.stub(),
  52. warn: sinon.stub(),
  53. error: sinon.stub(),
  54. err() {}
  55. }),
  56. 'settings-sharelatex': (this.settings = {
  57. maxEntitiesPerProject: 100
  58. }),
  59. '../Cooldown/CooldownManager': (this.CooldownManager = {}),
  60. '../../models/Folder': {
  61. Folder: this.FolderModel
  62. },
  63. '../../infrastructure/LockManager': (this.LockManager = {
  64. runWithLock: sinon.spy((namespace, id, runner, callback) =>
  65. runner(callback)
  66. )
  67. }),
  68. '../../models/Project': {
  69. Project: (this.ProjectModel = {})
  70. },
  71. './ProjectEntityHandler': (this.ProjectEntityHandler = {}),
  72. './ProjectLocator': (this.ProjectLocator = {}),
  73. './ProjectGetter': (this.ProjectGetter = {
  74. getProjectWithoutLock: sinon.stub().yields(null, this.project)
  75. })
  76. }
  77. }))
  78. })
  79. afterEach(function() {
  80. return tk.reset()
  81. })
  82. describe('addDoc', function() {
  83. beforeEach(function() {
  84. this.subject._confirmFolder = sinon.stub().yields(folder_id)
  85. this.subject._putElement = sinon.stub()
  86. this.doc = { _id: doc_id }
  87. return this.subject.addDoc(project_id, folder_id, this.doc, this.callback)
  88. })
  89. it('gets the project', function() {
  90. return this.ProjectGetter.getProjectWithoutLock
  91. .calledWith(project_id, {
  92. rootFolder: true,
  93. name: true,
  94. overleaf: true
  95. })
  96. .should.equal(true)
  97. })
  98. it('checks the folder exists', function() {
  99. return this.subject._confirmFolder
  100. .calledWith(this.project, folder_id)
  101. .should.equal(true)
  102. })
  103. it('puts the element in mongo', function() {
  104. return this.subject._putElement
  105. .calledWith(this.project, folder_id, this.doc, 'doc', this.callback)
  106. .should.equal(true)
  107. })
  108. })
  109. describe('addFile', function() {
  110. beforeEach(function() {
  111. this.subject._confirmFolder = sinon.stub().yields(folder_id)
  112. this.subject._putElement = sinon.stub()
  113. this.file = { _id: file_id }
  114. return this.subject.addFile(
  115. project_id,
  116. folder_id,
  117. this.file,
  118. this.callback
  119. )
  120. })
  121. it('gets the project', function() {
  122. return this.ProjectGetter.getProjectWithoutLock
  123. .calledWith(project_id, {
  124. rootFolder: true,
  125. name: true,
  126. overleaf: true
  127. })
  128. .should.equal(true)
  129. })
  130. it('checks the folder exists', function() {
  131. return this.subject._confirmFolder
  132. .calledWith(this.project, folder_id)
  133. .should.equal(true)
  134. })
  135. it('puts the element in mongo', function() {
  136. return this.subject._putElement
  137. .calledWith(this.project, folder_id, this.file, 'file', this.callback)
  138. .should.equal(true)
  139. })
  140. })
  141. describe('replaceFileWithNew', function() {
  142. beforeEach(function() {
  143. this.file = { _id: file_id }
  144. this.path = { mongo: 'file.png' }
  145. this.newFile = { _id: 'new-file-id' }
  146. this.newFile.linkedFileData = this.linkedFileData = { provider: 'url' }
  147. this.newProject = 'new-project'
  148. this.ProjectLocator.findElement = sinon
  149. .stub()
  150. .yields(null, this.file, this.path)
  151. this.ProjectModel.findOneAndUpdate = sinon
  152. .stub()
  153. .yields(null, this.newProject)
  154. this.ProjectModel.update = sinon.stub().yields()
  155. return this.subject.replaceFileWithNew(
  156. project_id,
  157. file_id,
  158. this.newFile,
  159. this.callback
  160. )
  161. })
  162. it('gets the project', function() {
  163. return this.ProjectGetter.getProjectWithoutLock
  164. .calledWith(project_id, {
  165. rootFolder: true,
  166. name: true,
  167. overleaf: true
  168. })
  169. .should.equal(true)
  170. })
  171. it('finds the existing element', function() {
  172. return this.ProjectLocator.findElement
  173. .calledWith({
  174. project: this.project,
  175. element_id: file_id,
  176. type: 'file'
  177. })
  178. .should.equal(true)
  179. })
  180. it('inserts a deletedFile reference for the old file', function() {
  181. return this.ProjectModel.update
  182. .calledWith(
  183. { _id: project_id },
  184. {
  185. $push: {
  186. deletedFiles: {
  187. _id: file_id,
  188. name: this.file.name,
  189. linkedFileData: this.file.linkedFileData,
  190. hash: this.file.hash,
  191. deletedAt: new Date()
  192. }
  193. }
  194. }
  195. )
  196. .should.equal(true)
  197. })
  198. it('increments the project version and sets the rev and created_at', function() {
  199. return this.ProjectModel.findOneAndUpdate
  200. .calledWith(
  201. { _id: project_id },
  202. {
  203. $inc: { version: 1, 'file.png.rev': 1 },
  204. $set: {
  205. 'file.png._id': this.newFile._id,
  206. 'file.png.created': new Date(),
  207. 'file.png.linkedFileData': this.linkedFileData,
  208. 'file.png.hash': this.hash
  209. }
  210. },
  211. { new: true }
  212. )
  213. .should.equal(true)
  214. })
  215. it('calls the callback', function() {
  216. return this.callback
  217. .calledWith(null, this.file, this.project, this.path, this.newProject)
  218. .should.equal(true)
  219. })
  220. })
  221. describe('mkdirp', function() {
  222. beforeEach(function() {
  223. this.parentFolder_id = '1jnjknjk'
  224. this.newFolder = { _id: 'newFolder_id_here' }
  225. this.lastFolder = { _id: '123das', folders: [] }
  226. this.rootFolder = { _id: 'rootFolderId' }
  227. this.project = { _id: project_id, rootFolder: [this.rootFolder] }
  228. this.ProjectGetter.getProjectWithOnlyFolders = sinon
  229. .stub()
  230. .yields(null, this.project)
  231. this.ProjectLocator.findElementByPath = function() {}
  232. sinon.stub(this.ProjectLocator, 'findElementByPath', (options, cb) => {
  233. const { path } = options
  234. this.parentFolder = { _id: 'parentFolder_id_here' }
  235. const lastFolder = path.substring(path.lastIndexOf('/'))
  236. if (lastFolder.indexOf('level1') === -1) {
  237. return cb('level1 is not the last foler ')
  238. } else {
  239. return cb(null, this.parentFolder)
  240. }
  241. })
  242. return (this.subject.addFolder = {
  243. withoutLock: (project_id, parentFolder_id, folderName, callback) => {
  244. return callback(null, { name: folderName }, this.parentFolder_id)
  245. }
  246. })
  247. })
  248. it('should return the root folder if the path is just a slash', function(done) {
  249. const path = '/'
  250. return this.subject.mkdirp(
  251. project_id,
  252. path,
  253. {},
  254. (err, folders, lastFolder) => {
  255. lastFolder.should.deep.equal(this.rootFolder)
  256. assert.equal(lastFolder.parentFolder_id, undefined)
  257. return done()
  258. }
  259. )
  260. })
  261. it('should make just one folder', function(done) {
  262. const path = '/differentFolder/'
  263. return this.subject.mkdirp(
  264. project_id,
  265. path,
  266. {},
  267. (err, folders, lastFolder) => {
  268. folders.length.should.equal(1)
  269. lastFolder.name.should.equal('differentFolder')
  270. lastFolder.parentFolder_id.should.equal(this.parentFolder_id)
  271. return done()
  272. }
  273. )
  274. })
  275. it('should make the final folder in path if it doesnt exist with one level', function(done) {
  276. const path = 'level1/level2'
  277. return this.subject.mkdirp(
  278. project_id,
  279. path,
  280. {},
  281. (err, folders, lastFolder) => {
  282. folders.length.should.equal(1)
  283. lastFolder.name.should.equal('level2')
  284. lastFolder.parentFolder_id.should.equal(this.parentFolder_id)
  285. return done()
  286. }
  287. )
  288. })
  289. it('should make the final folder in path if it doesnt exist with mutliple levels', function(done) {
  290. const path = 'level1/level2/level3'
  291. return this.subject.mkdirp(
  292. project_id,
  293. path,
  294. {},
  295. (err, folders, lastFolder) => {
  296. folders.length.should.equal(2)
  297. folders[0].name.should.equal('level2')
  298. folders[0].parentFolder_id.should.equal(this.parentFolder_id)
  299. lastFolder.name.should.equal('level3')
  300. lastFolder.parentFolder_id.should.equal(this.parentFolder_id)
  301. return done()
  302. }
  303. )
  304. })
  305. it('should work with slashes either side', function(done) {
  306. const path = '/level1/level2/level3/'
  307. return this.subject.mkdirp(
  308. project_id,
  309. path,
  310. {},
  311. (err, folders, lastFolder) => {
  312. folders.length.should.equal(2)
  313. folders[0].name.should.equal('level2')
  314. folders[0].parentFolder_id.should.equal(this.parentFolder_id)
  315. lastFolder.name.should.equal('level3')
  316. lastFolder.parentFolder_id.should.equal(this.parentFolder_id)
  317. return done()
  318. }
  319. )
  320. })
  321. it('should use a case-insensitive match by default', function(done) {
  322. const path = '/differentFolder/'
  323. return this.subject.mkdirp(
  324. project_id,
  325. path,
  326. {},
  327. (err, folders, lastFolder) => {
  328. this.ProjectLocator.findElementByPath
  329. .calledWithMatch({ exactCaseMatch: undefined })
  330. .should.equal(true)
  331. return done()
  332. }
  333. )
  334. })
  335. it('should use a case-sensitive match if exactCaseMatch option is set', function(done) {
  336. const path = '/differentFolder/'
  337. return this.subject.mkdirp(
  338. project_id,
  339. path,
  340. { exactCaseMatch: true },
  341. (err, folders, lastFolder) => {
  342. this.ProjectLocator.findElementByPath
  343. .calledWithMatch({ exactCaseMatch: true })
  344. .should.equal(true)
  345. return done()
  346. }
  347. )
  348. })
  349. })
  350. describe('moveEntity', function() {
  351. beforeEach(function() {
  352. this.pathAfterMove = {
  353. fileSystem: '/somewhere/else.txt'
  354. }
  355. this.ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
  356. this.ProjectEntityHandler.getAllEntitiesFromProject
  357. .onFirstCall()
  358. .yields(
  359. null,
  360. (this.oldDocs = ['old-doc']),
  361. (this.oldFiles = ['old-file'])
  362. )
  363. this.ProjectEntityHandler.getAllEntitiesFromProject
  364. .onSecondCall()
  365. .yields(
  366. null,
  367. (this.newDocs = ['new-doc']),
  368. (this.newFiles = ['new-file'])
  369. )
  370. this.doc = { lines: ['1234', '312343d'], rev: '1234' }
  371. this.path = {
  372. mongo: 'folders[0]',
  373. fileSystem: '/old_folder/somewhere.txt'
  374. }
  375. this.newProject = 'new-project'
  376. this.ProjectLocator.findElement = sinon
  377. .stub()
  378. .withArgs({
  379. project: this.project,
  380. element_id: this.docId,
  381. type: 'docs'
  382. })
  383. .yields(null, this.doc, this.path)
  384. this.subject._checkValidMove = sinon.stub().yields()
  385. this.subject._removeElementFromMongoArray = sinon
  386. .stub()
  387. .yields(null, this.newProject)
  388. this.subject._putElement = sinon
  389. .stub()
  390. .yields(null, { path: this.pathAfterMove }, this.newProject)
  391. return this.subject.moveEntity(
  392. project_id,
  393. doc_id,
  394. folder_id,
  395. 'docs',
  396. this.callback
  397. )
  398. })
  399. it('should get the project', function() {
  400. return this.ProjectGetter.getProjectWithoutLock
  401. .calledWith(project_id, {
  402. rootFolder: true,
  403. name: true,
  404. overleaf: true
  405. })
  406. .should.equal(true)
  407. })
  408. it('should find the doc to move', function() {
  409. return this.ProjectLocator.findElement
  410. .calledWith({ element_id: doc_id, type: 'docs', project: this.project })
  411. .should.equal(true)
  412. })
  413. it('should check this is a valid move', function() {
  414. return this.subject._checkValidMove
  415. .calledWith(this.project, 'docs', this.doc, this.path, folder_id)
  416. .should.equal(true)
  417. })
  418. it('should put the element in the new folder', function() {
  419. return this.subject._putElement
  420. .calledWith(this.project, folder_id, this.doc, 'docs')
  421. .should.equal(true)
  422. })
  423. it('should remove the element from its current position', function() {
  424. return this.subject._removeElementFromMongoArray
  425. .calledWith(this.ProjectModel, project_id, this.path.mongo, doc_id)
  426. .should.equal(true)
  427. })
  428. it('should remove the element from its current position after putting the element in the new folder', function() {
  429. return this.subject._removeElementFromMongoArray
  430. .calledAfter(this.subject._putElement)
  431. .should.equal(true)
  432. })
  433. it('calls the callback', function() {
  434. const changes = {
  435. oldDocs: this.oldDocs,
  436. newDocs: this.newDocs,
  437. oldFiles: this.oldFiles,
  438. newFiles: this.newFiles,
  439. newProject: this.newProject
  440. }
  441. return this.callback
  442. .calledWith(
  443. null,
  444. this.project,
  445. this.path.fileSystem,
  446. this.pathAfterMove.fileSystem,
  447. this.doc.rev,
  448. changes
  449. )
  450. .should.equal(true)
  451. })
  452. })
  453. describe('moveEntity must refuse to move the folder to a subfolder of itself', function() {
  454. beforeEach(function() {
  455. this.pathAfterMove = {
  456. fileSystem: '/somewhere/else.txt'
  457. }
  458. this.doc = { lines: ['1234', '312343d'], rev: '1234' }
  459. this.path = {
  460. mongo: 'folders[0]',
  461. fileSystem: '/old_folder/somewhere.txt'
  462. }
  463. this.newProject = 'new-project'
  464. this.ProjectLocator.findElement = sinon
  465. .stub()
  466. .withArgs({
  467. project: this.project,
  468. element_id: this.docId,
  469. type: 'docs'
  470. })
  471. .yields(null, this.doc, this.path)
  472. // return an error when moving a folder to a subfolder of itself
  473. this.subject._checkValidMove = sinon.stub().yields(new Error())
  474. this.subject._removeElementFromMongoArray = sinon
  475. .stub()
  476. .yields(null, this.project)
  477. this.subject._putElement = sinon
  478. .stub()
  479. .yields(null, { path: this.pathAfterMove }, this.newProject)
  480. return this.subject.moveEntity(
  481. project_id,
  482. doc_id,
  483. folder_id,
  484. 'docs',
  485. this.callback
  486. )
  487. })
  488. it('should get the project', function() {
  489. return this.ProjectGetter.getProjectWithoutLock
  490. .calledWith(project_id, {
  491. rootFolder: true,
  492. name: true,
  493. overleaf: true
  494. })
  495. .should.equal(true)
  496. })
  497. it('should find the doc to move', function() {
  498. return this.ProjectLocator.findElement
  499. .calledWith({ element_id: doc_id, type: 'docs', project: this.project })
  500. .should.equal(true)
  501. })
  502. it('should check this is an invalid move', function() {
  503. return this.subject._checkValidMove
  504. .calledWith(this.project, 'docs', this.doc, this.path, folder_id)
  505. .should.equal(true)
  506. })
  507. it('should not put the element in the new folder', function() {
  508. return this.subject._putElement.called.should.equal(false)
  509. })
  510. it('should not remove the element from its current position', function() {
  511. return this.subject._removeElementFromMongoArray.called.should.equal(
  512. false
  513. )
  514. })
  515. it('calls the callback with an error', function() {
  516. return this.callback.calledWith(new Error()).should.equal(true)
  517. })
  518. })
  519. describe('deleteEntity', function() {
  520. beforeEach(function() {
  521. this.path = { mongo: 'mongo.path', fileSystem: '/file/system/path' }
  522. this.doc = { _id: doc_id }
  523. this.ProjectLocator.findElement = sinon
  524. .stub()
  525. .callsArgWith(1, null, this.doc, this.path)
  526. this.subject._removeElementFromMongoArray = sinon.stub().yields()
  527. return this.subject.deleteEntity(project_id, doc_id, 'doc', this.callback)
  528. })
  529. it('should get the project', function() {
  530. return this.ProjectGetter.getProjectWithoutLock
  531. .calledWith(project_id, {
  532. rootFolder: true,
  533. name: true,
  534. overleaf: true
  535. })
  536. .should.equal(true)
  537. })
  538. it('should find the element', function() {
  539. return this.ProjectLocator.findElement
  540. .calledWith({
  541. project: this.project,
  542. element_id: this.doc._id,
  543. type: 'doc'
  544. })
  545. .should.equal(true)
  546. })
  547. it('should remove the element from the database', function() {
  548. return this.subject._removeElementFromMongoArray
  549. .calledWith(
  550. this.ProjectModel,
  551. project_id,
  552. this.path.mongo,
  553. this.doc._id
  554. )
  555. .should.equal(true)
  556. })
  557. it('calls the callbck', function() {
  558. return this.callback
  559. .calledWith(null, this.doc, this.path, this.project)
  560. .should.equal(true)
  561. })
  562. })
  563. describe('renameEntity', function() {
  564. beforeEach(function() {
  565. this.newName = 'new.tex'
  566. this.path = { mongo: 'mongo.path', fileSystem: '/old.tex' }
  567. this.project = {
  568. _id: ObjectId(project_id),
  569. rootFolder: [{ _id: ObjectId() }]
  570. }
  571. this.doc = { _id: doc_id, name: 'old.tex', rev: 1 }
  572. this.folder = { _id: folder_id }
  573. this.newProject = 'new-project'
  574. this.ProjectGetter.getProjectWithoutLock = sinon
  575. .stub()
  576. .yields(null, this.project)
  577. this.ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
  578. this.ProjectEntityHandler.getAllEntitiesFromProject
  579. .onFirstCall()
  580. .yields(
  581. null,
  582. (this.oldDocs = ['old-doc']),
  583. (this.oldFiles = ['old-file'])
  584. )
  585. this.ProjectEntityHandler.getAllEntitiesFromProject
  586. .onSecondCall()
  587. .yields(
  588. null,
  589. (this.newDocs = ['new-doc']),
  590. (this.newFiles = ['new-file'])
  591. )
  592. this.ProjectLocator.findElement = sinon
  593. .stub()
  594. .yields(null, this.doc, this.path, this.folder)
  595. this.subject._checkValidElementName = sinon.stub().yields()
  596. this.ProjectModel.findOneAndUpdate = sinon
  597. .stub()
  598. .callsArgWith(3, null, this.newProject)
  599. return this.subject.renameEntity(
  600. project_id,
  601. doc_id,
  602. 'doc',
  603. this.newName,
  604. this.callback
  605. )
  606. })
  607. it('should get the project', function() {
  608. return this.ProjectGetter.getProjectWithoutLock
  609. .calledWith(project_id, {
  610. rootFolder: true,
  611. name: true,
  612. overleaf: true
  613. })
  614. .should.equal(true)
  615. })
  616. it('should find the doc', function() {
  617. return this.ProjectLocator.findElement
  618. .calledWith({ element_id: doc_id, type: 'doc', project: this.project })
  619. .should.equal(true)
  620. })
  621. it('should check the new name is valid', function() {
  622. return this.subject._checkValidElementName
  623. .calledWith(this.folder, this.newName)
  624. .should.equal(true)
  625. })
  626. it('should update the doc name', function() {
  627. return this.ProjectModel.findOneAndUpdate
  628. .calledWith(
  629. { _id: project_id },
  630. { $set: { 'mongo.path.name': this.newName }, $inc: { version: 1 } },
  631. { new: true }
  632. )
  633. .should.equal(true)
  634. })
  635. it('calls the callback', function() {
  636. const changes = {
  637. oldDocs: this.oldDocs,
  638. newDocs: this.newDocs,
  639. oldFiles: this.oldFiles,
  640. newFiles: this.newFiles,
  641. newProject: this.newProject
  642. }
  643. return this.callback
  644. .calledWith(
  645. null,
  646. this.project,
  647. '/old.tex',
  648. '/new.tex',
  649. this.doc.rev,
  650. changes
  651. )
  652. .should.equal(true)
  653. })
  654. })
  655. describe('addFolder', function() {
  656. beforeEach(function() {
  657. this.folderName = 'folder1234'
  658. this.ProjectGetter.getProjectWithOnlyFolders = sinon
  659. .stub()
  660. .callsArgWith(1, null, this.project)
  661. this.subject._confirmFolder = sinon.stub().yields(folder_id)
  662. this.subject._putElement = sinon.stub().yields()
  663. return this.subject.addFolder(
  664. project_id,
  665. folder_id,
  666. this.folderName,
  667. this.callback
  668. )
  669. })
  670. it('gets the project', function() {
  671. return this.ProjectGetter.getProjectWithoutLock
  672. .calledWith(project_id, {
  673. rootFolder: true,
  674. name: true,
  675. overleaf: true
  676. })
  677. .should.equal(true)
  678. })
  679. it('checks the parent folder exists', function() {
  680. return this.subject._confirmFolder
  681. .calledWith(this.project, folder_id)
  682. .should.equal(true)
  683. })
  684. it('puts the element in mongo', function() {
  685. const folderMatcher = sinon.match(folder => {
  686. return folder.name === this.folderName
  687. })
  688. return this.subject._putElement
  689. .calledWithMatch(this.project, folder_id, folderMatcher, 'folder')
  690. .should.equal(true)
  691. })
  692. it('calls the callback', function() {
  693. const folderMatcher = sinon.match(folder => {
  694. return folder.name === this.folderName
  695. })
  696. return this.callback
  697. .calledWithMatch(null, folderMatcher, folder_id)
  698. .should.equal(true)
  699. })
  700. })
  701. describe('_removeElementFromMongoArray ', function() {
  702. beforeEach(function() {
  703. this.mongoPath = 'folders[0].folders[5]'
  704. this.id = '12344'
  705. this.entityId = '5678'
  706. this.ProjectModel.update = sinon.stub().yields()
  707. this.ProjectModel.findOneAndUpdate = sinon
  708. .stub()
  709. .yields(null, this.project)
  710. return this.subject._removeElementFromMongoArray(
  711. this.ProjectModel,
  712. this.id,
  713. this.mongoPath,
  714. this.entityId,
  715. this.callback
  716. )
  717. })
  718. it('should pull', function() {
  719. return this.ProjectModel.findOneAndUpdate
  720. .calledWith(
  721. { _id: this.id },
  722. {
  723. $pull: { 'folders[0]': { _id: this.entityId } },
  724. $inc: { version: 1 }
  725. },
  726. { new: true }
  727. )
  728. .should.equal(true)
  729. })
  730. it('should call the callback', function() {
  731. return this.callback.calledWith(null, this.project).should.equal(true)
  732. })
  733. })
  734. describe('_countElements', function() {
  735. beforeEach(function() {
  736. return (this.project = {
  737. _id: project_id,
  738. rootFolder: [
  739. {
  740. docs: [{ _id: 123 }, { _id: 345 }],
  741. fileRefs: [{ _id: 123 }, { _id: 345 }, { _id: 456 }],
  742. folders: [
  743. {
  744. docs: [{ _id: 123 }, { _id: 345 }, { _id: 456 }],
  745. fileRefs: {},
  746. folders: [
  747. {
  748. docs: [{ _id: 1234 }],
  749. fileRefs: [{ _id: 23123 }, { _id: 123213 }, { _id: 2312 }],
  750. folders: [
  751. {
  752. docs: [{ _id: 321321 }, { _id: 123213 }],
  753. fileRefs: [{ _id: 312321 }],
  754. folders: []
  755. }
  756. ]
  757. }
  758. ]
  759. },
  760. {
  761. docs: [{ _id: 123 }, { _id: 32131 }],
  762. fileRefs: [],
  763. folders: [
  764. {
  765. docs: [{ _id: 3123 }],
  766. fileRefs: [
  767. { _id: 321321 },
  768. { _id: 321321 },
  769. { _id: 313122 }
  770. ],
  771. folders: 0
  772. }
  773. ]
  774. }
  775. ]
  776. }
  777. ]
  778. })
  779. })
  780. it('should return the correct number', function() {
  781. return expect(this.subject._countElements(this.project)).to.equal(26)
  782. })
  783. it('should deal with null folders', function() {
  784. this.project.rootFolder[0].folders[0].folders = undefined
  785. return expect(this.subject._countElements(this.project)).to.equal(17)
  786. })
  787. it('should deal with null docs', function() {
  788. this.project.rootFolder[0].folders[0].docs = undefined
  789. return expect(this.subject._countElements(this.project)).to.equal(23)
  790. })
  791. it('should deal with null fileRefs', function() {
  792. this.project.rootFolder[0].folders[0].folders[0].fileRefs = undefined
  793. return expect(this.subject._countElements(this.project)).to.equal(23)
  794. })
  795. })
  796. describe('_putElement', function() {
  797. beforeEach(function() {
  798. this.project = {
  799. _id: project_id,
  800. rootFolder: [{ _id: ObjectId() }]
  801. }
  802. this.folder = {
  803. _id: ObjectId(),
  804. name: 'someFolder',
  805. docs: [{ name: 'another-doc.tex' }],
  806. fileRefs: [{ name: 'another-file.tex' }],
  807. folders: [{ name: 'another-folder' }]
  808. }
  809. this.doc = {
  810. _id: ObjectId(),
  811. name: 'new.tex'
  812. }
  813. this.path = { mongo: 'mongo.path', fileSystem: '/file/system/old.tex' }
  814. this.ProjectLocator.findElement = sinon
  815. .stub()
  816. .yields(null, this.folder, this.path)
  817. return (this.ProjectModel.findOneAndUpdate = sinon
  818. .stub()
  819. .yields(null, this.project))
  820. })
  821. describe('updating the project', function() {
  822. it('should use the correct mongo path', function(done) {
  823. return this.subject._putElement(
  824. this.project,
  825. this.folder._id,
  826. this.doc,
  827. 'docs',
  828. err => {
  829. this.ProjectModel.findOneAndUpdate.args[0][0]._id.should.equal(
  830. this.project._id
  831. )
  832. assert.deepEqual(
  833. this.ProjectModel.findOneAndUpdate.args[0][1].$push[
  834. this.path.mongo + '.docs'
  835. ],
  836. this.doc
  837. )
  838. return done()
  839. }
  840. )
  841. })
  842. it('should return the project in the callback', function(done) {
  843. return this.subject._putElement(
  844. this.project,
  845. this.folder._id,
  846. this.doc,
  847. 'docs',
  848. (err, path, project) => {
  849. assert.equal(project, this.project)
  850. return done()
  851. }
  852. )
  853. })
  854. it('should add an s onto the type if not included', function(done) {
  855. return this.subject._putElement(
  856. this.project,
  857. this.folder._id,
  858. this.doc,
  859. 'doc',
  860. err => {
  861. assert.deepEqual(
  862. this.ProjectModel.findOneAndUpdate.args[0][1].$push[
  863. this.path.mongo + '.docs'
  864. ],
  865. this.doc
  866. )
  867. return done()
  868. }
  869. )
  870. })
  871. it('should not call update if element is null', function(done) {
  872. return this.subject._putElement(
  873. this.project,
  874. this.folder._id,
  875. null,
  876. 'doc',
  877. err => {
  878. this.ProjectModel.findOneAndUpdate.called.should.equal(false)
  879. return done()
  880. }
  881. )
  882. })
  883. it('should default to root folder insert', function(done) {
  884. return this.subject._putElement(
  885. this.project,
  886. null,
  887. this.doc,
  888. 'doc',
  889. err => {
  890. this.ProjectLocator.findElement.args[0][0].element_id.should.equal(
  891. this.project.rootFolder[0]._id
  892. )
  893. return done()
  894. }
  895. )
  896. })
  897. it('should error if the element has no _id', function(done) {
  898. const doc = { name: 'something' }
  899. return this.subject._putElement(
  900. this.project,
  901. this.folder._id,
  902. doc,
  903. 'doc',
  904. err => {
  905. this.ProjectModel.findOneAndUpdate.called.should.equal(false)
  906. return done()
  907. }
  908. )
  909. })
  910. it('should error if element name contains invalid characters', function(done) {
  911. const doc = {
  912. _id: ObjectId(),
  913. name: 'something*bad'
  914. }
  915. return this.subject._putElement(
  916. this.project,
  917. this.folder._id,
  918. doc,
  919. 'doc',
  920. err => {
  921. this.ProjectModel.findOneAndUpdate.called.should.equal(false)
  922. err.should.deep.equal(
  923. new Errors.InvalidNameError('invalid element name')
  924. )
  925. return done()
  926. }
  927. )
  928. })
  929. it('should error if element name is too long', function(done) {
  930. const doc = {
  931. _id: ObjectId(),
  932. name: new Array(200).join('long-') + 'something'
  933. }
  934. return this.subject._putElement(
  935. this.project,
  936. this.folder._id,
  937. doc,
  938. 'doc',
  939. err => {
  940. this.ProjectModel.findOneAndUpdate.called.should.equal(false)
  941. err.should.deep.equal(
  942. new Errors.InvalidNameError('invalid element name')
  943. )
  944. return done()
  945. }
  946. )
  947. })
  948. it('should error if the folder name is too long', function(done) {
  949. this.path = {
  950. mongo: 'mongo.path',
  951. fileSystem: new Array(200).join('subdir/') + 'foo'
  952. }
  953. this.ProjectLocator.findElement.callsArgWith(
  954. 1,
  955. null,
  956. this.folder,
  957. this.path
  958. )
  959. const doc = {
  960. _id: ObjectId(),
  961. name: 'something'
  962. }
  963. return this.subject._putElement(
  964. this.project,
  965. this.folder._id,
  966. doc,
  967. 'doc',
  968. err => {
  969. this.ProjectModel.findOneAndUpdate.called.should.equal(false)
  970. err.should.deep.equal(new Errors.InvalidNameError('path too long'))
  971. return done()
  972. }
  973. )
  974. })
  975. it('should error if a document already exists with the same name', function(done) {
  976. const doc = {
  977. _id: ObjectId(),
  978. name: 'another-doc.tex'
  979. }
  980. return this.subject._putElement(
  981. this.project,
  982. this.folder,
  983. doc,
  984. 'doc',
  985. err => {
  986. this.ProjectModel.findOneAndUpdate.called.should.equal(false)
  987. err.should.deep.equal(
  988. new Errors.InvalidNameError('file already exists')
  989. )
  990. return done()
  991. }
  992. )
  993. })
  994. it('should error if a file already exists with the same name', function(done) {
  995. const doc = {
  996. _id: ObjectId(),
  997. name: 'another-file.tex'
  998. }
  999. return this.subject._putElement(
  1000. this.project,
  1001. this.folder,
  1002. doc,
  1003. 'doc',
  1004. err => {
  1005. this.ProjectModel.findOneAndUpdate.called.should.equal(false)
  1006. err.should.deep.equal(
  1007. new Errors.InvalidNameError('file already exists')
  1008. )
  1009. return done()
  1010. }
  1011. )
  1012. })
  1013. it('should error if a folder already exists with the same name', function(done) {
  1014. const doc = {
  1015. _id: ObjectId(),
  1016. name: 'another-folder'
  1017. }
  1018. return this.subject._putElement(
  1019. this.project,
  1020. this.folder,
  1021. doc,
  1022. 'doc',
  1023. err => {
  1024. this.ProjectModel.findOneAndUpdate.called.should.equal(false)
  1025. err.should.deep.equal(
  1026. new Errors.InvalidNameError('file already exists')
  1027. )
  1028. return done()
  1029. }
  1030. )
  1031. })
  1032. })
  1033. })
  1034. describe('_checkValidElementName', function() {
  1035. beforeEach(function() {
  1036. return (this.folder = {
  1037. docs: [{ name: 'doc_name' }],
  1038. fileRefs: [{ name: 'file_name' }],
  1039. folders: [{ name: 'folder_name' }]
  1040. })
  1041. })
  1042. it('returns an error if name matches any doc name', function() {
  1043. return this.subject._checkValidElementName(this.folder, 'doc_name', err =>
  1044. expect(err).to.deep.equal(
  1045. new Errors.InvalidNameError('file already exists')
  1046. )
  1047. )
  1048. })
  1049. it('returns an error if name matches any file name', function() {
  1050. return this.subject._checkValidElementName(
  1051. this.folder,
  1052. 'file_name',
  1053. err =>
  1054. expect(err).to.deep.equal(
  1055. new Errors.InvalidNameError('file already exists')
  1056. )
  1057. )
  1058. })
  1059. it('returns an error if name matches any folder name', function() {
  1060. return this.subject._checkValidElementName(
  1061. this.folder,
  1062. 'folder_name',
  1063. err =>
  1064. expect(err).to.deep.equal(
  1065. new Errors.InvalidNameError('file already exists')
  1066. )
  1067. )
  1068. })
  1069. it('returns nothing if name is valid', function() {
  1070. return this.subject._checkValidElementName(
  1071. this.folder,
  1072. 'unique_name',
  1073. err => expect(err).to.be.undefined
  1074. )
  1075. })
  1076. })
  1077. describe('_checkValidMove', function() {
  1078. beforeEach(function() {
  1079. this.destFolder = { _id: folder_id }
  1080. this.destFolderPath = { fileSystem: '/foo/bar' }
  1081. this.ProjectLocator.findElement = sinon
  1082. .stub()
  1083. .yields(null, this.destFolder, this.destFolderPath)
  1084. return (this.subject._checkValidElementName = sinon.stub().yields())
  1085. })
  1086. it('checks the element name is valid', function() {
  1087. this.doc = { _id: doc_id, name: 'doc_name' }
  1088. return this.subject._checkValidMove(
  1089. this.project,
  1090. 'doc',
  1091. this.doc,
  1092. { fileSystem: '/main.tex' },
  1093. this.destFolder._id,
  1094. err => {
  1095. expect(err).to.be.undefined
  1096. return this.subject._checkValidElementName
  1097. .calledWith(this.destFolder, this.doc.name)
  1098. .should.equal(true)
  1099. }
  1100. )
  1101. })
  1102. it('returns an error if trying to move a folder inside itself', function() {
  1103. const folder = { name: 'folder_name' }
  1104. return this.subject._checkValidMove(
  1105. this.project,
  1106. 'folder',
  1107. folder,
  1108. { fileSystem: '/foo' },
  1109. this.destFolder._id,
  1110. err => {
  1111. return expect(err).to.deep.equal(
  1112. new Errors.InvalidNameError(
  1113. 'destination folder is a child folder of me'
  1114. )
  1115. )
  1116. }
  1117. )
  1118. })
  1119. })
  1120. describe('_insertDeletedDocReference', function() {
  1121. beforeEach(function() {
  1122. this.doc = {
  1123. _id: ObjectId(),
  1124. name: 'test.tex'
  1125. }
  1126. this.callback = sinon.stub()
  1127. this.ProjectModel.update = sinon.stub().yields()
  1128. return this.subject._insertDeletedDocReference(
  1129. project_id,
  1130. this.doc,
  1131. this.callback
  1132. )
  1133. })
  1134. it('should insert the doc into deletedDocs', function() {
  1135. return this.ProjectModel.update
  1136. .calledWith(
  1137. {
  1138. _id: project_id
  1139. },
  1140. {
  1141. $push: {
  1142. deletedDocs: {
  1143. _id: this.doc._id,
  1144. name: this.doc.name,
  1145. deletedAt: new Date()
  1146. }
  1147. }
  1148. }
  1149. )
  1150. .should.equal(true)
  1151. })
  1152. it('should call the callback', function() {
  1153. return this.callback.called.should.equal(true)
  1154. })
  1155. })
  1156. })