2014-11-12 10:54:55 -05:00
|
|
|
sinon = require "sinon"
|
|
|
|
express = require "express"
|
|
|
|
|
|
|
|
module.exports = MockDocUpdaterServer =
|
|
|
|
docs: {}
|
|
|
|
|
|
|
|
createMockDoc: (project_id, doc_id, data) ->
|
|
|
|
MockDocUpdaterServer.docs["#{project_id}:#{doc_id}"] = data
|
|
|
|
|
|
|
|
getDocument: (project_id, doc_id, fromVersion, callback = (error, data) ->) ->
|
|
|
|
callback(
|
|
|
|
null, MockDocUpdaterServer.docs["#{project_id}:#{doc_id}"]
|
|
|
|
)
|
|
|
|
|
2014-11-17 07:23:30 -05:00
|
|
|
deleteProject: sinon.stub().callsArg(1)
|
|
|
|
|
2014-11-12 10:54:55 -05:00
|
|
|
getDocumentRequest: (req, res, next) ->
|
|
|
|
{project_id, doc_id} = req.params
|
|
|
|
{fromVersion} = req.query
|
|
|
|
fromVersion = parseInt(fromVersion, 10)
|
|
|
|
MockDocUpdaterServer.getDocument project_id, doc_id, fromVersion, (error, data) ->
|
|
|
|
return next(error) if error?
|
|
|
|
res.json data
|
2014-11-17 07:23:30 -05:00
|
|
|
|
|
|
|
deleteProjectRequest: (req, res, next) ->
|
|
|
|
{project_id} = req.params
|
|
|
|
MockDocUpdaterServer.deleteProject project_id, (error) ->
|
|
|
|
return next(error) if error?
|
|
|
|
res.sendStatus 204
|
2014-11-12 10:54:55 -05:00
|
|
|
|
|
|
|
running: false
|
|
|
|
run: (callback = (error) ->) ->
|
|
|
|
if MockDocUpdaterServer.running
|
|
|
|
return callback()
|
|
|
|
app = express()
|
|
|
|
app.get "/project/:project_id/doc/:doc_id", MockDocUpdaterServer.getDocumentRequest
|
2014-11-17 07:23:30 -05:00
|
|
|
app.delete "/project/:project_id", MockDocUpdaterServer.deleteProjectRequest
|
2014-11-12 10:54:55 -05:00
|
|
|
app.listen 3003, (error) ->
|
|
|
|
MockDocUpdaterServer.running = true
|
|
|
|
callback(error)
|
2017-10-20 10:19:20 -04:00
|
|
|
.on "error", (error) ->
|
|
|
|
console.error "error starting MockDocUpdaterServer:", error.message
|
|
|
|
process.exit(1)
|
|
|
|
|
2014-11-12 10:54:55 -05:00
|
|
|
|
2017-10-20 10:19:20 -04:00
|
|
|
sinon.spy MockDocUpdaterServer, "getDocument"
|