MockProjectHistoryApi.mjs 4.2 KB

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