projects.test.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. })
  123. describe('deleteProject', function () {
  124. it('deletes the project chunks', async function () {
  125. const projectId = fixtures.docs.initializedProject.id
  126. const historyResponse =
  127. await testServer.pseudoJwtBasicAuthClient.apis.Project.getLatestHistory(
  128. {
  129. project_id: projectId,
  130. }
  131. )
  132. expect(historyResponse.status).to.equal(HTTPStatus.OK)
  133. expect(historyResponse.body).to.have.property('chunk')
  134. const deleteResponse =
  135. await testServer.basicAuthClient.apis.Project.deleteProject({
  136. project_id: projectId,
  137. })
  138. expect(deleteResponse.status).to.equal(HTTPStatus.NO_CONTENT)
  139. await expectHttpError(
  140. testServer.pseudoJwtBasicAuthClient.apis.Project.getLatestHistory({
  141. project_id: projectId,
  142. }),
  143. HTTPStatus.NOT_FOUND
  144. )
  145. })
  146. it('deletes the project blobs', async function () {
  147. const projectId = fixtures.docs.initializedProject.id
  148. const token = testServer.createTokenForProject(projectId)
  149. const authHeaders = { Authorization: `Bearer ${token}` }
  150. const hash = testFiles.HELLO_TXT_HASH
  151. const fileContents = await fs.promises.readFile(
  152. testFiles.path('hello.txt')
  153. )
  154. const blobUrl = testServer.url(`/api/projects/${projectId}/blobs/${hash}`)
  155. const response1 = await fetch(blobUrl, {
  156. method: 'PUT',
  157. headers: authHeaders,
  158. body: fileContents,
  159. })
  160. expect(response1.ok).to.be.true
  161. const response2 = await fetch(blobUrl, { headers: authHeaders })
  162. const payload = await response2.text()
  163. expect(payload).to.equal(fileContents.toString())
  164. const deleteResponse =
  165. await testServer.basicAuthClient.apis.Project.deleteProject({
  166. project_id: projectId,
  167. })
  168. expect(deleteResponse.status).to.equal(HTTPStatus.NO_CONTENT)
  169. const response3 = await fetch(blobUrl, { headers: authHeaders })
  170. expect(response3.status).to.equal(HTTPStatus.NOT_FOUND)
  171. })
  172. })
  173. })