2020-02-19 06:15:25 -05:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-19 06:15:08 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-19 06:15:37 -05:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const modulePath = require('path').join(__dirname, '../../../app/js/UrlCache')
|
|
|
|
const { EventEmitter } = require('events')
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('UrlCache', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.callback = sinon.stub()
|
|
|
|
this.url = 'www.example.com/file'
|
|
|
|
this.project_id = 'project-id-123'
|
|
|
|
return (this.UrlCache = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
|
|
|
'./db': {},
|
|
|
|
'./UrlFetcher': (this.UrlFetcher = {}),
|
2021-07-12 12:47:21 -04:00
|
|
|
'@overleaf/settings': (this.Settings = {
|
2021-07-13 07:04:48 -04:00
|
|
|
path: { clsiCacheDir: '/cache/dir' },
|
2020-02-19 06:15:37 -05:00
|
|
|
}),
|
2021-07-13 07:04:48 -04:00
|
|
|
fs: (this.fs = { copyFile: sinon.stub().yields() }),
|
|
|
|
},
|
2020-02-19 06:15:37 -05:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('_doesUrlNeedDownloading', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.lastModified = new Date()
|
|
|
|
return (this.lastModifiedRoundedToSeconds = new Date(
|
|
|
|
Math.floor(this.lastModified.getTime() / 1000) * 1000
|
|
|
|
))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when URL does not exist in cache', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.UrlCache._findUrlDetails = sinon.stub().callsArgWith(2, null, null)
|
|
|
|
return this.UrlCache._doesUrlNeedDownloading(
|
|
|
|
this.project_id,
|
|
|
|
this.url,
|
|
|
|
this.lastModified,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should return the callback with true', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.calledWith(null, true).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('when URL does exist in cache', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.urlDetails = {}
|
|
|
|
return (this.UrlCache._findUrlDetails = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, null, this.urlDetails))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when the modified date is more recent than the cached modified date', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.urlDetails.lastModified = new Date(
|
|
|
|
this.lastModified.getTime() - 1000
|
|
|
|
)
|
|
|
|
return this.UrlCache._doesUrlNeedDownloading(
|
|
|
|
this.project_id,
|
|
|
|
this.url,
|
|
|
|
this.lastModified,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should get the url details', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.UrlCache._findUrlDetails
|
|
|
|
.calledWith(this.project_id, this.url)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should return the callback with true', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.calledWith(null, true).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when the cached modified date is more recent than the modified date', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.urlDetails.lastModified = new Date(
|
|
|
|
this.lastModified.getTime() + 1000
|
|
|
|
)
|
|
|
|
return this.UrlCache._doesUrlNeedDownloading(
|
|
|
|
this.project_id,
|
|
|
|
this.url,
|
|
|
|
this.lastModified,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should return the callback with false', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.calledWith(null, false).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when the cached modified date is equal to the modified date', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.urlDetails.lastModified = this.lastModified
|
|
|
|
return this.UrlCache._doesUrlNeedDownloading(
|
|
|
|
this.project_id,
|
|
|
|
this.url,
|
|
|
|
this.lastModified,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should return the callback with false', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.calledWith(null, false).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when the provided modified date does not exist', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.lastModified = null
|
|
|
|
return this.UrlCache._doesUrlNeedDownloading(
|
|
|
|
this.project_id,
|
|
|
|
this.url,
|
|
|
|
this.lastModified,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should return the callback with true', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.calledWith(null, true).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('when the URL does not have a modified date', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.urlDetails.lastModified = null
|
|
|
|
return this.UrlCache._doesUrlNeedDownloading(
|
|
|
|
this.project_id,
|
|
|
|
this.url,
|
|
|
|
this.lastModified,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should return the callback with true', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.calledWith(null, true).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('_ensureUrlIsInCache', function () {
|
|
|
|
beforeEach(function () {
|
2020-05-14 08:09:57 -04:00
|
|
|
this.UrlFetcher.pipeUrlToFileWithRetry = sinon.stub().callsArg(2)
|
2020-02-19 06:15:37 -05:00
|
|
|
return (this.UrlCache._updateOrCreateUrlDetails = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArg(3))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when the URL needs updating', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.UrlCache._doesUrlNeedDownloading = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(3, null, true)
|
|
|
|
return this.UrlCache._ensureUrlIsInCache(
|
|
|
|
this.project_id,
|
|
|
|
this.url,
|
|
|
|
this.lastModified,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should check that the url needs downloading', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.UrlCache._doesUrlNeedDownloading
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.url,
|
|
|
|
this.lastModifiedRoundedToSeconds
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should download the URL to the cache file', function () {
|
2020-05-14 08:09:57 -04:00
|
|
|
return this.UrlFetcher.pipeUrlToFileWithRetry
|
2020-02-19 06:15:37 -05:00
|
|
|
.calledWith(
|
|
|
|
this.url,
|
|
|
|
this.UrlCache._cacheFilePathForUrl(this.project_id, this.url)
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should update the database entry', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.UrlCache._updateOrCreateUrlDetails
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.url,
|
|
|
|
this.lastModifiedRoundedToSeconds
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should return the callback with the cache file path', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWith(
|
|
|
|
null,
|
|
|
|
this.UrlCache._cacheFilePathForUrl(this.project_id, this.url)
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('when the URL does not need updating', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.UrlCache._doesUrlNeedDownloading = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(3, null, false)
|
|
|
|
return this.UrlCache._ensureUrlIsInCache(
|
|
|
|
this.project_id,
|
|
|
|
this.url,
|
|
|
|
this.lastModified,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not download the URL to the cache file', function () {
|
2020-05-14 08:09:57 -04:00
|
|
|
return this.UrlFetcher.pipeUrlToFileWithRetry.called.should.equal(false)
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should return the callback with the cache file path', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWith(
|
|
|
|
null,
|
|
|
|
this.UrlCache._cacheFilePathForUrl(this.project_id, this.url)
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('downloadUrlToFile', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.cachePath = 'path/to/cached/url'
|
|
|
|
this.destPath = 'path/to/destination'
|
|
|
|
this.UrlCache._ensureUrlIsInCache = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(3, null, this.cachePath)
|
|
|
|
return this.UrlCache.downloadUrlToFile(
|
|
|
|
this.project_id,
|
|
|
|
this.url,
|
|
|
|
this.destPath,
|
|
|
|
this.lastModified,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should ensure the URL is downloaded and updated in the cache', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.UrlCache._ensureUrlIsInCache
|
|
|
|
.calledWith(this.project_id, this.url, this.lastModified)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should copy the file to the new location', function () {
|
2021-05-17 05:54:11 -04:00
|
|
|
return this.fs.copyFile
|
2020-02-19 06:15:37 -05:00
|
|
|
.calledWith(this.cachePath, this.destPath)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('_deleteUrlCacheFromDisk', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.fs.unlink = sinon.stub().callsArg(1)
|
|
|
|
return this.UrlCache._deleteUrlCacheFromDisk(
|
|
|
|
this.project_id,
|
|
|
|
this.url,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should delete the cache file', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.fs.unlink
|
|
|
|
.calledWith(
|
|
|
|
this.UrlCache._cacheFilePathForUrl(this.project_id, this.url)
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('_clearUrlFromCache', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.UrlCache._deleteUrlCacheFromDisk = sinon.stub().callsArg(2)
|
|
|
|
this.UrlCache._clearUrlDetails = sinon.stub().callsArg(2)
|
|
|
|
return this.UrlCache._clearUrlFromCache(
|
|
|
|
this.project_id,
|
|
|
|
this.url,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should delete the file on disk', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.UrlCache._deleteUrlCacheFromDisk
|
|
|
|
.calledWith(this.project_id, this.url)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should clear the entry in the database', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.UrlCache._clearUrlDetails
|
|
|
|
.calledWith(this.project_id, this.url)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('clearProject', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.urls = ['www.example.com/file1', 'www.example.com/file2']
|
|
|
|
this.UrlCache._findAllUrlsInProject = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, this.urls)
|
|
|
|
this.UrlCache._clearUrlFromCache = sinon.stub().callsArg(2)
|
|
|
|
return this.UrlCache.clearProject(this.project_id, this.callback)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should clear the cache for each url in the project', function () {
|
2021-07-13 07:04:48 -04:00
|
|
|
return Array.from(this.urls).map(url =>
|
2020-02-19 06:15:37 -05:00
|
|
|
this.UrlCache._clearUrlFromCache
|
|
|
|
.calledWith(this.project_id, url)
|
|
|
|
.should.equal(true)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|