projects.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. EditFileOperation,
  20. TextOperation,
  21. } = require('overleaf-editor-core')
  22. const testProjects = require('./support/test_projects')
  23. describe('project controller', function () {
  24. beforeEach(cleanup.everything)
  25. beforeEach(fixtures.create)
  26. describe('initializeProject', function () {
  27. it('can initialize a new project', async function () {
  28. const projectId = await testProjects.createEmptyProject()
  29. expect(projectId).to.be.a('string')
  30. })
  31. })
  32. describe('createZip', function () {
  33. let importSnapshot
  34. let createZip
  35. before(function () {
  36. importSnapshot =
  37. testServer.basicAuthClient.apis.ProjectImport.importSnapshot1
  38. createZip = testServer.basicAuthClient.apis.Project.createZip
  39. })
  40. beforeEach(function () {
  41. // Don't start the work in the background in this test --- it is flaky.
  42. sinon.stub(zipStore, 'storeZip').resolves()
  43. })
  44. afterEach(function () {
  45. zipStore.storeZip.restore()
  46. })
  47. it('creates a URL to a zip file', async function () {
  48. // Create a test blob.
  49. const testProjectId = fixtures.docs.uninitializedProject.id
  50. const response = await fetch(
  51. testServer.url(
  52. `/api/projects/${testProjectId}/blobs/${testFiles.HELLO_TXT_HASH}`
  53. ),
  54. {
  55. method: 'PUT',
  56. body: fs.createReadStream(testFiles.path('hello.txt')),
  57. headers: {
  58. Authorization: testServer.basicAuthHeader,
  59. },
  60. }
  61. )
  62. expect(response.ok).to.be.true
  63. // Import a project with the test blob.
  64. const testFilePathname = 'hello.txt'
  65. const testSnapshot = new Snapshot()
  66. testSnapshot.addFile(
  67. testFilePathname,
  68. File.fromHash(testFiles.HELLO_TXT_HASH)
  69. )
  70. const importResponse = await importSnapshot({
  71. project_id: testProjectId,
  72. snapshot: testSnapshot.toRaw(),
  73. })
  74. expect(importResponse.obj.projectId).to.equal(testProjectId)
  75. const createZipResponse = await createZip({
  76. project_id: testProjectId,
  77. version: 0,
  78. })
  79. expect(createZipResponse.status).to.equal(HTTPStatus.OK)
  80. const zipInfo = createZipResponse.obj
  81. expect(zipInfo.zipUrl).to.match(
  82. /^http:\/\/gcs:9090\/download\/storage\/v1\/b\/overleaf-test-zips/
  83. )
  84. expect(zipStore.storeZip.calledOnce).to.be.true
  85. })
  86. })
  87. // eslint-disable-next-line mocha/no-skipped-tests
  88. describe.skip('getLatestContent', function () {
  89. // TODO: remove this endpoint entirely, see
  90. // https://github.com/overleaf/write_latex/pull/5120#discussion_r244291862
  91. })
  92. describe('project with changes', function () {
  93. let projectId
  94. beforeEach(async function () {
  95. // used to provide a limit which forces us to persist all of the changes.
  96. const farFuture = new Date()
  97. farFuture.setTime(farFuture.getTime() + 7 * 24 * 3600 * 1000)
  98. const limits = {
  99. minChangeTimestamp: farFuture,
  100. maxChangeTimestamp: farFuture,
  101. }
  102. const changes = [
  103. new Change(
  104. [new AddFileOperation('test.tex', File.fromString('ab'))],
  105. new Date(),
  106. []
  107. ),
  108. new Change(
  109. [new AddFileOperation('other.tex', File.fromString('hello'))],
  110. new Date(),
  111. []
  112. ),
  113. ]
  114. projectId = await createEmptyProject()
  115. await persistChanges(projectId, changes, limits, 0)
  116. })
  117. describe('getLatestHashedContent', function () {
  118. it('returns a snapshot', async function () {
  119. const response =
  120. await testServer.basicAuthClient.apis.Project.getLatestHashedContent({
  121. project_id: projectId,
  122. })
  123. expect(response.status).to.equal(HTTPStatus.OK)
  124. const snapshot = Snapshot.fromRaw(response.obj)
  125. expect(snapshot.countFiles()).to.equal(2)
  126. expect(snapshot.getFile('test.tex').getHash()).to.equal(
  127. testFiles.STRING_AB_HASH
  128. )
  129. })
  130. })
  131. describe('getChanges', function () {
  132. it('returns all changes when not given a limit', async function () {
  133. const response =
  134. await testServer.basicAuthClient.apis.Project.getChanges({
  135. project_id: projectId,
  136. })
  137. expect(response.status).to.equal(HTTPStatus.OK)
  138. const changes = response.obj
  139. expect(changes.length).to.equal(2)
  140. const filenames = changes
  141. .flatMap(change => change.operations)
  142. .map(operation => operation.pathname)
  143. expect(filenames).to.deep.equal(['test.tex', 'other.tex'])
  144. })
  145. it('returns only requested changes', async function () {
  146. const response =
  147. await testServer.basicAuthClient.apis.Project.getChanges({
  148. project_id: projectId,
  149. since: 1,
  150. })
  151. expect(response.status).to.equal(HTTPStatus.OK)
  152. const changes = response.obj
  153. expect(changes.length).to.equal(1)
  154. const filenames = changes
  155. .flatMap(change => change.operations)
  156. .map(operation => operation.pathname)
  157. expect(filenames).to.deep.equal(['other.tex'])
  158. })
  159. it('rejects negative versions', async function () {
  160. await expect(
  161. testServer.basicAuthClient.apis.Project.getChanges({
  162. project_id: projectId,
  163. since: -1,
  164. })
  165. ).to.be.rejectedWith('Bad Request')
  166. })
  167. it('rejects out of bounds versions', async function () {
  168. await expect(
  169. testServer.basicAuthClient.apis.Project.getChanges({
  170. project_id: projectId,
  171. since: 20,
  172. })
  173. ).to.be.rejectedWith('Bad Request')
  174. })
  175. })
  176. })
  177. describe('project with many chunks', function () {
  178. let projectId
  179. beforeEach(async function () {
  180. // used to provide a limit which forces us to persist all of the changes.
  181. const farFuture = new Date()
  182. farFuture.setTime(farFuture.getTime() + 7 * 24 * 3600 * 1000)
  183. const limits = {
  184. minChangeTimestamp: farFuture,
  185. maxChangeTimestamp: farFuture,
  186. maxChunkChanges: 5,
  187. }
  188. const changes = [
  189. new Change(
  190. [new AddFileOperation('test.tex', File.fromString(''))],
  191. new Date(),
  192. []
  193. ),
  194. ]
  195. for (let i = 0; i < 20; i++) {
  196. const textOperation = new TextOperation()
  197. textOperation.retain(i)
  198. textOperation.insert('x')
  199. changes.push(
  200. new Change(
  201. [new EditFileOperation('test.tex', textOperation)],
  202. new Date(),
  203. []
  204. )
  205. )
  206. }
  207. projectId = await createEmptyProject()
  208. await persistChanges(projectId, changes, limits, 0)
  209. })
  210. it('returns all changes when not given a limit', async function () {
  211. const response = await testServer.basicAuthClient.apis.Project.getChanges(
  212. {
  213. project_id: projectId,
  214. }
  215. )
  216. expect(response.status).to.equal(HTTPStatus.OK)
  217. const changes = response.obj
  218. expect(changes.length).to.equal(21)
  219. expect(changes[10].operations[0].textOperation).to.deep.equal([9, 'x'])
  220. })
  221. it('returns only requested changes', async function () {
  222. const response = await testServer.basicAuthClient.apis.Project.getChanges(
  223. {
  224. project_id: projectId,
  225. since: 10,
  226. }
  227. )
  228. expect(response.status).to.equal(HTTPStatus.OK)
  229. const changes = response.obj
  230. expect(changes.length).to.equal(11)
  231. expect(changes[2].operations[0].textOperation).to.deep.equal([11, 'x'])
  232. })
  233. })
  234. describe('getLatestHistoryRaw', function () {
  235. it('should handles read', async function () {
  236. const projectId = fixtures.docs.initializedProject.id
  237. const response =
  238. await testServer.pseudoJwtBasicAuthClient.apis.Project.getLatestHistoryRaw(
  239. {
  240. project_id: projectId,
  241. readOnly: 'true',
  242. }
  243. )
  244. expect(response.body).to.deep.equal({
  245. startVersion: 0,
  246. endVersion: 1,
  247. endTimestamp: '2032-01-01T00:00:00.000Z',
  248. })
  249. })
  250. })
  251. describe('deleteProject', function () {
  252. it('deletes the project chunks', async function () {
  253. const projectId = fixtures.docs.initializedProject.id
  254. const historyResponse =
  255. await testServer.pseudoJwtBasicAuthClient.apis.Project.getLatestHistory(
  256. {
  257. project_id: projectId,
  258. }
  259. )
  260. expect(historyResponse.status).to.equal(HTTPStatus.OK)
  261. expect(historyResponse.body).to.have.property('chunk')
  262. const deleteResponse =
  263. await testServer.basicAuthClient.apis.Project.deleteProject({
  264. project_id: projectId,
  265. })
  266. expect(deleteResponse.status).to.equal(HTTPStatus.NO_CONTENT)
  267. await expectHttpError(
  268. testServer.pseudoJwtBasicAuthClient.apis.Project.getLatestHistory({
  269. project_id: projectId,
  270. }),
  271. HTTPStatus.NOT_FOUND
  272. )
  273. })
  274. it('deletes the project blobs', async function () {
  275. const projectId = fixtures.docs.initializedProject.id
  276. const token = testServer.createTokenForProject(projectId)
  277. const authHeaders = { Authorization: `Bearer ${token}` }
  278. const hash = testFiles.HELLO_TXT_HASH
  279. const fileContents = await fs.promises.readFile(
  280. testFiles.path('hello.txt')
  281. )
  282. const blobUrl = testServer.url(`/api/projects/${projectId}/blobs/${hash}`)
  283. const response1 = await fetch(blobUrl, {
  284. method: 'PUT',
  285. headers: authHeaders,
  286. body: fileContents,
  287. })
  288. expect(response1.ok).to.be.true
  289. const response2 = await fetch(blobUrl, { headers: authHeaders })
  290. const payload = await response2.text()
  291. expect(payload).to.equal(fileContents.toString())
  292. const deleteResponse =
  293. await testServer.basicAuthClient.apis.Project.deleteProject({
  294. project_id: projectId,
  295. })
  296. expect(deleteResponse.status).to.equal(HTTPStatus.NO_CONTENT)
  297. const response3 = await fetch(blobUrl, { headers: authHeaders })
  298. expect(response3.status).to.equal(HTTPStatus.NOT_FOUND)
  299. })
  300. })
  301. })