Merge pull request #14881 from overleaf/ab-beta-program-async-await

[web] Convert BetaProgramController to async/await

GitOrigin-RevId: 2423cb00b78b2f3fddb7bcd317d34ed4dbc12b95
This commit is contained in:
Jessica Lawshe
2023-10-24 08:47:38 -05:00
committed by Copybot
parent 8da063d640
commit 21514418e5
3 changed files with 140 additions and 127 deletions

View File

@@ -1,6 +1,8 @@
const SandboxedModule = require('sandboxed-module')
const path = require('path')
const sinon = require('sinon')
const { expect } = require('chai')
const MockResponse = require('../helpers/MockResponse')
const modulePath = path.join(
__dirname,
@@ -22,17 +24,23 @@ describe('BetaProgramController', function () {
},
}
this.SplitTestHandler = {
sessionMaintenance: sinon.stub().yields(),
promises: {
sessionMaintenance: sinon.stub(),
},
}
this.BetaProgramController = SandboxedModule.require(modulePath, {
requires: {
'../SplitTests/SplitTestHandler': this.SplitTestHandler,
'./BetaProgramHandler': (this.BetaProgramHandler = {
optIn: sinon.stub(),
optOut: sinon.stub(),
promises: {
optIn: sinon.stub().resolves(),
optOut: sinon.stub().resolves(),
},
}),
'../User/UserGetter': (this.UserGetter = {
getUser: sinon.stub(),
promises: {
getUser: sinon.stub().resolves(),
},
}),
'@overleaf/settings': (this.settings = {
languages: {},
@@ -43,23 +51,17 @@ describe('BetaProgramController', function () {
}),
},
})
this.res = {
send: sinon.stub(),
redirect: sinon.stub(),
render: sinon.stub(),
}
this.res = new MockResponse()
this.next = sinon.stub()
})
describe('optIn', function () {
beforeEach(function () {
this.BetaProgramHandler.optIn.callsArgWith(1, null)
})
it("should redirect to '/beta/participate'", function () {
this.BetaProgramController.optIn(this.req, this.res, this.next)
this.res.redirect.callCount.should.equal(1)
this.res.redirect.firstCall.args[0].should.equal('/beta/participate')
it("should redirect to '/beta/participate'", function (done) {
this.res.callback = () => {
this.res.redirectedTo.should.equal('/beta/participate')
done()
}
this.BetaProgramController.optIn(this.req, this.res, done)
})
it('should not call next with an error', function () {
@@ -69,19 +71,22 @@ describe('BetaProgramController', function () {
it('should call BetaProgramHandler.optIn', function () {
this.BetaProgramController.optIn(this.req, this.res, this.next)
this.BetaProgramHandler.optIn.callCount.should.equal(1)
this.BetaProgramHandler.promises.optIn.callCount.should.equal(1)
})
it('should invoke the session maintenance', function () {
this.BetaProgramController.optIn(this.req, this.res, this.next)
this.SplitTestHandler.sessionMaintenance.should.have.been.calledWith(
this.req
)
it('should invoke the session maintenance', function (done) {
this.res.callback = () => {
this.SplitTestHandler.promises.sessionMaintenance.should.have.been.calledWith(
this.req
)
done()
}
this.BetaProgramController.optIn(this.req, this.res, done)
})
describe('when BetaProgramHandler.opIn produces an error', function () {
beforeEach(function () {
this.BetaProgramHandler.optIn.callsArgWith(1, new Error('woops'))
this.BetaProgramHandler.promises.optIn.throws(new Error('woops'))
})
it("should not redirect to '/beta/participate'", function () {
@@ -89,75 +94,89 @@ describe('BetaProgramController', function () {
this.res.redirect.callCount.should.equal(0)
})
it('should produce an error', function () {
this.BetaProgramController.optIn(this.req, this.res, this.next)
this.next.callCount.should.equal(1)
this.next.firstCall.args[0].should.be.instanceof(Error)
it('should produce an error', function (done) {
this.BetaProgramController.optIn(this.req, this.res, err => {
expect(err).to.be.instanceof(Error)
done()
})
})
})
})
describe('optOut', function () {
beforeEach(function () {
this.BetaProgramHandler.optOut.callsArgWith(1, null)
it("should redirect to '/beta/participate'", function (done) {
this.res.callback = () => {
expect(this.res.redirectedTo).to.equal('/beta/participate')
done()
}
this.BetaProgramController.optOut(this.req, this.res, done)
})
it("should redirect to '/beta/participate'", function () {
this.BetaProgramController.optOut(this.req, this.res, this.next)
this.res.redirect.callCount.should.equal(1)
this.res.redirect.firstCall.args[0].should.equal('/beta/participate')
it('should not call next with an error', function (done) {
this.res.callback = () => {
this.next.callCount.should.equal(0)
done()
}
this.BetaProgramController.optOut(this.req, this.res, done)
})
it('should not call next with an error', function () {
this.BetaProgramController.optOut(this.req, this.res, this.next)
this.next.callCount.should.equal(0)
it('should call BetaProgramHandler.optOut', function (done) {
this.res.callback = () => {
this.BetaProgramHandler.promises.optOut.callCount.should.equal(1)
done()
}
this.BetaProgramController.optOut(this.req, this.res, done)
})
it('should call BetaProgramHandler.optOut', function () {
this.BetaProgramController.optOut(this.req, this.res, this.next)
this.BetaProgramHandler.optOut.callCount.should.equal(1)
})
it('should invoke the session maintenance', function () {
this.BetaProgramController.optOut(this.req, this.res, this.next)
this.SplitTestHandler.sessionMaintenance.should.have.been.calledWith(
this.req
)
it('should invoke the session maintenance', function (done) {
this.res.callback = () => {
this.SplitTestHandler.promises.sessionMaintenance.should.have.been.calledWith(
this.req,
null
)
done()
}
this.BetaProgramController.optOut(this.req, this.res, done)
})
describe('when BetaProgramHandler.optOut produces an error', function () {
beforeEach(function () {
this.BetaProgramHandler.optOut.callsArgWith(1, new Error('woops'))
this.BetaProgramHandler.promises.optOut.throws(new Error('woops'))
})
it("should not redirect to '/beta/participate'", function () {
this.BetaProgramController.optOut(this.req, this.res, this.next)
this.res.redirect.callCount.should.equal(0)
it("should not redirect to '/beta/participate'", function (done) {
this.BetaProgramController.optOut(this.req, this.res, error => {
expect(error).to.exist
expect(this.res.redirected).to.equal(false)
done()
})
})
it('should produce an error', function () {
this.BetaProgramController.optOut(this.req, this.res, this.next)
this.next.callCount.should.equal(1)
this.next.firstCall.args[0].should.be.instanceof(Error)
it('should produce an error', function (done) {
this.BetaProgramController.optOut(this.req, this.res, error => {
expect(error).to.exist
done()
})
})
})
})
describe('optInPage', function () {
beforeEach(function () {
this.UserGetter.getUser.yields(null, this.user)
this.UserGetter.promises.getUser.resolves(this.user)
})
it('should render the opt-in page', function () {
this.BetaProgramController.optInPage(this.req, this.res, this.next)
this.res.render.callCount.should.equal(1)
const { args } = this.res.render.firstCall
args[0].should.equal('beta_program/opt_in')
it('should render the opt-in page', function (done) {
this.res.callback = () => {
expect(this.res.renderedTemplate).to.equal('beta_program/opt_in')
done()
}
this.BetaProgramController.optInPage(this.req, this.res, done)
})
describe('when UserGetter.getUser produces an error', function () {
beforeEach(function () {
this.UserGetter.getUser.yields(new Error('woops'))
this.UserGetter.promises.getUser.throws(new Error('woops'))
})
it('should not render the opt-in page', function () {
@@ -165,10 +184,12 @@ describe('BetaProgramController', function () {
this.res.render.callCount.should.equal(0)
})
it('should produce an error', function () {
this.BetaProgramController.optInPage(this.req, this.res, this.next)
this.next.callCount.should.equal(1)
this.next.firstCall.args[0].should.be.instanceof(Error)
it('should produce an error', function (done) {
this.BetaProgramController.optInPage(this.req, this.res, error => {
expect(error).to.exist
expect(error).to.be.instanceof(Error)
done()
})
})
})
})