ProjectEntityMongoUpdateHandler.test.mjs 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. const { expect } = require('chai')
  2. const sinon = require('sinon')
  3. const tk = require('timekeeper')
  4. const Errors = require('../../../../app/src/Features/Errors/Errors')
  5. const { ObjectId } = require('mongodb-legacy')
  6. const SandboxedModule = require('sandboxed-module')
  7. const { Project } = require('../helpers/models/Project')
  8. const MODULE_PATH =
  9. '../../../../app/src/Features/Project/ProjectEntityMongoUpdateHandler'
  10. describe('ProjectEntityMongoUpdateHandler', function () {
  11. beforeEach(function () {
  12. tk.freeze(new Date())
  13. this.doc = {
  14. _id: new ObjectId(),
  15. name: 'test-doc.txt',
  16. lines: ['hello', 'world'],
  17. rev: 1234,
  18. }
  19. this.docPath = {
  20. mongo: 'rootFolder.0.docs.0',
  21. fileSystem: '/test-doc.txt',
  22. }
  23. this.file = {
  24. _id: new ObjectId(),
  25. name: 'something.jpg',
  26. linkedFileData: { provider: 'url' },
  27. hash: 'some-hash',
  28. }
  29. this.filePath = {
  30. fileSystem: '/something.png',
  31. mongo: 'rootFolder.0.fileRefs.0',
  32. }
  33. this.subfolder = { _id: new ObjectId(), name: 'test-subfolder' }
  34. this.subfolderPath = {
  35. fileSystem: '/test-folder/test-subfolder',
  36. mongo: 'rootFolder.0.folders.0.folders.0',
  37. }
  38. this.notSubfolder = { _id: new ObjectId(), name: 'test-folder-2' }
  39. this.notSubfolderPath = {
  40. fileSystem: '/test-folder-2/test-subfolder',
  41. mongo: 'rootFolder.0.folders.0.folders.0',
  42. }
  43. this.folder = {
  44. _id: new ObjectId(),
  45. name: 'test-folder',
  46. folders: [this.subfolder],
  47. }
  48. this.folderPath = {
  49. fileSystem: '/test-folder',
  50. mongo: 'rootFolder.0.folders.0',
  51. }
  52. this.rootFolder = {
  53. _id: new ObjectId(),
  54. folders: [this.folder],
  55. docs: [this.doc],
  56. fileRefs: [this.file],
  57. }
  58. this.rootFolderPath = {
  59. fileSystem: '/',
  60. mongo: 'rootFolder.0',
  61. }
  62. this.project = {
  63. _id: new ObjectId(),
  64. name: 'project name',
  65. rootFolder: [this.rootFolder],
  66. }
  67. this.Settings = { maxEntitiesPerProject: 100 }
  68. this.CooldownManager = {}
  69. this.LockManager = {
  70. promises: {
  71. runWithLock: sinon.spy((namespace, id, runner) => runner()),
  72. },
  73. }
  74. this.FolderModel = sinon.stub()
  75. this.ProjectMock = sinon.mock(Project)
  76. this.ProjectEntityHandler = {
  77. getAllEntitiesFromProject: sinon.stub(),
  78. }
  79. this.ProjectLocator = {
  80. findElementByMongoPath: sinon.stub().throws(new Error('not found')),
  81. promises: {
  82. findElement: sinon.stub().rejects(new Error('not found')),
  83. findElementByPath: sinon.stub().rejects(new Error('not found')),
  84. },
  85. }
  86. this.ProjectLocator.promises.findElement
  87. .withArgs({
  88. project: this.project,
  89. element_id: this.rootFolder._id,
  90. type: 'folder',
  91. })
  92. .resolves({
  93. element: this.rootFolder,
  94. path: this.rootFolderPath,
  95. })
  96. this.ProjectLocator.promises.findElement
  97. .withArgs({
  98. project: this.project,
  99. element_id: this.folder._id,
  100. type: 'folder',
  101. })
  102. .resolves({
  103. element: this.folder,
  104. path: this.folderPath,
  105. folder: this.rootFolder,
  106. })
  107. this.ProjectLocator.promises.findElement
  108. .withArgs({
  109. project: this.project,
  110. element_id: this.subfolder._id,
  111. type: 'folder',
  112. })
  113. .resolves({
  114. element: this.subfolder,
  115. path: this.subfolderPath,
  116. folder: this.folder,
  117. })
  118. this.ProjectLocator.promises.findElement
  119. .withArgs({
  120. project: this.project,
  121. element_id: this.file._id,
  122. type: 'file',
  123. })
  124. .resolves({
  125. element: this.file,
  126. path: this.filePath,
  127. folder: this.rootFolder,
  128. })
  129. this.ProjectLocator.promises.findElement
  130. .withArgs({
  131. project: this.project,
  132. element_id: this.doc._id,
  133. type: 'doc',
  134. })
  135. .resolves({
  136. element: this.doc,
  137. path: this.docPath,
  138. folder: this.rootFolder,
  139. })
  140. this.ProjectLocator.promises.findElementByPath
  141. .withArgs(
  142. sinon.match({
  143. project: this.project,
  144. path: '/',
  145. })
  146. )
  147. .resolves({ element: this.rootFolder, type: 'folder', folder: null })
  148. this.ProjectLocator.promises.findElementByPath
  149. .withArgs(
  150. sinon.match({
  151. project: this.project,
  152. path: '/test-folder',
  153. })
  154. )
  155. .resolves({
  156. element: this.folder,
  157. type: 'folder',
  158. folder: this.rootFolder,
  159. })
  160. this.ProjectLocator.promises.findElementByPath
  161. .withArgs(
  162. sinon.match({
  163. project: this.project,
  164. path: '/test-folder/test-subfolder',
  165. })
  166. )
  167. .resolves({
  168. element: this.subfolder,
  169. type: 'folder',
  170. folder: this.folder,
  171. })
  172. this.ProjectGetter = {
  173. promises: {
  174. getProjectWithoutLock: sinon
  175. .stub()
  176. .withArgs(this.project._id)
  177. .resolves(this.project),
  178. getProjectWithOnlyFolders: sinon.stub().resolves(this.project),
  179. },
  180. }
  181. this.FolderStructureBuilder = {
  182. buildFolderStructure: sinon.stub(),
  183. }
  184. this.subject = SandboxedModule.require(MODULE_PATH, {
  185. requires: {
  186. 'mongodb-legacy': { ObjectId },
  187. '@overleaf/settings': this.Settings,
  188. '../Cooldown/CooldownManager': this.CooldownManager,
  189. '../../models/Folder': { Folder: this.FolderModel },
  190. '../../infrastructure/LockManager': this.LockManager,
  191. '../../models/Project': { Project },
  192. './ProjectEntityHandler': this.ProjectEntityHandler,
  193. './ProjectLocator': this.ProjectLocator,
  194. './ProjectGetter': this.ProjectGetter,
  195. './FolderStructureBuilder': this.FolderStructureBuilder,
  196. },
  197. })
  198. })
  199. afterEach(function () {
  200. this.ProjectMock.restore()
  201. tk.reset()
  202. })
  203. describe('addDoc', function () {
  204. beforeEach(async function () {
  205. const doc = { _id: new ObjectId(), name: 'other.txt' }
  206. const userId = new ObjectId().toString()
  207. this.ProjectMock.expects('findOneAndUpdate')
  208. .withArgs(
  209. {
  210. _id: this.project._id,
  211. 'rootFolder.0.folders.0': { $exists: true },
  212. },
  213. {
  214. $push: { 'rootFolder.0.folders.0.docs': doc },
  215. $inc: { version: 1 },
  216. $set: { lastUpdated: new Date(), lastUpdatedBy: userId },
  217. }
  218. )
  219. .chain('exec')
  220. .resolves(this.project)
  221. this.result = await this.subject.promises.addDoc(
  222. this.project._id,
  223. this.folder._id,
  224. doc,
  225. userId
  226. )
  227. })
  228. it('adds the document in Mongo', function () {
  229. this.ProjectMock.verify()
  230. })
  231. it('returns path info and the project', function () {
  232. expect(this.result).to.deep.equal({
  233. result: {
  234. path: {
  235. mongo: 'rootFolder.0.folders.0',
  236. fileSystem: '/test-folder/other.txt',
  237. },
  238. },
  239. project: this.project,
  240. })
  241. })
  242. })
  243. describe('addFile', function () {
  244. let userId
  245. beforeEach(function () {
  246. userId = new ObjectId().toString()
  247. this.newFile = { _id: new ObjectId(), name: 'picture.jpg' }
  248. this.ProjectMock.expects('findOneAndUpdate')
  249. .withArgs(
  250. {
  251. _id: this.project._id,
  252. 'rootFolder.0.folders.0': { $exists: true },
  253. },
  254. {
  255. $push: { 'rootFolder.0.folders.0.fileRefs': this.newFile },
  256. $inc: { version: 1 },
  257. $set: { lastUpdated: new Date(), lastUpdatedBy: userId },
  258. }
  259. )
  260. .chain('exec')
  261. .resolves(this.project)
  262. })
  263. describe('happy path', function () {
  264. beforeEach(async function () {
  265. this.result = await this.subject.promises.addFile(
  266. this.project._id,
  267. this.folder._id,
  268. this.newFile,
  269. userId
  270. )
  271. })
  272. it('adds the file in Mongo', function () {
  273. this.ProjectMock.verify()
  274. })
  275. it('returns path info and the project', function () {
  276. expect(this.result).to.deep.equal({
  277. result: {
  278. path: {
  279. mongo: 'rootFolder.0.folders.0',
  280. fileSystem: '/test-folder/picture.jpg',
  281. },
  282. },
  283. project: this.project,
  284. })
  285. })
  286. })
  287. describe('when entity limit is reached', function () {
  288. beforeEach(function () {
  289. this.savedMaxEntities = this.Settings.maxEntitiesPerProject
  290. this.Settings.maxEntitiesPerProject = 3
  291. })
  292. afterEach(function () {
  293. this.Settings.maxEntitiesPerProject = this.savedMaxEntities
  294. })
  295. it('should throw an error', async function () {
  296. await expect(
  297. this.subject.promises.addFile(
  298. this.project._id,
  299. this.folder._id,
  300. this.newFile,
  301. userId
  302. )
  303. ).to.be.rejected
  304. })
  305. })
  306. })
  307. describe('addFolder', function () {
  308. beforeEach(async function () {
  309. const userId = new ObjectId().toString()
  310. const folderName = 'New folder'
  311. this.FolderModel.withArgs({ name: folderName }).returns({
  312. _id: new ObjectId(),
  313. name: folderName,
  314. })
  315. this.ProjectMock.expects('findOneAndUpdate')
  316. .withArgs(
  317. {
  318. _id: this.project._id,
  319. 'rootFolder.0.folders.0': { $exists: true },
  320. },
  321. {
  322. $push: {
  323. 'rootFolder.0.folders.0.folders': sinon.match({
  324. name: folderName,
  325. }),
  326. },
  327. $inc: { version: 1 },
  328. $set: { lastUpdated: new Date(), lastUpdatedBy: userId },
  329. }
  330. )
  331. .chain('exec')
  332. .resolves(this.project)
  333. await this.subject.promises.addFolder(
  334. this.project._id,
  335. this.folder._id,
  336. folderName,
  337. userId
  338. )
  339. })
  340. it('adds the folder in Mongo', function () {
  341. this.ProjectMock.verify()
  342. })
  343. })
  344. describe('replaceFileWithNew', function () {
  345. beforeEach(async function () {
  346. const newFile = {
  347. _id: new ObjectId(),
  348. name: 'some-other-file.png',
  349. linkedFileData: { some: 'data' },
  350. hash: 'some-hash',
  351. }
  352. // Update the file in place
  353. this.ProjectMock.expects('findOneAndUpdate')
  354. .withArgs(
  355. {
  356. _id: this.project._id,
  357. 'rootFolder.0.fileRefs.0': { $exists: true },
  358. },
  359. {
  360. $set: {
  361. 'rootFolder.0.fileRefs.0._id': newFile._id,
  362. 'rootFolder.0.fileRefs.0.created': sinon.match.date,
  363. 'rootFolder.0.fileRefs.0.linkedFileData': newFile.linkedFileData,
  364. 'rootFolder.0.fileRefs.0.hash': newFile.hash,
  365. lastUpdated: new Date(),
  366. lastUpdatedBy: 'userId',
  367. },
  368. $inc: {
  369. version: 1,
  370. 'rootFolder.0.fileRefs.0.rev': 1,
  371. },
  372. }
  373. )
  374. .chain('exec')
  375. .resolves(this.project)
  376. this.ProjectLocator.findElementByMongoPath
  377. .withArgs(this.project, 'rootFolder.0.fileRefs.0')
  378. .returns(newFile)
  379. await this.subject.promises.replaceFileWithNew(
  380. this.project._id,
  381. this.file._id,
  382. newFile,
  383. 'userId'
  384. )
  385. })
  386. it('updates the database', function () {
  387. this.ProjectMock.verify()
  388. })
  389. })
  390. describe('mkdirp', function () {
  391. describe('when the path is just a slash', function () {
  392. beforeEach(async function () {
  393. this.result = await this.subject.promises.mkdirp(this.project._id, '/')
  394. })
  395. it('should return the root folder', function () {
  396. expect(this.result.folder).to.deep.equal(this.rootFolder)
  397. })
  398. it('should not report a parent folder', function () {
  399. expect(this.result.folder.parentFolder_id).not.to.exist
  400. })
  401. it('should not return new folders', function () {
  402. expect(this.result.newFolders).to.have.length(0)
  403. })
  404. })
  405. describe('when the folder already exists', function () {
  406. beforeEach(async function () {
  407. this.result = await this.subject.promises.mkdirp(
  408. this.project._id,
  409. '/test-folder'
  410. )
  411. })
  412. it('should return the existing folder', function () {
  413. expect(this.result.folder).to.deep.equal(this.folder)
  414. })
  415. it('should report the parent folder', function () {
  416. expect(this.result.folder.parentFolder_id).to.equal(this.rootFolder._id)
  417. })
  418. it('should not return new folders', function () {
  419. expect(this.result.newFolders).to.have.length(0)
  420. })
  421. })
  422. describe('when the path is a new folder at the top level', function () {
  423. beforeEach(async function () {
  424. const userId = new ObjectId().toString()
  425. this.newFolder = { _id: new ObjectId(), name: 'new-folder' }
  426. this.FolderModel.returns(this.newFolder)
  427. this.exactCaseMatch = false
  428. this.ProjectMock.expects('findOneAndUpdate')
  429. .withArgs(
  430. { _id: this.project._id, 'rootFolder.0': { $exists: true } },
  431. {
  432. $push: { 'rootFolder.0.folders': this.newFolder },
  433. $inc: { version: 1 },
  434. $set: { lastUpdated: new Date(), lastUpdatedBy: userId },
  435. }
  436. )
  437. .chain('exec')
  438. .resolves(this.project)
  439. this.result = await this.subject.promises.mkdirp(
  440. this.project._id,
  441. '/new-folder/',
  442. userId,
  443. { exactCaseMatch: this.exactCaseMatch }
  444. )
  445. })
  446. it('should update the database', function () {
  447. this.ProjectMock.verify()
  448. })
  449. it('should make just one folder', function () {
  450. expect(this.result.newFolders).to.have.length(1)
  451. })
  452. it('should return the new folder', function () {
  453. expect(this.result.folder.name).to.equal('new-folder')
  454. })
  455. it('should return the parent folder', function () {
  456. expect(this.result.folder.parentFolder_id).to.equal(this.rootFolder._id)
  457. })
  458. it('should pass the exactCaseMatch option to ProjectLocator', function () {
  459. expect(
  460. this.ProjectLocator.promises.findElementByPath
  461. ).to.have.been.calledWithMatch({ exactCaseMatch: this.exactCaseMatch })
  462. })
  463. })
  464. describe('adding a subfolder', function () {
  465. beforeEach(async function () {
  466. const userId = new ObjectId().toString()
  467. this.newFolder = { _id: new ObjectId(), name: 'new-folder' }
  468. this.FolderModel.returns(this.newFolder)
  469. this.ProjectMock.expects('findOneAndUpdate')
  470. .withArgs(
  471. {
  472. _id: this.project._id,
  473. 'rootFolder.0.folders.0': { $exists: true },
  474. },
  475. {
  476. $push: {
  477. 'rootFolder.0.folders.0.folders': sinon.match({
  478. name: 'new-folder',
  479. }),
  480. },
  481. $inc: { version: 1 },
  482. $set: { lastUpdated: new Date(), lastUpdatedBy: userId },
  483. }
  484. )
  485. .chain('exec')
  486. .resolves(this.project)
  487. this.result = await this.subject.promises.mkdirp(
  488. this.project._id,
  489. '/test-folder/new-folder',
  490. userId
  491. )
  492. })
  493. it('should update the database', function () {
  494. this.ProjectMock.verify()
  495. })
  496. it('should create one folder', function () {
  497. expect(this.result.newFolders).to.have.length(1)
  498. })
  499. it('should return the new folder', function () {
  500. expect(this.result.folder.name).to.equal('new-folder')
  501. })
  502. it('should return the parent folder', function () {
  503. expect(this.result.folder.parentFolder_id).to.equal(this.folder._id)
  504. })
  505. })
  506. describe('when mutliple folders are missing', async function () {
  507. let userId
  508. beforeEach(function () {
  509. userId = new ObjectId().toString()
  510. this.folder1 = { _id: new ObjectId(), name: 'folder1' }
  511. this.folder1Path = {
  512. fileSystem: '/test-folder/folder1',
  513. mongo: 'rootFolder.0.folders.0.folders.0',
  514. }
  515. this.folder2 = { _id: new ObjectId(), name: 'folder2' }
  516. this.folder2Path = {
  517. fileSystem: '/test-folder/folder1/folder2',
  518. mongo: 'rootFolder.0.folders.0.folders.0.folders.0',
  519. }
  520. this.FolderModel.onFirstCall().returns(this.folder1)
  521. this.FolderModel.onSecondCall().returns(this.folder2)
  522. this.ProjectLocator.promises.findElement
  523. .withArgs({
  524. project: this.project,
  525. element_id: this.folder1._id,
  526. type: 'folder',
  527. })
  528. .resolves({
  529. element: this.folder1,
  530. path: this.folder1Path,
  531. })
  532. this.ProjectLocator.promises.findElement
  533. .withArgs({
  534. project: this.project,
  535. element_id: this.folder2._id,
  536. type: 'folder',
  537. })
  538. .resolves({
  539. element: this.folder2,
  540. path: this.folder2Path,
  541. })
  542. this.ProjectMock.expects('findOneAndUpdate')
  543. .withArgs(
  544. {
  545. _id: this.project._id,
  546. 'rootFolder.0.folders.0': { $exists: true },
  547. },
  548. {
  549. $push: {
  550. 'rootFolder.0.folders.0.folders': sinon.match({
  551. name: 'folder1',
  552. }),
  553. },
  554. $inc: { version: 1 },
  555. $set: { lastUpdated: new Date(), lastUpdatedBy: userId },
  556. }
  557. )
  558. .chain('exec')
  559. .resolves(this.project)
  560. this.ProjectMock.expects('findOneAndUpdate')
  561. .withArgs(
  562. {
  563. _id: this.project._id,
  564. 'rootFolder.0.folders.0.folders.0': { $exists: true },
  565. },
  566. {
  567. $push: {
  568. 'rootFolder.0.folders.0.folders.0.folders': sinon.match({
  569. name: 'folder2',
  570. }),
  571. },
  572. $inc: { version: 1 },
  573. $set: { lastUpdated: new Date(), lastUpdatedBy: userId },
  574. }
  575. )
  576. .chain('exec')
  577. .resolves(this.project)
  578. })
  579. ;[
  580. {
  581. description: 'without a trailing slash',
  582. path: '/test-folder/folder1/folder2',
  583. },
  584. {
  585. description: 'with a trailing slash',
  586. path: '/test-folder/folder1/folder2/',
  587. },
  588. ].forEach(({ description, path }) => {
  589. describe(description, function () {
  590. beforeEach(async function () {
  591. this.result = await this.subject.promises.mkdirp(
  592. this.project._id,
  593. path,
  594. userId
  595. )
  596. })
  597. it('should update the database', function () {
  598. this.ProjectMock.verify()
  599. })
  600. it('should add multiple folders', function () {
  601. const newFolders = this.result.newFolders
  602. expect(newFolders).to.have.length(2)
  603. expect(newFolders[0].name).to.equal('folder1')
  604. expect(newFolders[1].name).to.equal('folder2')
  605. })
  606. it('should return the last folder', function () {
  607. expect(this.result.folder.name).to.equal('folder2')
  608. })
  609. it('should return the parent folder', function () {
  610. expect(this.result.folder.parentFolder_id).to.equal(
  611. this.folder1._id
  612. )
  613. })
  614. })
  615. })
  616. })
  617. })
  618. describe('moveEntity', function () {
  619. describe('moving a doc into a different folder', function () {
  620. beforeEach(async function () {
  621. const userId = new ObjectId().toString()
  622. this.pathAfterMove = {
  623. fileSystem: '/somewhere/else.txt',
  624. }
  625. this.oldDocs = ['old-doc']
  626. this.oldFiles = ['old-file']
  627. this.newDocs = ['new-doc']
  628. this.newFiles = ['new-file']
  629. this.ProjectEntityHandler.getAllEntitiesFromProject
  630. .onFirstCall()
  631. .returns({ docs: this.oldDocs, files: this.oldFiles })
  632. this.ProjectEntityHandler.getAllEntitiesFromProject
  633. .onSecondCall()
  634. .returns({ docs: this.newDocs, files: this.newFiles })
  635. this.ProjectMock.expects('findOneAndUpdate')
  636. .withArgs(
  637. {
  638. _id: this.project._id,
  639. 'rootFolder.0.folders.0': { $exists: true },
  640. },
  641. {
  642. $push: { 'rootFolder.0.folders.0.docs': this.doc },
  643. $inc: { version: 1 },
  644. $set: { lastUpdated: new Date(), lastUpdatedBy: userId },
  645. }
  646. )
  647. .chain('exec')
  648. .resolves(this.project)
  649. this.ProjectMock.expects('findOneAndUpdate')
  650. .withArgs(
  651. { _id: this.project._id },
  652. {
  653. $pull: { 'rootFolder.0.docs': { _id: this.doc._id } },
  654. $inc: { version: 1 },
  655. $set: { lastUpdated: new Date(), lastUpdatedBy: userId },
  656. }
  657. )
  658. .chain('exec')
  659. .resolves(this.project)
  660. this.result = await this.subject.promises.moveEntity(
  661. this.project._id,
  662. this.doc._id,
  663. this.folder._id,
  664. 'doc',
  665. userId
  666. )
  667. })
  668. it('should update the database', function () {
  669. this.ProjectMock.verify()
  670. })
  671. it('should report what changed', function () {
  672. expect(this.result).to.deep.equal({
  673. project: this.project,
  674. startPath: '/test-doc.txt',
  675. endPath: '/test-folder/test-doc.txt',
  676. rev: this.doc.rev,
  677. changes: {
  678. oldDocs: this.oldDocs,
  679. newDocs: this.newDocs,
  680. oldFiles: this.oldFiles,
  681. newFiles: this.newFiles,
  682. newProject: this.project,
  683. },
  684. })
  685. })
  686. })
  687. describe('when moving a folder inside itself', function () {
  688. it('throws an error', async function () {
  689. await expect(
  690. this.subject.promises.moveEntity(
  691. this.project._id,
  692. this.folder._id,
  693. this.folder._id,
  694. 'folder'
  695. )
  696. ).to.be.rejectedWith(Errors.InvalidNameError)
  697. })
  698. })
  699. describe('when moving a folder to a subfolder of itself', function () {
  700. it('throws an error', async function () {
  701. await expect(
  702. this.subject.promises.moveEntity(
  703. this.project._id,
  704. this.folder._id,
  705. this.subfolder._id,
  706. 'folder'
  707. )
  708. ).to.be.rejectedWith(Errors.InvalidNameError)
  709. })
  710. })
  711. describe('when moving a folder to a subfolder which starts with the same characters', function () {
  712. it('does not throw an error', async function () {
  713. await expect(
  714. this.subject.promises.moveEntity(
  715. this.project._id,
  716. this.folder._id,
  717. this.notSubfolder._id,
  718. 'folder'
  719. )
  720. ).not.to.be.rejectedWith(Errors.InvalidNameError)
  721. })
  722. })
  723. })
  724. describe('deleteEntity', function () {
  725. beforeEach(async function () {
  726. const userId = new ObjectId().toString()
  727. this.ProjectMock.expects('findOneAndUpdate')
  728. .withArgs(
  729. { _id: this.project._id },
  730. {
  731. $pull: { 'rootFolder.0.docs': { _id: this.doc._id } },
  732. $inc: { version: 1 },
  733. $set: { lastUpdated: new Date(), lastUpdatedBy: userId },
  734. }
  735. )
  736. .chain('exec')
  737. .resolves(this.project)
  738. await this.subject.promises.deleteEntity(
  739. this.project._id,
  740. this.doc._id,
  741. 'doc',
  742. userId
  743. )
  744. })
  745. it('should update the database', function () {
  746. this.ProjectMock.verify()
  747. })
  748. })
  749. describe('renameEntity', function () {
  750. describe('happy path', function () {
  751. beforeEach(async function () {
  752. const userId = new ObjectId().toString()
  753. this.newName = 'new.tex'
  754. this.oldDocs = ['old-doc']
  755. this.oldFiles = ['old-file']
  756. this.newDocs = ['new-doc']
  757. this.newFiles = ['new-file']
  758. this.ProjectEntityHandler.getAllEntitiesFromProject
  759. .onFirstCall()
  760. .returns({ docs: this.oldDocs, files: this.oldFiles })
  761. this.ProjectEntityHandler.getAllEntitiesFromProject
  762. .onSecondCall()
  763. .returns({ docs: this.newDocs, files: this.newFiles })
  764. this.ProjectMock.expects('findOneAndUpdate')
  765. .withArgs(
  766. { _id: this.project._id, 'rootFolder.0.docs.0': { $exists: true } },
  767. {
  768. $set: {
  769. 'rootFolder.0.docs.0.name': this.newName,
  770. lastUpdated: new Date(),
  771. lastUpdatedBy: userId,
  772. },
  773. $inc: { version: 1 },
  774. }
  775. )
  776. .chain('exec')
  777. .resolves(this.project)
  778. this.result = await this.subject.promises.renameEntity(
  779. this.project._id,
  780. this.doc._id,
  781. 'doc',
  782. this.newName,
  783. userId
  784. )
  785. })
  786. it('should update the database', function () {
  787. this.ProjectMock.verify()
  788. })
  789. it('returns info', function () {
  790. expect(this.result).to.deep.equal({
  791. project: this.project,
  792. startPath: '/test-doc.txt',
  793. endPath: '/new.tex',
  794. rev: this.doc.rev,
  795. changes: {
  796. oldDocs: this.oldDocs,
  797. newDocs: this.newDocs,
  798. oldFiles: this.oldFiles,
  799. newFiles: this.newFiles,
  800. newProject: this.project,
  801. },
  802. })
  803. })
  804. })
  805. describe('name already exists', function () {
  806. it('should throw an error', async function () {
  807. await expect(
  808. this.subject.promises.renameEntity(
  809. this.project._id,
  810. this.doc._id,
  811. 'doc',
  812. this.folder.name
  813. )
  814. ).to.be.rejectedWith(Errors.DuplicateNameError)
  815. })
  816. })
  817. })
  818. describe('_putElement', function () {
  819. describe('updating the project', function () {
  820. describe('when the parent folder is given', function () {
  821. let userId
  822. beforeEach(function () {
  823. userId = new ObjectId().toString()
  824. this.newFile = { _id: new ObjectId(), name: 'new file.png' }
  825. this.ProjectMock.expects('findOneAndUpdate')
  826. .withArgs(
  827. {
  828. _id: this.project._id,
  829. 'rootFolder.0.folders.0': { $exists: true },
  830. },
  831. {
  832. $push: { 'rootFolder.0.folders.0.fileRefs': this.newFile },
  833. $inc: { version: 1 },
  834. $set: { lastUpdated: new Date(), lastUpdatedBy: userId },
  835. }
  836. )
  837. .chain('exec')
  838. .resolves(this.project)
  839. })
  840. it('should update the database', async function () {
  841. await this.subject.promises._putElement(
  842. this.project,
  843. this.folder._id,
  844. this.newFile,
  845. 'files',
  846. userId
  847. )
  848. this.ProjectMock.verify()
  849. })
  850. it('should add an s onto the type if not included', async function () {
  851. await this.subject.promises._putElement(
  852. this.project,
  853. this.folder._id,
  854. this.newFile,
  855. 'file',
  856. userId
  857. )
  858. this.ProjectMock.verify()
  859. })
  860. })
  861. describe('error cases', function () {
  862. it('should throw an error if element is null', async function () {
  863. await expect(
  864. this.subject.promises._putElement(
  865. this.project,
  866. this.folder._id,
  867. null,
  868. 'file'
  869. )
  870. ).to.be.rejected
  871. })
  872. it('should error if the element has no _id', async function () {
  873. const file = { name: 'something' }
  874. await expect(
  875. this.subject.promises._putElement(
  876. this.project,
  877. this.folder._id,
  878. file,
  879. 'file'
  880. )
  881. ).to.be.rejected
  882. })
  883. it('should error if element name contains invalid characters', async function () {
  884. const file = { _id: new ObjectId(), name: 'something*bad' }
  885. await expect(
  886. this.subject.promises._putElement(
  887. this.project,
  888. this.folder._id,
  889. file,
  890. 'file'
  891. )
  892. ).to.be.rejected
  893. })
  894. it('should error if element name is too long', async function () {
  895. const file = {
  896. _id: new ObjectId(),
  897. name: 'long-'.repeat(1000) + 'something',
  898. }
  899. await expect(
  900. this.subject.promises._putElement(
  901. this.project,
  902. this.folder._id,
  903. file,
  904. 'file'
  905. )
  906. ).to.be.rejectedWith(Errors.InvalidNameError)
  907. })
  908. it('should error if the folder name is too long', async function () {
  909. const file = {
  910. _id: new ObjectId(),
  911. name: 'something',
  912. }
  913. this.ProjectLocator.promises.findElement
  914. .withArgs({
  915. project: this.project,
  916. element_id: this.folder._id,
  917. type: 'folder',
  918. })
  919. .resolves({
  920. element: this.folder,
  921. path: { fileSystem: 'subdir/'.repeat(1000) + 'foo' },
  922. })
  923. await expect(
  924. this.subject.promises._putElement(
  925. this.project,
  926. this.folder._id,
  927. file,
  928. 'file'
  929. )
  930. ).to.be.rejectedWith(Errors.InvalidNameError)
  931. })
  932. ;['file', 'doc', 'folder'].forEach(entityType => {
  933. it(`should error if a ${entityType} already exists with the same name`, async function () {
  934. const file = {
  935. _id: new ObjectId(),
  936. name: this[entityType].name,
  937. }
  938. await expect(
  939. this.subject.promises._putElement(
  940. this.project,
  941. null,
  942. file,
  943. 'file'
  944. )
  945. ).to.be.rejectedWith(Errors.DuplicateNameError)
  946. })
  947. })
  948. })
  949. })
  950. describe('when the parent folder is not given', function () {
  951. it('should default to root folder insert', async function () {
  952. const userId = new ObjectId().toString()
  953. this.newFile = { _id: new ObjectId(), name: 'new file.png' }
  954. this.ProjectMock.expects('findOneAndUpdate')
  955. .withArgs(
  956. { _id: this.project._id, 'rootFolder.0': { $exists: true } },
  957. {
  958. $push: { 'rootFolder.0.fileRefs': this.newFile },
  959. $inc: { version: 1 },
  960. $set: { lastUpdated: new Date(), lastUpdatedBy: userId },
  961. }
  962. )
  963. .chain('exec')
  964. .resolves(this.project)
  965. await this.subject.promises._putElement(
  966. this.project,
  967. this.rootFolder._id,
  968. this.newFile,
  969. 'file',
  970. userId
  971. )
  972. })
  973. })
  974. })
  975. describe('createNewFolderStructure', function () {
  976. beforeEach(function () {
  977. this.mockRootFolder = 'MOCK_ROOT_FOLDER'
  978. this.docUploads = ['MOCK_DOC_UPLOAD']
  979. this.fileUploads = ['MOCK_FILE_UPLOAD']
  980. this.FolderStructureBuilder.buildFolderStructure
  981. .withArgs(this.docUploads, this.fileUploads)
  982. .returns(this.mockRootFolder)
  983. this.updateExpectation = this.ProjectMock.expects('findOneAndUpdate')
  984. .withArgs(
  985. {
  986. _id: this.project._id,
  987. 'rootFolder.0.folders.0': { $exists: false },
  988. 'rootFolder.0.docs.0': { $exists: false },
  989. 'rootFolder.0.files.0': { $exists: false },
  990. },
  991. { $set: { rootFolder: [this.mockRootFolder] }, $inc: { version: 1 } },
  992. { new: true, lean: true, fields: { version: 1 } }
  993. )
  994. .chain('exec')
  995. })
  996. describe('happy path', function () {
  997. beforeEach(async function () {
  998. this.updateExpectation.resolves({ version: 1 })
  999. await this.subject.promises.createNewFolderStructure(
  1000. this.project._id,
  1001. this.docUploads,
  1002. this.fileUploads
  1003. )
  1004. })
  1005. it('updates the database', function () {
  1006. this.ProjectMock.verify()
  1007. })
  1008. })
  1009. describe("when the update doesn't find a matching document", function () {
  1010. beforeEach(async function () {
  1011. this.updateExpectation.resolves(null)
  1012. })
  1013. it('throws an error', async function () {
  1014. await expect(
  1015. this.subject.promises.createNewFolderStructure(
  1016. this.project._id,
  1017. this.docUploads,
  1018. this.fileUploads
  1019. )
  1020. ).to.be.rejected
  1021. })
  1022. })
  1023. })
  1024. describe('replaceDocWithFile', function () {
  1025. it('should simultaneously remove the doc and add the file', async function () {
  1026. const userId = new ObjectId().toString()
  1027. this.ProjectMock.expects('findOneAndUpdate')
  1028. .withArgs(
  1029. { _id: this.project._id, 'rootFolder.0': { $exists: true } },
  1030. {
  1031. $pull: { 'rootFolder.0.docs': { _id: this.doc._id } },
  1032. $push: { 'rootFolder.0.fileRefs': this.file },
  1033. $inc: { version: 1 },
  1034. $set: { lastUpdated: new Date(), lastUpdatedBy: userId },
  1035. },
  1036. { new: true }
  1037. )
  1038. .chain('exec')
  1039. .resolves(this.project)
  1040. await this.subject.promises.replaceDocWithFile(
  1041. this.project._id,
  1042. this.doc._id,
  1043. this.file,
  1044. userId
  1045. )
  1046. this.ProjectMock.verify()
  1047. })
  1048. })
  1049. describe('replaceFileWithDoc', function () {
  1050. it('should simultaneously remove the file and add the doc', async function () {
  1051. const userId = new ObjectId().toString()
  1052. this.ProjectMock.expects('findOneAndUpdate')
  1053. .withArgs(
  1054. { _id: this.project._id, 'rootFolder.0': { $exists: true } },
  1055. {
  1056. $pull: { 'rootFolder.0.fileRefs': { _id: this.file._id } },
  1057. $push: { 'rootFolder.0.docs': this.doc },
  1058. $inc: { version: 1 },
  1059. $set: { lastUpdated: new Date(), lastUpdatedBy: userId },
  1060. },
  1061. { new: true }
  1062. )
  1063. .chain('exec')
  1064. .resolves(this.project)
  1065. await this.subject.promises.replaceFileWithDoc(
  1066. this.project._id,
  1067. this.file._id,
  1068. this.doc,
  1069. userId
  1070. )
  1071. this.ProjectMock.verify()
  1072. })
  1073. })
  1074. })