2020-06-23 13:30:34 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-06-23 13:30:29 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-06-23 13:30:45 -04:00
|
|
|
let MockDocUpdaterServer
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const express = require('express')
|
2014-11-12 10:54:55 -05:00
|
|
|
|
2020-06-23 13:30:45 -04:00
|
|
|
module.exports = MockDocUpdaterServer = {
|
|
|
|
docs: {},
|
2017-10-20 10:19:20 -04:00
|
|
|
|
2023-03-20 10:10:40 -04:00
|
|
|
createMockDoc(projectId, docId, data) {
|
|
|
|
return (MockDocUpdaterServer.docs[`${projectId}:${docId}`] = data)
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
|
|
|
|
2023-03-20 10:10:40 -04:00
|
|
|
getDocument(projectId, docId, fromVersion, callback) {
|
2020-06-23 13:30:45 -04:00
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-06-23 13:30:45 -04:00
|
|
|
}
|
2023-03-20 10:10:40 -04:00
|
|
|
return callback(null, MockDocUpdaterServer.docs[`${projectId}:${docId}`])
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
deleteProject: sinon.stub().callsArg(1),
|
|
|
|
|
|
|
|
getDocumentRequest(req, res, next) {
|
2023-03-20 10:10:40 -04:00
|
|
|
const { project_id: projectId, doc_id: docId } = req.params
|
2020-06-23 13:30:45 -04:00
|
|
|
let { fromVersion } = req.query
|
|
|
|
fromVersion = parseInt(fromVersion, 10)
|
|
|
|
return MockDocUpdaterServer.getDocument(
|
2023-03-20 10:10:40 -04:00
|
|
|
projectId,
|
|
|
|
docId,
|
2020-06-23 13:30:45 -04:00
|
|
|
fromVersion,
|
|
|
|
(error, data) => {
|
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2020-10-01 07:25:41 -04:00
|
|
|
if (!data) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
2020-06-23 13:30:45 -04:00
|
|
|
return res.json(data)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteProjectRequest(req, res, next) {
|
2023-03-20 10:10:40 -04:00
|
|
|
const { project_id: projectId } = req.params
|
|
|
|
return MockDocUpdaterServer.deleteProject(projectId, error => {
|
2020-06-23 13:30:45 -04:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
return res.sendStatus(204)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
running: false,
|
|
|
|
run(callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-06-23 13:30:45 -04:00
|
|
|
}
|
|
|
|
if (MockDocUpdaterServer.running) {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
const app = express()
|
|
|
|
app.get(
|
|
|
|
'/project/:project_id/doc/:doc_id',
|
|
|
|
MockDocUpdaterServer.getDocumentRequest
|
|
|
|
)
|
|
|
|
app.delete(
|
|
|
|
'/project/:project_id',
|
|
|
|
MockDocUpdaterServer.deleteProjectRequest
|
|
|
|
)
|
|
|
|
return app
|
2021-07-13 07:04:45 -04:00
|
|
|
.listen(3003, error => {
|
2020-06-23 13:30:45 -04:00
|
|
|
MockDocUpdaterServer.running = true
|
|
|
|
return callback(error)
|
|
|
|
})
|
2021-07-13 07:04:45 -04:00
|
|
|
.on('error', error => {
|
2020-06-23 13:30:45 -04:00
|
|
|
console.error('error starting MockDocUpdaterServer:', error.message)
|
|
|
|
return process.exit(1)
|
|
|
|
})
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sinon.spy(MockDocUpdaterServer, 'getDocument')
|