2014-02-14 11:39:05 -05:00
|
|
|
assert = require("chai").assert
|
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
|
|
|
expect = chai.expect
|
|
|
|
modulePath = "../../../app/js/FileController.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
|
|
|
|
describe "FileController", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
2014-02-25 11:38:13 -05:00
|
|
|
@PersistorManager =
|
|
|
|
sendStream: sinon.stub()
|
2014-02-25 10:52:30 -05:00
|
|
|
copyFile: sinon.stub()
|
|
|
|
deleteFile:sinon.stub()
|
|
|
|
|
2014-02-14 11:39:05 -05:00
|
|
|
@settings =
|
|
|
|
s3:
|
|
|
|
buckets:
|
|
|
|
user_files:"user_files"
|
|
|
|
@FileHandler =
|
|
|
|
getFile: sinon.stub()
|
|
|
|
deleteFile: sinon.stub()
|
|
|
|
insertFile: sinon.stub()
|
2016-03-13 19:45:48 -04:00
|
|
|
getDirectorySize: sinon.stub()
|
2014-02-14 11:39:05 -05:00
|
|
|
@LocalFileWriter = {}
|
|
|
|
@controller = SandboxedModule.require modulePath, requires:
|
|
|
|
"./LocalFileWriter":@LocalFileWriter
|
|
|
|
"./FileHandler": @FileHandler
|
2014-02-25 11:38:13 -05:00
|
|
|
"./PersistorManager":@PersistorManager
|
2019-01-09 05:31:59 -05:00
|
|
|
"./Errors": @Errors =
|
|
|
|
NotFoundError: sinon.stub()
|
2014-02-14 11:39:05 -05:00
|
|
|
"settings-sharelatex": @settings
|
2018-11-29 08:26:14 -05:00
|
|
|
"metrics-sharelatex":
|
|
|
|
inc:->
|
2014-02-14 11:39:05 -05:00
|
|
|
"logger-sharelatex":
|
|
|
|
log:->
|
|
|
|
err:->
|
|
|
|
@project_id = "project_id"
|
|
|
|
@file_id = "file_id"
|
|
|
|
@bucket = "user_files"
|
|
|
|
@key = "#{@project_id}/#{@file_id}"
|
2014-02-25 10:52:30 -05:00
|
|
|
@req =
|
2014-02-14 11:39:05 -05:00
|
|
|
key:@key
|
|
|
|
bucket:@bucket
|
|
|
|
query:{}
|
2014-02-25 10:52:30 -05:00
|
|
|
params:
|
2014-02-14 11:39:05 -05:00
|
|
|
project_id:@project_id
|
|
|
|
file_id:@file_id
|
2015-08-28 06:45:16 -04:00
|
|
|
headers: {}
|
2014-02-25 05:54:51 -05:00
|
|
|
@res =
|
2014-02-14 11:39:05 -05:00
|
|
|
setHeader: ->
|
|
|
|
@fileStream = {}
|
|
|
|
|
|
|
|
describe "getFile", ->
|
|
|
|
|
|
|
|
it "should pipe the stream", (done)->
|
|
|
|
@FileHandler.getFile.callsArgWith(3, null, @fileStream)
|
|
|
|
@fileStream.pipe = (res)=>
|
|
|
|
res.should.equal @res
|
|
|
|
done()
|
|
|
|
@controller.getFile @req, @res
|
|
|
|
|
|
|
|
it "should send a 200 if the cacheWarm param is true", (done)->
|
2014-02-26 04:06:47 -05:00
|
|
|
@req.query.cacheWarm = true
|
2014-02-14 11:39:05 -05:00
|
|
|
@FileHandler.getFile.callsArgWith(3, null, @fileStream)
|
|
|
|
@res.send = (statusCode)=>
|
|
|
|
statusCode.should.equal 200
|
|
|
|
done()
|
|
|
|
@controller.getFile @req, @res
|
|
|
|
|
|
|
|
it "should send a 500 if there is a problem", (done)->
|
|
|
|
@FileHandler.getFile.callsArgWith(3, "error")
|
|
|
|
@res.send = (code)=>
|
|
|
|
code.should.equal 500
|
|
|
|
done()
|
|
|
|
@controller.getFile @req, @res
|
|
|
|
|
2015-08-28 06:45:16 -04:00
|
|
|
describe "with a 'Range' header set", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@req.headers.range = 'bytes=0-8'
|
|
|
|
|
|
|
|
it "should pass 'start' and 'end' options to FileHandler", (done) ->
|
|
|
|
@FileHandler.getFile.callsArgWith(3, null, @fileStream)
|
|
|
|
@fileStream.pipe = (res)=>
|
|
|
|
expect(@FileHandler.getFile.lastCall.args[2].start).to.equal 0
|
|
|
|
expect(@FileHandler.getFile.lastCall.args[2].end).to.equal 8
|
|
|
|
done()
|
|
|
|
@controller.getFile @req, @res
|
|
|
|
|
2014-02-14 11:39:05 -05:00
|
|
|
describe "insertFile", ->
|
|
|
|
|
2014-02-25 11:38:13 -05:00
|
|
|
it "should send bucket name key and res to PersistorManager", (done)->
|
2014-02-14 11:39:05 -05:00
|
|
|
@FileHandler.insertFile.callsArgWith(3)
|
|
|
|
@res.send = =>
|
|
|
|
@FileHandler.insertFile.calledWith(@bucket, @key, @req).should.equal true
|
|
|
|
done()
|
|
|
|
@controller.insertFile @req, @res
|
|
|
|
|
|
|
|
describe "copyFile", ->
|
|
|
|
beforeEach ->
|
|
|
|
@oldFile_id = "old_file_id"
|
|
|
|
@oldProject_id = "old_project_id"
|
|
|
|
@req.body =
|
|
|
|
source:
|
|
|
|
project_id: @oldProject_id
|
|
|
|
file_id: @oldFile_id
|
|
|
|
|
2014-02-25 11:38:13 -05:00
|
|
|
it "should send bucket name and both keys to PersistorManager", (done)->
|
|
|
|
@PersistorManager.copyFile.callsArgWith(3)
|
2014-02-14 11:39:05 -05:00
|
|
|
@res.send = (code)=>
|
|
|
|
code.should.equal 200
|
2014-02-25 11:38:13 -05:00
|
|
|
@PersistorManager.copyFile.calledWith(@bucket, "#{@oldProject_id}/#{@oldFile_id}", @key).should.equal true
|
2014-02-14 11:39:05 -05:00
|
|
|
done()
|
|
|
|
@controller.copyFile @req, @res
|
|
|
|
|
2019-01-09 05:31:59 -05:00
|
|
|
it "should send a 404 if the original file was not found", (done) ->
|
|
|
|
@PersistorManager.copyFile.callsArgWith(3, new @Errors.NotFoundError())
|
|
|
|
@res.send = (code)=>
|
|
|
|
code.should.equal 404
|
|
|
|
done()
|
|
|
|
@controller.copyFile @req, @res
|
|
|
|
|
2014-02-14 11:39:05 -05:00
|
|
|
it "should send a 500 if there was an error", (done)->
|
2014-02-25 11:38:13 -05:00
|
|
|
@PersistorManager.copyFile.callsArgWith(3, "error")
|
2014-02-14 11:39:05 -05:00
|
|
|
@res.send = (code)=>
|
|
|
|
code.should.equal 500
|
|
|
|
done()
|
2015-08-28 06:45:16 -04:00
|
|
|
@controller.copyFile @req, @res
|
2014-02-14 11:39:05 -05:00
|
|
|
|
|
|
|
describe "delete file", ->
|
|
|
|
|
|
|
|
it "should tell the file handler", (done)->
|
|
|
|
@FileHandler.deleteFile.callsArgWith(2)
|
|
|
|
@res.send = (code)=>
|
|
|
|
code.should.equal 204
|
|
|
|
@FileHandler.deleteFile.calledWith(@bucket, @key).should.equal true
|
|
|
|
done()
|
|
|
|
@controller.deleteFile @req, @res
|
|
|
|
|
|
|
|
it "should send a 500 if there was an error", (done)->
|
|
|
|
@FileHandler.deleteFile.callsArgWith(2, "error")
|
|
|
|
@res.send = (code)->
|
|
|
|
code.should.equal 500
|
|
|
|
done()
|
|
|
|
@controller.deleteFile @req, @res
|
2015-08-28 07:02:50 -04:00
|
|
|
|
|
|
|
describe "_get_range", ->
|
|
|
|
|
|
|
|
it "should parse a valid Range header", (done) ->
|
|
|
|
result = @controller._get_range('bytes=0-200')
|
|
|
|
expect(result).to.not.equal null
|
|
|
|
expect(result.start).to.equal 0
|
|
|
|
expect(result.end).to.equal 200
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return null for an invalid Range header", (done) ->
|
|
|
|
result = @controller._get_range('wat')
|
|
|
|
expect(result).to.equal null
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return null for any type other than 'bytes'", (done) ->
|
|
|
|
result = @controller._get_range('carrots=0-200')
|
|
|
|
expect(result).to.equal null
|
|
|
|
done()
|
2016-03-13 15:22:14 -04:00
|
|
|
|
2016-03-13 19:45:48 -04:00
|
|
|
describe "directorySize", ->
|
2016-03-13 15:22:14 -04:00
|
|
|
|
2016-03-13 19:45:48 -04:00
|
|
|
it "should return total directory size bytes", (done) ->
|
|
|
|
@FileHandler.getDirectorySize.callsArgWith(2, null, 1024)
|
|
|
|
@controller.directorySize @req, json:(result)=>
|
|
|
|
expect(result['total bytes']).to.equal 1024
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should send a 500 if there was an error", (done)->
|
|
|
|
@FileHandler.getDirectorySize.callsArgWith(2, "error")
|
|
|
|
@res.send = (code)->
|
|
|
|
code.should.equal 500
|
|
|
|
done()
|
|
|
|
@controller.directorySize @req, @res
|