projects.test.js 6.5 KB

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