2020-02-19 06:15:25 -05:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// 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:
|
|
|
|
* 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')
|
2020-03-12 05:35:11 -04:00
|
|
|
const { expect } = require('chai')
|
2020-02-19 06:15:37 -05:00
|
|
|
const modulePath = require('path').join(__dirname, '../../../app/js/UrlFetcher')
|
|
|
|
const { EventEmitter } = require('events')
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('UrlFetcher', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.callback = sinon.stub()
|
|
|
|
this.url = 'https://www.example.com/file/here?query=string'
|
|
|
|
return (this.UrlFetcher = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
|
|
|
request: {
|
2021-07-13 07:04:48 -04:00
|
|
|
defaults: (this.defaults = sinon.stub().returns((this.request = {}))),
|
2020-02-19 06:15:37 -05:00
|
|
|
},
|
2021-10-06 04:11:59 -04:00
|
|
|
fs: (this.fs = {
|
|
|
|
rename: sinon.stub().yields(),
|
|
|
|
unlink: sinon.stub().yields(),
|
|
|
|
}),
|
2021-07-12 12:47:21 -04:00
|
|
|
'@overleaf/settings': (this.settings = {
|
2021-07-02 04:17:29 -04:00
|
|
|
apis: {
|
|
|
|
clsiPerf: {
|
2021-07-13 07:04:48 -04:00
|
|
|
host: 'localhost:3043',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
2020-02-19 06:15:37 -05:00
|
|
|
}))
|
|
|
|
})
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('pipeUrlToFileWithRetry', function () {
|
|
|
|
this.beforeEach(function () {
|
2020-05-14 08:09:57 -04:00
|
|
|
this.UrlFetcher.pipeUrlToFile = sinon.stub()
|
|
|
|
})
|
2020-02-19 06:15:37 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call pipeUrlToFile', function (done) {
|
2020-05-14 08:09:57 -04:00
|
|
|
this.UrlFetcher.pipeUrlToFile.callsArgWith(2)
|
2021-07-13 07:04:48 -04:00
|
|
|
this.UrlFetcher.pipeUrlToFileWithRetry(this.url, this.path, err => {
|
2020-05-14 08:09:57 -04:00
|
|
|
expect(err).to.equal(undefined)
|
|
|
|
this.UrlFetcher.pipeUrlToFile.called.should.equal(true)
|
|
|
|
done()
|
|
|
|
})
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call pipeUrlToFile multiple times on error', function (done) {
|
2020-06-02 04:51:34 -04:00
|
|
|
const error = new Error("couldn't download file")
|
2020-05-14 08:09:57 -04:00
|
|
|
this.UrlFetcher.pipeUrlToFile.callsArgWith(2, error)
|
2021-07-13 07:04:48 -04:00
|
|
|
this.UrlFetcher.pipeUrlToFileWithRetry(this.url, this.path, err => {
|
2020-05-14 08:09:57 -04:00
|
|
|
expect(err).to.equal(error)
|
|
|
|
this.UrlFetcher.pipeUrlToFile.callCount.should.equal(3)
|
|
|
|
done()
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call pipeUrlToFile twice if only 1 error', function (done) {
|
2020-05-14 08:09:57 -04:00
|
|
|
this.UrlFetcher.pipeUrlToFile.onCall(0).callsArgWith(2, 'error')
|
|
|
|
this.UrlFetcher.pipeUrlToFile.onCall(1).callsArgWith(2)
|
2021-07-13 07:04:48 -04:00
|
|
|
this.UrlFetcher.pipeUrlToFileWithRetry(this.url, this.path, err => {
|
2020-05-14 08:09:57 -04:00
|
|
|
expect(err).to.equal(undefined)
|
|
|
|
this.UrlFetcher.pipeUrlToFile.callCount.should.equal(2)
|
|
|
|
done()
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('pipeUrlToFile', function () {
|
|
|
|
it('should turn off the cookie jar in request', function () {
|
2020-05-14 08:09:57 -04:00
|
|
|
return this.defaults.calledWith({ jar: false }).should.equal(true)
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('rewrite url domain if filestoreDomainOveride is set', function () {
|
|
|
|
beforeEach(function () {
|
2020-05-14 08:09:57 -04:00
|
|
|
this.path = '/path/to/file/on/disk'
|
|
|
|
this.request.get = sinon
|
|
|
|
.stub()
|
|
|
|
.returns((this.urlStream = new EventEmitter()))
|
|
|
|
this.urlStream.pipe = sinon.stub()
|
|
|
|
this.urlStream.pause = sinon.stub()
|
|
|
|
this.urlStream.resume = sinon.stub()
|
|
|
|
this.fs.createWriteStream = sinon
|
|
|
|
.stub()
|
|
|
|
.returns((this.fileStream = new EventEmitter()))
|
|
|
|
return (this.fs.unlink = (file, callback) => callback())
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should use the normal domain when override not set', function (done) {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.UrlFetcher.pipeUrlToFile(this.url, this.path, () => {
|
2020-05-14 08:09:57 -04:00
|
|
|
this.request.get.args[0][0].url.should.equal(this.url)
|
2020-02-19 06:15:37 -05:00
|
|
|
return done()
|
|
|
|
})
|
|
|
|
this.res = { statusCode: 200 }
|
|
|
|
this.urlStream.emit('response', this.res)
|
|
|
|
this.urlStream.emit('end')
|
|
|
|
return this.fileStream.emit('finish')
|
|
|
|
})
|
|
|
|
|
2021-07-02 04:17:29 -04:00
|
|
|
it('should not use override clsiPerf domain when filestoreDomainOveride is set', function (done) {
|
|
|
|
this.settings.filestoreDomainOveride = '192.11.11.11'
|
|
|
|
const url = 'http://localhost:3043/file/here?query=string'
|
|
|
|
this.UrlFetcher.pipeUrlToFile(url, this.path, () => {
|
|
|
|
this.request.get.args[0][0].url.should.equal(url)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
this.res = { statusCode: 200 }
|
|
|
|
this.urlStream.emit('response', this.res)
|
|
|
|
this.urlStream.emit('end')
|
|
|
|
this.fileStream.emit('finish')
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should use override domain when filestoreDomainOveride is set', function (done) {
|
2020-05-14 08:09:57 -04:00
|
|
|
this.settings.filestoreDomainOveride = '192.11.11.11'
|
|
|
|
this.UrlFetcher.pipeUrlToFile(this.url, this.path, () => {
|
|
|
|
this.request.get.args[0][0].url.should.equal(
|
|
|
|
'192.11.11.11/file/here?query=string'
|
|
|
|
)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
this.res = { statusCode: 200 }
|
|
|
|
this.urlStream.emit('response', this.res)
|
|
|
|
this.urlStream.emit('end')
|
|
|
|
return this.fileStream.emit('finish')
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
2020-05-14 08:09:57 -04:00
|
|
|
})
|
2020-02-19 06:15:37 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('pipeUrlToFile', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-05-14 08:09:57 -04:00
|
|
|
this.path = '/path/to/file/on/disk'
|
|
|
|
this.request.get = sinon
|
|
|
|
.stub()
|
|
|
|
.returns((this.urlStream = new EventEmitter()))
|
|
|
|
this.urlStream.pipe = sinon.stub()
|
|
|
|
this.urlStream.pause = sinon.stub()
|
|
|
|
this.urlStream.resume = sinon.stub()
|
|
|
|
this.fs.createWriteStream = sinon
|
|
|
|
.stub()
|
|
|
|
.returns((this.fileStream = new EventEmitter()))
|
|
|
|
this.fs.unlink = (file, callback) => callback()
|
|
|
|
return done()
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('successfully', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-05-14 08:09:57 -04:00
|
|
|
this.UrlFetcher.pipeUrlToFile(this.url, this.path, () => {
|
|
|
|
this.callback()
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
this.res = { statusCode: 200 }
|
|
|
|
this.urlStream.emit('response', this.res)
|
|
|
|
this.urlStream.emit('end')
|
|
|
|
return this.fileStream.emit('finish')
|
|
|
|
})
|
2020-02-19 06:15:37 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should request the URL', function () {
|
2020-05-14 08:09:57 -04:00
|
|
|
return this.request.get
|
|
|
|
.calledWith(sinon.match({ url: this.url }))
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
2020-02-19 06:15:37 -05:00
|
|
|
|
2021-10-06 04:11:59 -04:00
|
|
|
it('should open the atomic file for writing', function () {
|
2020-05-14 08:09:57 -04:00
|
|
|
return this.fs.createWriteStream
|
2021-10-06 04:11:59 -04:00
|
|
|
.calledWith(this.path + '~')
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should move the atomic file to the target', function () {
|
|
|
|
return this.fs.rename
|
|
|
|
.calledWith(this.path + '~', this.path)
|
2020-05-14 08:09:57 -04:00
|
|
|
.should.equal(true)
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should pipe the URL to the file', function () {
|
2020-05-14 08:09:57 -04:00
|
|
|
return this.urlStream.pipe
|
|
|
|
.calledWith(this.fileStream)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
2020-03-12 05:35:11 -04:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-05-14 08:09:57 -04:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with non success status code', function () {
|
|
|
|
beforeEach(function (done) {
|
2021-07-13 07:04:48 -04:00
|
|
|
this.UrlFetcher.pipeUrlToFile(this.url, this.path, err => {
|
2020-05-14 08:09:57 -04:00
|
|
|
this.callback(err)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
this.res = { statusCode: 404 }
|
|
|
|
this.urlStream.emit('response', this.res)
|
|
|
|
return this.urlStream.emit('end')
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call the callback with an error', function () {
|
2020-05-14 08:09:57 -04:00
|
|
|
this.callback.calledWith(sinon.match(Error)).should.equal(true)
|
|
|
|
|
|
|
|
const message = this.callback.args[0][0].message
|
|
|
|
expect(message).to.include(
|
|
|
|
'URL returned non-success status code: 404'
|
|
|
|
)
|
|
|
|
})
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('with error', function () {
|
|
|
|
beforeEach(function (done) {
|
2021-07-13 07:04:48 -04:00
|
|
|
this.UrlFetcher.pipeUrlToFile(this.url, this.path, err => {
|
2020-05-14 08:09:57 -04:00
|
|
|
this.callback(err)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
return this.urlStream.emit(
|
|
|
|
'error',
|
|
|
|
(this.error = new Error('something went wrong'))
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call the callback with the error', function () {
|
2020-05-14 08:09:57 -04:00
|
|
|
return this.callback.calledWith(this.error).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should only call the callback once, even if end is called', function () {
|
2020-05-14 08:09:57 -04:00
|
|
|
this.urlStream.emit('end')
|
|
|
|
return this.callback.calledOnce.should.equal(true)
|
|
|
|
})
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|