TpdsControllerTests.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const SandboxedModule = require('sandboxed-module')
  13. const sinon = require('sinon')
  14. require('chai').should()
  15. const Errors = require('../../../../app/src/Features/Errors/Errors')
  16. const modulePath = require('path').join(
  17. __dirname,
  18. '../../../../app/src/Features/ThirdPartyDataStore/TpdsController.js'
  19. )
  20. describe('TpdsController', function() {
  21. beforeEach(function() {
  22. this.TpdsUpdateHandler = {}
  23. this.TpdsController = SandboxedModule.require(modulePath, {
  24. requires: {
  25. './TpdsUpdateHandler': this.TpdsUpdateHandler,
  26. './UpdateMerger': (this.UpdateMerger = {}),
  27. 'logger-sharelatex': {
  28. log() {},
  29. warn() {},
  30. err() {}
  31. },
  32. 'metrics-sharelatex': {
  33. inc() {}
  34. }
  35. }
  36. })
  37. this.user_id = 'dsad29jlkjas'
  38. })
  39. describe('getting an update', () => {
  40. it('should process the update with the update receiver', function(done) {
  41. const path = '/projectName/here.txt'
  42. const req = {
  43. pause() {},
  44. params: { 0: path, user_id: this.user_id },
  45. session: {
  46. destroy() {}
  47. },
  48. headers: {
  49. 'x-sl-update-source': (this.source = 'dropbox')
  50. }
  51. }
  52. this.TpdsUpdateHandler.newUpdate = sinon.stub().callsArg(5)
  53. const res = {
  54. sendStatus: () => {
  55. this.TpdsUpdateHandler.newUpdate
  56. .calledWith(
  57. this.user_id,
  58. 'projectName',
  59. '/here.txt',
  60. req,
  61. this.source
  62. )
  63. .should.equal(true)
  64. done()
  65. }
  66. }
  67. this.TpdsController.mergeUpdate(req, res)
  68. })
  69. it('should return a 500 error when the update receiver fails', function() {
  70. const path = '/projectName/here.txt'
  71. const req = {
  72. pause() {},
  73. params: { 0: path, user_id: this.user_id },
  74. session: {
  75. destroy() {}
  76. },
  77. headers: {
  78. 'x-sl-update-source': (this.source = 'dropbox')
  79. }
  80. }
  81. this.TpdsUpdateHandler.newUpdate = sinon
  82. .stub()
  83. .callsArgWith(5, 'update-receiver-error')
  84. const res = {
  85. sendStatus: sinon.stub()
  86. }
  87. this.TpdsController.mergeUpdate(req, res)
  88. res.sendStatus.calledWith(500).should.equal(true)
  89. })
  90. it('should return a 429 error when the update receiver fails due to too many requests error', function() {
  91. const path = '/projectName/here.txt'
  92. const req = {
  93. pause() {},
  94. params: { 0: path, user_id: this.user_id },
  95. session: {
  96. destroy() {}
  97. },
  98. headers: {
  99. 'x-sl-update-source': (this.source = 'dropbox')
  100. }
  101. }
  102. this.TpdsUpdateHandler.newUpdate = sinon
  103. .stub()
  104. .callsArgWith(5, new Errors.TooManyRequestsError('project on cooldown'))
  105. const res = {
  106. sendStatus: sinon.stub()
  107. }
  108. this.TpdsController.mergeUpdate(req, res)
  109. res.sendStatus.calledWith(429).should.equal(true)
  110. })
  111. })
  112. describe('getting a delete update', () =>
  113. it('should process the delete with the update reciver', function(done) {
  114. const path = '/projectName/here.txt'
  115. const req = {
  116. params: { 0: path, user_id: this.user_id },
  117. session: {
  118. destroy() {}
  119. },
  120. headers: {
  121. 'x-sl-update-source': (this.source = 'dropbox')
  122. }
  123. }
  124. this.TpdsUpdateHandler.deleteUpdate = sinon.stub().callsArg(4)
  125. const res = {
  126. sendStatus: () => {
  127. this.TpdsUpdateHandler.deleteUpdate
  128. .calledWith(this.user_id, 'projectName', '/here.txt', this.source)
  129. .should.equal(true)
  130. done()
  131. }
  132. }
  133. this.TpdsController.deleteUpdate(req, res)
  134. }))
  135. describe('parseParams', function() {
  136. it('should take the project name off the start and replace with slash', function() {
  137. const path = 'noSlashHere'
  138. const req = { params: { 0: path, user_id: this.user_id } }
  139. const result = this.TpdsController.parseParams(req)
  140. result.user_id.should.equal(this.user_id)
  141. result.filePath.should.equal('/')
  142. result.projectName.should.equal(path)
  143. })
  144. it('should take the project name off the start and return it with no slashes in', function() {
  145. const path = '/project/file.tex'
  146. const req = { params: { 0: path, user_id: this.user_id } }
  147. const result = this.TpdsController.parseParams(req)
  148. result.user_id.should.equal(this.user_id)
  149. result.filePath.should.equal('/file.tex')
  150. result.projectName.should.equal('project')
  151. })
  152. it('should take the project name of and return a slash for the file path', function() {
  153. const path = '/project_name'
  154. const req = { params: { 0: path, user_id: this.user_id } }
  155. const result = this.TpdsController.parseParams(req)
  156. result.projectName.should.equal('project_name')
  157. result.filePath.should.equal('/')
  158. })
  159. })
  160. describe('updateProjectContents', function() {
  161. beforeEach(function() {
  162. this.UpdateMerger.mergeUpdate = sinon.stub().callsArg(5)
  163. this.req = {
  164. params: {
  165. 0: (this.path = 'chapters/main.tex'),
  166. project_id: (this.project_id = 'project-id-123')
  167. },
  168. session: {
  169. destroy: sinon.stub()
  170. },
  171. headers: {
  172. 'x-sl-update-source': (this.source = 'github')
  173. }
  174. }
  175. this.res = { sendStatus: sinon.stub() }
  176. this.TpdsController.updateProjectContents(this.req, this.res)
  177. })
  178. it('should merge the update', function() {
  179. this.UpdateMerger.mergeUpdate
  180. .calledWith(
  181. null,
  182. this.project_id,
  183. `/${this.path}`,
  184. this.req,
  185. this.source
  186. )
  187. .should.equal(true)
  188. })
  189. it('should return a success', function() {
  190. this.res.sendStatus.calledWith(200).should.equal(true)
  191. })
  192. })
  193. describe('deleteProjectContents', function() {
  194. beforeEach(function() {
  195. this.UpdateMerger.deleteUpdate = sinon.stub().callsArg(4)
  196. this.req = {
  197. params: {
  198. 0: (this.path = 'chapters/main.tex'),
  199. project_id: (this.project_id = 'project-id-123')
  200. },
  201. session: {
  202. destroy: sinon.stub()
  203. },
  204. headers: {
  205. 'x-sl-update-source': (this.source = 'github')
  206. }
  207. }
  208. this.res = { sendStatus: sinon.stub() }
  209. this.TpdsController.deleteProjectContents(this.req, this.res)
  210. })
  211. it('should delete the file', function() {
  212. this.UpdateMerger.deleteUpdate
  213. .calledWith(null, this.project_id, `/${this.path}`, this.source)
  214. .should.equal(true)
  215. })
  216. it('should return a success', function() {
  217. this.res.sendStatus.calledWith(200).should.equal(true)
  218. })
  219. })
  220. })