TpdsControllerTests.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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.AuthenticationController = {
  24. getLoggedInUserId: sinon.stub().returns('user-id')
  25. }
  26. this.TpdsQueueManager = {
  27. promises: {
  28. getQueues: sinon.stub().returns('queues')
  29. }
  30. }
  31. this.TpdsController = SandboxedModule.require(modulePath, {
  32. globals: {
  33. console: console
  34. },
  35. requires: {
  36. './TpdsUpdateHandler': this.TpdsUpdateHandler,
  37. './UpdateMerger': (this.UpdateMerger = {}),
  38. '../Notifications/NotificationsBuilder': (this.NotificationsBuilder = {
  39. tpdsFileLimit: sinon.stub().returns({ create: sinon.stub() })
  40. }),
  41. '../Authentication/AuthenticationController': this
  42. .AuthenticationController,
  43. './TpdsQueueManager': this.TpdsQueueManager,
  44. 'logger-sharelatex': {
  45. log() {},
  46. warn() {},
  47. err() {}
  48. },
  49. 'metrics-sharelatex': {
  50. inc() {}
  51. }
  52. }
  53. })
  54. this.user_id = 'dsad29jlkjas'
  55. })
  56. describe('getting an update', function() {
  57. it('should process the update with the update receiver', function(done) {
  58. const path = '/projectName/here.txt'
  59. const req = {
  60. pause() {},
  61. params: { 0: path, user_id: this.user_id },
  62. session: {
  63. destroy() {}
  64. },
  65. headers: {
  66. 'x-sl-update-source': (this.source = 'dropbox')
  67. }
  68. }
  69. this.TpdsUpdateHandler.newUpdate = sinon.stub().callsArg(5)
  70. const res = {
  71. sendStatus: () => {
  72. this.TpdsUpdateHandler.newUpdate
  73. .calledWith(
  74. this.user_id,
  75. 'projectName',
  76. '/here.txt',
  77. req,
  78. this.source
  79. )
  80. .should.equal(true)
  81. done()
  82. }
  83. }
  84. this.TpdsController.mergeUpdate(req, res)
  85. })
  86. it('should return a 500 error when the update receiver fails', function() {
  87. const path = '/projectName/here.txt'
  88. const req = {
  89. pause() {},
  90. params: { 0: path, user_id: this.user_id },
  91. session: {
  92. destroy() {}
  93. },
  94. headers: {
  95. 'x-sl-update-source': (this.source = 'dropbox')
  96. }
  97. }
  98. this.TpdsUpdateHandler.newUpdate = sinon
  99. .stub()
  100. .callsArgWith(5, 'update-receiver-error')
  101. const res = {
  102. sendStatus: sinon.stub()
  103. }
  104. this.TpdsController.mergeUpdate(req, res)
  105. res.sendStatus.calledWith(500).should.equal(true)
  106. })
  107. it('should return a 400 error when the project is too big', function() {
  108. const path = '/projectName/here.txt'
  109. const req = {
  110. pause() {},
  111. params: { 0: path, user_id: this.user_id, projectName: 'projectName' },
  112. session: {
  113. destroy() {}
  114. },
  115. headers: {
  116. 'x-sl-update-source': (this.source = 'dropbox')
  117. }
  118. }
  119. this.TpdsUpdateHandler.newUpdate = sinon
  120. .stub()
  121. .callsArgWith(5, { message: 'project_has_too_many_files' })
  122. const res = {
  123. sendStatus: sinon.stub()
  124. }
  125. this.TpdsController.mergeUpdate(req, res)
  126. res.sendStatus.calledWith(400).should.equal(true)
  127. this.NotificationsBuilder.tpdsFileLimit
  128. .calledWith(this.user_id)
  129. .should.equal(true)
  130. })
  131. it('should return a 429 error when the update receiver fails due to too many requests error', function() {
  132. const path = '/projectName/here.txt'
  133. const req = {
  134. pause() {},
  135. params: { 0: path, user_id: this.user_id },
  136. session: {
  137. destroy() {}
  138. },
  139. headers: {
  140. 'x-sl-update-source': (this.source = 'dropbox')
  141. }
  142. }
  143. this.TpdsUpdateHandler.newUpdate = sinon
  144. .stub()
  145. .callsArgWith(5, new Errors.TooManyRequestsError('project on cooldown'))
  146. const res = {
  147. sendStatus: sinon.stub()
  148. }
  149. this.TpdsController.mergeUpdate(req, res)
  150. res.sendStatus.calledWith(429).should.equal(true)
  151. })
  152. })
  153. describe('getting a delete update', function() {
  154. it('should process the delete with the update reciver', function(done) {
  155. const path = '/projectName/here.txt'
  156. const req = {
  157. params: { 0: path, user_id: this.user_id },
  158. session: {
  159. destroy() {}
  160. },
  161. headers: {
  162. 'x-sl-update-source': (this.source = 'dropbox')
  163. }
  164. }
  165. this.TpdsUpdateHandler.deleteUpdate = sinon.stub().callsArg(4)
  166. const res = {
  167. sendStatus: () => {
  168. this.TpdsUpdateHandler.deleteUpdate
  169. .calledWith(this.user_id, 'projectName', '/here.txt', this.source)
  170. .should.equal(true)
  171. done()
  172. }
  173. }
  174. this.TpdsController.deleteUpdate(req, res)
  175. })
  176. })
  177. describe('parseParams', function() {
  178. it('should take the project name off the start and replace with slash', function() {
  179. const path = 'noSlashHere'
  180. const req = { params: { 0: path, user_id: this.user_id } }
  181. const result = this.TpdsController.parseParams(req)
  182. result.user_id.should.equal(this.user_id)
  183. result.filePath.should.equal('/')
  184. result.projectName.should.equal(path)
  185. })
  186. it('should take the project name off the start and it with no slashes in', function() {
  187. const path = '/project/file.tex'
  188. const req = { params: { 0: path, user_id: this.user_id } }
  189. const result = this.TpdsController.parseParams(req)
  190. result.user_id.should.equal(this.user_id)
  191. result.filePath.should.equal('/file.tex')
  192. result.projectName.should.equal('project')
  193. })
  194. it('should take the project name of and return a slash for the file path', function() {
  195. const path = '/project_name'
  196. const req = { params: { 0: path, user_id: this.user_id } }
  197. const result = this.TpdsController.parseParams(req)
  198. result.projectName.should.equal('project_name')
  199. result.filePath.should.equal('/')
  200. })
  201. })
  202. describe('updateProjectContents', function() {
  203. beforeEach(function() {
  204. this.UpdateMerger.mergeUpdate = sinon.stub().callsArg(5)
  205. this.req = {
  206. params: {
  207. 0: (this.path = 'chapters/main.tex'),
  208. project_id: (this.project_id = 'project-id-123')
  209. },
  210. session: {
  211. destroy: sinon.stub()
  212. },
  213. headers: {
  214. 'x-sl-update-source': (this.source = 'github')
  215. }
  216. }
  217. this.res = { sendStatus: sinon.stub() }
  218. this.TpdsController.updateProjectContents(this.req, this.res)
  219. })
  220. it('should merge the update', function() {
  221. this.UpdateMerger.mergeUpdate
  222. .calledWith(
  223. null,
  224. this.project_id,
  225. `/${this.path}`,
  226. this.req,
  227. this.source
  228. )
  229. .should.equal(true)
  230. })
  231. it('should return a success', function() {
  232. this.res.sendStatus.calledWith(200).should.equal(true)
  233. })
  234. })
  235. describe('deleteProjectContents', function() {
  236. beforeEach(function() {
  237. this.UpdateMerger.deleteUpdate = sinon.stub().callsArg(4)
  238. this.req = {
  239. params: {
  240. 0: (this.path = 'chapters/main.tex'),
  241. project_id: (this.project_id = 'project-id-123')
  242. },
  243. session: {
  244. destroy: sinon.stub()
  245. },
  246. headers: {
  247. 'x-sl-update-source': (this.source = 'github')
  248. }
  249. }
  250. this.res = { sendStatus: sinon.stub() }
  251. this.TpdsController.deleteProjectContents(this.req, this.res)
  252. })
  253. it('should delete the file', function() {
  254. this.UpdateMerger.deleteUpdate
  255. .calledWith(null, this.project_id, `/${this.path}`, this.source)
  256. .should.equal(true)
  257. })
  258. it('should return a success', function() {
  259. this.res.sendStatus.calledWith(200).should.equal(true)
  260. })
  261. })
  262. describe('getQueues', function() {
  263. beforeEach(function() {
  264. this.req = {}
  265. this.res = { json: sinon.stub() }
  266. this.next = sinon.stub()
  267. })
  268. describe('success', function() {
  269. beforeEach(async function() {
  270. await this.TpdsController.getQueues(this.req, this.res, this.next)
  271. })
  272. it('should use userId from session', function() {
  273. this.AuthenticationController.getLoggedInUserId.should.have.been
  274. .calledOnce
  275. this.TpdsQueueManager.promises.getQueues.should.have.been.calledWith(
  276. 'user-id'
  277. )
  278. })
  279. it('should call json with response', function() {
  280. this.res.json.should.have.been.calledWith('queues')
  281. this.next.should.not.have.been.called
  282. })
  283. })
  284. describe('error', function() {
  285. beforeEach(async function() {
  286. this.err = new Error()
  287. this.TpdsQueueManager.promises.getQueues = sinon
  288. .stub()
  289. .rejects(this.err)
  290. await this.TpdsController.getQueues(this.req, this.res, this.next)
  291. })
  292. it('should call next with error', function() {
  293. this.res.json.should.not.have.been.called
  294. this.next.should.have.been.calledWith(this.err)
  295. })
  296. })
  297. })
  298. })