TpdsUpdateHandlerTests.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const SandboxedModule = require('sandboxed-module')
  15. const sinon = require('sinon')
  16. require('chai').should()
  17. const { expect } = require('chai')
  18. const modulePath = require('path').join(
  19. __dirname,
  20. '../../../../app/src/Features/ThirdPartyDataStore/TpdsUpdateHandler.js'
  21. )
  22. describe('TpdsUpdateHandler', function() {
  23. beforeEach(function() {
  24. this.requestQueuer = {}
  25. this.updateMerger = {
  26. deleteUpdate(user_id, project_id, path, source, cb) {
  27. return cb()
  28. },
  29. mergeUpdate(user_id, project_id, path, update, source, cb) {
  30. return cb()
  31. }
  32. }
  33. this.editorController = {}
  34. this.project_id = 'dsjajilknaksdn'
  35. this.project = { _id: this.project_id, name: 'projectNameHere' }
  36. this.projectLocator = {
  37. findUsersProjectByName: sinon.stub().callsArgWith(2, null, this.project)
  38. }
  39. this.projectCreationHandler = {
  40. createBlankProject: sinon.stub().callsArgWith(2, null, this.project)
  41. }
  42. this.projectDeleter = {
  43. markAsDeletedByExternalSource: sinon.stub().callsArgWith(1)
  44. }
  45. this.rootDocManager = { setRootDocAutomatically: sinon.stub() }
  46. this.FileTypeManager = {
  47. shouldIgnore: sinon.stub().callsArgWith(1, null, false)
  48. }
  49. this.CooldownManager = {
  50. isProjectOnCooldown: sinon.stub().callsArgWith(1, null, false)
  51. }
  52. this.handler = SandboxedModule.require(modulePath, {
  53. globals: {
  54. console: console
  55. },
  56. requires: {
  57. './UpdateMerger': this.updateMerger,
  58. './Editor/EditorController': this.editorController,
  59. '../Project/ProjectLocator': this.projectLocator,
  60. '../Project/ProjectCreationHandler': this.projectCreationHandler,
  61. '../Project/ProjectDeleter': this.projectDeleter,
  62. '../Project/ProjectRootDocManager': this.rootDocManager,
  63. '../Uploads/FileTypeManager': this.FileTypeManager,
  64. '../Cooldown/CooldownManager': this.CooldownManager,
  65. 'logger-sharelatex': {
  66. log() {}
  67. }
  68. }
  69. })
  70. this.user_id = 'dsad29jlkjas'
  71. return (this.source = 'dropbox')
  72. })
  73. describe('getting an update', function() {
  74. it('should send the update to the update merger', function(done) {
  75. const path = '/path/here'
  76. const update = {}
  77. this.updateMerger.mergeUpdate = sinon.stub()
  78. this.updateMerger.mergeUpdate
  79. .withArgs(this.user_id, this.project_id, path, update, this.source)
  80. .callsArg(5)
  81. return this.handler.newUpdate(
  82. this.user_id,
  83. this.project.name,
  84. path,
  85. update,
  86. this.source,
  87. () => {
  88. this.projectCreationHandler.createBlankProject.called.should.equal(
  89. false
  90. )
  91. return done()
  92. }
  93. )
  94. })
  95. it('should create a new project if one does not already exit', function(done) {
  96. this.projectLocator.findUsersProjectByName = sinon.stub().callsArgWith(2)
  97. const path = '/'
  98. return this.handler.newUpdate(
  99. this.user_id,
  100. this.project.name,
  101. path,
  102. {},
  103. this.source,
  104. () => {
  105. this.projectCreationHandler.createBlankProject
  106. .calledWith(this.user_id, this.project.name)
  107. .should.equal(true)
  108. return done()
  109. }
  110. )
  111. })
  112. it('should set the root doc automatically if a new project is created', function(done) {
  113. this.projectLocator.findUsersProjectByName = sinon.stub().callsArgWith(2)
  114. this.handler._rootDocTimeoutLength = 0
  115. const path = '/'
  116. return this.handler.newUpdate(
  117. this.user_id,
  118. this.project.name,
  119. path,
  120. {},
  121. this.source,
  122. () => {
  123. return setTimeout(() => {
  124. this.rootDocManager.setRootDocAutomatically
  125. .calledWith(this.project._id)
  126. .should.equal(true)
  127. return done()
  128. }, 1)
  129. }
  130. )
  131. })
  132. it('should not update files that should be ignored', function(done) {
  133. this.FileTypeManager.shouldIgnore = sinon
  134. .stub()
  135. .callsArgWith(1, null, true)
  136. this.projectLocator.findUsersProjectByName = sinon.stub().callsArgWith(2)
  137. const path = '/.gitignore'
  138. this.updateMerger.mergeUpdate = sinon.stub()
  139. return this.handler.newUpdate(
  140. this.user_id,
  141. this.project.name,
  142. path,
  143. {},
  144. this.source,
  145. () => {
  146. this.updateMerger.mergeUpdate.called.should.equal(false)
  147. return done()
  148. }
  149. )
  150. })
  151. it('should check if the project is on cooldown', function(done) {
  152. this.CooldownManager.isProjectOnCooldown = sinon
  153. .stub()
  154. .callsArgWith(1, null, false)
  155. this.projectLocator.findUsersProjectByName = sinon.stub().callsArgWith(2)
  156. const path = '/path/here'
  157. const update = {}
  158. this.updateMerger.mergeUpdate = sinon.stub()
  159. this.updateMerger.mergeUpdate
  160. .withArgs(this.user_id, this.project_id, path, update, this.source)
  161. .callsArg(5)
  162. return this.handler.newUpdate(
  163. this.user_id,
  164. this.project.name,
  165. path,
  166. update,
  167. this.source,
  168. err => {
  169. expect(err).to.be.oneOf([null, undefined])
  170. this.CooldownManager.isProjectOnCooldown.callCount.should.equal(1)
  171. this.CooldownManager.isProjectOnCooldown
  172. .calledWith(this.project_id)
  173. .should.equal(true)
  174. this.FileTypeManager.shouldIgnore.callCount.should.equal(1)
  175. this.updateMerger.mergeUpdate.callCount.should.equal(1)
  176. return done()
  177. }
  178. )
  179. })
  180. it('should return error and not proceed with update if project is on cooldown', function(done) {
  181. this.CooldownManager.isProjectOnCooldown = sinon
  182. .stub()
  183. .callsArgWith(1, null, true)
  184. this.projectLocator.findUsersProjectByName = sinon.stub().callsArgWith(2)
  185. this.FileTypeManager.shouldIgnore = sinon
  186. .stub()
  187. .callsArgWith(1, null, false)
  188. const path = '/path/here'
  189. const update = {}
  190. this.updateMerger.mergeUpdate = sinon.stub()
  191. this.updateMerger.mergeUpdate
  192. .withArgs(this.user_id, this.project_id, path, update, this.source)
  193. .callsArg(5)
  194. return this.handler.newUpdate(
  195. this.user_id,
  196. this.project.name,
  197. path,
  198. update,
  199. this.source,
  200. err => {
  201. expect(err).to.not.be.oneOf([null, undefined])
  202. expect(err).to.be.instanceof(Error)
  203. this.CooldownManager.isProjectOnCooldown.callCount.should.equal(1)
  204. this.CooldownManager.isProjectOnCooldown
  205. .calledWith(this.project_id)
  206. .should.equal(true)
  207. this.FileTypeManager.shouldIgnore.callCount.should.equal(0)
  208. this.updateMerger.mergeUpdate.callCount.should.equal(0)
  209. return done()
  210. }
  211. )
  212. })
  213. })
  214. describe('getting a delete :', function() {
  215. it('should call deleteEntity in the collaberation manager', function(done) {
  216. const path = '/delete/this'
  217. const update = {}
  218. this.updateMerger.deleteUpdate = sinon.stub().callsArg(4)
  219. return this.handler.deleteUpdate(
  220. this.user_id,
  221. this.project.name,
  222. path,
  223. this.source,
  224. () => {
  225. this.projectDeleter.markAsDeletedByExternalSource
  226. .calledWith(this.project._id)
  227. .should.equal(false)
  228. this.updateMerger.deleteUpdate
  229. .calledWith(this.user_id, this.project_id, path, this.source)
  230. .should.equal(true)
  231. return done()
  232. }
  233. )
  234. })
  235. it('should mark the project as deleted by external source if path is a single slash', function(done) {
  236. const path = '/'
  237. return this.handler.deleteUpdate(
  238. this.user_id,
  239. this.project.name,
  240. path,
  241. this.source,
  242. () => {
  243. this.projectDeleter.markAsDeletedByExternalSource
  244. .calledWith(this.project._id)
  245. .should.equal(true)
  246. return done()
  247. }
  248. )
  249. })
  250. })
  251. })