mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
3553fb2d9d
Fix spelling of "middleware" GitOrigin-RevId: d2b2b20ad8a6871cd6366303e75b340f0f2f2dda
88 lines
3.3 KiB
CoffeeScript
88 lines
3.3 KiB
CoffeeScript
SandboxedModule = require('sandboxed-module')
|
|
sinon = require('sinon')
|
|
require('chai').should()
|
|
expect = require('chai').expect
|
|
modulePath = require('path').join __dirname, '../../../../app/js/Features/Cooldown/CooldownMiddleware'
|
|
|
|
|
|
describe "CooldownMiddleware", ->
|
|
|
|
beforeEach ->
|
|
@CooldownManager =
|
|
isProjectOnCooldown: sinon.stub()
|
|
@CooldownMiddleware = SandboxedModule.require modulePath, requires:
|
|
'./CooldownManager': @CooldownManager
|
|
'logger-sharelatex': {log: sinon.stub()}
|
|
|
|
describe 'freezeProject', ->
|
|
|
|
describe 'when project is on cooldown', ->
|
|
beforeEach ->
|
|
@CooldownManager.isProjectOnCooldown = sinon.stub().callsArgWith(1, null, true)
|
|
@req = {params: {Project_id: 'abc'}}
|
|
@res = {sendStatus: sinon.stub()}
|
|
@next = sinon.stub()
|
|
|
|
it 'should call CooldownManager.isProjectOnCooldown', ->
|
|
@CooldownMiddleware.freezeProject @req, @res, @next
|
|
@CooldownManager.isProjectOnCooldown.callCount.should.equal 1
|
|
@CooldownManager.isProjectOnCooldown.calledWith('abc').should.equal true
|
|
|
|
it 'should not produce an error', ->
|
|
@CooldownMiddleware.freezeProject @req, @res, @next
|
|
@next.callCount.should.equal 0
|
|
|
|
it 'should send a 429 status', ->
|
|
@CooldownMiddleware.freezeProject @req, @res, @next
|
|
@res.sendStatus.callCount.should.equal 1
|
|
@res.sendStatus.calledWith(429).should.equal true
|
|
|
|
describe 'when project is not on cooldown', ->
|
|
beforeEach ->
|
|
@CooldownManager.isProjectOnCooldown = sinon.stub().callsArgWith(1, null, false)
|
|
@req = {params: {Project_id: 'abc'}}
|
|
@res = {sendStatus: sinon.stub()}
|
|
@next = sinon.stub()
|
|
|
|
it 'should call CooldownManager.isProjectOnCooldown', ->
|
|
@CooldownMiddleware.freezeProject @req, @res, @next
|
|
@CooldownManager.isProjectOnCooldown.callCount.should.equal 1
|
|
@CooldownManager.isProjectOnCooldown.calledWith('abc').should.equal true
|
|
|
|
it 'call next with no arguments', ->
|
|
@CooldownMiddleware.freezeProject @req, @res, @next
|
|
@next.callCount.should.equal 1
|
|
expect(@next.lastCall.args.length).to.equal 0
|
|
|
|
describe 'when isProjectOnCooldown produces an error', ->
|
|
beforeEach ->
|
|
@CooldownManager.isProjectOnCooldown = sinon.stub().callsArgWith(1, new Error('woops'))
|
|
@req = {params: {Project_id: 'abc'}}
|
|
@res = {sendStatus: sinon.stub()}
|
|
@next = sinon.stub()
|
|
|
|
it 'should call CooldownManager.isProjectOnCooldown', ->
|
|
@CooldownMiddleware.freezeProject @req, @res, @next
|
|
@CooldownManager.isProjectOnCooldown.callCount.should.equal 1
|
|
@CooldownManager.isProjectOnCooldown.calledWith('abc').should.equal true
|
|
|
|
it 'call next with an error', ->
|
|
@CooldownMiddleware.freezeProject @req, @res, @next
|
|
@next.callCount.should.equal 1
|
|
expect(@next.lastCall.args[0]).to.be.instanceof Error
|
|
|
|
describe 'when projectId is not part of route', ->
|
|
beforeEach ->
|
|
@CooldownManager.isProjectOnCooldown = sinon.stub().callsArgWith(1, null, true)
|
|
@req = {params: {lol: 'abc'}}
|
|
@res = {sendStatus: sinon.stub()}
|
|
@next = sinon.stub()
|
|
|
|
it 'call next with an error', ->
|
|
@CooldownMiddleware.freezeProject @req, @res, @next
|
|
@next.callCount.should.equal 1
|
|
expect(@next.lastCall.args[0]).to.be.instanceof Error
|
|
|
|
it 'should not call CooldownManager.isProjectOnCooldown', ->
|
|
@CooldownMiddleware.freezeProject @req, @res, @next
|
|
@CooldownManager.isProjectOnCooldown.callCount.should.equal 0
|