2014-02-12 05:40:42 -05:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
|
|
|
modulePath = "../../../../app/js/HttpController.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
Errors = require "../../../../app/js/Errors.js"
|
|
|
|
|
2016-01-20 12:36:06 -05:00
|
|
|
describe "HttpController.setDoc", ->
|
2014-02-12 05:40:42 -05:00
|
|
|
beforeEach ->
|
|
|
|
@HttpController = SandboxedModule.require modulePath, requires:
|
|
|
|
"./DocumentManager": @DocumentManager = {}
|
|
|
|
"./ProjectManager": {}
|
|
|
|
"logger-sharelatex" : @logger = { log: sinon.stub() }
|
|
|
|
"./Metrics": @Metrics = {}
|
|
|
|
|
|
|
|
@Metrics.Timer = class Timer
|
|
|
|
done: sinon.stub()
|
|
|
|
|
|
|
|
@project_id = "project-id-123"
|
|
|
|
@doc_id = "doc-id-123"
|
|
|
|
@lines = ["one", "two", "three"]
|
2014-03-11 08:47:26 -04:00
|
|
|
@source = "dropbox"
|
|
|
|
@user_id = "user-id-123"
|
2014-02-12 05:40:42 -05:00
|
|
|
@res =
|
|
|
|
send: sinon.stub()
|
|
|
|
@req =
|
2016-04-29 10:08:21 -04:00
|
|
|
headers: {}
|
2014-02-12 05:40:42 -05:00
|
|
|
params:
|
|
|
|
project_id: @project_id
|
|
|
|
doc_id: @doc_id
|
|
|
|
body:
|
|
|
|
lines: @lines
|
2014-03-11 08:47:26 -04:00
|
|
|
source: @source
|
|
|
|
user_id: @user_id
|
2014-02-12 05:40:42 -05:00
|
|
|
@next = sinon.stub()
|
|
|
|
|
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
2014-03-11 08:47:26 -04:00
|
|
|
@DocumentManager.setDocWithLock = sinon.stub().callsArgWith(5)
|
2014-02-12 05:40:42 -05:00
|
|
|
@HttpController.setDoc(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should set the doc", ->
|
|
|
|
@DocumentManager.setDocWithLock
|
2014-03-11 08:47:26 -04:00
|
|
|
.calledWith(@project_id, @doc_id, @lines, @source, @user_id)
|
2014-02-12 05:40:42 -05:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a successful No Content response", ->
|
|
|
|
@res.send
|
|
|
|
.calledWith(204)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should log the request", ->
|
|
|
|
@logger.log
|
2014-03-11 08:47:26 -04:00
|
|
|
.calledWith(doc_id: @doc_id, project_id: @project_id, lines: @lines, source: @source, user_id: @user_id, "setting doc via http")
|
2014-02-12 05:40:42 -05:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should time the request", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
|
|
|
describe "when an errors occurs", ->
|
|
|
|
beforeEach ->
|
2014-03-11 08:47:26 -04:00
|
|
|
@DocumentManager.setDocWithLock = sinon.stub().callsArgWith(5, new Error("oops"))
|
2014-02-12 05:40:42 -05:00
|
|
|
@HttpController.setDoc(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should call next with the error", ->
|
|
|
|
@next
|
|
|
|
.calledWith(new Error("oops"))
|
|
|
|
.should.equal true
|
|
|
|
|
2016-04-29 10:08:21 -04:00
|
|
|
describe "when the payload is too large", ->
|
|
|
|
beforeEach ->
|
2016-05-11 10:55:21 -04:00
|
|
|
lines = []
|
2016-05-12 04:26:50 -04:00
|
|
|
for _ in [0..200000]
|
2016-05-11 10:55:21 -04:00
|
|
|
lines.push "test test test"
|
|
|
|
@req.body.lines = lines
|
2016-04-29 10:08:21 -04:00
|
|
|
@DocumentManager.setDocWithLock = sinon.stub().callsArgWith(5)
|
|
|
|
@HttpController.setDoc(@req, @res, @next)
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2016-04-29 10:08:21 -04:00
|
|
|
it 'should send back a 406 response', ->
|
|
|
|
@res.send.calledWith(406).should.equal true
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2016-04-29 10:08:21 -04:00
|
|
|
it 'should not call setDocWithLock', ->
|
|
|
|
@DocumentManager.setDocWithLock.callCount.should.equal 0
|