projects.test.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. 'use strict'
  2. const { expect } = require('chai')
  3. const fs = require('node:fs')
  4. const HTTPStatus = require('http-status')
  5. const fetch = require('node-fetch')
  6. const sinon = require('sinon')
  7. const cleanup = require('../storage/support/cleanup')
  8. const fixtures = require('../storage/support/fixtures')
  9. const testFiles = require('../storage/support/test_files')
  10. const { zipStore, persistChanges } = require('../../../../storage')
  11. const { expectHttpError } = require('./support/expect_response')
  12. const testServer = require('./support/test_server')
  13. const { createEmptyProject } = require('./support/test_projects')
  14. const {
  15. File,
  16. Snapshot,
  17. Change,
  18. AddFileOperation,
  19. } = require('overleaf-editor-core')
  20. const testProjects = require('./support/test_projects')
  21. describe('project controller', function () {
  22. beforeEach(cleanup.everything)
  23. beforeEach(fixtures.create)
  24. describe('initializeProject', function () {
  25. it('can initialize a new project', async function () {
  26. const projectId = await testProjects.createEmptyProject()
  27. expect(projectId).to.be.a('string')
  28. })
  29. })
  30. describe('createZip', function () {
  31. let importSnapshot
  32. let createZip
  33. before(function () {
  34. importSnapshot =
  35. testServer.basicAuthClient.apis.ProjectImport.importSnapshot1
  36. createZip = testServer.basicAuthClient.apis.Project.createZip
  37. })
  38. beforeEach(function () {
  39. // Don't start the work in the background in this test --- it is flaky.
  40. sinon.stub(zipStore, 'storeZip').resolves()
  41. })
  42. afterEach(function () {
  43. zipStore.storeZip.restore()
  44. })
  45. it('creates a URL to a zip file', async function () {
  46. // Create a test blob.
  47. const testProjectId = fixtures.docs.uninitializedProject.id
  48. const response = await fetch(
  49. testServer.url(
  50. `/api/projects/${testProjectId}/blobs/${testFiles.HELLO_TXT_HASH}`
  51. ),
  52. {
  53. method: 'PUT',
  54. body: fs.createReadStream(testFiles.path('hello.txt')),
  55. headers: {
  56. Authorization: testServer.basicAuthHeader,
  57. },
  58. }
  59. )
  60. expect(response.ok).to.be.true
  61. // Import a project with the test blob.
  62. const testFilePathname = 'hello.txt'
  63. const testSnapshot = new Snapshot()
  64. testSnapshot.addFile(
  65. testFilePathname,
  66. File.fromHash(testFiles.HELLO_TXT_HASH)
  67. )
  68. const importResponse = await importSnapshot({
  69. project_id: testProjectId,
  70. snapshot: testSnapshot.toRaw(),
  71. })
  72. expect(importResponse.obj.projectId).to.equal(testProjectId)
  73. const createZipResponse = await createZip({
  74. project_id: testProjectId,
  75. version: 0,
  76. })
  77. expect(createZipResponse.status).to.equal(HTTPStatus.OK)
  78. const zipInfo = createZipResponse.obj
  79. expect(zipInfo.zipUrl).to.match(
  80. /^http:\/\/gcs:9090\/download\/storage\/v1\/b\/overleaf-test-zips/
  81. )
  82. expect(zipStore.storeZip.calledOnce).to.be.true
  83. })
  84. })
  85. // eslint-disable-next-line mocha/no-skipped-tests
  86. describe.skip('getLatestContent', function () {
  87. // TODO: remove this endpoint entirely, see
  88. // https://github.com/overleaf/write_latex/pull/5120#discussion_r244291862
  89. })
  90. describe('getLatestHashedContent', function () {
  91. let limitsToPersistImmediately
  92. before(function () {
  93. // used to provide a limit which forces us to persist all of the changes.
  94. const farFuture = new Date()
  95. farFuture.setTime(farFuture.getTime() + 7 * 24 * 3600 * 1000)
  96. limitsToPersistImmediately = {
  97. minChangeTimestamp: farFuture,
  98. maxChangeTimestamp: farFuture,
  99. }
  100. })
  101. it('returns a snaphot', async function () {
  102. const changes = [
  103. new Change(
  104. [new AddFileOperation('test.tex', File.fromString('ab'))],
  105. new Date(),
  106. []
  107. ),
  108. ]
  109. const projectId = await createEmptyProject()
  110. await persistChanges(projectId, changes, limitsToPersistImmediately, 0)
  111. const response =
  112. await testServer.basicAuthClient.apis.Project.getLatestHashedContent({
  113. project_id: projectId,
  114. })
  115. expect(response.status).to.equal(HTTPStatus.OK)
  116. const snapshot = Snapshot.fromRaw(response.obj)
  117. expect(snapshot.countFiles()).to.equal(1)
  118. expect(snapshot.getFile('test.tex').getHash()).to.equal(
  119. testFiles.STRING_AB_HASH
  120. )
  121. })
  122. describe('getLatestHistoryRaw', function () {
  123. it('should handles read', async function () {
  124. const projectId = fixtures.docs.initializedProject.id
  125. const response =
  126. await testServer.pseudoJwtBasicAuthClient.apis.Project.getLatestHistoryRaw(
  127. {
  128. project_id: projectId,
  129. readOnly: 'true',
  130. }
  131. )
  132. expect(response.body).to.deep.equal({
  133. startVersion: 0,
  134. endVersion: 1,
  135. endTimestamp: '2032-01-01T00:00:00.000Z',
  136. })
  137. })
  138. })
  139. })
  140. describe('deleteProject', function () {
  141. it('deletes the project chunks', async function () {
  142. const projectId = fixtures.docs.initializedProject.id
  143. const historyResponse =
  144. await testServer.pseudoJwtBasicAuthClient.apis.Project.getLatestHistory(
  145. {
  146. project_id: projectId,
  147. }
  148. )
  149. expect(historyResponse.status).to.equal(HTTPStatus.OK)
  150. expect(historyResponse.body).to.have.property('chunk')
  151. const deleteResponse =
  152. await testServer.basicAuthClient.apis.Project.deleteProject({
  153. project_id: projectId,
  154. })
  155. expect(deleteResponse.status).to.equal(HTTPStatus.NO_CONTENT)
  156. await expectHttpError(
  157. testServer.pseudoJwtBasicAuthClient.apis.Project.getLatestHistory({
  158. project_id: projectId,
  159. }),
  160. HTTPStatus.NOT_FOUND
  161. )
  162. })
  163. it('deletes the project blobs', async function () {
  164. const projectId = fixtures.docs.initializedProject.id
  165. const token = testServer.createTokenForProject(projectId)
  166. const authHeaders = { Authorization: `Bearer ${token}` }
  167. const hash = testFiles.HELLO_TXT_HASH
  168. const fileContents = await fs.promises.readFile(
  169. testFiles.path('hello.txt')
  170. )
  171. const blobUrl = testServer.url(`/api/projects/${projectId}/blobs/${hash}`)
  172. const response1 = await fetch(blobUrl, {
  173. method: 'PUT',
  174. headers: authHeaders,
  175. body: fileContents,
  176. })
  177. expect(response1.ok).to.be.true
  178. const response2 = await fetch(blobUrl, { headers: authHeaders })
  179. const payload = await response2.text()
  180. expect(payload).to.equal(fileContents.toString())
  181. const deleteResponse =
  182. await testServer.basicAuthClient.apis.Project.deleteProject({
  183. project_id: projectId,
  184. })
  185. expect(deleteResponse.status).to.equal(HTTPStatus.NO_CONTENT)
  186. const response3 = await fetch(blobUrl, { headers: authHeaders })
  187. expect(response3.status).to.equal(HTTPStatus.NOT_FOUND)
  188. })
  189. })
  190. })