NumericProjectIdTests.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { expect } from 'chai'
  2. import nock from 'nock'
  3. import request from 'request'
  4. import * as ProjectHistoryApp from './helpers/ProjectHistoryApp.js'
  5. import * as ProjectHistoryClient from './helpers/ProjectHistoryClient.js'
  6. const MockHistoryStore = () => nock('http://127.0.0.1:3100')
  7. const MockWeb = () => nock('http://127.0.0.1:3000')
  8. const fixture = path => new URL(`../fixtures/${path}`, import.meta.url)
  9. /**
  10. * These tests verify that endpoints accept numeric project IDs (in addition to ObjectId strings).
  11. * This is needed for v1 history projects which use numeric project IDs.
  12. */
  13. describe('NumericProjectId', function () {
  14. beforeEach(async function () {
  15. await ProjectHistoryApp.ensureRunning()
  16. // Use a numeric project ID (simulating v1 history projects)
  17. this.numericProjectId = 123456
  18. MockHistoryStore().post('/api/projects').reply(200, {
  19. projectId: this.numericProjectId,
  20. })
  21. const olProject = await ProjectHistoryClient.initializeProject(
  22. this.numericProjectId
  23. )
  24. this.historyId = olProject.id
  25. MockWeb()
  26. .get(`/project/${this.numericProjectId}/details`)
  27. .reply(200, {
  28. name: 'Test Project',
  29. overleaf: { history: { id: this.historyId } },
  30. })
  31. .persist()
  32. MockHistoryStore()
  33. .get(`/api/projects/${this.historyId}/latest/history`)
  34. .replyWithFile(200, fixture('chunks/7-8.json'))
  35. .persist()
  36. MockHistoryStore()
  37. .get(`/api/projects/${this.historyId}/versions/7/history`)
  38. .replyWithFile(200, fixture('chunks/7-8.json'))
  39. .persist()
  40. MockHistoryStore()
  41. .get(`/api/projects/${this.historyId}/versions/8/history`)
  42. .replyWithFile(200, fixture('chunks/7-8.json'))
  43. .persist()
  44. })
  45. afterEach(function () {
  46. nock.cleanAll()
  47. })
  48. function makeRequest(options) {
  49. return new Promise((resolve, reject) => {
  50. request(options, (error, response, body) => {
  51. if (error) return reject(error)
  52. resolve({ response, body })
  53. })
  54. })
  55. }
  56. it('should accept numeric project_id for flush', async function () {
  57. const { response } = await makeRequest({
  58. method: 'POST',
  59. url: `http://127.0.0.1:3054/project/${this.numericProjectId}/flush`,
  60. })
  61. expect(response.statusCode).to.equal(204)
  62. })
  63. it('should accept numeric project_id for dump', async function () {
  64. const { response } = await makeRequest({
  65. method: 'GET',
  66. url: `http://127.0.0.1:3054/project/${this.numericProjectId}/dump`,
  67. })
  68. expect(response.statusCode).to.equal(200)
  69. })
  70. it('should accept numeric project_id for filetree diff', async function () {
  71. const { response } = await makeRequest({
  72. method: 'GET',
  73. url: `http://127.0.0.1:3054/project/${this.numericProjectId}/filetree/diff`,
  74. qs: { from: 7, to: 8 },
  75. })
  76. expect(response.statusCode).to.equal(200)
  77. })
  78. it('should accept numeric project_id for updates', async function () {
  79. const { response } = await makeRequest({
  80. method: 'GET',
  81. url: `http://127.0.0.1:3054/project/${this.numericProjectId}/updates`,
  82. qs: { min_count: 1 },
  83. })
  84. expect(response.statusCode).to.equal(200)
  85. })
  86. it('should accept numeric project_id for version', async function () {
  87. const { response } = await makeRequest({
  88. method: 'GET',
  89. url: `http://127.0.0.1:3054/project/${this.numericProjectId}/version`,
  90. })
  91. expect(response.statusCode).to.equal(200)
  92. })
  93. it('should accept numeric project_id for snapshot', async function () {
  94. const { response } = await makeRequest({
  95. method: 'GET',
  96. url: `http://127.0.0.1:3054/project/${this.numericProjectId}/snapshot`,
  97. })
  98. expect(response.statusCode).to.equal(200)
  99. })
  100. it('should accept numeric project_id for getLabels', async function () {
  101. const { response } = await makeRequest({
  102. method: 'GET',
  103. url: `http://127.0.0.1:3054/project/${this.numericProjectId}/labels`,
  104. })
  105. expect(response.statusCode).to.equal(200)
  106. })
  107. it('should accept numeric project_id for createLabel', async function () {
  108. const { response } = await makeRequest({
  109. method: 'POST',
  110. url: `http://127.0.0.1:3054/project/${this.numericProjectId}/labels`,
  111. json: {
  112. comment: 'test label',
  113. version: 7,
  114. user_id: '507f1f77bcf86cd799439011',
  115. },
  116. })
  117. expect(response.statusCode).to.equal(200)
  118. })
  119. it('should accept numeric history_id for getProjectBlob', async function () {
  120. const blobHash = 'a'.repeat(40)
  121. const blobContent = 'test blob content'
  122. MockHistoryStore()
  123. .get(`/api/projects/${this.historyId}/blobs/${blobHash}`)
  124. .reply(200, blobContent)
  125. const { response, body } = await makeRequest({
  126. method: 'GET',
  127. url: `http://127.0.0.1:3054/project/${this.historyId}/blob/${blobHash}`,
  128. })
  129. expect(response.statusCode).to.equal(200)
  130. expect(body).to.equal(blobContent)
  131. })
  132. })