TpdsUpdateHandlerTests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const { expect } = require('chai')
  4. const { ObjectId } = require('mongodb')
  5. const Errors = require('../../../../app/src/Features/Errors/Errors')
  6. const MODULE_PATH =
  7. '../../../../app/src/Features/ThirdPartyDataStore/TpdsUpdateHandler.js'
  8. describe('TpdsUpdateHandler', function () {
  9. beforeEach(function () {
  10. this.clock = sinon.useFakeTimers()
  11. })
  12. afterEach(function () {
  13. this.clock.restore()
  14. })
  15. beforeEach(function () {
  16. this.projectName = 'My recipes'
  17. this.projects = {
  18. active1: { _id: new ObjectId(), name: this.projectName },
  19. active2: { _id: new ObjectId(), name: this.projectName },
  20. archived1: {
  21. _id: new ObjectId(),
  22. name: this.projectName,
  23. archived: [this.userId],
  24. },
  25. archived2: {
  26. _id: new ObjectId(),
  27. name: this.projectName,
  28. archived: [this.userId],
  29. },
  30. }
  31. this.userId = new ObjectId()
  32. this.source = 'dropbox'
  33. this.path = `/some/file`
  34. this.update = {}
  35. this.folderPath = '/some/folder'
  36. this.folder = {
  37. _id: new ObjectId(),
  38. parentFolder_id: new ObjectId(),
  39. }
  40. this.CooldownManager = {
  41. promises: {
  42. isProjectOnCooldown: sinon.stub().resolves(false),
  43. },
  44. }
  45. this.FileTypeManager = {
  46. promises: {
  47. shouldIgnore: sinon.stub().resolves(false),
  48. },
  49. }
  50. this.Modules = {
  51. promises: {
  52. hooks: { fire: sinon.stub().resolves() },
  53. },
  54. }
  55. this.notification = {
  56. create: sinon.stub().resolves(),
  57. }
  58. this.NotificationsBuilder = {
  59. promises: {
  60. dropboxDuplicateProjectNames: sinon.stub().returns(this.notification),
  61. },
  62. }
  63. this.ProjectCreationHandler = {
  64. promises: {
  65. createBlankProject: sinon.stub().resolves(this.projects.active1),
  66. },
  67. }
  68. this.ProjectDeleter = {
  69. promises: {
  70. markAsDeletedByExternalSource: sinon.stub().resolves(),
  71. },
  72. }
  73. this.ProjectGetter = {
  74. promises: {
  75. findUsersProjectsByName: sinon.stub(),
  76. findAllUsersProjects: sinon
  77. .stub()
  78. .resolves({ owned: [this.projects.active1], readAndWrite: [] }),
  79. },
  80. }
  81. this.ProjectHelper = {
  82. isArchivedOrTrashed: sinon.stub().returns(false),
  83. }
  84. this.ProjectHelper.isArchivedOrTrashed
  85. .withArgs(this.projects.archived1, this.userId)
  86. .returns(true)
  87. this.ProjectHelper.isArchivedOrTrashed
  88. .withArgs(this.projects.archived2, this.userId)
  89. .returns(true)
  90. this.RootDocManager = {
  91. promises: {
  92. setRootDocAutomatically: sinon.stub().resolves(),
  93. },
  94. }
  95. this.UpdateMerger = {
  96. promises: {
  97. deleteUpdate: sinon.stub().resolves(),
  98. mergeUpdate: sinon.stub().resolves(),
  99. createFolder: sinon.stub().resolves(this.folder),
  100. },
  101. }
  102. this.TpdsUpdateHandler = SandboxedModule.require(MODULE_PATH, {
  103. requires: {
  104. '../Cooldown/CooldownManager': this.CooldownManager,
  105. '../Uploads/FileTypeManager': this.FileTypeManager,
  106. '../../infrastructure/Modules': this.Modules,
  107. '../Notifications/NotificationsBuilder': this.NotificationsBuilder,
  108. '../Project/ProjectCreationHandler': this.ProjectCreationHandler,
  109. '../Project/ProjectDeleter': this.ProjectDeleter,
  110. '../Project/ProjectGetter': this.ProjectGetter,
  111. '../Project/ProjectHelper': this.ProjectHelper,
  112. '../Project/ProjectRootDocManager': this.RootDocManager,
  113. './UpdateMerger': this.UpdateMerger,
  114. },
  115. })
  116. })
  117. describe('getting an update', function () {
  118. describe('byId', function () {
  119. describe('with no matching project', function () {
  120. beforeEach(function () {
  121. this.projectId = new ObjectId().toString()
  122. })
  123. receiveUpdateById()
  124. expectProjectNotCreated()
  125. expectUpdateNotProcessed()
  126. })
  127. describe('with one matching active project', function () {
  128. beforeEach(function () {
  129. this.projectId = this.projects.active1._id.toString()
  130. })
  131. receiveUpdateById()
  132. expectProjectNotCreated()
  133. expectUpdateProcessed()
  134. })
  135. })
  136. describe('with no matching project', function () {
  137. setupMatchingProjects([])
  138. receiveUpdate()
  139. expectProjectCreated()
  140. expectUpdateProcessed()
  141. })
  142. describe('with one matching active project', function () {
  143. setupMatchingProjects(['active1'])
  144. receiveUpdate()
  145. expectProjectNotCreated()
  146. expectUpdateProcessed()
  147. })
  148. describe('with one matching archived project', function () {
  149. setupMatchingProjects(['archived1'])
  150. receiveUpdate()
  151. expectProjectNotCreated()
  152. expectUpdateNotProcessed()
  153. expectDropboxNotUnlinked()
  154. })
  155. describe('with two matching active projects', function () {
  156. setupMatchingProjects(['active1', 'active2'])
  157. receiveUpdate()
  158. expectProjectNotCreated()
  159. expectUpdateNotProcessed()
  160. expectDropboxUnlinked()
  161. })
  162. describe('with two matching archived projects', function () {
  163. setupMatchingProjects(['archived1', 'archived2'])
  164. receiveUpdate()
  165. expectProjectNotCreated()
  166. expectUpdateNotProcessed()
  167. expectDropboxNotUnlinked()
  168. })
  169. describe('with one matching active and one matching archived project', function () {
  170. setupMatchingProjects(['active1', 'archived1'])
  171. receiveUpdate()
  172. expectProjectNotCreated()
  173. expectUpdateNotProcessed()
  174. expectDropboxUnlinked()
  175. })
  176. describe('update to a file that should be ignored', async function () {
  177. setupMatchingProjects(['active1'])
  178. beforeEach(function () {
  179. this.FileTypeManager.promises.shouldIgnore.resolves(true)
  180. })
  181. receiveUpdate()
  182. expectProjectNotCreated()
  183. expectUpdateNotProcessed()
  184. expectDropboxNotUnlinked()
  185. })
  186. describe('update to a project on cooldown', async function () {
  187. setupMatchingProjects(['active1'])
  188. setupProjectOnCooldown()
  189. beforeEach(async function () {
  190. await expect(
  191. this.TpdsUpdateHandler.promises.newUpdate(
  192. this.userId,
  193. '', // projectId
  194. this.projectName,
  195. this.path,
  196. this.update,
  197. this.source
  198. )
  199. ).to.be.rejectedWith(Errors.TooManyRequestsError)
  200. })
  201. expectUpdateNotProcessed()
  202. })
  203. })
  204. describe('getting a file delete', function () {
  205. describe('byId', function () {
  206. describe('with no matching project', function () {
  207. beforeEach(function () {
  208. this.projectId = new ObjectId().toString()
  209. })
  210. receiveFileDeleteById()
  211. expectDeleteNotProcessed()
  212. expectProjectNotDeleted()
  213. })
  214. describe('with one matching active project', function () {
  215. beforeEach(function () {
  216. this.projectId = this.projects.active1._id.toString()
  217. })
  218. receiveFileDeleteById()
  219. expectDeleteProcessed()
  220. expectProjectNotDeleted()
  221. })
  222. })
  223. describe('with no matching project', function () {
  224. setupMatchingProjects([])
  225. receiveFileDelete()
  226. expectDeleteNotProcessed()
  227. expectProjectNotDeleted()
  228. })
  229. describe('with one matching active project', function () {
  230. setupMatchingProjects(['active1'])
  231. receiveFileDelete()
  232. expectDeleteProcessed()
  233. expectProjectNotDeleted()
  234. })
  235. describe('with one matching archived project', function () {
  236. setupMatchingProjects(['archived1'])
  237. receiveFileDelete()
  238. expectDeleteNotProcessed()
  239. expectProjectNotDeleted()
  240. })
  241. describe('with two matching active projects', function () {
  242. setupMatchingProjects(['active1', 'active2'])
  243. receiveFileDelete()
  244. expectDeleteNotProcessed()
  245. expectProjectNotDeleted()
  246. expectDropboxUnlinked()
  247. })
  248. describe('with two matching archived projects', function () {
  249. setupMatchingProjects(['archived1', 'archived2'])
  250. receiveFileDelete()
  251. expectDeleteNotProcessed()
  252. expectProjectNotDeleted()
  253. expectDropboxNotUnlinked()
  254. })
  255. describe('with one matching active and one matching archived project', function () {
  256. setupMatchingProjects(['active1', 'archived1'])
  257. receiveFileDelete()
  258. expectDeleteNotProcessed()
  259. expectProjectNotDeleted()
  260. expectDropboxUnlinked()
  261. })
  262. })
  263. describe('getting a project delete', function () {
  264. describe('with no matching project', function () {
  265. setupMatchingProjects([])
  266. receiveProjectDelete()
  267. expectDeleteNotProcessed()
  268. expectProjectNotDeleted()
  269. })
  270. describe('with one matching active project', function () {
  271. setupMatchingProjects(['active1'])
  272. receiveProjectDelete()
  273. expectDeleteNotProcessed()
  274. expectProjectDeleted()
  275. })
  276. describe('with one matching archived project', function () {
  277. setupMatchingProjects(['archived1'])
  278. receiveProjectDelete()
  279. expectDeleteNotProcessed()
  280. expectProjectNotDeleted()
  281. })
  282. describe('with two matching active projects', function () {
  283. setupMatchingProjects(['active1', 'active2'])
  284. receiveProjectDelete()
  285. expectDeleteNotProcessed()
  286. expectProjectNotDeleted()
  287. expectDropboxUnlinked()
  288. })
  289. describe('with two matching archived projects', function () {
  290. setupMatchingProjects(['archived1', 'archived2'])
  291. receiveProjectDelete()
  292. expectDeleteNotProcessed()
  293. expectProjectNotDeleted()
  294. expectDropboxNotUnlinked()
  295. })
  296. describe('with one matching active and one matching archived project', function () {
  297. setupMatchingProjects(['active1', 'archived1'])
  298. receiveProjectDelete()
  299. expectDeleteNotProcessed()
  300. expectProjectNotDeleted()
  301. expectDropboxUnlinked()
  302. })
  303. })
  304. describe('getting a folder update', function () {
  305. describe('with no matching project', function () {
  306. setupMatchingProjects([])
  307. receiveFolderUpdate()
  308. expectProjectCreated()
  309. expectFolderUpdateProcessed()
  310. })
  311. describe('with one matching active project', function () {
  312. setupMatchingProjects(['active1'])
  313. receiveFolderUpdate()
  314. expectProjectNotCreated()
  315. expectFolderUpdateProcessed()
  316. })
  317. describe('with one matching archived project', function () {
  318. setupMatchingProjects(['archived1'])
  319. receiveFolderUpdate()
  320. expectProjectNotCreated()
  321. expectFolderUpdateNotProcessed()
  322. expectDropboxNotUnlinked()
  323. })
  324. describe('with two matching active projects', function () {
  325. setupMatchingProjects(['active1', 'active2'])
  326. receiveFolderUpdate()
  327. expectProjectNotCreated()
  328. expectFolderUpdateNotProcessed()
  329. expectDropboxUnlinked()
  330. })
  331. describe('with two matching archived projects', function () {
  332. setupMatchingProjects(['archived1', 'archived2'])
  333. receiveFolderUpdate()
  334. expectProjectNotCreated()
  335. expectFolderUpdateNotProcessed()
  336. expectDropboxNotUnlinked()
  337. })
  338. describe('with one matching active and one matching archived project', function () {
  339. setupMatchingProjects(['active1', 'archived1'])
  340. receiveFolderUpdate()
  341. expectProjectNotCreated()
  342. expectFolderUpdateNotProcessed()
  343. expectDropboxUnlinked()
  344. })
  345. describe('update to a project on cooldown', async function () {
  346. setupMatchingProjects(['active1'])
  347. setupProjectOnCooldown()
  348. beforeEach(async function () {
  349. await expect(
  350. this.TpdsUpdateHandler.promises.createFolder(
  351. this.userId,
  352. this.projectId,
  353. this.projectName,
  354. this.path
  355. )
  356. ).to.be.rejectedWith(Errors.TooManyRequestsError)
  357. })
  358. expectFolderUpdateNotProcessed()
  359. })
  360. })
  361. })
  362. /* Setup helpers */
  363. function setupMatchingProjects(projectKeys) {
  364. beforeEach(function () {
  365. const projects = projectKeys.map(key => this.projects[key])
  366. this.ProjectGetter.promises.findUsersProjectsByName
  367. .withArgs(this.userId, this.projectName)
  368. .resolves(projects)
  369. })
  370. }
  371. function setupProjectOnCooldown() {
  372. beforeEach(function () {
  373. this.CooldownManager.promises.isProjectOnCooldown
  374. .withArgs(this.projects.active1._id)
  375. .resolves(true)
  376. })
  377. }
  378. /* Test helpers */
  379. function receiveUpdate() {
  380. beforeEach(async function () {
  381. await this.TpdsUpdateHandler.promises.newUpdate(
  382. this.userId,
  383. '', // projectId
  384. this.projectName,
  385. this.path,
  386. this.update,
  387. this.source
  388. )
  389. })
  390. }
  391. function receiveUpdateById() {
  392. beforeEach(function (done) {
  393. this.TpdsUpdateHandler.newUpdate(
  394. this.userId,
  395. this.projectId,
  396. '', // projectName
  397. this.path,
  398. this.update,
  399. this.source,
  400. done
  401. )
  402. })
  403. }
  404. function receiveFileDelete() {
  405. beforeEach(async function () {
  406. await this.TpdsUpdateHandler.promises.deleteUpdate(
  407. this.userId,
  408. '', // projectId
  409. this.projectName,
  410. this.path,
  411. this.source
  412. )
  413. })
  414. }
  415. function receiveFileDeleteById() {
  416. beforeEach(function (done) {
  417. this.TpdsUpdateHandler.deleteUpdate(
  418. this.userId,
  419. this.projectId,
  420. '', // projectName
  421. this.path,
  422. this.source,
  423. done
  424. )
  425. })
  426. }
  427. function receiveProjectDelete() {
  428. beforeEach(async function () {
  429. await this.TpdsUpdateHandler.promises.deleteUpdate(
  430. this.userId,
  431. '', // projectId
  432. this.projectName,
  433. '/',
  434. this.source
  435. )
  436. })
  437. }
  438. function receiveFolderUpdate() {
  439. beforeEach(async function () {
  440. await this.TpdsUpdateHandler.promises.createFolder(
  441. this.userId,
  442. this.projectId,
  443. this.projectName,
  444. this.folderPath
  445. )
  446. })
  447. }
  448. /* Expectations */
  449. function expectProjectCreated() {
  450. it('creates a project', function () {
  451. expect(
  452. this.ProjectCreationHandler.promises.createBlankProject
  453. ).to.have.been.calledWith(this.userId, this.projectName)
  454. })
  455. it('sets the root doc', function () {
  456. // Fire pending timers
  457. this.clock.next()
  458. expect(
  459. this.RootDocManager.promises.setRootDocAutomatically
  460. ).to.have.been.calledWith(this.projects.active1._id)
  461. })
  462. }
  463. function expectProjectNotCreated() {
  464. it('does not create a project', function () {
  465. expect(this.ProjectCreationHandler.promises.createBlankProject).not.to.have
  466. .been.called
  467. })
  468. it('does not set the root doc', function () {
  469. // Fire pending timers
  470. this.clock.next()
  471. expect(this.RootDocManager.promises.setRootDocAutomatically).not.to.have
  472. .been.called
  473. })
  474. }
  475. function expectUpdateProcessed() {
  476. it('processes the update', function () {
  477. expect(this.UpdateMerger.promises.mergeUpdate).to.have.been.calledWith(
  478. this.userId,
  479. this.projects.active1._id,
  480. this.path,
  481. this.update,
  482. this.source
  483. )
  484. })
  485. }
  486. function expectUpdateNotProcessed() {
  487. it('does not process the update', function () {
  488. expect(this.UpdateMerger.promises.mergeUpdate).not.to.have.been.called
  489. })
  490. }
  491. function expectFolderUpdateProcessed() {
  492. it('processes the folder update', function () {
  493. expect(this.UpdateMerger.promises.createFolder).to.have.been.calledWith(
  494. this.projects.active1._id,
  495. this.folderPath
  496. )
  497. })
  498. }
  499. function expectFolderUpdateNotProcessed() {
  500. it("doesn't process the folder update", function () {
  501. expect(this.UpdateMerger.promises.createFolder).not.to.have.been.called
  502. })
  503. }
  504. function expectDropboxUnlinked() {
  505. it('unlinks Dropbox', function () {
  506. expect(this.Modules.promises.hooks.fire).to.have.been.calledWith(
  507. 'removeDropbox',
  508. this.userId,
  509. 'duplicate-projects'
  510. )
  511. })
  512. it('creates a notification that dropbox was unlinked', function () {
  513. expect(
  514. this.NotificationsBuilder.promises.dropboxDuplicateProjectNames
  515. ).to.have.been.calledWith(this.userId)
  516. expect(this.notification.create).to.have.been.calledWith(this.projectName)
  517. })
  518. }
  519. function expectDropboxNotUnlinked() {
  520. it('does not unlink Dropbox', function () {
  521. expect(this.Modules.promises.hooks.fire).not.to.have.been.called
  522. })
  523. it('does not create a notification that dropbox was unlinked', function () {
  524. expect(this.NotificationsBuilder.promises.dropboxDuplicateProjectNames).not
  525. .to.have.been.called
  526. })
  527. }
  528. function expectDeleteProcessed() {
  529. it('processes the delete', function () {
  530. expect(this.UpdateMerger.promises.deleteUpdate).to.have.been.calledWith(
  531. this.userId,
  532. this.projects.active1._id,
  533. this.path,
  534. this.source
  535. )
  536. })
  537. }
  538. function expectDeleteNotProcessed() {
  539. it('does not process the delete', function () {
  540. expect(this.UpdateMerger.promises.deleteUpdate).not.to.have.been.called
  541. })
  542. }
  543. function expectProjectDeleted() {
  544. it('deletes the project', function () {
  545. expect(
  546. this.ProjectDeleter.promises.markAsDeletedByExternalSource
  547. ).to.have.been.calledWith(this.projects.active1._id)
  548. })
  549. }
  550. function expectProjectNotDeleted() {
  551. it('does not delete the project', function () {
  552. expect(this.ProjectDeleter.promises.markAsDeletedByExternalSource).not.to
  553. .have.been.called
  554. })
  555. }