Merge pull request #30539 from overleaf/mfb-promisify-cooldown-manager
promisify cooldown manager GitOrigin-RevId: 88c8c7d7e306042524ecd400fb15527206472cb1
This commit is contained in:
committed by
Copybot
parent
29f5026dda
commit
3261ffa4d0
@@ -22,35 +22,27 @@ describe('CooldownManager', function () {
|
||||
describe('isProjectOnCooldown', function () {
|
||||
describe('when no project is on cooldown', function () {
|
||||
it('returns false for project 1', async function (ctx) {
|
||||
const result = await CooldownManager.promises.isProjectOnCooldown(
|
||||
ctx.project1Id
|
||||
)
|
||||
const result = await CooldownManager.isProjectOnCooldown(ctx.project1Id)
|
||||
expect(result).to.be.false
|
||||
})
|
||||
|
||||
it('returns false for project 2', async function (ctx) {
|
||||
const result = await CooldownManager.promises.isProjectOnCooldown(
|
||||
ctx.project2Id
|
||||
)
|
||||
const result = await CooldownManager.isProjectOnCooldown(ctx.project2Id)
|
||||
expect(result).to.be.false
|
||||
})
|
||||
})
|
||||
describe('when project 1 is on cooldown', function () {
|
||||
beforeEach(async function (ctx) {
|
||||
await CooldownManager.promises.putProjectOnCooldown(ctx.project1Id)
|
||||
await CooldownManager.putProjectOnCooldown(ctx.project1Id)
|
||||
})
|
||||
|
||||
it('returns true for project 1', async function (ctx) {
|
||||
const result = await CooldownManager.promises.isProjectOnCooldown(
|
||||
ctx.project1Id
|
||||
)
|
||||
const result = await CooldownManager.isProjectOnCooldown(ctx.project1Id)
|
||||
expect(result).to.be.true
|
||||
})
|
||||
|
||||
it('returns false for project 2', async function (ctx) {
|
||||
const result = await CooldownManager.promises.isProjectOnCooldown(
|
||||
ctx.project2Id
|
||||
)
|
||||
const result = await CooldownManager.isProjectOnCooldown(ctx.project2Id)
|
||||
expect(result).to.be.false
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,125 +0,0 @@
|
||||
import { expect, vi } from 'vitest'
|
||||
import sinon from 'sinon'
|
||||
const modulePath = new URL(
|
||||
'../../../../app/src/Features/Cooldown/CooldownMiddleware.mjs',
|
||||
import.meta.url
|
||||
).pathname
|
||||
|
||||
describe('CooldownMiddleware', function () {
|
||||
beforeEach(async function (ctx) {
|
||||
ctx.CooldownManager = { isProjectOnCooldown: sinon.stub() }
|
||||
|
||||
vi.doMock(
|
||||
'../../../../app/src/Features/Cooldown/CooldownManager.mjs',
|
||||
() => ({
|
||||
default: ctx.CooldownManager,
|
||||
})
|
||||
)
|
||||
|
||||
ctx.CooldownMiddleware = (await import(modulePath)).default
|
||||
})
|
||||
|
||||
describe('freezeProject', function () {
|
||||
describe('when project is on cooldown', function () {
|
||||
beforeEach(function (ctx) {
|
||||
ctx.CooldownManager.isProjectOnCooldown = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, true)
|
||||
ctx.req = { params: { Project_id: 'abc' } }
|
||||
ctx.res = { sendStatus: sinon.stub() }
|
||||
return (ctx.next = sinon.stub())
|
||||
})
|
||||
|
||||
it('should call CooldownManager.isProjectOnCooldown', function (ctx) {
|
||||
ctx.CooldownMiddleware.freezeProject(ctx.req, ctx.res, ctx.next)
|
||||
ctx.CooldownManager.isProjectOnCooldown.callCount.should.equal(1)
|
||||
return ctx.CooldownManager.isProjectOnCooldown
|
||||
.calledWith('abc')
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('should not produce an error', function (ctx) {
|
||||
ctx.CooldownMiddleware.freezeProject(ctx.req, ctx.res, ctx.next)
|
||||
return ctx.next.callCount.should.equal(0)
|
||||
})
|
||||
|
||||
it('should send a 429 status', function (ctx) {
|
||||
ctx.CooldownMiddleware.freezeProject(ctx.req, ctx.res, ctx.next)
|
||||
ctx.res.sendStatus.callCount.should.equal(1)
|
||||
return ctx.res.sendStatus.calledWith(429).should.equal(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('when project is not on cooldown', function () {
|
||||
beforeEach(function (ctx) {
|
||||
ctx.CooldownManager.isProjectOnCooldown = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, false)
|
||||
ctx.req = { params: { Project_id: 'abc' } }
|
||||
ctx.res = { sendStatus: sinon.stub() }
|
||||
return (ctx.next = sinon.stub())
|
||||
})
|
||||
|
||||
it('should call CooldownManager.isProjectOnCooldown', function (ctx) {
|
||||
ctx.CooldownMiddleware.freezeProject(ctx.req, ctx.res, ctx.next)
|
||||
ctx.CooldownManager.isProjectOnCooldown.callCount.should.equal(1)
|
||||
return ctx.CooldownManager.isProjectOnCooldown
|
||||
.calledWith('abc')
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('call next with no arguments', function (ctx) {
|
||||
ctx.CooldownMiddleware.freezeProject(ctx.req, ctx.res, ctx.next)
|
||||
ctx.next.callCount.should.equal(1)
|
||||
return expect(ctx.next.lastCall.args.length).to.equal(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('when isProjectOnCooldown produces an error', function () {
|
||||
beforeEach(function (ctx) {
|
||||
ctx.CooldownManager.isProjectOnCooldown = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, new Error('woops'))
|
||||
ctx.req = { params: { Project_id: 'abc' } }
|
||||
ctx.res = { sendStatus: sinon.stub() }
|
||||
return (ctx.next = sinon.stub())
|
||||
})
|
||||
|
||||
it('should call CooldownManager.isProjectOnCooldown', function (ctx) {
|
||||
ctx.CooldownMiddleware.freezeProject(ctx.req, ctx.res, ctx.next)
|
||||
ctx.CooldownManager.isProjectOnCooldown.callCount.should.equal(1)
|
||||
return ctx.CooldownManager.isProjectOnCooldown
|
||||
.calledWith('abc')
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('call next with an error', function (ctx) {
|
||||
ctx.CooldownMiddleware.freezeProject(ctx.req, ctx.res, ctx.next)
|
||||
ctx.next.callCount.should.equal(1)
|
||||
return expect(ctx.next.lastCall.args[0]).to.be.instanceof(Error)
|
||||
})
|
||||
})
|
||||
|
||||
describe('when projectId is not part of route', function () {
|
||||
beforeEach(function (ctx) {
|
||||
ctx.CooldownManager.isProjectOnCooldown = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, true)
|
||||
ctx.req = { params: { lol: 'abc' } }
|
||||
ctx.res = { sendStatus: sinon.stub() }
|
||||
return (ctx.next = sinon.stub())
|
||||
})
|
||||
|
||||
it('call next with an error', function (ctx) {
|
||||
ctx.CooldownMiddleware.freezeProject(ctx.req, ctx.res, ctx.next)
|
||||
ctx.next.callCount.should.equal(1)
|
||||
return expect(ctx.next.lastCall.args[0]).to.be.instanceof(Error)
|
||||
})
|
||||
|
||||
it('should not call CooldownManager.isProjectOnCooldown', function (ctx) {
|
||||
ctx.CooldownMiddleware.freezeProject(ctx.req, ctx.res, ctx.next)
|
||||
return ctx.CooldownManager.isProjectOnCooldown.callCount.should.equal(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -45,9 +45,7 @@ describe('TpdsUpdateHandler', function () {
|
||||
}
|
||||
|
||||
ctx.CooldownManager = {
|
||||
promises: {
|
||||
isProjectOnCooldown: sinon.stub().resolves(false),
|
||||
},
|
||||
isProjectOnCooldown: sinon.stub().resolves(false),
|
||||
}
|
||||
ctx.FileTypeManager = {
|
||||
shouldIgnore: sinon.stub().returns(false),
|
||||
@@ -487,7 +485,7 @@ function setupMatchingProjects(projectKeys) {
|
||||
|
||||
function setupProjectOnCooldown() {
|
||||
beforeEach(function (ctx) {
|
||||
ctx.CooldownManager.promises.isProjectOnCooldown
|
||||
ctx.CooldownManager.isProjectOnCooldown
|
||||
.withArgs(ctx.projects.active1._id)
|
||||
.resolves(true)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user