ProjectEntityMongoUpdateHandlerTests.js 36 KB

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