add acceptance tests for project history API

This commit is contained in:
Hayden Faulds 2017-10-05 15:14:35 +01:00
parent 04ecd1e7ee
commit d1f0c4ceae
7 changed files with 73 additions and 26 deletions

View file

@ -41,7 +41,7 @@ pipeline {
stage('Acceptance Tests') { stage('Acceptance Tests') {
steps { steps {
sh 'docker pull sharelatex/acceptance-test-runner' sh 'docker pull sharelatex/acceptance-test-runner'
sh 'docker run --rm -v $(pwd):/app sharelatex/acceptance-test-runner' sh 'docker run --rm -e SHARELATEX_ENABLE_PROJECT_HISTORY=true -v $(pwd):/app sharelatex/acceptance-test-runner'
} }
} }
stage('Package') { stage('Package') {

View file

@ -15,8 +15,8 @@ module.exports =
trackchanges: trackchanges:
url: "http://localhost:3015" url: "http://localhost:3015"
project_history: project_history:
enabled: process.env.SHARELATEX_ENABLE_PROJECT_HISTORY == 'true'
url: "http://localhost:3054" url: "http://localhost:3054"
enabled: true
redis: redis:
realtime: realtime:

View file

@ -8,6 +8,7 @@ rclient_history = require("redis-sharelatex").createClient(Settings.redis.histor
rclient_du = require("redis-sharelatex").createClient(Settings.redis.documentupdater) rclient_du = require("redis-sharelatex").createClient(Settings.redis.documentupdater)
Keys = Settings.redis.documentupdater.key_schema Keys = Settings.redis.documentupdater.key_schema
HistoryKeys = Settings.redis.history.key_schema HistoryKeys = Settings.redis.history.key_schema
ProjectHistoryKeys = Settings.redis.project_history.key_schema
MockTrackChangesApi = require "./helpers/MockTrackChangesApi" MockTrackChangesApi = require "./helpers/MockTrackChangesApi"
MockWebApi = require "./helpers/MockWebApi" MockWebApi = require "./helpers/MockWebApi"
@ -58,6 +59,11 @@ describe "Applying updates to a doc", ->
result.should.equal 1 result.should.equal 1
done() done()
it "should push the applied updates to the project history changes api", (done) ->
rclient_history.lrange ProjectHistoryKeys.projectHistoryOps({@project_id}), 0, -1, (error, updates) =>
throw error if error?
JSON.parse(updates[0]).op.should.deep.equal @update.op
done()
describe "when the document is loaded", -> describe "when the document is loaded", ->
before (done) -> before (done) ->
@ -89,6 +95,12 @@ describe "Applying updates to a doc", ->
result.should.equal 1 result.should.equal 1
done() done()
it "should push the applied updates to the project history changes api", (done) ->
rclient_history.lrange ProjectHistoryKeys.projectHistoryOps({@project_id}), 0, -1, (error, updates) =>
JSON.parse(updates[0]).op.should.deep.equal @update.op
done()
describe "when the document has been deleted", -> describe "when the document has been deleted", ->
describe "when the ops come in a single linear order", -> describe "when the ops come in a single linear order", ->
before (done) -> before (done) ->

View file

@ -3,6 +3,7 @@ chai = require("chai")
chai.should() chai.should()
MockTrackChangesApi = require "./helpers/MockTrackChangesApi" MockTrackChangesApi = require "./helpers/MockTrackChangesApi"
MockProjectHistoryApi = require "./helpers/MockProjectHistoryApi"
MockWebApi = require "./helpers/MockWebApi" MockWebApi = require "./helpers/MockWebApi"
DocUpdaterClient = require "./helpers/DocUpdaterClient" DocUpdaterClient = require "./helpers/DocUpdaterClient"
@ -18,11 +19,13 @@ describe "Deleting a document", ->
}] }]
v: @version v: @version
@result = ["one", "one and a half", "two", "three"] @result = ["one", "one and a half", "two", "three"]
sinon.spy MockTrackChangesApi, "flushDoc" sinon.spy MockTrackChangesApi, "flushDoc"
sinon.spy MockProjectHistoryApi, "flushProject"
after -> after ->
MockTrackChangesApi.flushDoc.restore() MockTrackChangesApi.flushDoc.restore()
MockProjectHistoryApi.flushProject.restore()
describe "when the updated doc exists in the doc updater", -> describe "when the updated doc exists in the doc updater", ->
before (done) -> before (done) ->
@ -60,10 +63,13 @@ describe "Deleting a document", ->
.calledWith(@project_id, @doc_id) .calledWith(@project_id, @doc_id)
.should.equal true .should.equal true
done() done()
it "should flush track changes", -> it "should flush track changes", ->
MockTrackChangesApi.flushDoc.calledWith(@doc_id).should.equal true MockTrackChangesApi.flushDoc.calledWith(@doc_id).should.equal true
it "should flush project history", ->
MockProjectHistoryApi.flushProject.calledWith(@project_id).should.equal true
describe "when the doc is not in the doc updater", -> describe "when the doc is not in the doc updater", ->
before (done) -> before (done) ->
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()] [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
@ -93,9 +99,9 @@ describe "Deleting a document", ->
.calledWith(@project_id, @doc_id) .calledWith(@project_id, @doc_id)
.should.equal true .should.equal true
done() done()
it "should flush track changes", -> it "should flush track changes", ->
MockTrackChangesApi.flushDoc.calledWith(@doc_id).should.equal true MockTrackChangesApi.flushDoc.calledWith(@doc_id).should.equal true
it "should flush project history", ->
MockProjectHistoryApi.flushProject.calledWith(@project_id).should.equal true

View file

@ -4,6 +4,7 @@ chai.should()
async = require "async" async = require "async"
MockTrackChangesApi = require "./helpers/MockTrackChangesApi" MockTrackChangesApi = require "./helpers/MockTrackChangesApi"
MockProjectHistoryApi = require "./helpers/MockProjectHistoryApi"
MockWebApi = require "./helpers/MockWebApi" MockWebApi = require "./helpers/MockWebApi"
DocUpdaterClient = require "./helpers/DocUpdaterClient" DocUpdaterClient = require "./helpers/DocUpdaterClient"
@ -38,11 +39,13 @@ describe "Deleting a project", ->
lines: doc.lines lines: doc.lines
version: doc.update.v version: doc.update.v
} }
sinon.spy MockTrackChangesApi, "flushDoc" sinon.spy MockTrackChangesApi, "flushDoc"
sinon.spy MockProjectHistoryApi, "flushProject"
after -> after ->
MockTrackChangesApi.flushDoc.restore() MockTrackChangesApi.flushDoc.restore()
MockProjectHistoryApi.flushProject.restore()
describe "with documents which have been updated", -> describe "with documents which have been updated", ->
before (done) -> before (done) ->
@ -84,9 +87,10 @@ describe "Deleting a project", ->
), () -> ), () ->
MockWebApi.getDocument.restore() MockWebApi.getDocument.restore()
done() done()
it "should flush each doc in track changes", -> it "should flush each doc in track changes", ->
for doc in @docs for doc in @docs
MockTrackChangesApi.flushDoc.calledWith(doc.id).should.equal true MockTrackChangesApi.flushDoc.calledWith(doc.id).should.equal true
it "should flush each doc in project history", ->
MockProjectHistoryApi.flushProject.calledWith(@project_id).should.equal true

View file

@ -7,6 +7,7 @@ rclient_du = require("redis-sharelatex").createClient(Settings.redis.documentupd
Keys = Settings.redis.documentupdater.key_schema Keys = Settings.redis.documentupdater.key_schema
MockTrackChangesApi = require "./helpers/MockTrackChangesApi" MockTrackChangesApi = require "./helpers/MockTrackChangesApi"
MockProjectHistoryApi = require "./helpers/MockProjectHistoryApi"
MockWebApi = require "./helpers/MockWebApi" MockWebApi = require "./helpers/MockWebApi"
DocUpdaterClient = require "./helpers/DocUpdaterClient" DocUpdaterClient = require "./helpers/DocUpdaterClient"
@ -25,14 +26,16 @@ describe "Setting a document", ->
@newLines = ["these", "are", "the", "new", "lines"] @newLines = ["these", "are", "the", "new", "lines"]
@source = "dropbox" @source = "dropbox"
@user_id = "user-id-123" @user_id = "user-id-123"
sinon.spy MockTrackChangesApi, "flushDoc" sinon.spy MockTrackChangesApi, "flushDoc"
sinon.spy MockProjectHistoryApi, "flushProject"
sinon.spy MockWebApi, "setDocument" sinon.spy MockWebApi, "setDocument"
after -> after ->
MockWebApi.setDocument.restore()
MockTrackChangesApi.flushDoc.restore() MockTrackChangesApi.flushDoc.restore()
MockProjectHistoryApi.flushProject.restore()
MockWebApi.setDocument.restore()
describe "when the updated doc exists in the doc updater", -> describe "when the updated doc exists in the doc updater", ->
before (done) -> before (done) ->
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()] [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
@ -64,13 +67,13 @@ describe "Setting a document", ->
DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) => DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
doc.version.should.equal @version + 2 doc.version.should.equal @version + 2
done() done()
it "should leave the document in redis", (done) -> it "should leave the document in redis", (done) ->
rclient_du.get Keys.docLines({doc_id: @doc_id}), (error, lines) => rclient_du.get Keys.docLines({doc_id: @doc_id}), (error, lines) =>
throw error if error? throw error if error?
expect(JSON.parse(lines)).to.deep.equal @newLines expect(JSON.parse(lines)).to.deep.equal @newLines
done() done()
describe "when the updated doc does not exist in the doc updater", -> describe "when the updated doc does not exist in the doc updater", ->
before (done) -> before (done) ->
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()] [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
@ -78,7 +81,7 @@ describe "Setting a document", ->
DocUpdaterClient.setDocLines @project_id, @doc_id, @newLines, @source, @user_id, false, (error, res, body) => DocUpdaterClient.setDocLines @project_id, @doc_id, @newLines, @source, @user_id, false, (error, res, body) =>
@statusCode = res.statusCode @statusCode = res.statusCode
setTimeout done, 200 setTimeout done, 200
it "should return a 204 status code", -> it "should return a 204 status code", ->
@statusCode.should.equal 204 @statusCode.should.equal 204
@ -86,16 +89,19 @@ describe "Setting a document", ->
MockWebApi.setDocument MockWebApi.setDocument
.calledWith(@project_id, @doc_id, @newLines) .calledWith(@project_id, @doc_id, @newLines)
.should.equal true .should.equal true
it "should flush track changes", -> it "should flush track changes", ->
MockTrackChangesApi.flushDoc.calledWith(@doc_id).should.equal true MockTrackChangesApi.flushDoc.calledWith(@doc_id).should.equal true
it "should flush project history", ->
MockProjectHistoryApi.flushProject.calledWith(@project_id).should.equal true
it "should remove the document from redis", (done) -> it "should remove the document from redis", (done) ->
rclient_du.get Keys.docLines({doc_id: @doc_id}), (error, lines) => rclient_du.get Keys.docLines({doc_id: @doc_id}), (error, lines) =>
throw error if error? throw error if error?
expect(lines).to.not.exist expect(lines).to.not.exist
done() done()
describe "with track changes", -> describe "with track changes", ->
before -> before ->
@lines = ["one", "one and a half", "two", "three"] @lines = ["one", "one and a half", "two", "three"]
@ -123,14 +129,14 @@ describe "Setting a document", ->
DocUpdaterClient.setDocLines @project_id, @doc_id, @lines, @source, @user_id, true, (error, res, body) => DocUpdaterClient.setDocLines @project_id, @doc_id, @lines, @source, @user_id, true, (error, res, body) =>
@statusCode = res.statusCode @statusCode = res.statusCode
setTimeout done, 200 setTimeout done, 200
it "should undo the tracked changes", (done) -> it "should undo the tracked changes", (done) ->
DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, data) => DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, data) =>
throw error if error? throw error if error?
ranges = data.ranges ranges = data.ranges
expect(ranges.changes).to.be.undefined expect(ranges.changes).to.be.undefined
done() done()
describe "without the undo flag", -> describe "without the undo flag", ->
before (done) -> before (done) ->
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()] [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
@ -143,7 +149,7 @@ describe "Setting a document", ->
DocUpdaterClient.setDocLines @project_id, @doc_id, @lines, @source, @user_id, false, (error, res, body) => DocUpdaterClient.setDocLines @project_id, @doc_id, @lines, @source, @user_id, false, (error, res, body) =>
@statusCode = res.statusCode @statusCode = res.statusCode
setTimeout done, 200 setTimeout done, 200
it "should not undo the tracked changes", (done) -> it "should not undo the tracked changes", (done) ->
DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, data) => DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, data) =>
throw error if error? throw error if error?
@ -151,4 +157,4 @@ describe "Setting a document", ->
expect(ranges.changes.length).to.equal 1 expect(ranges.changes.length).to.equal 1
done() done()

View file

@ -0,0 +1,19 @@
express = require("express")
app = express()
module.exports = MockProjectHistoryApi =
flushProject: (doc_id, callback = (error) ->) ->
callback()
run: () ->
app.post "/project/:project_id/flush", (req, res, next) =>
@flushProject req.params.project_id, (error) ->
if error?
res.send 500
else
res.send 204
app.listen 3054, (error) ->
throw error if error?
MockProjectHistoryApi.run()