project_expiry.test.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. 'use strict'
  2. const BPromise = require('bluebird')
  3. const { expect } = require('chai')
  4. const HTTPStatus = require('http-status')
  5. const fetch = require('node-fetch')
  6. const fs = BPromise.promisifyAll(require('node:fs'))
  7. const cleanup = require('../storage/support/cleanup')
  8. const fixtures = require('../storage/support/fixtures')
  9. const testFiles = require('../storage/support/test_files')
  10. const testProjects = require('./support/test_projects')
  11. const testServer = require('./support/test_server')
  12. const { Change, File, Operation } = require('overleaf-editor-core')
  13. const queueChanges = require('../../../../storage/lib/queue_changes')
  14. const { getState } = require('../../../../storage/lib/chunk_store/redis')
  15. describe('project expiry', function () {
  16. beforeEach(cleanup.everything)
  17. beforeEach(fixtures.create)
  18. it('expire redis buffer', async function () {
  19. const basicAuthClient = testServer.basicAuthClient
  20. const projectId = await testProjects.createEmptyProject()
  21. // upload an empty file
  22. const response = await fetch(
  23. testServer.url(
  24. `/api/projects/${projectId}/blobs/${File.EMPTY_FILE_HASH}`,
  25. { qs: { pathname: 'main.tex' } }
  26. ),
  27. {
  28. method: 'PUT',
  29. body: fs.createReadStream(testFiles.path('empty.tex')),
  30. headers: {
  31. Authorization: testServer.basicAuthHeader,
  32. },
  33. }
  34. )
  35. expect(response.ok).to.be.true
  36. const testFile = File.fromHash(File.EMPTY_FILE_HASH)
  37. const testChange = new Change(
  38. [Operation.addFile('main.tex', testFile)],
  39. new Date()
  40. )
  41. await queueChanges(projectId, [testChange], 0)
  42. // Verify that the changes are queued and not yet persisted
  43. const initialState = await getState(projectId)
  44. expect(initialState.persistedVersion).to.be.null
  45. expect(initialState.changes).to.have.lengthOf(1)
  46. const importResponse =
  47. await basicAuthClient.apis.ProjectImport.flushChanges({
  48. project_id: projectId,
  49. })
  50. expect(importResponse.status).to.equal(HTTPStatus.OK)
  51. // Verify that the changes were persisted to the chunk store
  52. const flushedState = await getState(projectId)
  53. expect(flushedState.persistedVersion).to.equal(1)
  54. const expireResponse =
  55. await basicAuthClient.apis.ProjectImport.expireProject({
  56. project_id: projectId,
  57. })
  58. expect(expireResponse.status).to.equal(HTTPStatus.OK)
  59. const finalState = await getState(projectId)
  60. expect(finalState).to.deep.equal({
  61. changes: [],
  62. expireTime: null,
  63. headSnapshot: null,
  64. headVersion: null,
  65. persistTime: null,
  66. persistedVersion: null,
  67. })
  68. })
  69. })