From 3f1a93004600c0710ccd55b650e33facd2583153 Mon Sep 17 00:00:00 2001 From: Kristina <7614497+khjrtbrg@users.noreply.github.com> Date: Wed, 2 Jul 2025 14:42:17 +0200 Subject: [PATCH] [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 --- .../Subscription/SubscriptionController.js | 22 ++++----- .../SubscriptionControllerTests.js | 47 ++++++++++++------- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/services/web/app/src/Features/Subscription/SubscriptionController.js b/services/web/app/src/Features/Subscription/SubscriptionController.js index 5856682166..4e3ad8be27 100644 --- a/services/web/app/src/Features/Subscription/SubscriptionController.js +++ b/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) + } } /** diff --git a/services/web/test/unit/src/Subscription/SubscriptionControllerTests.js b/services/web/test/unit/src/Subscription/SubscriptionControllerTests.js index 087df52815..e0b61417f1 100644 --- a/services/web/test/unit/src/Subscription/SubscriptionControllerTests.js +++ b/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) }) })