2020-06-23 13:30:34 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
handle-callback-err,
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
let MockWebServer;
|
|
|
|
const sinon = require("sinon");
|
|
|
|
const express = require("express");
|
2014-11-10 06:27:08 -05:00
|
|
|
|
2020-06-23 13:30:29 -04:00
|
|
|
module.exports = (MockWebServer = {
|
|
|
|
projects: {},
|
|
|
|
privileges: {},
|
2014-11-10 06:27:08 -05:00
|
|
|
|
2020-06-23 13:30:29 -04:00
|
|
|
createMockProject(project_id, privileges, project) {
|
|
|
|
MockWebServer.privileges[project_id] = privileges;
|
|
|
|
return MockWebServer.projects[project_id] = project;
|
|
|
|
},
|
2014-11-10 06:27:08 -05:00
|
|
|
|
2020-06-23 13:30:29 -04:00
|
|
|
joinProject(project_id, user_id, callback) {
|
|
|
|
if (callback == null) { callback = function(error, project, privilegeLevel) {}; }
|
|
|
|
return callback(
|
2014-11-10 06:27:08 -05:00
|
|
|
null,
|
2014-11-12 10:54:55 -05:00
|
|
|
MockWebServer.projects[project_id],
|
|
|
|
MockWebServer.privileges[project_id][user_id]
|
2020-06-23 13:30:29 -04:00
|
|
|
);
|
|
|
|
},
|
2014-11-10 06:27:08 -05:00
|
|
|
|
2020-06-23 13:30:29 -04:00
|
|
|
joinProjectRequest(req, res, next) {
|
|
|
|
const {project_id} = req.params;
|
|
|
|
const {user_id} = req.query;
|
|
|
|
if (project_id === 'rate-limited') {
|
|
|
|
return res.status(429).send();
|
|
|
|
} else {
|
2020-06-23 13:30:34 -04:00
|
|
|
return MockWebServer.joinProject(project_id, user_id, (error, project, privilegeLevel) => {
|
2020-06-23 13:30:29 -04:00
|
|
|
if (error != null) { return next(error); }
|
|
|
|
return res.json({
|
|
|
|
project,
|
|
|
|
privilegeLevel
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2014-11-10 06:27:08 -05:00
|
|
|
|
2020-06-23 13:30:29 -04:00
|
|
|
running: false,
|
|
|
|
run(callback) {
|
|
|
|
if (callback == null) { callback = function(error) {}; }
|
|
|
|
if (MockWebServer.running) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
const app = express();
|
|
|
|
app.post("/project/:project_id/join", MockWebServer.joinProjectRequest);
|
2020-06-23 13:30:34 -04:00
|
|
|
return app.listen(3000, (error) => {
|
2020-06-23 13:30:29 -04:00
|
|
|
MockWebServer.running = true;
|
|
|
|
return callback(error);
|
2020-06-23 13:30:34 -04:00
|
|
|
}).on("error", (error) => {
|
2020-06-23 13:30:29 -04:00
|
|
|
console.error("error starting MockWebServer:", error.message);
|
|
|
|
return process.exit(1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-10-20 10:19:20 -04:00
|
|
|
|
2014-11-10 06:27:08 -05:00
|
|
|
|
2020-06-23 13:30:29 -04:00
|
|
|
sinon.spy(MockWebServer, "joinProject");
|