Merge pull request #15 from sharelatex/ja-require-ranges

Validate that ranges are present in a set document request
This commit is contained in:
James Allen 2017-01-31 10:13:45 +01:00 committed by GitHub
commit 048c6860d1
6 changed files with 41 additions and 13 deletions

View file

@ -42,8 +42,8 @@ module.exports = DocManager =
return callback(null, docs) return callback(null, docs)
updateDoc: (project_id, doc_id, lines, version, ranges, callback = (error, modified, rev) ->) -> updateDoc: (project_id, doc_id, lines, version, ranges, callback = (error, modified, rev) ->) ->
if !lines? or !version? if !lines? or !version? or !ranges?
return callback(new Error("no lines or version provided")) return callback(new Error("no lines, version or ranges provided"))
DocManager.getDoc project_id, doc_id, {version: true, rev: true, lines: true, version: true, ranges: true}, (err, doc)-> DocManager.getDoc project_id, doc_id, {version: true, rev: true, lines: true, version: true, ranges: true}, (err, doc)->
if err? and !(err instanceof Errors.NotFoundError) if err? and !(err instanceof Errors.NotFoundError)

View file

@ -62,6 +62,11 @@ module.exports = HttpController =
logger.error project_id: project_id, doc_id: doc_id, "no doc version provided" logger.error project_id: project_id, doc_id: doc_id, "no doc version provided"
res.send 400 # Bad Request res.send 400 # Bad Request
return return
if !ranges?
logger.error project_id: project_id, doc_id: doc_id, "no doc ranges provided"
res.send 400 # Bad Request
return
logger.log project_id: project_id, doc_id: doc_id, "got http request to update doc" logger.log project_id: project_id, doc_id: doc_id, "got http request to update doc"
DocManager.updateDoc project_id, doc_id, lines, version, ranges, (error, modified, rev) -> DocManager.updateDoc project_id, doc_id, lines, version, ranges, (error, modified, rev) ->

View file

@ -3,10 +3,8 @@ _ = require "underscore"
module.exports = RangeManager = module.exports = RangeManager =
shouldUpdateRanges: (doc_ranges, incoming_ranges) -> shouldUpdateRanges: (doc_ranges, incoming_ranges) ->
# TODO: If we have no incoming_ranges, just ignore for now while if !incoming_ranges?
# we're rolling this out, but eventually this should be a mandatory throw new Error("expected incoming_ranges")
# field and this will become an error condition
return false if !incoming_ranges?
# If the ranges are empty, we don't store them in the DB, so set # If the ranges are empty, we don't store them in the DB, so set
# doc_ranges to an empty object as default, since this is was the # doc_ranges to an empty object as default, since this is was the

View file

@ -263,14 +263,21 @@ describe "DocManager", ->
@DocManager.updateDoc @project_id, @doc_id, @newDocLines, null, @originalRanges, @callback @DocManager.updateDoc @project_id, @doc_id, @newDocLines, null, @originalRanges, @callback
it "should return an error", -> it "should return an error", ->
@callback.calledWith(new Error("no lines or version provided")).should.equal true @callback.calledWith(new Error("no lines, version or ranges provided")).should.equal true
describe "when the lines are null", -> describe "when the lines are null", ->
beforeEach -> beforeEach ->
@DocManager.updateDoc @project_id, @doc_id, null, @version, @originalRanges, @callback @DocManager.updateDoc @project_id, @doc_id, null, @version, @originalRanges, @callback
it "should return an error", -> it "should return an error", ->
@callback.calledWith(new Error("no lines or version provided")).should.equal true @callback.calledWith(new Error("no lines, version or ranges provided")).should.equal true
describe "when the ranges are null", ->
beforeEach ->
@DocManager.updateDoc @project_id, @doc_id, @newDocLines, @version, null, @callback
it "should return an error", ->
@callback.calledWith(new Error("no lines, version or ranges provided")).should.equal true
describe "when there is a generic error getting the doc", -> describe "when there is a generic error getting the doc", ->
beforeEach -> beforeEach ->

View file

@ -241,6 +241,7 @@ describe "HttpController", ->
@req.body = @req.body =
lines: @lines = ["hello", "world"] lines: @lines = ["hello", "world"]
version: @version = 42 version: @version = 42
ranges: {}
@DocManager.updateDoc = sinon.stub().yields(null, false, @rev = 5) @DocManager.updateDoc = sinon.stub().yields(null, false, @rev = 5)
@HttpController.updateDoc @req, @res, @next @HttpController.updateDoc @req, @res, @next
@ -251,7 +252,7 @@ describe "HttpController", ->
describe "when the doc lines are not provided", -> describe "when the doc lines are not provided", ->
beforeEach -> beforeEach ->
@req.body = { version: 42 } @req.body = { version: 42, ranges: {} }
@DocManager.updateDoc = sinon.stub().yields(null, false) @DocManager.updateDoc = sinon.stub().yields(null, false)
@HttpController.updateDoc @req, @res, @next @HttpController.updateDoc @req, @res, @next
@ -263,9 +264,23 @@ describe "HttpController", ->
.calledWith(400) .calledWith(400)
.should.equal true .should.equal true
describe "when the doc version is not provided", -> describe "when the doc version are not provided", ->
beforeEach -> beforeEach ->
@req.body = { lines : [ "foo" ]} @req.body = { version: 42, lines: ["hello world"] }
@DocManager.updateDoc = sinon.stub().yields(null, false)
@HttpController.updateDoc @req, @res, @next
it "should not update the document", ->
@DocManager.updateDoc.called.should.equal false
it "should return a 400 (bad request) response", ->
@res.send
.calledWith(400)
.should.equal true
describe "when the doc ranges is not provided", ->
beforeEach ->
@req.body = { lines : [ "foo" ], version: 42 }
@DocManager.updateDoc = sinon.stub().yields(null, false) @DocManager.updateDoc = sinon.stub().yields(null, false)
@HttpController.updateDoc @req, @res, @next @HttpController.updateDoc @req, @res, @next

View file

@ -1,6 +1,7 @@
SandboxedModule = require('sandboxed-module') SandboxedModule = require('sandboxed-module')
sinon = require('sinon') sinon = require('sinon')
require('chai').should() require('chai').should()
expect = require('chai').expect
modulePath = require('path').join __dirname, '../../../app/js/RangeManager' modulePath = require('path').join __dirname, '../../../app/js/RangeManager'
ObjectId = require("mongojs").ObjectId ObjectId = require("mongojs").ObjectId
assert = require("chai").assert assert = require("chai").assert
@ -110,8 +111,10 @@ describe "RangeManager", ->
@ranges_copy = @RangeManager.jsonRangesToMongo(JSON.parse(JSON.stringify(@ranges))) @ranges_copy = @RangeManager.jsonRangesToMongo(JSON.parse(JSON.stringify(@ranges)))
describe "with a blank new range", -> describe "with a blank new range", ->
it "should return false", -> it "should throw an error", ->
@RangeManager.shouldUpdateRanges(@ranges, null).should.equal false expect(() =>
@RangeManager.shouldUpdateRanges(@ranges, null)
).to.throw(Error)
describe "with a blank old range", -> describe "with a blank old range", ->
it "should treat it like {}", -> it "should treat it like {}", ->