UrlCacheTests.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SandboxedModule = require('sandboxed-module')
  14. const sinon = require('sinon')
  15. const { expect } = require('chai')
  16. const modulePath = require('path').join(__dirname, '../../../app/js/UrlCache')
  17. describe('UrlCache', function () {
  18. beforeEach(function () {
  19. this.callback = sinon.stub()
  20. this.url =
  21. 'http://filestore/project/60b0dd39c418bc00598a0d22/file/60ae721ffb1d920027d3201f'
  22. this.project_id = '60b0dd39c418bc00598a0d22'
  23. return (this.UrlCache = SandboxedModule.require(modulePath, {
  24. requires: {
  25. './UrlFetcher': (this.UrlFetcher = {
  26. promises: { pipeUrlToFileWithRetry: sinon.stub().resolves() },
  27. }),
  28. '@overleaf/settings': (this.Settings = {
  29. path: { clsiCacheDir: '/cache/dir' },
  30. }),
  31. 'fs-extra': (this.fse = { remove: sinon.stub().resolves() }),
  32. fs: (this.fs = {
  33. promises: {
  34. copyFile: sinon.stub().resolves(),
  35. },
  36. }),
  37. },
  38. }))
  39. })
  40. describe('downloadUrlToFile', function () {
  41. beforeEach(function () {
  42. this.destPath = 'path/to/destination'
  43. })
  44. it('should not download on the happy path', function (done) {
  45. this.UrlCache.downloadUrlToFile(
  46. this.project_id,
  47. this.url,
  48. this.destPath,
  49. this.lastModified,
  50. error => {
  51. expect(error).to.not.exist
  52. expect(
  53. this.UrlFetcher.promises.pipeUrlToFileWithRetry.called
  54. ).to.equal(false)
  55. done()
  56. }
  57. )
  58. })
  59. it('should download on cache miss', function (done) {
  60. const codedError = new Error()
  61. codedError.code = 'ENOENT'
  62. this.fs.promises.copyFile.onCall(0).rejects(codedError)
  63. this.fs.promises.copyFile.onCall(1).resolves()
  64. this.UrlCache.downloadUrlToFile(
  65. this.project_id,
  66. this.url,
  67. this.destPath,
  68. this.lastModified,
  69. error => {
  70. expect(error).to.not.exist
  71. expect(
  72. this.UrlFetcher.promises.pipeUrlToFileWithRetry.called
  73. ).to.equal(true)
  74. done()
  75. }
  76. )
  77. })
  78. it('should raise non cache-miss errors', function (done) {
  79. const codedError = new Error()
  80. codedError.code = 'FOO'
  81. this.fs.promises.copyFile.rejects(codedError)
  82. this.UrlCache.downloadUrlToFile(
  83. this.project_id,
  84. this.url,
  85. this.destPath,
  86. this.lastModified,
  87. error => {
  88. expect(error).to.equal(codedError)
  89. done()
  90. }
  91. )
  92. })
  93. })
  94. describe('clearProject', function () {
  95. beforeEach(function (done) {
  96. this.UrlCache.clearProject(this.project_id, done)
  97. })
  98. it('should clear the cache in bulk', function () {
  99. expect(
  100. this.fse.remove.calledWith('/cache/dir/' + this.project_id)
  101. ).to.equal(true)
  102. })
  103. })
  104. })