NumericProjectIdTests.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { expect } from 'chai'
  2. import nock from 'nock'
  3. import * as ProjectHistoryApp from './helpers/ProjectHistoryApp.js'
  4. import * as ProjectHistoryClient from './helpers/ProjectHistoryClient.js'
  5. import { fetchStringWithResponse } from '@overleaf/fetch-utils'
  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. async function makeRequest(options) {
  49. const { method, url, qs = {}, json } = options
  50. const u = new URL(url)
  51. u.search = new URLSearchParams(qs).toString()
  52. return await fetchStringWithResponse(u, {
  53. method,
  54. json,
  55. })
  56. }
  57. it('should accept numeric project_id for flush', async function () {
  58. const { response } = await makeRequest({
  59. method: 'POST',
  60. url: `http://127.0.0.1:3054/project/${this.numericProjectId}/flush`,
  61. })
  62. expect(response.status).to.equal(204)
  63. })
  64. it('should accept numeric project_id for dump', async function () {
  65. const { response } = await makeRequest({
  66. method: 'GET',
  67. url: `http://127.0.0.1:3054/project/${this.numericProjectId}/dump`,
  68. })
  69. expect(response.status).to.equal(200)
  70. })
  71. it('should accept numeric project_id for filetree diff', async function () {
  72. const { response } = await makeRequest({
  73. method: 'GET',
  74. url: `http://127.0.0.1:3054/project/${this.numericProjectId}/filetree/diff`,
  75. qs: { from: 7, to: 8 },
  76. })
  77. expect(response.status).to.equal(200)
  78. })
  79. it('should accept numeric project_id for updates', async function () {
  80. const { response } = await makeRequest({
  81. method: 'GET',
  82. url: `http://127.0.0.1:3054/project/${this.numericProjectId}/updates`,
  83. qs: { min_count: 1 },
  84. })
  85. expect(response.status).to.equal(200)
  86. })
  87. it('should accept numeric project_id for version', async function () {
  88. const { response } = await makeRequest({
  89. method: 'GET',
  90. url: `http://127.0.0.1:3054/project/${this.numericProjectId}/version`,
  91. })
  92. expect(response.status).to.equal(200)
  93. })
  94. it('should accept numeric project_id for snapshot', async function () {
  95. const { response } = await makeRequest({
  96. method: 'GET',
  97. url: `http://127.0.0.1:3054/project/${this.numericProjectId}/snapshot`,
  98. })
  99. expect(response.status).to.equal(200)
  100. })
  101. it('should accept numeric project_id for getLabels', async function () {
  102. const { response } = await makeRequest({
  103. method: 'GET',
  104. url: `http://127.0.0.1:3054/project/${this.numericProjectId}/labels`,
  105. })
  106. expect(response.status).to.equal(200)
  107. })
  108. it('should accept numeric project_id for createLabel', async function () {
  109. const { response } = await makeRequest({
  110. method: 'POST',
  111. url: `http://127.0.0.1:3054/project/${this.numericProjectId}/labels`,
  112. json: {
  113. comment: 'test label',
  114. version: 7,
  115. user_id: '507f1f77bcf86cd799439011',
  116. },
  117. })
  118. expect(response.status).to.equal(200)
  119. })
  120. it('should accept numeric history_id for getProjectBlob', async function () {
  121. const blobHash = 'a'.repeat(40)
  122. const blobContent = 'test blob content'
  123. MockHistoryStore()
  124. .get(`/api/projects/${this.historyId}/blobs/${blobHash}`)
  125. .reply(200, blobContent)
  126. const { response, body } = await makeRequest({
  127. method: 'GET',
  128. url: `http://127.0.0.1:3054/project/${this.historyId}/blob/${blobHash}`,
  129. })
  130. expect(response.status).to.equal(200)
  131. expect(body).to.equal(blobContent)
  132. })
  133. })