Przeglądaj źródła

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

* Promisify `ensureAffiliationMiddleware`

* In `ensureAffiliationMiddleware`, throw when UserNotFoundError

* Unnest object `_InstitutionHubsController`

* Format fix

GitOrigin-RevId: 5b3c6c24724520353540b8d8dd05005b6fa749ff
Antoine Clausse 1 rok temu
rodzic
commit
c8be2e25cf

+ 1 - 1
services/web/app/src/Features/User/UserController.js

@@ -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 {

+ 17 - 0
services/web/test/unit/src/User/UserControllerTests.js

@@ -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))
+      })
+    })
   })
 })