Просмотр исходного кода

[web] make SubscriptionController.cancelSubscription return a status (#26734)

* [web] make SubscriptionController.cancelSubscription return a status
* [web] update acceptance test to match cancel subscription behavior

GitOrigin-RevId: 507809dcb7fa645c2a69e38cdf4a9e3f736622e1
Kristina 1 год назад
Родитель
Сommit
3f1a930046

+ 10 - 12
services/web/app/src/Features/Subscription/SubscriptionController.js

@@ -304,20 +304,18 @@ async function resumeSubscription(req, res, next) {
   }
 }
 
-function cancelSubscription(req, res, next) {
+async function cancelSubscription(req, res, next) {
   const user = SessionManager.getSessionUser(req.session)
   logger.debug({ userId: user._id }, 'canceling subscription')
-  SubscriptionHandler.cancelSubscription(user, function (err) {
-    if (err) {
-      OError.tag(err, 'something went wrong canceling subscription', {
-        user_id: user._id,
-      })
-      return next(err)
-    }
-    // Note: this redirect isn't used in the main flow as the redirection is
-    // handled by Angular
-    res.redirect('/user/subscription/canceled')
-  })
+  try {
+    await SubscriptionHandler.promises.cancelSubscription(user)
+    return res.sendStatus(200)
+  } catch (err) {
+    OError.tag(err, 'something went wrong canceling subscription', {
+      user_id: user._id,
+    })
+    return next(err)
+  }
 }
 
 /**

+ 29 - 18
services/web/test/unit/src/Subscription/SubscriptionControllerTests.js

@@ -487,28 +487,39 @@ describe('SubscriptionController', function () {
   })
 
   describe('cancelSubscription', function () {
-    beforeEach(function (done) {
-      this.res = {
-        redirect() {
-          done()
-        },
-      }
-      sinon.spy(this.res, 'redirect')
-      this.SubscriptionController.cancelSubscription(this.req, this.res)
-    })
-
-    it('should tell the handler to cancel this user', function (done) {
-      this.SubscriptionHandler.cancelSubscription
+    it('should tell the handler to cancel this user', async function () {
+      this.next = sinon.stub()
+      await this.SubscriptionController.cancelSubscription(
+        this.req,
+        this.res,
+        this.next
+      )
+      this.SubscriptionHandler.promises.cancelSubscription
         .calledWith(this.user)
         .should.equal(true)
-      done()
     })
 
-    it('should redurect to the subscription page', function (done) {
-      this.res.redirect
-        .calledWith('/user/subscription/canceled')
-        .should.equal(true)
-      done()
+    it('should return a 200 on success', async function () {
+      this.next = sinon.stub()
+      await this.SubscriptionController.cancelSubscription(
+        this.req,
+        this.res,
+        this.next
+      )
+      expect(this.res.statusCode).to.equal(200)
+    })
+
+    it('should call next with error', async function () {
+      this.SubscriptionHandler.promises.cancelSubscription.rejects(
+        new Error('cancel error')
+      )
+      this.next = sinon.stub()
+      await this.SubscriptionController.cancelSubscription(
+        this.req,
+        this.res,
+        this.next
+      )
+      this.next.calledWith(sinon.match.instanceOf(Error)).should.equal(true)
     })
   })