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 MockWebServer
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const express = require('express')
|
2024-02-12 05:37:06 -05:00
|
|
|
const bodyParser = require('body-parser')
|
2014-11-10 06:27:08 -05:00
|
|
|
|
2020-06-23 13:30:45 -04:00
|
|
|
module.exports = MockWebServer = {
|
|
|
|
projects: {},
|
|
|
|
privileges: {},
|
2023-05-30 08:21:55 -04:00
|
|
|
userMetadata: {},
|
2017-10-20 10:19:20 -04:00
|
|
|
|
2023-05-30 08:21:55 -04:00
|
|
|
createMockProject(projectId, privileges, project, metadataByUser) {
|
2023-03-20 10:10:40 -04:00
|
|
|
MockWebServer.privileges[projectId] = privileges
|
2023-05-30 08:21:55 -04:00
|
|
|
MockWebServer.userMetadata[projectId] = metadataByUser
|
2023-03-20 10:10:40 -04:00
|
|
|
return (MockWebServer.projects[projectId] = project)
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
|
|
|
|
2023-06-13 04:35:39 -04:00
|
|
|
inviteUserToProject(projectId, user, privileges) {
|
|
|
|
MockWebServer.privileges[projectId][user._id] = privileges
|
|
|
|
MockWebServer.userMetadata[projectId][user._id] = user
|
|
|
|
},
|
|
|
|
|
|
|
|
joinProject(projectId, userId, anonymousAccessToken, 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-05-30 08:21:55 -04:00
|
|
|
const project = MockWebServer.projects[projectId]
|
|
|
|
const privilegeLevel =
|
2023-11-29 09:50:21 -05:00
|
|
|
MockWebServer.privileges[projectId]?.[userId] ||
|
|
|
|
MockWebServer.privileges[projectId]?.[anonymousAccessToken]
|
2023-05-30 08:21:55 -04:00
|
|
|
const userMetadata = MockWebServer.userMetadata[projectId]?.[userId]
|
|
|
|
return callback(null, project, privilegeLevel, userMetadata)
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
joinProjectRequest(req, res, next) {
|
2023-03-20 10:10:40 -04:00
|
|
|
const { project_id: projectId } = req.params
|
2024-02-12 05:37:06 -05:00
|
|
|
const { anonymousAccessToken, userId } = req.body
|
2023-03-20 10:10:40 -04:00
|
|
|
if (projectId === '404404404404404404404404') {
|
2021-09-01 05:10:10 -04:00
|
|
|
// not-found
|
2020-08-27 06:39:25 -04:00
|
|
|
return res.status(404).send()
|
|
|
|
}
|
2023-03-20 10:10:40 -04:00
|
|
|
if (projectId === '403403403403403403403403') {
|
2021-09-01 05:10:10 -04:00
|
|
|
// forbidden
|
2020-08-27 05:51:34 -04:00
|
|
|
return res.status(403).send()
|
|
|
|
}
|
2023-03-20 10:10:40 -04:00
|
|
|
if (projectId === '429429429429429429429429') {
|
2021-09-01 05:10:10 -04:00
|
|
|
// rate-limited
|
2020-06-23 13:30:45 -04:00
|
|
|
return res.status(429).send()
|
|
|
|
} else {
|
|
|
|
return MockWebServer.joinProject(
|
2023-03-20 10:10:40 -04:00
|
|
|
projectId,
|
|
|
|
userId,
|
2023-06-13 04:35:39 -04:00
|
|
|
anonymousAccessToken,
|
2023-05-30 08:21:55 -04:00
|
|
|
(error, project, privilegeLevel, userMetadata) => {
|
2020-06-23 13:30:45 -04:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2023-11-29 09:50:21 -05:00
|
|
|
if (!project) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
2020-06-23 13:30:45 -04:00
|
|
|
return res.json({
|
|
|
|
project,
|
2021-07-13 07:04:45 -04:00
|
|
|
privilegeLevel,
|
2023-05-30 08:21:55 -04:00
|
|
|
isRestrictedUser: !!userMetadata?.isRestrictedUser,
|
|
|
|
isTokenMember: !!userMetadata?.isTokenMember,
|
|
|
|
isInvitedMember: !!userMetadata?.isInvitedMember,
|
2020-06-23 13:30:45 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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 (MockWebServer.running) {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
const app = express()
|
2024-02-12 05:37:06 -05:00
|
|
|
app.use(bodyParser.json())
|
2020-06-23 13:30:45 -04:00
|
|
|
app.post('/project/:project_id/join', MockWebServer.joinProjectRequest)
|
|
|
|
return app
|
2021-07-13 07:04:45 -04:00
|
|
|
.listen(3000, error => {
|
2020-06-23 13:30:45 -04:00
|
|
|
MockWebServer.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 MockWebServer:', error.message)
|
|
|
|
return process.exit(1)
|
|
|
|
})
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sinon.spy(MockWebServer, 'joinProject')
|