overleaf/services/real-time/test/acceptance/coffee/helpers/MockWebServer.coffee

44 lines
1.2 KiB
CoffeeScript
Raw Normal View History

2014-11-10 06:27:08 -05:00
sinon = require "sinon"
express = require "express"
2014-11-12 10:54:55 -05:00
module.exports = MockWebServer =
2014-11-10 06:27:08 -05:00
projects: {}
privileges: {}
createMockProject: (project_id, privileges, project) ->
2014-11-12 10:54:55 -05:00
MockWebServer.privileges[project_id] = privileges
MockWebServer.projects[project_id] = project
2014-11-10 06:27:08 -05:00
joinProject: (project_id, user_id, callback = (error, project, privilegeLevel) ->) ->
callback(
null,
2014-11-12 10:54:55 -05:00
MockWebServer.projects[project_id],
MockWebServer.privileges[project_id][user_id]
2014-11-10 06:27:08 -05:00
)
joinProjectRequest: (req, res, next) ->
{project_id} = req.params
{user_id} = req.query
2014-11-12 10:54:55 -05:00
MockWebServer.joinProject project_id, user_id, (error, project, privilegeLevel) ->
2014-11-10 06:27:08 -05:00
return next(error) if error?
res.json {
project: project
privilegeLevel: privilegeLevel
}
running: false
run: (callback = (error) ->) ->
2014-11-12 10:54:55 -05:00
if MockWebServer.running
2014-11-10 06:27:08 -05:00
return callback()
app = express()
2014-11-12 10:54:55 -05:00
app.post "/project/:project_id/join", MockWebServer.joinProjectRequest
2014-11-10 06:27:08 -05:00
app.listen 3000, (error) ->
2014-11-12 10:54:55 -05:00
MockWebServer.running = true
2014-11-10 06:27:08 -05:00
callback(error)
2017-10-20 10:19:20 -04:00
.on "error", (error) ->
console.error "error starting MockWebServer:", error.message
process.exit(1)
2014-11-10 06:27:08 -05:00
2017-10-20 10:19:20 -04:00
sinon.spy MockWebServer, "joinProject"