overleaf/services/real-time/test/acceptance/coffee/helpers/MockDocUpdaterServer.js

66 lines
2 KiB
JavaScript
Raw Normal View History

/*
* 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
*/
let MockDocUpdaterServer;
const sinon = require("sinon");
const express = require("express");
2014-11-12 10:54:55 -05:00
module.exports = (MockDocUpdaterServer = {
docs: {},
2014-11-12 10:54:55 -05:00
createMockDoc(project_id, doc_id, data) {
return MockDocUpdaterServer.docs[`${project_id}:${doc_id}`] = data;
},
2014-11-12 10:54:55 -05:00
getDocument(project_id, doc_id, fromVersion, callback) {
if (callback == null) { callback = function(error, data) {}; }
return callback(
null, MockDocUpdaterServer.docs[`${project_id}:${doc_id}`]
);
},
2014-11-12 10:54:55 -05:00
deleteProject: sinon.stub().callsArg(1),
getDocumentRequest(req, res, next) {
const {project_id, doc_id} = req.params;
let {fromVersion} = req.query;
fromVersion = parseInt(fromVersion, 10);
return MockDocUpdaterServer.getDocument(project_id, doc_id, fromVersion, function(error, data) {
if (error != null) { return next(error); }
return res.json(data);
});
},
deleteProjectRequest(req, res, next) {
const {project_id} = req.params;
return MockDocUpdaterServer.deleteProject(project_id, function(error) {
if (error != null) { return next(error); }
return res.sendStatus(204);
});
},
2014-11-12 10:54:55 -05:00
running: false,
run(callback) {
if (callback == null) { callback = function(error) {}; }
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.listen(3003, function(error) {
MockDocUpdaterServer.running = true;
return callback(error);
}).on("error", function(error) {
console.error("error starting MockDocUpdaterServer:", error.message);
return process.exit(1);
});
}
});
2017-10-20 10:19:20 -04:00
2014-11-12 10:54:55 -05:00
sinon.spy(MockDocUpdaterServer, "getDocument");