UrlCacheTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. require('chai').should()
  16. const modulePath = require('path').join(__dirname, '../../../app/js/UrlCache')
  17. const { EventEmitter } = require('events')
  18. describe('UrlCache', function () {
  19. beforeEach(function () {
  20. this.callback = sinon.stub()
  21. this.url = 'www.example.com/file'
  22. this.project_id = 'project-id-123'
  23. return (this.UrlCache = SandboxedModule.require(modulePath, {
  24. requires: {
  25. './db': {},
  26. './UrlFetcher': (this.UrlFetcher = {}),
  27. 'logger-sharelatex': (this.logger = { log: sinon.stub() }),
  28. 'settings-sharelatex': (this.Settings = {
  29. path: { clsiCacheDir: '/cache/dir' }
  30. }),
  31. fs: (this.fs = {})
  32. }
  33. }))
  34. })
  35. describe('_doesUrlNeedDownloading', function () {
  36. beforeEach(function () {
  37. this.lastModified = new Date()
  38. return (this.lastModifiedRoundedToSeconds = new Date(
  39. Math.floor(this.lastModified.getTime() / 1000) * 1000
  40. ))
  41. })
  42. describe('when URL does not exist in cache', function () {
  43. beforeEach(function () {
  44. this.UrlCache._findUrlDetails = sinon.stub().callsArgWith(2, null, null)
  45. return this.UrlCache._doesUrlNeedDownloading(
  46. this.project_id,
  47. this.url,
  48. this.lastModified,
  49. this.callback
  50. )
  51. })
  52. return it('should return the callback with true', function () {
  53. return this.callback.calledWith(null, true).should.equal(true)
  54. })
  55. })
  56. return describe('when URL does exist in cache', function () {
  57. beforeEach(function () {
  58. this.urlDetails = {}
  59. return (this.UrlCache._findUrlDetails = sinon
  60. .stub()
  61. .callsArgWith(2, null, this.urlDetails))
  62. })
  63. describe('when the modified date is more recent than the cached modified date', function () {
  64. beforeEach(function () {
  65. this.urlDetails.lastModified = new Date(
  66. this.lastModified.getTime() - 1000
  67. )
  68. return this.UrlCache._doesUrlNeedDownloading(
  69. this.project_id,
  70. this.url,
  71. this.lastModified,
  72. this.callback
  73. )
  74. })
  75. it('should get the url details', function () {
  76. return this.UrlCache._findUrlDetails
  77. .calledWith(this.project_id, this.url)
  78. .should.equal(true)
  79. })
  80. return it('should return the callback with true', function () {
  81. return this.callback.calledWith(null, true).should.equal(true)
  82. })
  83. })
  84. describe('when the cached modified date is more recent than the modified date', function () {
  85. beforeEach(function () {
  86. this.urlDetails.lastModified = new Date(
  87. this.lastModified.getTime() + 1000
  88. )
  89. return this.UrlCache._doesUrlNeedDownloading(
  90. this.project_id,
  91. this.url,
  92. this.lastModified,
  93. this.callback
  94. )
  95. })
  96. return it('should return the callback with false', function () {
  97. return this.callback.calledWith(null, false).should.equal(true)
  98. })
  99. })
  100. describe('when the cached modified date is equal to the modified date', function () {
  101. beforeEach(function () {
  102. this.urlDetails.lastModified = this.lastModified
  103. return this.UrlCache._doesUrlNeedDownloading(
  104. this.project_id,
  105. this.url,
  106. this.lastModified,
  107. this.callback
  108. )
  109. })
  110. return it('should return the callback with false', function () {
  111. return this.callback.calledWith(null, false).should.equal(true)
  112. })
  113. })
  114. describe('when the provided modified date does not exist', function () {
  115. beforeEach(function () {
  116. this.lastModified = null
  117. return this.UrlCache._doesUrlNeedDownloading(
  118. this.project_id,
  119. this.url,
  120. this.lastModified,
  121. this.callback
  122. )
  123. })
  124. return it('should return the callback with true', function () {
  125. return this.callback.calledWith(null, true).should.equal(true)
  126. })
  127. })
  128. return describe('when the URL does not have a modified date', function () {
  129. beforeEach(function () {
  130. this.urlDetails.lastModified = null
  131. return this.UrlCache._doesUrlNeedDownloading(
  132. this.project_id,
  133. this.url,
  134. this.lastModified,
  135. this.callback
  136. )
  137. })
  138. return it('should return the callback with true', function () {
  139. return this.callback.calledWith(null, true).should.equal(true)
  140. })
  141. })
  142. })
  143. })
  144. describe('_ensureUrlIsInCache', function () {
  145. beforeEach(function () {
  146. this.UrlFetcher.pipeUrlToFileWithRetry = sinon.stub().callsArg(2)
  147. return (this.UrlCache._updateOrCreateUrlDetails = sinon
  148. .stub()
  149. .callsArg(3))
  150. })
  151. describe('when the URL needs updating', function () {
  152. beforeEach(function () {
  153. this.UrlCache._doesUrlNeedDownloading = sinon
  154. .stub()
  155. .callsArgWith(3, null, true)
  156. return this.UrlCache._ensureUrlIsInCache(
  157. this.project_id,
  158. this.url,
  159. this.lastModified,
  160. this.callback
  161. )
  162. })
  163. it('should check that the url needs downloading', function () {
  164. return this.UrlCache._doesUrlNeedDownloading
  165. .calledWith(
  166. this.project_id,
  167. this.url,
  168. this.lastModifiedRoundedToSeconds
  169. )
  170. .should.equal(true)
  171. })
  172. it('should download the URL to the cache file', function () {
  173. return this.UrlFetcher.pipeUrlToFileWithRetry
  174. .calledWith(
  175. this.url,
  176. this.UrlCache._cacheFilePathForUrl(this.project_id, this.url)
  177. )
  178. .should.equal(true)
  179. })
  180. it('should update the database entry', function () {
  181. return this.UrlCache._updateOrCreateUrlDetails
  182. .calledWith(
  183. this.project_id,
  184. this.url,
  185. this.lastModifiedRoundedToSeconds
  186. )
  187. .should.equal(true)
  188. })
  189. return it('should return the callback with the cache file path', function () {
  190. return this.callback
  191. .calledWith(
  192. null,
  193. this.UrlCache._cacheFilePathForUrl(this.project_id, this.url)
  194. )
  195. .should.equal(true)
  196. })
  197. })
  198. return describe('when the URL does not need updating', function () {
  199. beforeEach(function () {
  200. this.UrlCache._doesUrlNeedDownloading = sinon
  201. .stub()
  202. .callsArgWith(3, null, false)
  203. return this.UrlCache._ensureUrlIsInCache(
  204. this.project_id,
  205. this.url,
  206. this.lastModified,
  207. this.callback
  208. )
  209. })
  210. it('should not download the URL to the cache file', function () {
  211. return this.UrlFetcher.pipeUrlToFileWithRetry.called.should.equal(false)
  212. })
  213. return it('should return the callback with the cache file path', function () {
  214. return this.callback
  215. .calledWith(
  216. null,
  217. this.UrlCache._cacheFilePathForUrl(this.project_id, this.url)
  218. )
  219. .should.equal(true)
  220. })
  221. })
  222. })
  223. describe('downloadUrlToFile', function () {
  224. beforeEach(function () {
  225. this.cachePath = 'path/to/cached/url'
  226. this.destPath = 'path/to/destination'
  227. this.UrlCache._copyFile = sinon.stub().callsArg(2)
  228. this.UrlCache._ensureUrlIsInCache = sinon
  229. .stub()
  230. .callsArgWith(3, null, this.cachePath)
  231. return this.UrlCache.downloadUrlToFile(
  232. this.project_id,
  233. this.url,
  234. this.destPath,
  235. this.lastModified,
  236. this.callback
  237. )
  238. })
  239. it('should ensure the URL is downloaded and updated in the cache', function () {
  240. return this.UrlCache._ensureUrlIsInCache
  241. .calledWith(this.project_id, this.url, this.lastModified)
  242. .should.equal(true)
  243. })
  244. it('should copy the file to the new location', function () {
  245. return this.UrlCache._copyFile
  246. .calledWith(this.cachePath, this.destPath)
  247. .should.equal(true)
  248. })
  249. return it('should call the callback', function () {
  250. return this.callback.called.should.equal(true)
  251. })
  252. })
  253. describe('_deleteUrlCacheFromDisk', function () {
  254. beforeEach(function () {
  255. this.fs.unlink = sinon.stub().callsArg(1)
  256. return this.UrlCache._deleteUrlCacheFromDisk(
  257. this.project_id,
  258. this.url,
  259. this.callback
  260. )
  261. })
  262. it('should delete the cache file', function () {
  263. return this.fs.unlink
  264. .calledWith(
  265. this.UrlCache._cacheFilePathForUrl(this.project_id, this.url)
  266. )
  267. .should.equal(true)
  268. })
  269. return it('should call the callback', function () {
  270. return this.callback.called.should.equal(true)
  271. })
  272. })
  273. describe('_clearUrlFromCache', function () {
  274. beforeEach(function () {
  275. this.UrlCache._deleteUrlCacheFromDisk = sinon.stub().callsArg(2)
  276. this.UrlCache._clearUrlDetails = sinon.stub().callsArg(2)
  277. return this.UrlCache._clearUrlFromCache(
  278. this.project_id,
  279. this.url,
  280. this.callback
  281. )
  282. })
  283. it('should delete the file on disk', function () {
  284. return this.UrlCache._deleteUrlCacheFromDisk
  285. .calledWith(this.project_id, this.url)
  286. .should.equal(true)
  287. })
  288. it('should clear the entry in the database', function () {
  289. return this.UrlCache._clearUrlDetails
  290. .calledWith(this.project_id, this.url)
  291. .should.equal(true)
  292. })
  293. return it('should call the callback', function () {
  294. return this.callback.called.should.equal(true)
  295. })
  296. })
  297. return describe('clearProject', function () {
  298. beforeEach(function () {
  299. this.urls = ['www.example.com/file1', 'www.example.com/file2']
  300. this.UrlCache._findAllUrlsInProject = sinon
  301. .stub()
  302. .callsArgWith(1, null, this.urls)
  303. this.UrlCache._clearUrlFromCache = sinon.stub().callsArg(2)
  304. return this.UrlCache.clearProject(this.project_id, this.callback)
  305. })
  306. it('should clear the cache for each url in the project', function () {
  307. return Array.from(this.urls).map((url) =>
  308. this.UrlCache._clearUrlFromCache
  309. .calledWith(this.project_id, url)
  310. .should.equal(true)
  311. )
  312. })
  313. return it('should call the callback', function () {
  314. return this.callback.called.should.equal(true)
  315. })
  316. })
  317. })