[web] Promisify ensureAffiliationMiddleware and refactor InstitutionHubsController (#22242 feedback) (#22261)

* Promisify `ensureAffiliationMiddleware`

* In `ensureAffiliationMiddleware`, throw when UserNotFoundError

* Unnest object `_InstitutionHubsController`

* Format fix

GitOrigin-RevId: 5b3c6c24724520353540b8d8dd05005b6fa749ff
This commit is contained in:
Antoine Clausse
2025-01-20 11:04:15 +01:00
committed by Copybot
parent 01032daa34
commit 01fb8ba69f
2 changed files with 18 additions and 1 deletions

View File

@@ -202,7 +202,7 @@ async function ensureAffiliationMiddleware(req, res, next) {
try {
user = await UserGetter.promises.getUser(userId)
} catch (error) {
return new Errors.UserNotFoundError({ info: { userId } })
throw new Errors.UserNotFoundError({ info: { userId } })
}
// if the user does not have permission to add an affiliation, we skip this middleware
try {

View File

@@ -1106,5 +1106,22 @@ describe('UserController', function () {
expect(this.next).to.be.calledWith(sinon.match.instanceOf(Error))
})
})
describe('when user is not found', function () {
beforeEach(async function () {
this.UserGetter.promises.getUser.rejects(new Error('not found'))
this.Features.hasFeature.withArgs('affiliations').returns(true)
this.req.query.ensureAffiliation = true
await this.UserController.ensureAffiliationMiddleware(
this.req,
this.res,
this.next
)
})
it('should return the error', function () {
expect(this.next).to.be.calledWith(sinon.match.instanceOf(Error))
})
})
})
})