ProjectEntityMongoUpdateHandler.test.mjs 34 KB

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