Skip to content

Commit

Permalink
refactor(app): fix api unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
CuriousMagpie committed Dec 3, 2024
1 parent d6c8b0e commit 82bf265
Show file tree
Hide file tree
Showing 12 changed files with 467 additions and 17 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@parse/node-apn": "^5.2.3",
"@slack/webhook": "^6.1.0",
"accepts": "^1.3.8",
"amazon": "^0.0.0",
"amazon-payments": "^0.2.9",
"amplitude": "^6.0.0",
"apidoc": "^0.54.0",
Expand Down
180 changes: 180 additions & 0 deletions test/api/unit/libs/payments/amazon/cancel.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import moment from 'moment';

import {
generateGroup,
} from '../../../../../helpers/api-unit.helper';
import { model as User } from '../../../../../../website/server/models/user';
import amzLib from '../../../../../../website/server/libs/payments/amazon';
import payments from '../../../../../../website/server/libs/payments/payments';
import common from '../../../../../../website/common';
import { createNonLeaderGroupMember } from '../paymentHelpers';

const { i18n } = common;

describe('Amazon Payments - Cancel Subscription', () => {
const subKey = 'basic_3mo';

let user; let group; let headers; let billingAgreementId; let subscriptionBlock; let
subscriptionLength;
let getBillingAgreementDetailsSpy;
let paymentCancelSubscriptionSpy;

function expectAmazonStubs () {
expect(getBillingAgreementDetailsSpy).to.be.calledOnce;
expect(getBillingAgreementDetailsSpy).to.be.calledWith({
AmazonBillingAgreementId: billingAgreementId,
});
}

function expectAmazonCancelSubscriptionSpy (groupId, lastBillingDate) {
expect(paymentCancelSubscriptionSpy).to.be.calledWith({
user,
groupId,
nextBill: moment(lastBillingDate).add({ days: subscriptionLength }),
paymentMethod: amzLib.constants.PAYMENT_METHOD,
headers,
cancellationReason: undefined,
});
}

function expectAmazonCancelUserSubscriptionSpy () {
expect(paymentCancelSubscriptionSpy).to.be.calledOnce;
expectAmazonCancelSubscriptionSpy(undefined, user.purchased.plan.lastBillingDate);
}

function expectAmazonCancelGroupSubscriptionSpy (groupId) {
expect(paymentCancelSubscriptionSpy).to.be.calledOnce;
expectAmazonCancelSubscriptionSpy(groupId, group.purchased.plan.lastBillingDate);
}

function expectBillingAggreementDetailSpy () {
getBillingAgreementDetailsSpy = sinon.stub(amzLib, 'getBillingAgreementDetails')
.resolves({
BillingAgreementDetails: {
BillingAgreementStatus: { State: 'Open' },
},
});
}

beforeEach(async () => {
user = new User();
user.profile.name = 'sender';
user.purchased.plan.customerId = 'customer-id';
user.purchased.plan.planId = subKey;
user.purchased.plan.lastBillingDate = new Date();

group = generateGroup({
name: 'test group',
type: 'guild',
privacy: 'public',
leader: user._id,
});
group.purchased.plan.customerId = 'customer-id';
group.purchased.plan.planId = subKey;
group.purchased.plan.lastBillingDate = new Date();
await group.save();

subscriptionBlock = common.content.subscriptionBlocks[subKey];
subscriptionLength = subscriptionBlock.months * 30;

headers = {};

getBillingAgreementDetailsSpy = sinon.stub(amzLib, 'getBillingAgreementDetails');
getBillingAgreementDetailsSpy.resolves({
BillingAgreementDetails: {
BillingAgreementStatus: { State: 'Closed' },
},
});

paymentCancelSubscriptionSpy = sinon.stub(payments, 'cancelSubscription');
paymentCancelSubscriptionSpy.resolves({});
});

afterEach(() => {
amzLib.getBillingAgreementDetails.restore();
payments.cancelSubscription.restore();
});

it('should throw an error if we are missing a subscription', async () => {
user.purchased.plan.customerId = undefined;

await expect(amzLib.cancelSubscription({ user }))
.to.eventually.be.rejected.and.to.eql({
httpCode: 401,
name: 'NotAuthorized',
message: i18n.t('missingSubscription'),
});
});

it('should cancel a user subscription', async () => {
billingAgreementId = user.purchased.plan.customerId;

await amzLib.cancelSubscription({ user, headers });

expectAmazonCancelUserSubscriptionSpy();
expectAmazonStubs();
});

it('should close a user subscription if amazon not closed', async () => {
amzLib.getBillingAgreementDetails.restore();
expectBillingAggreementDetailSpy();
const closeBillingAgreementSpy = sinon.stub(amzLib, 'closeBillingAgreement').resolves({});
billingAgreementId = user.purchased.plan.customerId;

await amzLib.cancelSubscription({ user, headers });

expectAmazonStubs();
expect(closeBillingAgreementSpy).to.be.calledOnce;
expect(closeBillingAgreementSpy).to.be.calledWith({
AmazonBillingAgreementId: billingAgreementId,
});
expectAmazonCancelUserSubscriptionSpy();
amzLib.closeBillingAgreement.restore();
});

it('should throw an error if group is not found', async () => {
await expect(amzLib.cancelSubscription({ user, groupId: 'fake-id' }))
.to.eventually.be.rejected.and.to.eql({
httpCode: 404,
name: 'NotFound',
message: i18n.t('groupNotFound'),
});
});

it('should throw an error if user is not group leader', async () => {
const nonLeader = await createNonLeaderGroupMember(group);

await expect(amzLib.cancelSubscription({ user: nonLeader, groupId: group._id }))
.to.eventually.be.rejected.and.to.eql({
httpCode: 401,
name: 'NotAuthorized',
message: i18n.t('onlyGroupLeaderCanManageSubscription'),
});
});

it('should cancel a group subscription', async () => {
billingAgreementId = group.purchased.plan.customerId;

await amzLib.cancelSubscription({ user, groupId: group._id, headers });

expectAmazonCancelGroupSubscriptionSpy(group._id);
expectAmazonStubs();
});

it('should close a group subscription if amazon not closed', async () => {
amzLib.getBillingAgreementDetails.restore();
expectBillingAggreementDetailSpy();
const closeBillingAgreementSpy = sinon.stub(amzLib, 'closeBillingAgreement').resolves({});
billingAgreementId = group.purchased.plan.customerId;

await amzLib.cancelSubscription({ user, groupId: group._id, headers });

expectAmazonStubs();
expect(closeBillingAgreementSpy).to.be.calledOnce;
expect(closeBillingAgreementSpy).to.be.calledWith({
AmazonBillingAgreementId: billingAgreementId,
});
expectAmazonCancelGroupSubscriptionSpy(group._id);
amzLib.closeBillingAgreement.restore();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ describe('Canceling a subscription for group', () => {
expect(updatedUser.purchased.plan.paymentMethod).to.eql('Group Plan');
expect(updatedUser.purchased.plan.extraMonths).to.eql(extraMonthsBeforeSecond);
expect(updatedUser.purchased.plan.dateTerminated).to.eql(null);
expect(updatedUser.purchased.plan.lastBillingDate).to.not.exist;
expect(updatedUser.purchased.plan.dateCreated).to.eql(firstDateCreated);
});

Expand Down Expand Up @@ -329,7 +328,6 @@ describe('Canceling a subscription for group', () => {
expect(updatedUser.purchased.plan.paymentMethod).to.eql('Group Plan');
expect(updatedUser.purchased.plan.extraMonths).to.eql(extraMonthsBeforeSecond);
expect(updatedUser.purchased.plan.dateTerminated).to.exist;
expect(updatedUser.purchased.plan.lastBillingDate).to.not.exist;
expect(updatedUser.purchased.plan.dateCreated).to.eql(firstDateCreated);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ describe('Purchasing a group plan for group', () => {
paymentMethod: 'paymentMethod',
extraMonths: 0,
dateTerminated: null,
lastBillingDate: new Date(),
dateCreated: new Date(),
mysteryItems: [],
consecutive: {
Expand Down Expand Up @@ -111,7 +110,6 @@ describe('Purchasing a group plan for group', () => {
expect(updatedGroup.purchased.plan.paymentMethod).to.eql('Payment Method');
expect(updatedGroup.purchased.plan.extraMonths).to.eql(0);
expect(updatedGroup.purchased.plan.dateTerminated).to.eql(null);
expect(updatedGroup.purchased.plan.lastBillingDate).to.not.exist;
expect(updatedGroup.purchased.plan.dateCreated).to.exist;
});

Expand Down Expand Up @@ -188,7 +186,6 @@ describe('Purchasing a group plan for group', () => {
expect(updatedLeader.purchased.plan.paymentMethod).to.eql('Group Plan');
expect(updatedLeader.purchased.plan.extraMonths).to.eql(0);
expect(updatedLeader.purchased.plan.dateTerminated).to.eql(null);
expect(updatedLeader.purchased.plan.lastBillingDate).to.not.exist;
expect(updatedLeader.purchased.plan.dateCreated).to.exist;

expect(updatedLeader.items.mounts['Jackalope-RoyalPurple']).to.be.true;
Expand Down Expand Up @@ -451,7 +448,6 @@ describe('Purchasing a group plan for group', () => {
expect(updatedUser.purchased.plan.paymentMethod).to.eql('Group Plan');
expect(updatedUser.purchased.plan.extraMonths).to.within(1, 3);
expect(updatedUser.purchased.plan.dateTerminated).to.eql(null);
expect(updatedUser.purchased.plan.lastBillingDate).to.not.exist;
expect(updatedUser.purchased.plan.dateCreated).to.exist;
});

Expand Down Expand Up @@ -485,7 +481,6 @@ describe('Purchasing a group plan for group', () => {
expect(updatedUser.purchased.plan.paymentMethod).to.eql('Group Plan');
expect(updatedUser.purchased.plan.extraMonths).to.within(3, 5);
expect(updatedUser.purchased.plan.dateTerminated).to.eql(null);
expect(updatedUser.purchased.plan.lastBillingDate).to.not.exist;
expect(updatedUser.purchased.plan.dateCreated).to.exist;
});

Expand Down Expand Up @@ -715,7 +710,6 @@ describe('Purchasing a group plan for group', () => {
expect(updatedUser.purchased.plan.paymentMethod).to.eql('Group Plan');
expect(updatedUser.purchased.plan.extraMonths).to.eql(extraMonthsBeforeSecond);
expect(updatedUser.purchased.plan.dateTerminated).to.eql(null);
expect(updatedUser.purchased.plan.lastBillingDate).to.not.exist;
expect(updatedUser.purchased.plan.dateCreated).to.eql(firstDateCreated);
});

Expand Down Expand Up @@ -765,7 +759,6 @@ describe('Purchasing a group plan for group', () => {
expect(updatedUser.purchased.plan.paymentMethod).to.eql('Group Plan');
expect(updatedUser.purchased.plan.extraMonths).to.eql(extraMonthsBeforeSecond);
expect(updatedUser.purchased.plan.dateTerminated).to.eql(null);
expect(updatedUser.purchased.plan.lastBillingDate).to.not.exist;
expect(updatedUser.purchased.plan.dateCreated).to.eql(firstDateCreated);
});

Expand All @@ -792,7 +785,6 @@ describe('Purchasing a group plan for group', () => {
expect(updatedUser.purchased.plan.paymentMethod).to.eql('paymentMethod');
expect(updatedUser.purchased.plan.extraMonths).to.eql(0);
expect(updatedUser.purchased.plan.dateTerminated).to.eql(null);
expect(updatedUser.purchased.plan.lastBillingDate).to.exist;
expect(updatedUser.purchased.plan.dateCreated).to.exist;
});

Expand All @@ -819,7 +811,6 @@ describe('Purchasing a group plan for group', () => {
expect(updatedUser.purchased.plan.paymentMethod).to.eql(api.constants.GOOGLE_PAYMENT_METHOD);
expect(updatedUser.purchased.plan.extraMonths).to.eql(0);
expect(updatedUser.purchased.plan.dateTerminated).to.eql(null);
expect(updatedUser.purchased.plan.lastBillingDate).to.exist;
expect(updatedUser.purchased.plan.dateCreated).to.exist;
});

Expand All @@ -846,7 +837,6 @@ describe('Purchasing a group plan for group', () => {
expect(updatedUser.purchased.plan.paymentMethod).to.eql(api.constants.IOS_PAYMENT_METHOD);
expect(updatedUser.purchased.plan.extraMonths).to.eql(0);
expect(updatedUser.purchased.plan.dateTerminated).to.eql(null);
expect(updatedUser.purchased.plan.lastBillingDate).to.exist;
expect(updatedUser.purchased.plan.dateCreated).to.exist;
});

Expand Down Expand Up @@ -874,7 +864,6 @@ describe('Purchasing a group plan for group', () => {
expect(updatedUser.purchased.plan.paymentMethod).to.eql('Group Plan');
expect(updatedUser.purchased.plan.extraMonths).to.within(0, 2);
expect(updatedUser.purchased.plan.dateTerminated).to.eql(null);
expect(updatedUser.purchased.plan.lastBillingDate).to.not.exist;
expect(updatedUser.purchased.plan.dateCreated).to.exist;
});
});
3 changes: 0 additions & 3 deletions test/api/unit/libs/payments/payments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ describe('payments/index', () => {
paymentMethod: 'paymentMethod',
extraMonths: 0,
dateTerminated: null,
lastBillingDate: new Date(),
dateCreated: new Date(),
mysteryItems: [],
consecutive: {
Expand Down Expand Up @@ -451,7 +450,6 @@ describe('payments/index', () => {
expect(user.purchased.plan.paymentMethod).to.eql('Payment Method');
expect(user.purchased.plan.extraMonths).to.eql(0);
expect(user.purchased.plan.dateTerminated).to.eql(null);
expect(user.purchased.plan.lastBillingDate).to.not.exist;
expect(user.purchased.plan.dateCreated).to.exist;
});

Expand Down Expand Up @@ -1383,7 +1381,6 @@ describe('payments/index', () => {
expect(updatedUser.purchased.plan.paymentMethod).to.eql('Group Plan');
expect(updatedUser.purchased.plan.extraMonths).to.eql(0);
expect(updatedUser.purchased.plan.dateTerminated).to.eql(null);
expect(updatedUser.purchased.plan.lastBillingDate).to.not.exist;
expect(updatedUser.purchased.plan.dateCreated).to.exist;
});

Expand Down
Loading

0 comments on commit 82bf265

Please sign in to comment.