MockProjectHistoryApi.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. const AbstractMockApi = require('./AbstractMockApi')
  2. const _ = require('lodash')
  3. const { ObjectId } = require('mongodb')
  4. const {
  5. plainTextResponse,
  6. } = require('../../../../app/src/infrastructure/Response')
  7. class MockProjectHistoryApi extends AbstractMockApi {
  8. reset() {
  9. this.docs = {}
  10. this.oldFiles = {}
  11. this.projectVersions = {}
  12. this.labels = {}
  13. this.projectSnapshots = {}
  14. this.projectHistoryId = 1
  15. }
  16. addOldFile(projectId, version, pathname, content) {
  17. this.oldFiles[`${projectId}:${version}:${pathname}`] = content
  18. }
  19. addProjectSnapshot(projectId, version, snapshot) {
  20. this.projectSnapshots[`${projectId}:${version}`] = snapshot
  21. }
  22. setProjectVersion(projectId, version) {
  23. this.projectVersions[projectId] = { version }
  24. }
  25. setProjectVersionInfo(projectId, versionInfo) {
  26. this.projectVersions[projectId] = versionInfo
  27. }
  28. addLabel(projectId, label) {
  29. if (label.id == null) {
  30. label.id = new ObjectId().toString()
  31. }
  32. if (this.labels[projectId] == null) {
  33. this.labels[projectId] = {}
  34. }
  35. this.labels[projectId][label.id] = label
  36. }
  37. deleteLabel(projectId, labelId) {
  38. delete this.labels[projectId][labelId]
  39. }
  40. getLabels(projectId) {
  41. if (this.labels[projectId] == null) {
  42. return null
  43. }
  44. return _.values(this.labels[projectId])
  45. }
  46. applyRoutes() {
  47. this.app.post('/project', (req, res) => {
  48. res.json({ project: { id: this.projectHistoryId++ } })
  49. })
  50. this.app.delete('/project/:projectId', (req, res) => {
  51. res.sendStatus(204)
  52. })
  53. this.app.get(
  54. '/project/:projectId/version/:version/:pathname',
  55. (req, res) => {
  56. const { projectId, version, pathname } = req.params
  57. const key = `${projectId}:${version}:${pathname}`
  58. if (this.oldFiles[key] != null) {
  59. plainTextResponse(res, this.oldFiles[key])
  60. } else {
  61. res.sendStatus(404)
  62. }
  63. }
  64. )
  65. this.app.get('/project/:projectId/version/:version', (req, res) => {
  66. const { projectId, version } = req.params
  67. const key = `${projectId}:${version}`
  68. if (this.projectSnapshots[key] != null) {
  69. res.json(this.projectSnapshots[key])
  70. } else {
  71. res.sendStatus(404)
  72. }
  73. })
  74. this.app.get('/project/:projectId/version', (req, res) => {
  75. const { projectId } = req.params
  76. if (this.projectVersions[projectId] != null) {
  77. res.json(this.projectVersions[projectId])
  78. } else {
  79. res.sendStatus(404)
  80. }
  81. })
  82. this.app.get('/project/:projectId/labels', (req, res) => {
  83. const { projectId } = req.params
  84. const labels = this.getLabels(projectId)
  85. if (labels != null) {
  86. res.json(labels)
  87. } else {
  88. res.sendStatus(404)
  89. }
  90. })
  91. this.app.post('/project/:projectId/user/:user_id/labels', (req, res) => {
  92. const { projectId } = req.params
  93. const { comment, version } = req.body
  94. const labelId = new ObjectId().toString()
  95. this.addLabel(projectId, { id: labelId, comment, version })
  96. res.json({ label_id: labelId, comment, version })
  97. })
  98. this.app.delete(
  99. '/project/:projectId/user/:user_id/labels/:labelId',
  100. (req, res) => {
  101. const { projectId, labelId } = req.params
  102. const label =
  103. this.labels[projectId] != null
  104. ? this.labels[projectId][labelId]
  105. : undefined
  106. if (label != null) {
  107. this.deleteLabel(projectId, labelId)
  108. res.sendStatus(204)
  109. } else {
  110. res.sendStatus(404)
  111. }
  112. }
  113. )
  114. this.app.post('/project/:projectId/flush', (req, res) => {
  115. res.sendStatus(200)
  116. })
  117. }
  118. }
  119. module.exports = MockProjectHistoryApi
  120. // type hint for the inherited `instance` method
  121. /**
  122. * @function instance
  123. * @memberOf MockProjectHistoryApi
  124. * @static
  125. * @returns {MockProjectHistoryApi}
  126. */