[web] Remove promises exports from Controller modules (#22242)

* Remove promises object from CollaboratorsInviteController.mjs

* Define functions at root

* Remove mentions of undefined `revokeInviteForUser`

* Remove unused `doLogout`

* Remove promises object from UserController.js

* Remove unused `makeChangePreview`

* Remove promises object from SubscriptionController.js (`getRecommendedCurrency` and `getLatamCountryBannerDetails`)

* Remove promises object from CollabratecController.mjs

* Remove promises object from SSOController.mjs

* Remove promises object from ReferencesApiController.mjs

* Remove promises object from MetricsEmailController.mjs

* Remove promises object from InstitutionHubsController.mjs

* Remove promises object from DocumentUpdaterController.mjs

* Remove promises object from SubscriptionAdminController.mjs

* Fixup unit tests

* Add expects that controllers don't error

* Promisify `ensureAffiliationMiddleware`

GitOrigin-RevId: 311c8afa7d5c8e4f051408d305b6b4147a020edc
This commit is contained in:
Antoine Clausse
2025-01-17 09:05:36 +00:00
committed by Copybot
parent ae34c4b8cc
commit b9fb636f0b
9 changed files with 487 additions and 540 deletions
@@ -238,17 +238,17 @@ describe('CollaboratorsInviteController', function () {
})
describe('when all goes well', function (done) {
beforeEach(function (done) {
this.CollaboratorsInviteController.promises._checkShouldInviteEmail =
sinon.stub().resolves(true)
this.CollaboratorsInviteController.promises._checkRateLimit = sinon
beforeEach(async function () {
this.CollaboratorsInviteController._checkShouldInviteEmail = sinon
.stub()
.resolves(true)
this.res.callback = () => done()
this.CollaboratorsInviteController.inviteToProject(
this.CollaboratorsInviteController._checkRateLimit = sinon
.stub()
.resolves(true)
await this.CollaboratorsInviteController.inviteToProject(
this.req,
this.res,
done
this.res
)
})
@@ -269,10 +269,11 @@ describe('CollaboratorsInviteController', function () {
})
it('should have called _checkShouldInviteEmail', function () {
this.CollaboratorsInviteController.promises._checkShouldInviteEmail.callCount.should.equal(
this.CollaboratorsInviteController._checkShouldInviteEmail.callCount.should.equal(
1
)
this.CollaboratorsInviteController.promises._checkShouldInviteEmail
this.CollaboratorsInviteController._checkShouldInviteEmail
.calledWith(this.targetEmail)
.should.equal(true)
})
@@ -322,9 +323,10 @@ describe('CollaboratorsInviteController', function () {
describe('readAndWrite collaborator', function () {
beforeEach(function (done) {
this.privileges = 'readAndWrite'
this.CollaboratorsInviteController.promises._checkShouldInviteEmail =
sinon.stub().resolves(true)
this.CollaboratorsInviteController.promises._checkRateLimit = sinon
this.CollaboratorsInviteController._checkShouldInviteEmail = sinon
.stub()
.resolves(true)
this.CollaboratorsInviteController._checkRateLimit = sinon
.stub()
.resolves(true)
this.res.callback = () => done()
@@ -343,10 +345,10 @@ describe('CollaboratorsInviteController', function () {
})
it('should not have called _checkShouldInviteEmail', function () {
this.CollaboratorsInviteController.promises._checkShouldInviteEmail.callCount.should.equal(
this.CollaboratorsInviteController._checkShouldInviteEmail.callCount.should.equal(
0
)
this.CollaboratorsInviteController.promises._checkShouldInviteEmail
this.CollaboratorsInviteController._checkShouldInviteEmail
.calledWith(this.currentUser, this.targetEmail)
.should.equal(false)
})
@@ -364,9 +366,10 @@ describe('CollaboratorsInviteController', function () {
email: this.targetEmail,
privileges: (this.privileges = 'readOnly'),
}
this.CollaboratorsInviteController.promises._checkShouldInviteEmail =
sinon.stub().resolves(true)
this.CollaboratorsInviteController.promises._checkRateLimit = sinon
this.CollaboratorsInviteController._checkShouldInviteEmail = sinon
.stub()
.resolves(true)
this.CollaboratorsInviteController._checkRateLimit = sinon
.stub()
.resolves(true)
this.res.callback = () => done()
@@ -391,10 +394,10 @@ describe('CollaboratorsInviteController', function () {
})
it('should have called _checkShouldInviteEmail', function () {
this.CollaboratorsInviteController.promises._checkShouldInviteEmail.callCount.should.equal(
this.CollaboratorsInviteController._checkShouldInviteEmail.callCount.should.equal(
1
)
this.CollaboratorsInviteController.promises._checkShouldInviteEmail
this.CollaboratorsInviteController._checkShouldInviteEmail
.calledWith(this.targetEmail)
.should.equal(true)
})
@@ -438,9 +441,10 @@ describe('CollaboratorsInviteController', function () {
describe('when all goes well', function (done) {
beforeEach(function (done) {
this.CollaboratorsInviteController.promises._checkShouldInviteEmail =
sinon.stub().resolves(true)
this.CollaboratorsInviteController.promises._checkRateLimit = sinon
this.CollaboratorsInviteController._checkShouldInviteEmail = sinon
.stub()
.resolves(true)
this.CollaboratorsInviteController._checkRateLimit = sinon
.stub()
.resolves(true)
this.res.callback = () => done()
@@ -468,10 +472,10 @@ describe('CollaboratorsInviteController', function () {
})
it('should have called _checkShouldInviteEmail', function () {
this.CollaboratorsInviteController.promises._checkShouldInviteEmail.callCount.should.equal(
this.CollaboratorsInviteController._checkShouldInviteEmail.callCount.should.equal(
1
)
this.CollaboratorsInviteController.promises._checkShouldInviteEmail
this.CollaboratorsInviteController._checkShouldInviteEmail
.calledWith(this.targetEmail)
.should.equal(true)
})
@@ -513,9 +517,10 @@ describe('CollaboratorsInviteController', function () {
describe('when the user is not allowed to add more collaborators', function () {
beforeEach(function (done) {
this.CollaboratorsInviteController.promises._checkShouldInviteEmail =
sinon.stub().resolves(true)
this.CollaboratorsInviteController.promises._checkRateLimit = sinon
this.CollaboratorsInviteController._checkShouldInviteEmail = sinon
.stub()
.resolves(true)
this.CollaboratorsInviteController._checkRateLimit = sinon
.stub()
.resolves(true)
this.LimitationsManager.promises.canAddXCollaborators.resolves(false)
@@ -533,10 +538,10 @@ describe('CollaboratorsInviteController', function () {
})
it('should not have called _checkShouldInviteEmail', function () {
this.CollaboratorsInviteController.promises._checkShouldInviteEmail.callCount.should.equal(
this.CollaboratorsInviteController._checkShouldInviteEmail.callCount.should.equal(
0
)
this.CollaboratorsInviteController.promises._checkShouldInviteEmail
this.CollaboratorsInviteController._checkShouldInviteEmail
.calledWith(this.currentUser, this.targetEmail)
.should.equal(false)
})
@@ -550,9 +555,10 @@ describe('CollaboratorsInviteController', function () {
describe('when canAddXCollaborators produces an error', function () {
beforeEach(function (done) {
this.CollaboratorsInviteController.promises._checkShouldInviteEmail =
sinon.stub().resolves(true)
this.CollaboratorsInviteController.promises._checkRateLimit = sinon
this.CollaboratorsInviteController._checkShouldInviteEmail = sinon
.stub()
.resolves(true)
this.CollaboratorsInviteController._checkRateLimit = sinon
.stub()
.resolves(true)
this.LimitationsManager.promises.canAddXCollaborators.rejects(
@@ -572,10 +578,10 @@ describe('CollaboratorsInviteController', function () {
})
it('should not have called _checkShouldInviteEmail', function () {
this.CollaboratorsInviteController.promises._checkShouldInviteEmail.callCount.should.equal(
this.CollaboratorsInviteController._checkShouldInviteEmail.callCount.should.equal(
0
)
this.CollaboratorsInviteController.promises._checkShouldInviteEmail
this.CollaboratorsInviteController._checkShouldInviteEmail
.calledWith(this.currentUser, this.targetEmail)
.should.equal(false)
})
@@ -589,9 +595,10 @@ describe('CollaboratorsInviteController', function () {
describe('when inviteToProject produces an error', function () {
beforeEach(function (done) {
this.CollaboratorsInviteController.promises._checkShouldInviteEmail =
sinon.stub().resolves(true)
this.CollaboratorsInviteController.promises._checkRateLimit = sinon
this.CollaboratorsInviteController._checkShouldInviteEmail = sinon
.stub()
.resolves(true)
this.CollaboratorsInviteController._checkRateLimit = sinon
.stub()
.resolves(true)
this.CollaboratorsInviteHandler.promises.inviteToProject.rejects(
@@ -620,10 +627,10 @@ describe('CollaboratorsInviteController', function () {
})
it('should have called _checkShouldInviteEmail', function () {
this.CollaboratorsInviteController.promises._checkShouldInviteEmail.callCount.should.equal(
this.CollaboratorsInviteController._checkShouldInviteEmail.callCount.should.equal(
1
)
this.CollaboratorsInviteController.promises._checkShouldInviteEmail
this.CollaboratorsInviteController._checkShouldInviteEmail
.calledWith(this.targetEmail)
.should.equal(true)
})
@@ -645,9 +652,10 @@ describe('CollaboratorsInviteController', function () {
describe('when _checkShouldInviteEmail disallows the invite', function () {
beforeEach(function (done) {
this.CollaboratorsInviteController.promises._checkShouldInviteEmail =
sinon.stub().resolves(false)
this.CollaboratorsInviteController.promises._checkRateLimit = sinon
this.CollaboratorsInviteController._checkShouldInviteEmail = sinon
.stub()
.resolves(false)
this.CollaboratorsInviteController._checkRateLimit = sinon
.stub()
.resolves(true)
this.res.callback = () => done()
@@ -667,10 +675,10 @@ describe('CollaboratorsInviteController', function () {
})
it('should have called _checkShouldInviteEmail', function () {
this.CollaboratorsInviteController.promises._checkShouldInviteEmail.callCount.should.equal(
this.CollaboratorsInviteController._checkShouldInviteEmail.callCount.should.equal(
1
)
this.CollaboratorsInviteController.promises._checkShouldInviteEmail
this.CollaboratorsInviteController._checkShouldInviteEmail
.calledWith(this.targetEmail)
.should.equal(true)
})
@@ -684,9 +692,10 @@ describe('CollaboratorsInviteController', function () {
describe('when _checkShouldInviteEmail produces an error', function () {
beforeEach(function (done) {
this.CollaboratorsInviteController.promises._checkShouldInviteEmail =
sinon.stub().rejects(new Error('woops'))
this.CollaboratorsInviteController.promises._checkRateLimit = sinon
this.CollaboratorsInviteController._checkShouldInviteEmail = sinon
.stub()
.rejects(new Error('woops'))
this.CollaboratorsInviteController._checkRateLimit = sinon
.stub()
.resolves(true)
this.next.callsFake(() => done())
@@ -703,10 +712,10 @@ describe('CollaboratorsInviteController', function () {
})
it('should have called _checkShouldInviteEmail', function () {
this.CollaboratorsInviteController.promises._checkShouldInviteEmail.callCount.should.equal(
this.CollaboratorsInviteController._checkShouldInviteEmail.callCount.should.equal(
1
)
this.CollaboratorsInviteController.promises._checkShouldInviteEmail
this.CollaboratorsInviteController._checkShouldInviteEmail
.calledWith(this.targetEmail)
.should.equal(true)
})
@@ -721,9 +730,10 @@ describe('CollaboratorsInviteController', function () {
describe('when the user invites themselves to the project', function () {
beforeEach(function () {
this.req.body.email = this.currentUser.email
this.CollaboratorsInviteController.promises._checkShouldInviteEmail =
sinon.stub().resolves(true)
this.CollaboratorsInviteController.promises._checkRateLimit = sinon
this.CollaboratorsInviteController._checkShouldInviteEmail = sinon
.stub()
.resolves(true)
this.CollaboratorsInviteController._checkRateLimit = sinon
.stub()
.resolves(true)
this.CollaboratorsInviteController.inviteToProject(
@@ -748,7 +758,7 @@ describe('CollaboratorsInviteController', function () {
})
it('should not have called _checkShouldInviteEmail', function () {
this.CollaboratorsInviteController.promises._checkShouldInviteEmail.callCount.should.equal(
this.CollaboratorsInviteController._checkShouldInviteEmail.callCount.should.equal(
0
)
})
@@ -765,14 +775,14 @@ describe('CollaboratorsInviteController', function () {
})
describe('when _checkRateLimit returns false', function () {
beforeEach(function (done) {
this.CollaboratorsInviteController.promises._checkShouldInviteEmail =
sinon.stub().resolves(true)
this.CollaboratorsInviteController.promises._checkRateLimit = sinon
beforeEach(async function () {
this.CollaboratorsInviteController._checkShouldInviteEmail = sinon
.stub()
.resolves(true)
this.CollaboratorsInviteController._checkRateLimit = sinon
.stub()
.resolves(false)
this.res.callback = () => done()
this.CollaboratorsInviteController.inviteToProject(
await this.CollaboratorsInviteController.inviteToProject(
this.req,
this.res,
this.next
@@ -1281,7 +1291,7 @@ describe('CollaboratorsInviteController', function () {
Project_id: this.projectId,
invite_id: this.invite._id.toString(),
}
this.CollaboratorsInviteController.promises._checkRateLimit = sinon
this.CollaboratorsInviteController._checkRateLimit = sinon
.stub()
.resolves(true)
})
@@ -1316,7 +1326,7 @@ describe('CollaboratorsInviteController', function () {
})
it('should check the rate limit', function () {
this.CollaboratorsInviteController.promises._checkRateLimit.callCount.should.equal(
this.CollaboratorsInviteController._checkRateLimit.callCount.should.equal(
1
)
})
@@ -1600,12 +1610,8 @@ describe('CollaboratorsInviteController', function () {
describe('when we should be restricting to existing accounts', function () {
beforeEach(function () {
this.settings.restrictInvitesToExistingAccounts = true
this.call = callback => {
this.CollaboratorsInviteController._checkShouldInviteEmail(
this.email,
callback
)
}
this.call = () =>
this.CollaboratorsInviteController._checkShouldInviteEmail(this.email)
})
describe('when user account is present', function () {
@@ -1614,12 +1620,12 @@ describe('CollaboratorsInviteController', function () {
this.UserGetter.promises.getUserByAnyEmail.resolves(this.user)
})
it('should callback with `true`', function (done) {
this.call((err, shouldAllow) => {
expect(err).to.equal(null)
expect(shouldAllow).to.equal(true)
done()
})
it('should callback with `true`', async function () {
const shouldAllow =
await this.CollaboratorsInviteController._checkShouldInviteEmail(
this.email
)
expect(shouldAllow).to.equal(true)
})
})
@@ -1629,25 +1635,22 @@ describe('CollaboratorsInviteController', function () {
this.UserGetter.promises.getUserByAnyEmail.resolves(this.user)
})
it('should callback with `false`', function (done) {
this.call((err, shouldAllow) => {
expect(err).to.equal(null)
expect(shouldAllow).to.equal(false)
done()
})
it('should callback with `false`', async function () {
const shouldAllow =
await this.CollaboratorsInviteController._checkShouldInviteEmail(
this.email
)
expect(shouldAllow).to.equal(false)
})
it('should have called getUser', function (done) {
this.call((err, shouldAllow) => {
if (err) {
return done(err)
}
this.UserGetter.promises.getUserByAnyEmail.callCount.should.equal(1)
this.UserGetter.promises.getUserByAnyEmail
.calledWith(this.email, { _id: 1 })
.should.equal(true)
done()
})
it('should have called getUser', async function () {
await this.CollaboratorsInviteController._checkShouldInviteEmail(
this.email
)
this.UserGetter.promises.getUserByAnyEmail.callCount.should.equal(1)
this.UserGetter.promises.getUserByAnyEmail
.calledWith(this.email, { _id: 1 })
.should.equal(true)
})
})
@@ -1657,13 +1660,12 @@ describe('CollaboratorsInviteController', function () {
this.UserGetter.promises.getUserByAnyEmail.rejects(new Error('woops'))
})
it('should callback with an error', function (done) {
this.call((err, shouldAllow) => {
expect(err).to.not.equal(null)
expect(err).to.be.instanceof(Error)
expect(shouldAllow).to.equal(undefined)
done()
})
it('should callback with an error', async function () {
await expect(
this.CollaboratorsInviteController._checkShouldInviteEmail(
this.email
)
).to.be.rejected
})
})
})
@@ -1678,90 +1680,60 @@ describe('CollaboratorsInviteController', function () {
.resolves(17)
})
it('should callback with `true` when rate limit under', function (done) {
this.CollaboratorsInviteController._checkRateLimit(
this.currentUserId,
(err, result) => {
if (err) {
return done(err)
}
expect(this.rateLimiter.consume).to.have.been.calledWith(
this.currentUserId
)
result.should.equal(true)
done()
}
it('should callback with `true` when rate limit under', async function () {
const result = await this.CollaboratorsInviteController._checkRateLimit(
this.currentUserId
)
expect(this.rateLimiter.consume).to.have.been.calledWith(
this.currentUserId
)
result.should.equal(true)
})
it('should callback with `false` when rate limit hit', function (done) {
it('should callback with `false` when rate limit hit', async function () {
this.rateLimiter.consume.rejects({ remainingPoints: 0 })
this.CollaboratorsInviteController._checkRateLimit(
const result = await this.CollaboratorsInviteController._checkRateLimit(
this.currentUserId
)
expect(this.rateLimiter.consume).to.have.been.calledWith(
this.currentUserId
)
result.should.equal(false)
})
it('should allow 10x the collaborators', async function () {
await this.CollaboratorsInviteController._checkRateLimit(
this.currentUserId
)
expect(this.rateLimiter.consume).to.have.been.calledWith(
this.currentUserId,
(err, result) => {
if (err) {
return done(err)
}
expect(this.rateLimiter.consume).to.have.been.calledWith(
this.currentUserId
)
result.should.equal(false)
done()
}
Math.floor(40000 / 170)
)
})
it('should allow 10x the collaborators', function (done) {
this.CollaboratorsInviteController._checkRateLimit(
this.currentUserId,
(err, result) => {
if (err) {
return done(err)
}
expect(this.rateLimiter.consume).to.have.been.calledWith(
this.currentUserId,
Math.floor(40000 / 170)
)
done()
}
)
})
it('should allow 200 requests when collaborators is -1', function (done) {
it('should allow 200 requests when collaborators is -1', async function () {
this.LimitationsManager.promises.allowedNumberOfCollaboratorsForUser
.withArgs(this.currentUserId)
.resolves(-1)
this.CollaboratorsInviteController._checkRateLimit(
await this.CollaboratorsInviteController._checkRateLimit(
this.currentUserId
)
expect(this.rateLimiter.consume).to.have.been.calledWith(
this.currentUserId,
(err, result) => {
if (err) {
return done(err)
}
expect(this.rateLimiter.consume).to.have.been.calledWith(
this.currentUserId,
Math.floor(40000 / 200)
)
done()
}
Math.floor(40000 / 200)
)
})
it('should allow 10 requests when user has no collaborators set', function (done) {
it('should allow 10 requests when user has no collaborators set', async function () {
this.LimitationsManager.promises.allowedNumberOfCollaboratorsForUser
.withArgs(this.currentUserId)
.resolves(null)
this.CollaboratorsInviteController._checkRateLimit(
await this.CollaboratorsInviteController._checkRateLimit(
this.currentUserId
)
expect(this.rateLimiter.consume).to.have.been.calledWith(
this.currentUserId,
(err, result) => {
if (err) {
return done(err)
}
expect(this.rateLimiter.consume).to.have.been.calledWith(
this.currentUserId,
Math.floor(40000 / 10)
)
done()
}
Math.floor(40000 / 10)
)
})
})
@@ -38,6 +38,7 @@ describe('DocumentUpdaterController', function () {
}
this.lines = ['test', '', 'testing']
this.res = new MockResponse()
this.next = sinon.stub()
this.doc = { name: 'myfile.tex' }
})
@@ -53,8 +54,7 @@ describe('DocumentUpdaterController', function () {
})
it('should call the document updater handler with the project_id and doc_id', async function () {
await this.controller.promises.getDoc(this.req, this.res)
await this.controller.getDoc(this.req, this.res, this.next)
expect(
this.DocumentUpdaterHandler.promises.getDocument
).to.have.been.calledOnceWith(
@@ -65,13 +65,14 @@ describe('DocumentUpdaterController', function () {
})
it('should return the content', async function () {
await this.controller.promises.getDoc(this.req, this.res)
await this.controller.getDoc(this.req, this.res)
expect(this.next).to.not.have.been.called
expect(this.res.statusCode).to.equal(200)
expect(this.res.body).to.equal('test\n\ntesting')
})
it('should find the doc in the project', async function () {
await this.controller.promises.getDoc(this.req, this.res)
await this.controller.getDoc(this.req, this.res)
expect(
this.ProjectLocator.promises.findElement
).to.have.been.calledOnceWith({
@@ -82,7 +83,7 @@ describe('DocumentUpdaterController', function () {
})
it('should set the Content-Disposition header', async function () {
await this.controller.promises.getDoc(this.req, this.res)
await this.controller.getDoc(this.req, this.res)
expect(this.res.setContentDisposition).to.have.been.calledWith(
'attachment',
{ filename: this.doc.name }
@@ -916,7 +916,7 @@ describe('UserController', function () {
describe('ensureAffiliationMiddleware', function () {
describe('without affiliations feature', function () {
beforeEach(async function () {
await this.UserController.promises.ensureAffiliationMiddleware(
await this.UserController.ensureAffiliationMiddleware(
this.req,
this.res,
this.next
@@ -938,7 +938,7 @@ describe('UserController', function () {
describe('without ensureAffiliation query parameter', function () {
beforeEach(async function () {
this.Features.hasFeature.withArgs('affiliations').returns(true)
await this.UserController.promises.ensureAffiliationMiddleware(
await this.UserController.ensureAffiliationMiddleware(
this.req,
this.res,
this.next
@@ -968,7 +968,7 @@ describe('UserController', function () {
]
this.Features.hasFeature.withArgs('affiliations').returns(true)
this.req.query.ensureAffiliation = true
await this.UserController.promises.ensureAffiliationMiddleware(
await this.UserController.ensureAffiliationMiddleware(
this.req,
this.res,
this.next
@@ -1005,7 +1005,7 @@ describe('UserController', function () {
this.Features.hasFeature.withArgs('affiliations').returns(true)
this.req.query.ensureAffiliation = true
this.req.assertPermission = sinon.stub()
await this.UserController.promises.ensureAffiliationMiddleware(
await this.UserController.ensureAffiliationMiddleware(
this.req,
this.res,
this.next
@@ -1047,7 +1047,7 @@ describe('UserController', function () {
this.Features.hasFeature.withArgs('affiliations').returns(true)
this.req.query.ensureAffiliation = true
this.req.assertPermission = sinon.stub()
await this.UserController.promises.ensureAffiliationMiddleware(
await this.UserController.ensureAffiliationMiddleware(
this.req,
this.res,
this.next
@@ -1089,7 +1089,7 @@ describe('UserController', function () {
this.Features.hasFeature.withArgs('affiliations').returns(true)
this.req.query.ensureAffiliation = true
this.req.assertPermission = sinon.stub()
await this.UserController.promises.ensureAffiliationMiddleware(
await this.UserController.ensureAffiliationMiddleware(
this.req,
this.res,
this.next