TpdsUpdateHandlerTests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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.CooldownManager = {
  36. isProjectOnCooldown: sinon.stub().yields(null, false),
  37. }
  38. this.FileTypeManager = {
  39. shouldIgnore: sinon.stub().yields(null, false),
  40. }
  41. this.Modules = {
  42. hooks: { fire: sinon.stub().yields() },
  43. }
  44. this.notification = {
  45. create: sinon.stub().yields(),
  46. }
  47. this.NotificationsBuilder = {
  48. dropboxDuplicateProjectNames: sinon.stub().returns(this.notification),
  49. }
  50. this.ProjectCreationHandler = {
  51. createBlankProject: sinon.stub().yields(null, this.projects.active1),
  52. }
  53. this.ProjectDeleter = {
  54. markAsDeletedByExternalSource: sinon.stub().yields(),
  55. }
  56. this.ProjectGetter = {
  57. findUsersProjectsByName: sinon.stub(),
  58. }
  59. this.ProjectHelper = {
  60. isArchivedOrTrashed: sinon.stub().returns(false),
  61. }
  62. this.ProjectHelper.isArchivedOrTrashed
  63. .withArgs(this.projects.archived1, this.userId)
  64. .returns(true)
  65. this.ProjectHelper.isArchivedOrTrashed
  66. .withArgs(this.projects.archived2, this.userId)
  67. .returns(true)
  68. this.RootDocManager = { setRootDocAutomatically: sinon.stub() }
  69. this.UpdateMerger = {
  70. deleteUpdate: sinon.stub().yields(),
  71. mergeUpdate: sinon.stub().yields(),
  72. }
  73. this.TpdsUpdateHandler = SandboxedModule.require(MODULE_PATH, {
  74. requires: {
  75. '../Cooldown/CooldownManager': this.CooldownManager,
  76. '../Uploads/FileTypeManager': this.FileTypeManager,
  77. '../../infrastructure/Modules': this.Modules,
  78. '../Notifications/NotificationsBuilder': this.NotificationsBuilder,
  79. '../Project/ProjectCreationHandler': this.ProjectCreationHandler,
  80. '../Project/ProjectDeleter': this.ProjectDeleter,
  81. '../Project/ProjectGetter': this.ProjectGetter,
  82. '../Project/ProjectHelper': this.ProjectHelper,
  83. '../Project/ProjectRootDocManager': this.RootDocManager,
  84. './UpdateMerger': this.UpdateMerger,
  85. },
  86. })
  87. })
  88. describe('getting an update', function () {
  89. describe('with no matching project', function () {
  90. setupMatchingProjects([])
  91. receiveUpdate()
  92. expectProjectCreated()
  93. expectUpdateProcessed()
  94. })
  95. describe('with one matching active project', function () {
  96. setupMatchingProjects(['active1'])
  97. receiveUpdate()
  98. expectProjectNotCreated()
  99. expectUpdateProcessed()
  100. })
  101. describe('with one matching archived project', function () {
  102. setupMatchingProjects(['archived1'])
  103. receiveUpdate()
  104. expectProjectNotCreated()
  105. expectUpdateNotProcessed()
  106. expectDropboxNotUnlinked()
  107. })
  108. describe('with two matching active projects', function () {
  109. setupMatchingProjects(['active1', 'active2'])
  110. receiveUpdate()
  111. expectProjectNotCreated()
  112. expectUpdateNotProcessed()
  113. expectDropboxUnlinked()
  114. })
  115. describe('with two matching archived projects', function () {
  116. setupMatchingProjects(['archived1', 'archived2'])
  117. receiveUpdate()
  118. expectProjectNotCreated()
  119. expectUpdateNotProcessed()
  120. expectDropboxNotUnlinked()
  121. })
  122. describe('with one matching active and one matching archived project', function () {
  123. setupMatchingProjects(['active1', 'archived1'])
  124. receiveUpdate()
  125. expectProjectNotCreated()
  126. expectUpdateNotProcessed()
  127. expectDropboxUnlinked()
  128. })
  129. describe('update to a file that should be ignored', function (done) {
  130. setupMatchingProjects(['active1'])
  131. beforeEach(function () {
  132. this.FileTypeManager.shouldIgnore.yields(null, true)
  133. })
  134. receiveUpdate()
  135. expectProjectNotCreated()
  136. expectUpdateNotProcessed()
  137. expectDropboxNotUnlinked()
  138. })
  139. describe('update to a project on cooldown', function (done) {
  140. setupMatchingProjects(['active1'])
  141. setupProjectOnCooldown()
  142. beforeEach(function (done) {
  143. this.TpdsUpdateHandler.newUpdate(
  144. this.userId,
  145. this.projectName,
  146. this.path,
  147. this.update,
  148. this.source,
  149. err => {
  150. expect(err).to.be.instanceof(Errors.TooManyRequestsError)
  151. done()
  152. }
  153. )
  154. })
  155. expectUpdateNotProcessed()
  156. })
  157. })
  158. describe('getting a file delete', function () {
  159. describe('with no matching project', function () {
  160. setupMatchingProjects([])
  161. receiveFileDelete()
  162. expectDeleteNotProcessed()
  163. expectProjectNotDeleted()
  164. })
  165. describe('with one matching active project', function () {
  166. setupMatchingProjects(['active1'])
  167. receiveFileDelete()
  168. expectDeleteProcessed()
  169. expectProjectNotDeleted()
  170. })
  171. describe('with one matching archived project', function () {
  172. setupMatchingProjects(['archived1'])
  173. receiveFileDelete()
  174. expectDeleteNotProcessed()
  175. expectProjectNotDeleted()
  176. })
  177. describe('with two matching active projects', function () {
  178. setupMatchingProjects(['active1', 'active2'])
  179. receiveFileDelete()
  180. expectDeleteNotProcessed()
  181. expectProjectNotDeleted()
  182. expectDropboxUnlinked()
  183. })
  184. describe('with two matching archived projects', function () {
  185. setupMatchingProjects(['archived1', 'archived2'])
  186. receiveFileDelete()
  187. expectDeleteNotProcessed()
  188. expectProjectNotDeleted()
  189. expectDropboxNotUnlinked()
  190. })
  191. describe('with one matching active and one matching archived project', function () {
  192. setupMatchingProjects(['active1', 'archived1'])
  193. receiveFileDelete()
  194. expectDeleteNotProcessed()
  195. expectProjectNotDeleted()
  196. expectDropboxUnlinked()
  197. })
  198. })
  199. describe('getting a project delete', function () {
  200. describe('with no matching project', function () {
  201. setupMatchingProjects([])
  202. receiveProjectDelete()
  203. expectDeleteNotProcessed()
  204. expectProjectNotDeleted()
  205. })
  206. describe('with one matching active project', function () {
  207. setupMatchingProjects(['active1'])
  208. receiveProjectDelete()
  209. expectDeleteNotProcessed()
  210. expectProjectDeleted()
  211. })
  212. describe('with one matching archived project', function () {
  213. setupMatchingProjects(['archived1'])
  214. receiveProjectDelete()
  215. expectDeleteNotProcessed()
  216. expectProjectNotDeleted()
  217. })
  218. describe('with two matching active projects', function () {
  219. setupMatchingProjects(['active1', 'active2'])
  220. receiveProjectDelete()
  221. expectDeleteNotProcessed()
  222. expectProjectNotDeleted()
  223. expectDropboxUnlinked()
  224. })
  225. describe('with two matching archived projects', function () {
  226. setupMatchingProjects(['archived1', 'archived2'])
  227. receiveProjectDelete()
  228. expectDeleteNotProcessed()
  229. expectProjectNotDeleted()
  230. expectDropboxNotUnlinked()
  231. })
  232. describe('with one matching active and one matching archived project', function () {
  233. setupMatchingProjects(['active1', 'archived1'])
  234. receiveProjectDelete()
  235. expectDeleteNotProcessed()
  236. expectProjectNotDeleted()
  237. expectDropboxUnlinked()
  238. })
  239. })
  240. })
  241. /* Setup helpers */
  242. function setupMatchingProjects(projectKeys) {
  243. beforeEach(function () {
  244. const projects = projectKeys.map(key => this.projects[key])
  245. this.ProjectGetter.findUsersProjectsByName
  246. .withArgs(this.userId, this.projectName)
  247. .yields(null, projects)
  248. })
  249. }
  250. function setupProjectOnCooldown() {
  251. beforeEach(function () {
  252. this.CooldownManager.isProjectOnCooldown
  253. .withArgs(this.projects.active1._id)
  254. .yields(null, true)
  255. })
  256. }
  257. /* Test helpers */
  258. function receiveUpdate() {
  259. beforeEach(function (done) {
  260. this.TpdsUpdateHandler.newUpdate(
  261. this.userId,
  262. this.projectName,
  263. this.path,
  264. this.update,
  265. this.source,
  266. done
  267. )
  268. })
  269. }
  270. function receiveFileDelete() {
  271. beforeEach(function (done) {
  272. this.TpdsUpdateHandler.deleteUpdate(
  273. this.userId,
  274. this.projectName,
  275. this.path,
  276. this.source,
  277. done
  278. )
  279. })
  280. }
  281. function receiveProjectDelete() {
  282. beforeEach(function (done) {
  283. this.TpdsUpdateHandler.deleteUpdate(
  284. this.userId,
  285. this.projectName,
  286. '/',
  287. this.source,
  288. done
  289. )
  290. })
  291. }
  292. /* Expectations */
  293. function expectProjectCreated() {
  294. it('creates a project', function () {
  295. expect(
  296. this.ProjectCreationHandler.createBlankProject
  297. ).to.have.been.calledWith(this.userId, this.projectName)
  298. })
  299. it('sets the root doc', function () {
  300. // Fire pending timers
  301. this.clock.runAll()
  302. expect(this.RootDocManager.setRootDocAutomatically).to.have.been.calledWith(
  303. this.projects.active1._id
  304. )
  305. })
  306. }
  307. function expectProjectNotCreated() {
  308. it('does not create a project', function () {
  309. expect(this.ProjectCreationHandler.createBlankProject).not.to.have.been
  310. .called
  311. })
  312. it('does not set the root doc', function () {
  313. // Fire pending timers
  314. this.clock.runAll()
  315. expect(this.RootDocManager.setRootDocAutomatically).not.to.have.been.called
  316. })
  317. }
  318. function expectUpdateProcessed() {
  319. it('processes the update', function () {
  320. expect(this.UpdateMerger.mergeUpdate).to.have.been.calledWith(
  321. this.userId,
  322. this.projects.active1._id,
  323. this.path,
  324. this.update,
  325. this.source
  326. )
  327. })
  328. }
  329. function expectUpdateNotProcessed() {
  330. it('does not process the update', function () {
  331. expect(this.UpdateMerger.mergeUpdate).not.to.have.been.called
  332. })
  333. }
  334. function expectDropboxUnlinked() {
  335. it('unlinks Dropbox', function () {
  336. expect(this.Modules.hooks.fire).to.have.been.calledWith(
  337. 'removeDropbox',
  338. this.userId,
  339. 'duplicate-projects'
  340. )
  341. })
  342. it('creates a notification that dropbox was unlinked', function () {
  343. expect(
  344. this.NotificationsBuilder.dropboxDuplicateProjectNames
  345. ).to.have.been.calledWith(this.userId)
  346. expect(this.notification.create).to.have.been.calledWith(this.projectName)
  347. })
  348. }
  349. function expectDropboxNotUnlinked() {
  350. it('does not unlink Dropbox', function () {
  351. expect(this.Modules.hooks.fire).not.to.have.been.called
  352. })
  353. it('does not create a notification that dropbox was unlinked', function () {
  354. expect(this.NotificationsBuilder.dropboxDuplicateProjectNames).not.to.have
  355. .been.called
  356. })
  357. }
  358. function expectDeleteProcessed() {
  359. it('processes the delete', function () {
  360. expect(this.UpdateMerger.deleteUpdate).to.have.been.calledWith(
  361. this.userId,
  362. this.projects.active1._id,
  363. this.path,
  364. this.source
  365. )
  366. })
  367. }
  368. function expectDeleteNotProcessed() {
  369. it('does not process the delete', function () {
  370. expect(this.UpdateMerger.deleteUpdate).not.to.have.been.called
  371. })
  372. }
  373. function expectProjectDeleted() {
  374. it('deletes the project', function () {
  375. expect(
  376. this.ProjectDeleter.markAsDeletedByExternalSource
  377. ).to.have.been.calledWith(this.projects.active1._id)
  378. })
  379. }
  380. function expectProjectNotDeleted() {
  381. it('does not delete the project', function () {
  382. expect(this.ProjectDeleter.markAsDeletedByExternalSource).not.to.have.been
  383. .called
  384. })
  385. }