2014-02-12 17:27:43 +00:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
sinon = require('sinon')
|
|
|
|
require('chai').should()
|
|
|
|
modulePath = require('path').join __dirname, '../../../app/js/UrlFetcher'
|
|
|
|
EventEmitter = require("events").EventEmitter
|
|
|
|
|
|
|
|
describe "UrlFetcher", ->
|
|
|
|
beforeEach ->
|
|
|
|
@callback = sinon.stub()
|
2018-07-03 15:41:34 +00:00
|
|
|
@url = "https://www.example.com/file/here?query=string"
|
2014-02-12 17:27:43 +00:00
|
|
|
@UrlFetcher = SandboxedModule.require modulePath, requires:
|
|
|
|
request: defaults: @defaults = sinon.stub().returns(@request = {})
|
|
|
|
fs: @fs = {}
|
2015-04-29 14:55:58 +00:00
|
|
|
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
|
2018-07-03 15:41:34 +00:00
|
|
|
"settings-sharelatex": @settings = {}
|
2014-02-12 17:27:43 +00:00
|
|
|
|
|
|
|
it "should turn off the cookie jar in request", ->
|
|
|
|
@defaults.calledWith(jar: false)
|
|
|
|
.should.equal true
|
2018-07-03 15:41:34 +00:00
|
|
|
|
|
|
|
describe "rewrite url domain if filestoreDomainOveride is set", ->
|
|
|
|
beforeEach ->
|
|
|
|
@path = "/path/to/file/on/disk"
|
|
|
|
@request.get = sinon.stub().returns(@urlStream = new EventEmitter)
|
|
|
|
@urlStream.pipe = sinon.stub()
|
|
|
|
@urlStream.pause = sinon.stub()
|
|
|
|
@urlStream.resume = sinon.stub()
|
|
|
|
@fs.createWriteStream = sinon.stub().returns(@fileStream = new EventEmitter)
|
|
|
|
@fs.unlink = (file, callback) -> callback()
|
|
|
|
|
|
|
|
it "should use the normal domain when override not set", (done)->
|
|
|
|
@UrlFetcher.pipeUrlToFile @url, @path, =>
|
|
|
|
@request.get.args[0][0].url.should.equal @url
|
|
|
|
done()
|
|
|
|
@res = statusCode: 200
|
|
|
|
@urlStream.emit "response", @res
|
|
|
|
@urlStream.emit "end"
|
|
|
|
@fileStream.emit "finish"
|
|
|
|
|
|
|
|
|
|
|
|
it "should use override domain when filestoreDomainOveride is set", (done)->
|
|
|
|
@settings.filestoreDomainOveride = "192.11.11.11"
|
|
|
|
@UrlFetcher.pipeUrlToFile @url, @path, =>
|
|
|
|
@request.get.args[0][0].url.should.equal "192.11.11.11/file/here?query=string"
|
|
|
|
done()
|
|
|
|
@res = statusCode: 200
|
|
|
|
@urlStream.emit "response", @res
|
|
|
|
@urlStream.emit "end"
|
|
|
|
@fileStream.emit "finish"
|
|
|
|
|
2018-03-05 10:39:46 +00:00
|
|
|
describe "pipeUrlToFile", ->
|
|
|
|
beforeEach (done)->
|
2014-02-12 17:27:43 +00:00
|
|
|
@path = "/path/to/file/on/disk"
|
|
|
|
@request.get = sinon.stub().returns(@urlStream = new EventEmitter)
|
|
|
|
@urlStream.pipe = sinon.stub()
|
2015-05-15 13:07:15 +00:00
|
|
|
@urlStream.pause = sinon.stub()
|
|
|
|
@urlStream.resume = sinon.stub()
|
|
|
|
@fs.createWriteStream = sinon.stub().returns(@fileStream = new EventEmitter)
|
2015-04-29 14:55:58 +00:00
|
|
|
@fs.unlink = (file, callback) -> callback()
|
2018-03-05 10:39:46 +00:00
|
|
|
done()
|
2014-02-12 17:27:43 +00:00
|
|
|
|
|
|
|
describe "successfully", ->
|
2018-03-05 10:39:46 +00:00
|
|
|
beforeEach (done)->
|
|
|
|
@UrlFetcher.pipeUrlToFile @url, @path, =>
|
|
|
|
@callback()
|
|
|
|
done()
|
2014-02-12 17:27:43 +00:00
|
|
|
@res = statusCode: 200
|
|
|
|
@urlStream.emit "response", @res
|
|
|
|
@urlStream.emit "end"
|
2015-05-15 13:07:15 +00:00
|
|
|
@fileStream.emit "finish"
|
|
|
|
|
2018-03-05 10:39:46 +00:00
|
|
|
|
|
|
|
it "should request the URL", ->
|
|
|
|
@request.get
|
|
|
|
.calledWith(sinon.match {"url": @url})
|
|
|
|
.should.equal true
|
|
|
|
|
2015-05-15 13:07:15 +00:00
|
|
|
it "should open the file for writing", ->
|
|
|
|
@fs.createWriteStream
|
|
|
|
.calledWith(@path)
|
|
|
|
.should.equal true
|
2014-02-12 17:27:43 +00:00
|
|
|
|
|
|
|
it "should pipe the URL to the file", ->
|
|
|
|
@urlStream.pipe
|
|
|
|
.calledWith(@fileStream)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
|
@callback.called.should.equal true
|
|
|
|
|
|
|
|
describe "with non success status code", ->
|
2018-03-05 10:39:46 +00:00
|
|
|
beforeEach (done)->
|
|
|
|
@UrlFetcher.pipeUrlToFile @url, @path, (err)=>
|
|
|
|
@callback(err)
|
|
|
|
done()
|
2014-02-12 17:27:43 +00:00
|
|
|
@res = statusCode: 404
|
|
|
|
@urlStream.emit "response", @res
|
|
|
|
@urlStream.emit "end"
|
|
|
|
|
|
|
|
it "should call the callback with an error", ->
|
|
|
|
@callback
|
|
|
|
.calledWith(new Error("URL returned non-success status code: 404"))
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "with error", ->
|
2018-03-05 10:39:46 +00:00
|
|
|
beforeEach (done)->
|
|
|
|
@UrlFetcher.pipeUrlToFile @url, @path, (err)=>
|
|
|
|
@callback(err)
|
|
|
|
done()
|
2014-02-12 17:27:43 +00:00
|
|
|
@urlStream.emit "error", @error = new Error("something went wrong")
|
|
|
|
|
|
|
|
it "should call the callback with the error", ->
|
|
|
|
@callback
|
|
|
|
.calledWith(@error)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should only call the callback once, even if end is called", ->
|
|
|
|
@urlStream.emit "end"
|
|
|
|
@callback.calledOnce.should.equal true
|
|
|
|
|