Get append end point working with a simple acceptance test

This commit is contained in:
James Allen 2014-01-27 17:51:09 +00:00
parent 533b8e59a3
commit 5dea123b13
7 changed files with 88 additions and 32 deletions

View file

@ -1,4 +1,6 @@
**.swp
node_modules/
app/js
app.js
test/unit/js
test/acceptance/js

View file

@ -1,20 +1,26 @@
Settings = require "settings-sharelatex"
express = require "express"
app = express()
ConversoinManager = require "./app/js/ConversionManager"
HistoryManager = require "./app/js/HistoryManager"
logger = require "logger-sharelatex"
logger.initialize("history")
app.post express.bodyParser(), "/doc/:doc_id/flush", (req, res, next) ->
project_id = req.params.project_id
docOps = req.body.docOps
logger.log doc_id: doc_id, "compressing doc history"
ConversionManager.convertAndSaveRawOps doc_id, docOps, (error) ->
app.post "/doc/:doc_id/history", express.bodyParser(), (req, res, next) ->
doc_id = req.params.doc_id
docOps = req.body.docOps
version = req.body.version
logger.log doc_id: doc_id, version: version, "compressing doc history"
HistoryManager.compressAndSaveRawUpdates doc_id, docOps, (error) ->
return next(error) if error?
res.send 204 # No content
res.send 204
app.use (error, req, res, next) ->
logger.error err: error, "an internal error occured"
req.send 500
app.listen(3014)
app.listen (Settings.port ||= 3014), (error) ->
if error?
logger.error err: error, "could not start history server"
logger.log "history api listening on port 3014"

View file

@ -1,6 +1,7 @@
{db, ObjectId} = require "./mongojs"
UpdateCompressor = require "./UpdateCompressor"
logger = require "logger-sharelatex"
async = require "async"
module.exports = HistoryManager =
getLastCompressedUpdate: (doc_id, callback = (error, update) ->) ->
@ -25,15 +26,19 @@ module.exports = HistoryManager =
else
callback null, null
insertCompressedUpdates: (doc_id, docUpdates, callback = (error) ->) ->
db.docHistory.update {
doc_id: ObjectId(doc_id)
}, {
$push:
docUpdates:
$each: docUpdates
}, {
upsert: true
insertCompressedUpdates: (doc_id, updates, callback = (error) ->) ->
jobs = []
for update in updates
do (update) ->
jobs.push (callback) -> HistoryManager.insertCompressedUpdate doc_id, update, callback
async.series jobs, callback
insertCompressedUpdate: (doc_id, update, callback = (error) ->) ->
logger.log doc_id: doc_id, update: update, "inserting compressed update"
db.docHistory.insert {
doc_id: ObjectId(doc_id.toString())
op: update.op
meta: update.meta
}, callback
compressAndSaveRawUpdates: (doc_id, rawUpdates, callback = (error) ->) ->
@ -41,12 +46,9 @@ module.exports = HistoryManager =
if length == 0
return callback()
HistoryManager.popLastCompressedUpdate doc_id, (error, lastCompressedUpdate) ->
return callback(error) if error?
compressedUpdates = UpdateCompressor.compressRawUpdates lastCompressedUpdate, rawUpdates
HistoryManager.insertCompressedUpdates doc_id, compressedUpdates, (error) ->
return callback(error) if error?
logger.log doc_id: doc_id, rawUpdatesLength: length, compressedUpdatesLength: compressedUpdates.length, "compressed doc updates"

View file

@ -1,3 +1,4 @@
module.exports =
mongo:
url: 'mongodb://127.0.0.1/sharelatexTesting'
port: 3014

View file

@ -1,14 +1,15 @@
{
"name": "history-sharelatex",
"version": "0.0.1",
"dependencies": {
"async": "",
"chai": "",
"express": "3.3.5",
"sandboxed-module": "",
"sinon": "",
"mongojs": "0.7.2",
"settings": "git+ssh://git@bitbucket.org:sharelatex/settings-sharelatex.git#master",
"logger": "git+ssh://git@bitbucket.org:sharelatex/logger-sharelatex.git#bunyan"
}
"name": "history-sharelatex",
"version": "0.0.1",
"dependencies": {
"async": "",
"chai": "",
"express": "3.3.5",
"sandboxed-module": "",
"sinon": "",
"mongojs": "~0.9.11",
"settings": "git+ssh://git@bitbucket.org:sharelatex/settings-sharelatex.git#master",
"logger": "git+ssh://git@bitbucket.org:sharelatex/logger-sharelatex.git#bunyan",
"request": "~2.33.0"
}
}

View file

@ -38,6 +38,12 @@ namespace 'compile' do
end
puts 'finished app compile'
end
sh %{coffee -c app.coffee} do |ok, res|
if ! ok
raise "error compiling root app file: #{res}"
end
puts 'finished root app file compile'
end
end
desc "compiles unit tests"

View file

@ -0,0 +1,38 @@
sinon = require "sinon"
chai = require("chai")
chai.should()
mongojs = require "../../../app/js/mongojs"
db = mongojs.db
ObjectId = mongojs.ObjectId
Settings = require "settings-sharelatex"
request = require "request"
describe "Appending doc ops to the history", ->
describe "when the history does not exist yet", ->
before (done) ->
@doc_id = ObjectId().toString()
@user_id = ObjectId().toString()
@updates = [{
op: [{ i: "f", p: 3 }]
meta: { ts: Date.now(), user_id: @user_id }
}, {
op: [{ i: "o", p: 4 }]
meta: { ts: Date.now(), user_id: @user_id }
}, {
op: [{ i: "o", p: 5 }]
meta: { ts: Date.now(), user_id: @user_id }
}]
@version = 3
request.post {
url: "http://localhost:#{Settings.port}/doc/#{@doc_id}/history"
json:
version: @version
docOps: @updates
}, (@error, @response, @body) =>
done()
it "should return a successful response", ->
@response.statusCode.should.equal 204