2021-02-25 07:22:24 -05:00
|
|
|
const AbstractMockApi = require('./AbstractMockApi')
|
2019-05-29 05:21:06 -04:00
|
|
|
const _ = require('lodash')
|
2020-09-23 04:49:26 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2022-01-17 05:19:53 -05:00
|
|
|
const {
|
|
|
|
plainTextResponse,
|
|
|
|
} = require('../../../../app/src/infrastructure/Response')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-02-25 07:22:24 -05:00
|
|
|
class MockProjectHistoryApi extends AbstractMockApi {
|
|
|
|
reset() {
|
|
|
|
this.docs = {}
|
|
|
|
this.oldFiles = {}
|
|
|
|
this.projectVersions = {}
|
|
|
|
this.labels = {}
|
|
|
|
this.projectSnapshots = {}
|
|
|
|
this.projectHistoryId = 1
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-03-04 04:38:40 -05:00
|
|
|
addOldFile(projectId, version, pathname, content) {
|
|
|
|
this.oldFiles[`${projectId}:${version}:${pathname}`] = content
|
2021-02-25 07:22:24 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-03-04 04:38:40 -05:00
|
|
|
addProjectSnapshot(projectId, version, snapshot) {
|
|
|
|
this.projectSnapshots[`${projectId}:${version}`] = snapshot
|
2021-02-25 07:22:24 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-03-04 04:38:40 -05:00
|
|
|
setProjectVersion(projectId, version) {
|
|
|
|
this.projectVersions[projectId] = { version }
|
2021-02-25 07:22:24 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-03-04 04:38:40 -05:00
|
|
|
setProjectVersionInfo(projectId, versionInfo) {
|
|
|
|
this.projectVersions[projectId] = versionInfo
|
2021-02-25 07:22:24 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-03-04 04:38:40 -05:00
|
|
|
addLabel(projectId, label) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (label.id == null) {
|
|
|
|
label.id = new ObjectId().toString()
|
|
|
|
}
|
2020-03-04 04:38:40 -05:00
|
|
|
if (this.labels[projectId] == null) {
|
|
|
|
this.labels[projectId] = {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-04 04:38:40 -05:00
|
|
|
this.labels[projectId][label.id] = label
|
2021-02-25 07:22:24 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-03-04 04:38:40 -05:00
|
|
|
deleteLabel(projectId, labelId) {
|
|
|
|
delete this.labels[projectId][labelId]
|
2021-02-25 07:22:24 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-03-04 04:38:40 -05:00
|
|
|
getLabels(projectId) {
|
|
|
|
if (this.labels[projectId] == null) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return null
|
|
|
|
}
|
2020-03-04 04:38:40 -05:00
|
|
|
return _.values(this.labels[projectId])
|
2021-02-25 07:22:24 -05:00
|
|
|
}
|
2019-10-01 07:30:10 -04:00
|
|
|
|
2021-02-25 07:22:24 -05:00
|
|
|
applyRoutes() {
|
|
|
|
this.app.post('/project', (req, res) => {
|
2020-03-04 04:38:40 -05:00
|
|
|
res.json({ project: { id: this.projectHistoryId++ } })
|
|
|
|
})
|
|
|
|
|
2021-02-25 07:22:24 -05:00
|
|
|
this.app.delete('/project/:projectId', (req, res) => {
|
2020-03-04 04:38:40 -05:00
|
|
|
res.sendStatus(204)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-02-25 07:22:24 -05:00
|
|
|
this.app.get(
|
2020-03-04 04:38:40 -05:00
|
|
|
'/project/:projectId/version/:version/:pathname',
|
2021-02-25 07:22:24 -05:00
|
|
|
(req, res) => {
|
2020-03-04 04:38:40 -05:00
|
|
|
const { projectId, version, pathname } = req.params
|
|
|
|
const key = `${projectId}:${version}:${pathname}`
|
2019-05-29 05:21:06 -04:00
|
|
|
if (this.oldFiles[key] != null) {
|
2022-01-17 05:19:53 -05:00
|
|
|
plainTextResponse(res, this.oldFiles[key])
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
2020-05-06 06:02:16 -04:00
|
|
|
res.sendStatus(404)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-02-25 07:22:24 -05:00
|
|
|
this.app.get('/project/:projectId/version/:version', (req, res) => {
|
2020-03-04 04:38:40 -05:00
|
|
|
const { projectId, version } = req.params
|
|
|
|
const key = `${projectId}:${version}`
|
2019-05-29 05:21:06 -04:00
|
|
|
if (this.projectSnapshots[key] != null) {
|
2020-03-04 04:38:40 -05:00
|
|
|
res.json(this.projectSnapshots[key])
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
2020-03-04 04:38:40 -05:00
|
|
|
res.sendStatus(404)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-02-25 07:22:24 -05:00
|
|
|
this.app.get('/project/:projectId/version', (req, res) => {
|
2020-03-04 04:38:40 -05:00
|
|
|
const { projectId } = req.params
|
|
|
|
if (this.projectVersions[projectId] != null) {
|
|
|
|
res.json(this.projectVersions[projectId])
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
2020-05-06 06:02:16 -04:00
|
|
|
res.sendStatus(404)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-02-25 07:22:24 -05:00
|
|
|
this.app.get('/project/:projectId/labels', (req, res) => {
|
2020-03-04 04:38:40 -05:00
|
|
|
const { projectId } = req.params
|
|
|
|
const labels = this.getLabels(projectId)
|
2019-05-29 05:21:06 -04:00
|
|
|
if (labels != null) {
|
2020-03-04 04:38:40 -05:00
|
|
|
res.json(labels)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
2020-05-06 06:02:16 -04:00
|
|
|
res.sendStatus(404)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-02-25 07:22:24 -05:00
|
|
|
this.app.post('/project/:projectId/user/:user_id/labels', (req, res) => {
|
|
|
|
const { projectId } = req.params
|
|
|
|
const { comment, version } = req.body
|
|
|
|
const labelId = new ObjectId().toString()
|
|
|
|
this.addLabel(projectId, { id: labelId, comment, version })
|
|
|
|
res.json({ label_id: labelId, comment, version })
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-02-25 07:22:24 -05:00
|
|
|
this.app.delete(
|
2020-03-04 04:38:40 -05:00
|
|
|
'/project/:projectId/user/:user_id/labels/:labelId',
|
2021-02-25 07:22:24 -05:00
|
|
|
(req, res) => {
|
2020-03-04 04:38:40 -05:00
|
|
|
const { projectId, labelId } = req.params
|
2019-05-29 05:21:06 -04:00
|
|
|
const label =
|
2020-03-04 04:38:40 -05:00
|
|
|
this.labels[projectId] != null
|
|
|
|
? this.labels[projectId][labelId]
|
2019-05-29 05:21:06 -04:00
|
|
|
: undefined
|
|
|
|
if (label != null) {
|
2020-03-04 04:38:40 -05:00
|
|
|
this.deleteLabel(projectId, labelId)
|
2020-05-06 06:02:16 -04:00
|
|
|
res.sendStatus(204)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
2020-05-06 06:02:16 -04:00
|
|
|
res.sendStatus(404)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-02-25 07:22:24 -05:00
|
|
|
this.app.post('/project/:projectId/flush', (req, res) => {
|
2020-03-04 04:38:40 -05:00
|
|
|
res.sendStatus(200)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 07:22:24 -05:00
|
|
|
module.exports = MockProjectHistoryApi
|
|
|
|
|
|
|
|
// type hint for the inherited `instance` method
|
|
|
|
/**
|
|
|
|
* @function instance
|
|
|
|
* @memberOf MockProjectHistoryApi
|
|
|
|
* @static
|
|
|
|
* @returns {MockProjectHistoryApi}
|
|
|
|
*/
|