From c6d07983b2a91ba695f6621c55b9882d3551a9f4 Mon Sep 17 00:00:00 2001 From: Phillip Thelen Date: Tue, 22 Feb 2022 16:15:05 +0100 Subject: [PATCH 1/4] Fix issue with validating android sub cancellation --- test/api/unit/libs/payments/google.test.js | 22 +++++++++++++++++++++- website/server/libs/payments/google.js | 2 +- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/test/api/unit/libs/payments/google.test.js b/test/api/unit/libs/payments/google.test.js index 266abf04b3..33aabc8cfa 100644 --- a/test/api/unit/libs/payments/google.test.js +++ b/test/api/unit/libs/payments/google.test.js @@ -256,7 +256,7 @@ describe('Google Payments', () => { expirationDate, }); iapGetPurchaseDataStub = sinon.stub(iap, 'getPurchaseData') - .returns([{ expirationDate: expirationDate.toDate() }]); + .returns([{ expirationDate: expirationDate.toDate(), autoRenewig: false }]); iapIsValidatedStub = sinon.stub(iap, 'isValidated') .returns(true); @@ -325,5 +325,25 @@ describe('Google Payments', () => { headers, }); }); + + it('should not cancel a user subscription with autorenew', async () => { + iapGetPurchaseDataStub = sinon.stub(iap, 'getPurchaseData') + .returns([{ autoRenewig: true }]); + await googlePayments.cancelSubscribe(user, headers); + + expect(iapSetupStub).to.be.calledOnce; + expect(iapValidateStub).to.be.calledOnce; + expect(iapValidateStub).to.be.calledWith(iap.GOOGLE, { + data: receipt, + signature, + }); + expect(iapIsValidatedStub).to.be.calledOnce; + expect(iapIsValidatedStub).to.be.calledWith({ + expirationDate, + }); + expect(iapGetPurchaseDataStub).to.be.calledOnce; + + expect(paymentCancelSubscriptionSpy).to.not.be.called; + }); }); }); diff --git a/website/server/libs/payments/google.js b/website/server/libs/payments/google.js index b6c734cf55..95057dad41 100644 --- a/website/server/libs/payments/google.js +++ b/website/server/libs/payments/google.js @@ -246,7 +246,7 @@ api.cancelSubscribe = async function cancelSubscribe (user, headers) { if (purchases.length === 0) throw new NotAuthorized(this.constants.RESPONSE_INVALID_RECEIPT); const subscriptionData = purchases[0]; // Check to make sure the sub isn't active anymore. - if (subscriptionData.autoRenews) return; + if (subscriptionData.autoRenewing === false) return; dateTerminated = new Date(Number(subscriptionData.expirationDate)); } catch (err) { From 7080715bcc8cd8c42ae89a5926abb6edab223c9b Mon Sep 17 00:00:00 2001 From: SabreCat Date: Tue, 22 Feb 2022 10:13:32 -0600 Subject: [PATCH 2/4] fix(tests): renewig typos --- test/api/unit/libs/payments/google.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/api/unit/libs/payments/google.test.js b/test/api/unit/libs/payments/google.test.js index 33aabc8cfa..d912769f24 100644 --- a/test/api/unit/libs/payments/google.test.js +++ b/test/api/unit/libs/payments/google.test.js @@ -256,7 +256,7 @@ describe('Google Payments', () => { expirationDate, }); iapGetPurchaseDataStub = sinon.stub(iap, 'getPurchaseData') - .returns([{ expirationDate: expirationDate.toDate(), autoRenewig: false }]); + .returns([{ expirationDate: expirationDate.toDate(), autoRenewing: false }]); iapIsValidatedStub = sinon.stub(iap, 'isValidated') .returns(true); @@ -328,7 +328,7 @@ describe('Google Payments', () => { it('should not cancel a user subscription with autorenew', async () => { iapGetPurchaseDataStub = sinon.stub(iap, 'getPurchaseData') - .returns([{ autoRenewig: true }]); + .returns([{ autoRenewing: true }]); await googlePayments.cancelSubscribe(user, headers); expect(iapSetupStub).to.be.calledOnce; From 08352c5f498bd870ee75df586a9795a2f6e559d6 Mon Sep 17 00:00:00 2001 From: SabreCat Date: Tue, 22 Feb 2022 11:26:15 -0600 Subject: [PATCH 3/4] fix(subs): correct cancellation check logic and test --- test/api/unit/libs/payments/google.test.js | 1 + website/server/libs/payments/google.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/test/api/unit/libs/payments/google.test.js b/test/api/unit/libs/payments/google.test.js index d912769f24..3cf40b0a4e 100644 --- a/test/api/unit/libs/payments/google.test.js +++ b/test/api/unit/libs/payments/google.test.js @@ -327,6 +327,7 @@ describe('Google Payments', () => { }); it('should not cancel a user subscription with autorenew', async () => { + iap.getPurchaseData.restore(); iapGetPurchaseDataStub = sinon.stub(iap, 'getPurchaseData') .returns([{ autoRenewing: true }]); await googlePayments.cancelSubscribe(user, headers); diff --git a/website/server/libs/payments/google.js b/website/server/libs/payments/google.js index 95057dad41..86f0967213 100644 --- a/website/server/libs/payments/google.js +++ b/website/server/libs/payments/google.js @@ -246,7 +246,7 @@ api.cancelSubscribe = async function cancelSubscribe (user, headers) { if (purchases.length === 0) throw new NotAuthorized(this.constants.RESPONSE_INVALID_RECEIPT); const subscriptionData = purchases[0]; // Check to make sure the sub isn't active anymore. - if (subscriptionData.autoRenewing === false) return; + if (subscriptionData.autoRenewing !== false) return; dateTerminated = new Date(Number(subscriptionData.expirationDate)); } catch (err) { From 4295b5533992f6cf0af93301389071ef9b32eda9 Mon Sep 17 00:00:00 2001 From: SabreCat Date: Tue, 22 Feb 2022 12:18:20 -0600 Subject: [PATCH 4/4] 4.222.2 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6a080b7630..bb88a83a25 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "habitica", - "version": "4.222.1", + "version": "4.222.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 9695f6adfd..424138103a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "habitica", "description": "A habit tracker app which treats your goals like a Role Playing Game.", - "version": "4.222.1", + "version": "4.222.2", "main": "./website/server/index.js", "dependencies": { "@babel/core": "^7.16.12",