Files
overleaf-cep/services/web/test/unit/src/InactiveData/InactiveProjectManager.test.mjs
Jakob Ackermann d56477565c [web] deduplicate getting the project when loading the editor (#32762)
* [web] enable async local storage on all the endpoints

Co-authored-by: Brian Gough <brian.gough@overleaf.com>

* [web] deduplicate getting the project when loading the editor

Co-authored-by: Brian Gough <brian.gough@overleaf.com>

* [web] use ProjectAccess state for computing analytics segmentation

Co-authored-by: Brian Gough <brian.gough@overleaf.com>

* [web] restore ownership of active flag and deferredTpdsFlushCounter

* [web] add missing await

* [web] update unit tests

* [web] add metrics for project access caching

* [web] add missing test mock

* [web] invalidate async local storage when changing project access

* [web] deduplicate project lookup when checking for token access

* [web] add helper function for getting cached ProjectAccess

* [web] add acceptance test for caching of ProjectAccess

* [web] account for saas-only project access in tests

* Revert "[web] enable async local storage on all the endpoints"

This reverts commit 1b82f3b935040e8cfd180d1f6bf4183a655580e2.

* [web] add async local storage to project endpoints in top-50

* [web] invalidate async local storage for project access from modules

Co-authored-by: Jessica Lawshe <jessica.lawshe@overleaf.com>

---------

Co-authored-by: Brian Gough <brian.gough@overleaf.com>
Co-authored-by: Jessica Lawshe <jessica.lawshe@overleaf.com>
GitOrigin-RevId: 3eea7956b24e6f937dc1c17948681063d4dca3ea
2026-04-17 08:07:08 +00:00

172 lines
5.5 KiB
JavaScript

import { vi, expect } from 'vitest'
import sinon from 'sinon'
import mongodb, { ReadPreference } from 'mongodb-legacy'
const { ObjectId } = mongodb
const modulePath =
'../../../../app/src/Features/InactiveData/InactiveProjectManager'
describe('InactiveProjectManager', function () {
beforeEach(async function (ctx) {
ctx.settings = {}
ctx.DocstoreManager = {
promises: {
unarchiveProject: sinon.stub(),
archiveProject: sinon.stub(),
},
}
ctx.DocumentUpdaterHandler = {
promises: {
flushProjectToMongoAndDelete: sinon.stub(),
},
}
ctx.ProjectUpdateHandler = {
promises: {
markAsActive: sinon.stub(),
markAsInactive: sinon.stub(),
},
}
ctx.ProjectGetter = { promises: { getProject: sinon.stub() } }
ctx.Modules = { promises: { hooks: { fire: sinon.stub() } } }
vi.doMock('mongodb-legacy', () => ({
default: { ObjectId },
}))
vi.doMock('@overleaf/settings', () => ({
default: ctx.settings,
}))
vi.doMock('../../../../app/src/Features/Docstore/DocstoreManager', () => ({
default: ctx.DocstoreManager,
}))
vi.doMock(
'../../../../app/src/Features/DocumentUpdater/DocumentUpdaterHandler',
() => ({
default: ctx.DocumentUpdaterHandler,
})
)
vi.doMock(
'../../../../app/src/Features/Project/ProjectUpdateHandler',
() => ({
default: ctx.ProjectUpdateHandler,
})
)
vi.doMock('../../../../app/src/models/Project', () => ({}))
vi.doMock('../../../../app/src/infrastructure/Modules', () => ({
default: ctx.Modules,
}))
vi.doMock('../../../../app/src/infrastructure/mongodb', () => ({
default: {
ObjectId,
READ_PREFERENCE_SECONDARY: ReadPreference.secondaryPreferred.mode,
},
}))
ctx.InactiveProjectManager = (await import(modulePath)).default
ctx.project_id = '1234'
})
describe('reactivateProjectIfRequired', function () {
beforeEach(function (ctx) {
ctx.project = { _id: ctx.project_id, active: false }
ctx.ProjectUpdateHandler.promises.markAsActive.resolves()
})
it('should call unarchiveProject', async function (ctx) {
ctx.DocstoreManager.promises.unarchiveProject.resolves()
await ctx.InactiveProjectManager.promises.reactivateProjectIfRequired(
ctx.project
)
ctx.DocstoreManager.promises.unarchiveProject
.calledWith(ctx.project_id)
.should.equal(true)
ctx.ProjectUpdateHandler.promises.markAsActive
.calledWith(ctx.project_id)
.should.equal(true)
})
it('should not mark project as active if error with unarchiving', async function (ctx) {
ctx.DocstoreManager.promises.unarchiveProject.rejects()
await expect(
ctx.InactiveProjectManager.promises.reactivateProjectIfRequired(
ctx.project
)
).to.be.rejected
ctx.DocstoreManager.promises.unarchiveProject
.calledWith(ctx.project_id)
.should.equal(true)
ctx.ProjectUpdateHandler.promises.markAsActive
.calledWith(ctx.project_id)
.should.equal(false)
})
it('should not call unarchiveProject if it is active', async function (ctx) {
ctx.project.active = true
ctx.DocstoreManager.promises.unarchiveProject.resolves()
await ctx.InactiveProjectManager.promises.reactivateProjectIfRequired(
ctx.project
)
ctx.DocstoreManager.promises.unarchiveProject
.calledWith(ctx.project_id)
.should.equal(false)
ctx.ProjectUpdateHandler.promises.markAsActive
.calledWith(ctx.project_id)
.should.equal(false)
})
})
describe('deactivateProject', function () {
it('should call archiveProject and markAsInactive after flushing', async function (ctx) {
ctx.DocstoreManager.promises.archiveProject.resolves()
ctx.DocumentUpdaterHandler.promises.flushProjectToMongoAndDelete.resolves()
ctx.ProjectUpdateHandler.promises.markAsInactive.resolves()
ctx.Modules.promises.hooks.fire.resolves()
await ctx.InactiveProjectManager.promises.deactivateProject(
ctx.project_id
)
ctx.DocumentUpdaterHandler.promises.flushProjectToMongoAndDelete
.calledWith(ctx.project_id)
.should.equal(true)
ctx.Modules.promises.hooks.fire
.calledWith('deactivateProject', ctx.project_id)
.should.equal(true)
ctx.DocstoreManager.promises.archiveProject
.calledWith(ctx.project_id)
.should.equal(true)
ctx.ProjectUpdateHandler.promises.markAsInactive
.calledWith(ctx.project_id)
.should.equal(true)
})
it('should not call markAsInactive if there was a problem archiving in docstore', async function (ctx) {
ctx.DocstoreManager.promises.archiveProject.rejects()
ctx.DocumentUpdaterHandler.promises.flushProjectToMongoAndDelete.resolves()
ctx.ProjectUpdateHandler.promises.markAsInactive.resolves()
ctx.Modules.promises.hooks.fire.resolves()
await expect(
ctx.InactiveProjectManager.promises.deactivateProject(ctx.project_id)
).to.be.rejected
ctx.DocumentUpdaterHandler.promises.flushProjectToMongoAndDelete
.calledWith(ctx.project_id)
.should.equal(true)
ctx.DocstoreManager.promises.archiveProject
.calledWith(ctx.project_id)
.should.equal(true)
ctx.ProjectUpdateHandler.promises.markAsInactive
.calledWith(ctx.project_id)
.should.equal(false)
})
})
})