mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-07-23 12:00:57 +00:00
commit dd0a410fa6c3741dc0d6793283cf4df3c37790a5
Author: Kalista Payne <sabrecat@gmail.com>
Date: Mon Nov 4 14:24:30 2024 -0600
fix(subs): center next hourglass message
commit 72d92ffd76bb43fee8ba2bbabd211e595afbd664
Author: Kalista Payne <sabrecat@gmail.com>
Date: Fri Nov 1 14:17:59 2024 -0500
fix(subs): don't hide HG preview entirely
commit ea0ecb0c3d519ed3d5c42266367eaaa7283ac5de
Author: Kalista Payne <sabrecat@gmail.com>
Date: Fri Nov 1 13:01:06 2024 -0500
fix(subs): Google wording and HG escape
commit 2bd2c69e18e37c8c8c7106c62f186c372d25c5d2
Author: Kalista Payne <sabrecat@gmail.com>
Date: Fri Nov 1 09:25:30 2024 -0500
fix(layout): tighten cancellation note
commit eb2fc40d241b18d4ffff04c35e744f05e6e9ff52
Author: Kalista Payne <sabrecat@gmail.com>
Date: Thu Oct 24 15:41:43 2024 -0500
fix(g1g1): don't try to find Gems promo during bogo
commit d3eea86bd773c5236e8a0f619639e49db846c2ba
Author: Kalista Payne <sabrecat@gmail.com>
Date: Thu Oct 24 15:00:09 2024 -0500
fix(subs): fix typeError
commit e3ae9a2d6736f238c6aaaec37a5bf38d64afafe8
Author: Kalista Payne <sabrecat@gmail.com>
Date: Thu Oct 24 13:57:27 2024 -0500
fix(subs): also redirect to subs after gift sub
commit 690163a0dec3a45329062905c90454c7cd7c83fd
Author: Phillip Thelen <phillip@habitica.com>
Date: Wed Oct 23 16:42:38 2024 +0200
fix test
commit 2ad7541fc0de429c152e6824f65d2b11b84a9809
Author: Phillip Thelen <phillip@habitica.com>
Date: Wed Oct 23 16:34:52 2024 +0200
fix test
commit 7e337a9e591f2e8b27684567290a70f1b2d58aa0
Author: Phillip Thelen <phillip@habitica.com>
Date: Wed Oct 23 11:54:15 2024 +0200
remove only
commit 7462b8a57f852ecfc52e74fb50d6cff1751bef74
Author: Phillip Thelen <phillip@habitica.com>
Date: Wed Oct 23 11:51:25 2024 +0200
fix bug with incorrectly giving HG bonus
commit acd6183e95a5783dfa29e6c2b142f965c3c67411
Author: Kalista Payne <sabrecat@gmail.com>
Date: Mon Oct 21 17:22:26 2024 -0500
fix(subs): unhovery and un-12-monthy
commit 935e9fd6ec2688ac7339c56ce0ff03bfdae30c77
Author: Kalista Payne <sabrecat@gmail.com>
Date: Fri Oct 18 14:50:17 2024 -0500
fix(subs): try again on gifts
commit 6e1fb7df38d90e5c3ccebee9bb86dbb8f8a4678f
Author: Kalista Payne <sabrecat@gmail.com>
Date: Thu Oct 17 18:19:20 2024 -0500
fix(lint): do negate object ig
commit 71d434b94ea3b1a2c9381fd70f2e637473e00cac
Author: Kalista Payne <sabrecat@gmail.com>
Date: Thu Oct 17 18:15:11 2024 -0500
fix(lint): unnecessary ternary
commit b90b0bb9c39b931714526a9d20910968b055038d
Author: Kalista Payne <sabrecat@gmail.com>
Date: Thu Oct 17 17:34:24 2024 -0500
fix(subs): gifts DON't renew
commit 19469304c5a5881329ea1682e2070f9666d49ee4
Author: Kalista Payne <sabrecat@gmail.com>
Date: Thu Oct 17 17:13:29 2024 -0500
fix(subs): pass autoRenews through Stripe
commit 6819e7b7e518969c58ebab4400f3147f0ddea1b3
Author: Kalista Payne <sabrecat@gmail.com>
Date: Thu Oct 17 16:03:25 2024 -0500
fix(subscriptions): minor visual updates
commit 74633b5e5ea71d66681ad0e84873f3080ab5d361
Author: Kalista Payne <sabrecat@gmail.com>
Date: Wed Oct 16 17:27:09 2024 -0500
fix(subscriptions): more gift layout revisions
commit a90ccb89de36a85acc214bb0b88479e0b78f1660
Author: Kalista Payne <sabrecat@gmail.com>
Date: Wed Oct 16 15:37:50 2024 -0500
fix(subscription): update layout when gifting
commit c24b2db8dc6642669068f0a79d9b0990d43decb9
Author: Phillip Thelen <phillip@habitica.com>
Date: Mon Oct 14 16:11:46 2024 +0200
fix issue with promo hourglasses
commit 7a61c72b47cd3403fe0f3edf91522277738068cc
Author: Phillip Thelen <phillip@habitica.com>
Date: Mon Oct 14 15:59:40 2024 +0200
don’t give additional HG for new sub if they already got one this month
commit f14cb090265ed830eb76c7f452e806257312370e
Author: Phillip Thelen <phillip@habitica.com>
Date: Mon Oct 14 10:38:01 2024 +0200
Admin panel display fixes
commit f4cff698cfb80f9ad2da7ecb626f84277f97eb7c
Author: Kalista Payne <sabrecat@gmail.com>
Date: Thu Oct 3 17:58:59 2024 -0500
fix(stripe): correct redirect after success
commit c468b58f3f783c58e9b48f9698b45473b526d3d4
Author: Kalista Payne <sabrecat@gmail.com>
Date: Thu Oct 3 17:35:37 2024 -0500
fix(subs): correct border-radius and redirect
commit 78fb9e31d64f25aa091e24f95f25dc6dedc844a6
Author: Kalista Payne <sabrecat@gmail.com>
Date: Wed Oct 2 17:41:49 2024 -0500
fix(css): correct and refactor heights and selection states
commit e2babe8053a778b64d51bd3d18866e69fb326a3c
Author: Kalista Payne <sabrecat@gmail.com>
Date: Mon Sep 30 16:45:29 2024 -0500
feat(subscription): max Gems progress readout
commit 61af8302a349f70d60886492b3d4f05dd5463a51
Author: Phillip Thelen <phillip@habitica.com>
Date: Fri Sep 27 15:11:22 2024 +0200
fix test
commit ef8ff0ea9eebcbd682a34fd7f52722b92fdfae16
Author: Phillip Thelen <phillip@habitica.com>
Date: Fri Sep 27 14:14:44 2024 +0200
show date for hourglass bonus if it was received
commit 4bafafdc8d493aad960dcf0d4957d3dad2d5e8da
Author: Phillip Thelen <phillip@habitica.com>
Date: Fri Sep 27 14:12:52 2024 +0200
add new field for cumulative subscription count
commit 30096247b73bdb76aa5b10dd4c964a78d2511e69
Author: Phillip Thelen <phillip@habitica.com>
Date: Fri Sep 27 13:39:49 2024 +0200
fix missing transaction type
commit 70872651b09613a8fe1a19ee2e19dac398b3134d
Author: Phillip Thelen <phillip@habitica.com>
Date: Fri Sep 27 13:31:40 2024 +0200
fix admin panel strings
commit f3398db65f26db558f38ecce8fe4795ff73650cb
Author: Kalista Payne <sabrecat@gmail.com>
Date: Thu Sep 26 23:11:16 2024 -0500
WIP(subs): extant Stripe state
commit c6b2020109b2cdbc7dd8579c884c65f81e757c25
Author: Phillip Thelen <phillip@habitica.com>
Date: Thu Sep 26 11:41:55 2024 +0200
fix admin panel display
commit d9afc96d2db8021db7e6310a009c15004ccc5c38
Author: Phillip Thelen <phillip@habitica.com>
Date: Thu Sep 26 11:40:16 2024 +0200
Fix hourglass logic for upgrades
commit 6e2c8eeb649481afc349e6eb7741bcc82909c3c4
Author: Phillip Thelen <phillip@habitica.com>
Date: Wed Sep 25 17:48:54 2024 +0200
fix hourglass count
commit cd752fbdce79f24bbdbaf6fd9558f207754c5cc3
Author: Kalista Payne <sabrecat@gmail.com>
Date: Fri Sep 20 12:24:21 2024 -0500
WIP(frontend): draft of main subs page view
commit 0102b29d599e47192d7346180ecd549c79177156
Author: Kalista Payne <sabe@habitica.com>
Date: Wed Sep 18 15:29:08 2024 -0500
fix(admin): correct logic and style for shrimple subs
commit 5469a5c5c3fddcf611018c1de077de3499df787a
Author: Kalista Payne <sabe@habitica.com>
Date: Wed Sep 18 15:07:36 2024 -0500
fix(test): short circuit this.
commit 526193ee6c9d07915d0373d07bb8ee0554fe2614
Author: Phillip Thelen <phillip@habitica.com>
Date: Wed Sep 18 14:42:06 2024 +0200
fix gem limit
commit 19cf1636aa1371147ea92478485a653d612d9755
Author: Phillip Thelen <phillip@habitica.com>
Date: Tue Aug 13 17:00:40 2024 +0200
return nextHourglassDate again
commit eea36e3ed54633c345d628d1d3d08e03a3e416a3
Author: Phillip Thelen <phillip@habitica.com>
Date: Tue Aug 13 13:11:22 2024 +0200
subscription test improvements
commit ca78e7433031e79c61aba67235481e0b1c569a55
Author: Phillip Thelen <phillip@habitica.com>
Date: Mon Aug 12 15:46:15 2024 +0200
add more subscription tests
commit f4c4f93a081a89d4c79aec1e87dac97d90c1d587
Author: Phillip Thelen <phillip@habitica.com>
Date: Fri Aug 9 13:35:22 2024 +0200
finish basic implementation of new logic
commit e036742048b92c2e2f29724fb02462f117d91aea
Author: Phillip Thelen <phillip@habitica.com>
Date: Fri Aug 9 11:37:44 2024 +0200
cleanup
commit 643186568866ddea0a234b68d37ad4ab634bd147
Author: Phillip Thelen <phillip@habitica.com>
Date: Wed Aug 7 05:41:18 2024 -0400
update cron tests
commit 930d875ae9d518b0b504ec97638e94c7296ad388
Author: Phillip Thelen <phillip@habitica.com>
Date: Thu Aug 8 10:36:50 2024 +0200
begin refactoring
commit 96623608d064b94cfa40e5da736f13c696995df9
Author: Phillip Thelen <phillip@habitica.com>
Date: Tue Aug 6 16:28:16 2024 +0200
begin removing obsolete tests
1433 lines
53 KiB
JavaScript
1433 lines
53 KiB
JavaScript
import moment from 'moment';
|
|
|
|
import * as sender from '../../../../../website/server/libs/email';
|
|
import common from '../../../../../website/common';
|
|
import api from '../../../../../website/server/libs/payments/payments';
|
|
import * as analytics from '../../../../../website/server/libs/analyticsService';
|
|
import * as notifications from '../../../../../website/server/libs/pushNotifications';
|
|
import { model as User } from '../../../../../website/server/models/user';
|
|
import { translate as t } from '../../../../helpers/api-integration/v3';
|
|
import {
|
|
generateGroup,
|
|
} from '../../../../helpers/api-unit.helper';
|
|
import * as worldState from '../../../../../website/server/libs/worldState';
|
|
import { TransactionModel } from '../../../../../website/server/models/transaction';
|
|
import { REPEATING_EVENTS } from '../../../../../website/common/script/content/constants/events';
|
|
|
|
describe('payments/index', () => {
|
|
let user;
|
|
let group;
|
|
let data;
|
|
let plan;
|
|
|
|
beforeEach(async () => {
|
|
user = new User();
|
|
user.profile.name = 'sender';
|
|
user.auth.local.username = 'sender';
|
|
await user.save();
|
|
|
|
group = generateGroup({
|
|
name: 'test group',
|
|
type: 'guild',
|
|
privacy: 'public',
|
|
leader: user._id,
|
|
});
|
|
await group.save();
|
|
|
|
sandbox.stub(sender, 'sendTxn');
|
|
sandbox.stub(user, 'sendMessage');
|
|
sandbox.stub(analytics.mockAnalyticsService, 'trackPurchase');
|
|
sandbox.stub(analytics.mockAnalyticsService, 'track');
|
|
sandbox.stub(notifications, 'sendNotification');
|
|
|
|
data = {
|
|
user,
|
|
sub: {
|
|
key: 'basic_3mo',
|
|
},
|
|
customerId: 'customer-id',
|
|
paymentMethod: 'Payment Method',
|
|
headers: {
|
|
'x-client': 'habitica-web',
|
|
'user-agent': '',
|
|
},
|
|
};
|
|
|
|
plan = {
|
|
planId: 'basic_3mo',
|
|
customerId: 'customer-id',
|
|
dateUpdated: new Date(),
|
|
gemsBought: 0,
|
|
paymentMethod: 'paymentMethod',
|
|
extraMonths: 0,
|
|
dateTerminated: null,
|
|
lastBillingDate: new Date(),
|
|
dateCreated: new Date(),
|
|
mysteryItems: [],
|
|
consecutive: {
|
|
trinkets: 0,
|
|
gemCapExtra: 0,
|
|
},
|
|
};
|
|
});
|
|
|
|
afterEach(() => {
|
|
sandbox.restore();
|
|
});
|
|
|
|
describe('#createSubscription', () => {
|
|
context('Purchasing a subscription as a gift', () => {
|
|
let recipient;
|
|
|
|
beforeEach(() => {
|
|
recipient = new User();
|
|
recipient.profile.name = 'recipient';
|
|
data.gift = {
|
|
member: recipient,
|
|
subscription: {
|
|
key: 'basic_3mo',
|
|
months: 3,
|
|
},
|
|
};
|
|
});
|
|
|
|
it('awards the Royal Purple Jackalope pet', async () => {
|
|
await api.createSubscription(data);
|
|
|
|
expect(recipient.items.pets['Jackalope-RoyalPurple']).to.eql(5);
|
|
});
|
|
|
|
it('adds extra months to an existing subscription', async () => {
|
|
recipient.purchased.plan = plan;
|
|
|
|
expect(recipient.purchased.plan.extraMonths).to.eql(0);
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(recipient.purchased.plan.extraMonths).to.eql(3);
|
|
});
|
|
|
|
it('add a transaction entry to the recipient', async () => {
|
|
await api.createSubscription(data);
|
|
|
|
const transactions = await TransactionModel
|
|
.find({ userId: recipient._id })
|
|
.sort({ createdAt: -1 })
|
|
.exec();
|
|
|
|
expect(transactions).to.have.lengthOf(1);
|
|
});
|
|
|
|
it('does not set negative extraMonths if plan has past dateTerminated date', async () => {
|
|
const dateTerminated = moment().subtract(2, 'months').toDate();
|
|
recipient.purchased.plan.dateTerminated = dateTerminated;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(recipient.purchased.plan.extraMonths).to.eql(0);
|
|
});
|
|
|
|
it('does not reset Gold-to-Gems cap on an existing subscription', async () => {
|
|
recipient.purchased.plan = plan;
|
|
recipient.purchased.plan.gemsBought = 12;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(recipient.purchased.plan.gemsBought).to.eql(12);
|
|
});
|
|
|
|
it('adds to date terminated for an existing plan with a future terminated date', async () => {
|
|
const dateTerminated = moment().add(1, 'months').toDate();
|
|
recipient.purchased.plan = plan;
|
|
recipient.purchased.plan.dateTerminated = dateTerminated;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(recipient.purchased.plan.dateTerminated).to.eql(moment(dateTerminated).add(3, 'months').toDate());
|
|
});
|
|
|
|
it('replaces date terminated for an account with a past terminated date', async () => {
|
|
const dateTerminated = moment().subtract(1, 'months').toDate();
|
|
recipient.purchased.plan.dateTerminated = dateTerminated;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(moment(recipient.purchased.plan.dateTerminated).format('YYYY-MM-DD')).to.eql(moment().add(3, 'months').format('YYYY-MM-DD'));
|
|
});
|
|
|
|
it('sets a dateTerminated date for a user without an existing subscription', async () => {
|
|
expect(recipient.purchased.plan.dateTerminated).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(recipient.purchased.plan.dateTerminated).to.exist;
|
|
});
|
|
|
|
it('sets plan.dateUpdated if it did not previously exist', async () => {
|
|
expect(recipient.purchased.plan.dateUpdated).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(recipient.purchased.plan.dateUpdated).to.exist;
|
|
});
|
|
|
|
it('does not reset gemCapExtra if they already had one', async () => {
|
|
recipient.purchased.plan.consecutive.gemCapExtra = 10;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(recipient.purchased.plan.consecutive.gemCapExtra).to.eql(10);
|
|
});
|
|
|
|
it('sets gemCapExtra to 0 if they receive a 3 month sub', async () => {
|
|
data.gift.subscription.key = 'basic_3mo';
|
|
data.gift.subscription.months = 3;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(recipient.purchased.plan.consecutive.gemCapExtra).to.eql(0);
|
|
});
|
|
|
|
it('sets gemCapExtra to max if they receive a 12 month sub', async () => {
|
|
recipient.purchased.plan.consecutive.gemCapExtra = 10;
|
|
|
|
data.gift.subscription.key = 'basic_12mo';
|
|
data.gift.subscription.months = 12;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(recipient.purchased.plan.consecutive.gemCapExtra).to.eql(26);
|
|
});
|
|
|
|
it('gives user 1 hourglass if they have no active subscription', async () => {
|
|
await api.createSubscription(data);
|
|
expect(recipient.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
});
|
|
|
|
it('does not give any hourglasses if they have an active subscription', async () => {
|
|
recipient.purchased.plan = plan;
|
|
await api.createSubscription(data);
|
|
expect(recipient.purchased.plan.consecutive.trinkets).to.eql(plan.consecutive.trinkets);
|
|
});
|
|
|
|
it('sets plan.dateUpdated if it did exist but the user has cancelled', async () => {
|
|
recipient.purchased.plan.dateUpdated = moment().subtract(1, 'days').toDate();
|
|
recipient.purchased.plan.dateTerminated = moment().subtract(1, 'days').toDate();
|
|
recipient.purchased.plan.customerId = 'testing';
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(moment(recipient.purchased.plan.dateUpdated).date()).to.eql(moment().date());
|
|
});
|
|
|
|
it('sets plan.dateUpdated if it did exist but the user has a corrupt plan', async () => {
|
|
recipient.purchased.plan.dateUpdated = moment().subtract(1, 'days').toDate();
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(moment(recipient.purchased.plan.dateUpdated).date()).to.eql(moment().date());
|
|
});
|
|
|
|
it('sets plan.dateCreated if it did not previously exist', async () => {
|
|
expect(recipient.purchased.plan.dateCreated).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(recipient.purchased.plan.dateCreated).to.exist;
|
|
});
|
|
|
|
it('sets plan.dateCurrentTypeCreated if it did not previously exist', async () => {
|
|
expect(recipient.purchased.plan.dateCurrentTypeCreated).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(recipient.purchased.plan.dateCurrentTypeCreated).to.exist;
|
|
});
|
|
|
|
it('keeps plan.dateCreated when changing subscription type', async () => {
|
|
await api.createSubscription(data);
|
|
const initialDate = recipient.purchased.plan.dateCreated;
|
|
await api.createSubscription(data);
|
|
expect(recipient.purchased.plan.dateCreated).to.eql(initialDate);
|
|
});
|
|
|
|
it('sets plan.dateCurrentTypeCreated when changing subscription type', async () => {
|
|
await api.createSubscription(data);
|
|
const initialDate = recipient.purchased.plan.dateCurrentTypeCreated;
|
|
await api.createSubscription(data);
|
|
expect(recipient.purchased.plan.dateCurrentTypeCreated).to.not.eql(initialDate);
|
|
});
|
|
|
|
it('does not change plan.customerId if it already exists', async () => {
|
|
recipient.purchased.plan = plan;
|
|
data.customerId = 'purchaserCustomerId';
|
|
|
|
expect(recipient.purchased.plan.customerId).to.eql('customer-id');
|
|
await api.createSubscription(data);
|
|
|
|
expect(recipient.purchased.plan.customerId).to.eql('customer-id');
|
|
});
|
|
|
|
it('sets plan.customerId to "Gift" if it does not already exist', async () => {
|
|
expect(recipient.purchased.plan.customerId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(recipient.purchased.plan.customerId).to.eql('Gift');
|
|
});
|
|
|
|
it('increases the buyer\'s transaction count', async () => {
|
|
expect(user.purchased.txnCount).to.eql(0);
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.txnCount).to.eql(1);
|
|
});
|
|
|
|
it('sends an email about the gift', async () => {
|
|
await api.createSubscription(data);
|
|
|
|
expect(sender.sendTxn).to.be.calledWith(recipient, 'gifted-subscription', [
|
|
{ name: 'GIFTER', content: 'sender' },
|
|
{ name: 'X_MONTHS_SUBSCRIPTION', content: 3 },
|
|
]);
|
|
});
|
|
|
|
it('sends a push notification about the gift', async () => {
|
|
await api.createSubscription(data);
|
|
expect(notifications.sendNotification).to.be.calledOnce;
|
|
});
|
|
|
|
it('tracks subscription purchase as gift', async () => {
|
|
await api.createSubscription(data);
|
|
|
|
expect(analytics.mockAnalyticsService.trackPurchase).to.be.calledOnce;
|
|
expect(analytics.mockAnalyticsService.trackPurchase).to.be.calledWith({
|
|
uuid: user._id,
|
|
groupId: undefined,
|
|
itemPurchased: 'Subscription',
|
|
sku: 'payment method-subscription',
|
|
purchaseType: 'subscribe',
|
|
paymentMethod: data.paymentMethod,
|
|
quantity: 1,
|
|
gift: true,
|
|
purchaseValue: 15,
|
|
firstPurchase: true,
|
|
headers: {
|
|
'x-client': 'habitica-web',
|
|
'user-agent': '',
|
|
},
|
|
});
|
|
});
|
|
|
|
context('No Active Promotion', () => {
|
|
beforeEach(() => {
|
|
sinon.stub(worldState, 'getCurrentEventList').returns([]);
|
|
});
|
|
|
|
afterEach(() => {
|
|
worldState.getCurrentEventList.restore();
|
|
});
|
|
|
|
it('sends a private message about the gift', async () => {
|
|
await api.createSubscription(data);
|
|
const msg = '`Hello recipient, sender has sent you 3 months of subscription!`';
|
|
|
|
expect(user.sendMessage).to.be.calledOnce;
|
|
expect(user.sendMessage).to.be.calledWith(
|
|
recipient,
|
|
{ receiverMsg: msg, senderMsg: msg, save: false },
|
|
);
|
|
});
|
|
});
|
|
|
|
context('Active Promotion', () => {
|
|
beforeEach(() => {
|
|
sinon.stub(worldState, 'getCurrentEventList').returns([{
|
|
...REPEATING_EVENTS.giftOneGetOne,
|
|
event: 'g1g1',
|
|
}]);
|
|
});
|
|
|
|
afterEach(() => {
|
|
worldState.getCurrentEventList.restore();
|
|
});
|
|
|
|
it('creates a gift subscription for purchaser and recipient if none exist', async () => {
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.items.pets['Jackalope-RoyalPurple']).to.eql(5);
|
|
expect(user.purchased.plan.customerId).to.eql('Gift');
|
|
expect(user.purchased.plan.dateTerminated).to.exist;
|
|
expect(user.purchased.plan.dateUpdated).to.exist;
|
|
expect(user.purchased.plan.dateCreated).to.exist;
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(0);
|
|
|
|
expect(recipient.items.pets['Jackalope-RoyalPurple']).to.eql(5);
|
|
expect(recipient.purchased.plan.customerId).to.eql('Gift');
|
|
expect(recipient.purchased.plan.dateTerminated).to.exist;
|
|
expect(recipient.purchased.plan.dateUpdated).to.exist;
|
|
expect(recipient.purchased.plan.dateCreated).to.exist;
|
|
expect(recipient.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
expect(recipient.purchased.plan.consecutive.gemCapExtra).to.eql(0);
|
|
});
|
|
|
|
it('adds extraMonths to existing subscription for purchaser and creates a gift subscription for recipient without sub', async () => {
|
|
user.purchased.plan = plan;
|
|
|
|
expect(user.purchased.plan.extraMonths).to.eql(0);
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(0);
|
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(0);
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.extraMonths).to.eql(3);
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(0);
|
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(0);
|
|
|
|
expect(recipient.items.pets['Jackalope-RoyalPurple']).to.eql(5);
|
|
expect(recipient.purchased.plan.customerId).to.eql('Gift');
|
|
expect(recipient.purchased.plan.dateTerminated).to.exist;
|
|
expect(recipient.purchased.plan.dateUpdated).to.exist;
|
|
expect(recipient.purchased.plan.dateCreated).to.exist;
|
|
});
|
|
|
|
it('adds extraMonths to existing subscription for recipient and creates a gift subscription for purchaser without sub', async () => {
|
|
recipient.purchased.plan = plan;
|
|
|
|
expect(recipient.purchased.plan.extraMonths).to.eql(0);
|
|
expect(recipient.purchased.plan.consecutive.trinkets).to.eql(0);
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(recipient.purchased.plan.extraMonths).to.eql(3);
|
|
expect(recipient.purchased.plan.consecutive.trinkets).to.eql(0);
|
|
|
|
expect(user.items.pets['Jackalope-RoyalPurple']).to.eql(5);
|
|
expect(user.purchased.plan.customerId).to.eql('Gift');
|
|
expect(user.purchased.plan.dateTerminated).to.exist;
|
|
expect(user.purchased.plan.dateUpdated).to.exist;
|
|
expect(user.purchased.plan.dateCreated).to.exist;
|
|
});
|
|
|
|
it('adds extraMonths to existing subscriptions for purchaser and recipient', async () => {
|
|
user.purchased.plan = plan;
|
|
recipient.purchased.plan = plan;
|
|
|
|
expect(user.purchased.plan.extraMonths).to.eql(0);
|
|
expect(recipient.purchased.plan.extraMonths).to.eql(0);
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(0);
|
|
expect(recipient.purchased.plan.consecutive.trinkets).to.eql(0);
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.extraMonths).to.eql(3);
|
|
expect(recipient.purchased.plan.extraMonths).to.eql(3);
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(0);
|
|
expect(recipient.purchased.plan.consecutive.trinkets).to.eql(0);
|
|
});
|
|
|
|
it('sends a private message about the promotion', async () => {
|
|
await api.createSubscription(data);
|
|
const msg = '`Hello sender, you received 3 months of subscription as part of our holiday gift-giving promotion!`';
|
|
|
|
expect(user.sendMessage).to.be.calledTwice;
|
|
expect(user.sendMessage).to.be.calledWith(user, { receiverMsg: msg, save: false });
|
|
});
|
|
});
|
|
});
|
|
|
|
context('Purchasing a subscription for self', () => {
|
|
it('creates a subscription', async () => {
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_3mo');
|
|
expect(user.purchased.plan.customerId).to.eql('customer-id');
|
|
expect(user.purchased.plan.dateUpdated).to.exist;
|
|
expect(user.purchased.plan.gemsBought).to.eql(0);
|
|
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;
|
|
});
|
|
|
|
it('sets plan.dateCreated if it did not previously exist', async () => {
|
|
expect(user.purchased.plan.dateCreated).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.dateCreated).to.exist;
|
|
});
|
|
|
|
it('sets plan.dateCurrentTypeCreated if it did not previously exist', async () => {
|
|
expect(user.purchased.plan.dateCurrentTypeCreated).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.dateCurrentTypeCreated).to.exist;
|
|
});
|
|
|
|
it('keeps plan.dateCreated when changing subscription type', async () => {
|
|
await api.createSubscription(data);
|
|
const initialDate = user.purchased.plan.dateCreated;
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.dateCreated).to.eql(initialDate);
|
|
});
|
|
|
|
it('sets plan.dateCurrentTypeCreated when changing subscription type', async () => {
|
|
await api.createSubscription(data);
|
|
const initialDate = user.purchased.plan.dateCurrentTypeCreated;
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.dateCurrentTypeCreated).to.not.eql(initialDate);
|
|
});
|
|
|
|
it('awards the Royal Purple Jackalope pet', async () => {
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.items.pets['Jackalope-RoyalPurple']).to.eql(5);
|
|
});
|
|
|
|
it('sets extraMonths if plan has dateTerminated date', async () => {
|
|
user.purchased.plan = plan;
|
|
user.purchased.plan.dateTerminated = moment(new Date()).add(2, 'months');
|
|
expect(user.purchased.plan.extraMonths).to.eql(0);
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.extraMonths).to.within(1.9, 2);
|
|
});
|
|
|
|
it('does not set negative extraMonths if plan has past dateTerminated date', async () => {
|
|
user.purchased.plan = plan;
|
|
user.purchased.plan.dateTerminated = moment(new Date()).subtract(2, 'months');
|
|
expect(user.purchased.plan.extraMonths).to.eql(0);
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.extraMonths).to.eql(0);
|
|
});
|
|
|
|
it('does not reset Gold-to-Gems cap on additional subscription', async () => {
|
|
user.purchased.plan = plan;
|
|
user.purchased.plan.gemsBought = 10;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.gemsBought).to.eql(10);
|
|
});
|
|
|
|
it('sets lastBillingDate if payment method is "Amazon Payments"', async () => {
|
|
data.paymentMethod = 'Amazon Payments';
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.lastBillingDate).to.exist;
|
|
});
|
|
|
|
it('increases the user\'s transaction count', async () => {
|
|
expect(user.purchased.txnCount).to.eql(0);
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.txnCount).to.eql(1);
|
|
});
|
|
|
|
it('sends a transaction email', async () => {
|
|
await api.createSubscription(data);
|
|
|
|
expect(sender.sendTxn).to.be.calledOnce;
|
|
expect(sender.sendTxn).to.be.calledWith(data.user, 'subscription-begins');
|
|
});
|
|
|
|
it('tracks subscription purchase', async () => {
|
|
await api.createSubscription(data);
|
|
|
|
expect(analytics.mockAnalyticsService.trackPurchase).to.be.calledOnce;
|
|
expect(analytics.mockAnalyticsService.trackPurchase).to.be.calledWith({
|
|
uuid: user._id,
|
|
groupId: undefined,
|
|
itemPurchased: 'Subscription',
|
|
sku: 'payment method-subscription',
|
|
purchaseType: 'subscribe',
|
|
paymentMethod: data.paymentMethod,
|
|
quantity: 1,
|
|
gift: false,
|
|
purchaseValue: 15,
|
|
firstPurchase: true,
|
|
headers: {
|
|
'x-client': 'habitica-web',
|
|
'user-agent': '',
|
|
},
|
|
});
|
|
});
|
|
|
|
context('Upgrades subscription', () => {
|
|
it('from basic_earned to basic_6mo', async () => {
|
|
data.sub.key = 'basic_earned';
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_earned');
|
|
expect(user.purchased.plan.customerId).to.eql('customer-id');
|
|
const created = user.purchased.plan.dateCreated;
|
|
const updated = user.purchased.plan.dateUpdated;
|
|
|
|
data.sub.key = 'basic_6mo';
|
|
data.updatedFrom = { key: 'basic_earned' };
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_6mo');
|
|
expect(user.purchased.plan.dateCreated).to.eql(created);
|
|
expect(user.purchased.plan.dateUpdated).to.not.eql(updated);
|
|
expect(user.purchased.plan.customerId).to.eql('customer-id');
|
|
});
|
|
|
|
it('from basic_3mo to basic_12mo', async () => {
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_3mo');
|
|
expect(user.purchased.plan.customerId).to.eql('customer-id');
|
|
const created = user.purchased.plan.dateCreated;
|
|
const updated = user.purchased.plan.dateUpdated;
|
|
|
|
data.sub.key = 'basic_12mo';
|
|
data.updatedFrom = { key: 'basic_3mo' };
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
expect(user.purchased.plan.dateCreated).to.eql(created);
|
|
expect(user.purchased.plan.dateUpdated).to.not.eql(updated);
|
|
expect(user.purchased.plan.customerId).to.eql('customer-id');
|
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(26);
|
|
});
|
|
});
|
|
|
|
context('Downgrades subscription', () => {
|
|
it('from basic_6mo to basic_earned', async () => {
|
|
data.sub.key = 'basic_6mo';
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_6mo');
|
|
expect(user.purchased.plan.customerId).to.eql('customer-id');
|
|
const created = user.purchased.plan.dateCreated;
|
|
const updated = user.purchased.plan.dateUpdated;
|
|
|
|
data.sub.key = 'basic_earned';
|
|
data.updatedFrom = { key: 'basic_6mo' };
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_earned');
|
|
expect(user.purchased.plan.dateCreated).to.eql(created);
|
|
expect(user.purchased.plan.dateUpdated).to.not.eql(updated);
|
|
expect(user.purchased.plan.customerId).to.eql('customer-id');
|
|
});
|
|
|
|
it('from basic_12mo to basic_3mo', async () => {
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
data.sub.key = 'basic_12mo';
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
expect(user.purchased.plan.customerId).to.eql('customer-id');
|
|
const created = user.purchased.plan.dateCreated;
|
|
const updated = user.purchased.plan.dateUpdated;
|
|
|
|
data.sub.key = 'basic_3mo';
|
|
data.updatedFrom = { key: 'basic_12mo' };
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_3mo');
|
|
expect(user.purchased.plan.dateCreated).to.eql(created);
|
|
expect(user.purchased.plan.dateUpdated).to.not.eql(updated);
|
|
expect(user.purchased.plan.customerId).to.eql('customer-id');
|
|
});
|
|
});
|
|
});
|
|
|
|
context('Block subscription perks', () => {
|
|
it('adds 26 to plan.consecutive.gemCapExtra for 12 month block', async () => {
|
|
data.sub.key = 'basic_12mo';
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(26);
|
|
});
|
|
|
|
it('does not raise plan.consecutive.gemCapExtra higher than 26', async () => {
|
|
data.sub.key = 'basic_12mo';
|
|
|
|
await api.createSubscription(data);
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(26);
|
|
});
|
|
|
|
it('adds a plan.consecutive.trinkets for 3 month block', async () => {
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
});
|
|
|
|
it('adds 1 plan.consecutive.trinkets for 6 month block', async () => {
|
|
data.sub.key = 'basic_6mo';
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
});
|
|
|
|
it('adds 1 plan.consecutive.trinkets for 12 month block if they had promo', async () => {
|
|
user.purchased.plan.hourglassPromoReceived = new Date();
|
|
data.sub.key = 'basic_12mo';
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
});
|
|
|
|
it('adds 12 plan.consecutive.trinkets for 12 month block', async () => {
|
|
data.sub.key = 'basic_12mo';
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(13);
|
|
});
|
|
|
|
context('Upgrades subscription', () => {
|
|
context('Using payDifference logic', () => {
|
|
beforeEach(async () => {
|
|
data.updatedFrom = { logic: 'payDifference' };
|
|
});
|
|
it('Adds 26 to plan.consecutive.gemCapExtra when upgrading from basic_3mo to basic_12mo', async () => {
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_3mo');
|
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(0);
|
|
|
|
data.sub.key = 'basic_12mo';
|
|
data.updatedFrom.key = 'basic_3mo';
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(26);
|
|
});
|
|
|
|
it('Adds 12 to plan.consecutive.trinkets when upgrading from basic_6mo to basic_12mo', async () => {
|
|
data.sub.key = 'basic_6mo';
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_6mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
|
|
data.sub.key = 'basic_12mo';
|
|
data.updatedFrom.key = 'basic_6mo';
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(13);
|
|
});
|
|
|
|
it('Adds 12 to plan.consecutive.trinkets when upgrading from basic_3mo to basic_12mo', async () => {
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_3mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
|
|
data.sub.key = 'basic_12mo';
|
|
data.updatedFrom.key = 'basic_3mo';
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(13);
|
|
});
|
|
});
|
|
|
|
context('Using payFull logic', () => {
|
|
beforeEach(async () => {
|
|
data.updatedFrom = { logic: 'payFull' };
|
|
});
|
|
|
|
it('Adds 26 to plan.consecutive.gemCapExtra when upgrading from basic_3mo to basic_12mo', async () => {
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_3mo');
|
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(0);
|
|
|
|
data.sub.key = 'basic_12mo';
|
|
data.updatedFrom.key = 'basic_3mo';
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(26);
|
|
});
|
|
|
|
it('Adds 12 to plan.consecutive.trinkets when upgrading from basic_6mo to basic_12mo', async () => {
|
|
data.sub.key = 'basic_6mo';
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_6mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
|
|
data.sub.key = 'basic_12mo';
|
|
data.updatedFrom.key = 'basic_6mo';
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(13);
|
|
});
|
|
|
|
it('Adds 12 to plan.consecutive.trinkets when upgrading from basic_3mo to basic_12mo', async () => {
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_3mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
|
|
data.sub.key = 'basic_12mo';
|
|
data.updatedFrom.key = 'basic_3mo';
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(13);
|
|
});
|
|
});
|
|
|
|
context('Using refundAndRepay logic', () => {
|
|
let clock;
|
|
beforeEach(async () => {
|
|
clock = sinon.useFakeTimers(new Date('2022-01-01'));
|
|
data.updatedFrom = { logic: 'refundAndRepay' };
|
|
});
|
|
context('Upgrades within first half of subscription', () => {
|
|
it('Adds 26 to plan.consecutive.gemCapExtra when upgrading from basic_3mo to basic_12mo', async () => {
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_3mo');
|
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(0);
|
|
|
|
data.sub.key = 'basic_12mo';
|
|
data.updatedFrom.key = 'basic_3mo';
|
|
clock.restore();
|
|
clock = sinon.useFakeTimers(new Date('2022-02-05'));
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(26);
|
|
});
|
|
|
|
it('Adds 12 to plan.consecutive.trinkets when upgrading from basic_3mo to basic_12mo', async () => {
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_3mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
|
|
data.sub.key = 'basic_12mo';
|
|
data.updatedFrom.key = 'basic_3mo';
|
|
clock.restore();
|
|
clock = sinon.useFakeTimers(new Date('2022-01-31'));
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(13);
|
|
});
|
|
|
|
it('Adds 12 to plan.consecutive.trinkets when upgrading from basic_6mo to basic_12mo', async () => {
|
|
data.sub.key = 'basic_6mo';
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_6mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
|
|
data.sub.key = 'basic_12mo';
|
|
data.updatedFrom.key = 'basic_6mo';
|
|
clock.restore();
|
|
clock = sinon.useFakeTimers(new Date('2022-01-28'));
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(13);
|
|
});
|
|
|
|
it('2 plan.consecutive.trinkets when upgrading from basic_6mo to basic_12mo after initial cycle', async () => {
|
|
data.sub.key = 'basic_6mo';
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_6mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
|
|
data.sub.key = 'basic_12mo';
|
|
data.updatedFrom.key = 'basic_6mo';
|
|
clock.restore();
|
|
clock = sinon.useFakeTimers(new Date('2022-08-28'));
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(13);
|
|
});
|
|
|
|
it('Adds 12 to plan.consecutive.trinkets when upgrading from basic_3mo to basic_12mo after initial cycle', async () => {
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_3mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
|
|
data.sub.key = 'basic_12mo';
|
|
data.updatedFrom.key = 'basic_3mo';
|
|
clock.restore();
|
|
clock = sinon.useFakeTimers(new Date('2022-07-31'));
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(13);
|
|
});
|
|
});
|
|
context('Upgrades within second half of subscription', () => {
|
|
it('Adds 0 to plan.consecutive.gemCapExtra from basic_earned to basic_6mo', async () => {
|
|
data.sub.key = 'basic_earned';
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_earned');
|
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(0);
|
|
|
|
data.sub.key = 'basic_6mo';
|
|
data.updatedFrom.key = 'basic_earned';
|
|
clock.restore();
|
|
clock = sinon.useFakeTimers(new Date('2022-01-20'));
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_6mo');
|
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(0);
|
|
});
|
|
|
|
it('Adds 26 to plan.consecutive.gemCapExtra when upgrading from basic_3mo to basic_12mo', async () => {
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_3mo');
|
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(0);
|
|
|
|
data.sub.key = 'basic_12mo';
|
|
data.updatedFrom.key = 'basic_3mo';
|
|
clock.restore();
|
|
clock = sinon.useFakeTimers(new Date('2022-02-24'));
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(26);
|
|
});
|
|
|
|
it('Adds 0 to plan.consecutive.trinkets from basic_earned to basic_6mo', async () => {
|
|
data.sub.key = 'basic_earned';
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_earned');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
|
|
data.sub.key = 'basic_6mo';
|
|
data.updatedFrom.key = 'basic_earned';
|
|
clock.restore();
|
|
clock = sinon.useFakeTimers(new Date('2022-01-28'));
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_6mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
});
|
|
|
|
it('Adds 12 to plan.consecutive.trinkets when upgrading from basic_6mo to basic_12mo', async () => {
|
|
data.sub.key = 'basic_6mo';
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_6mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
|
|
data.sub.key = 'basic_12mo';
|
|
data.updatedFrom.key = 'basic_6mo';
|
|
clock.restore();
|
|
clock = sinon.useFakeTimers(new Date('2022-05-28'));
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(13);
|
|
});
|
|
|
|
it('Adds 12 to plan.consecutive.trinkets when upgrading from basic_3mo to basic_12mo', async () => {
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_3mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
|
|
data.sub.key = 'basic_12mo';
|
|
data.updatedFrom.key = 'basic_3mo';
|
|
clock.restore();
|
|
clock = sinon.useFakeTimers(new Date('2022-03-03'));
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(13);
|
|
});
|
|
|
|
it('Adds 0 to plan.consecutive.trinkets from basic_earned to basic_6mo after initial cycle', async () => {
|
|
data.sub.key = 'basic_earned';
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_earned');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
|
|
data.sub.key = 'basic_6mo';
|
|
data.updatedFrom.key = 'basic_earned';
|
|
clock.restore();
|
|
clock = sinon.useFakeTimers(new Date('2022-05-28'));
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_6mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
});
|
|
|
|
it('Adds 12 to plan.consecutive.trinkets when upgrading from basic_6mo to basic_12mo after initial cycle', async () => {
|
|
data.sub.key = 'basic_6mo';
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_6mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
|
|
data.sub.key = 'basic_12mo';
|
|
data.updatedFrom.key = 'basic_6mo';
|
|
clock.restore();
|
|
clock = sinon.useFakeTimers(new Date('2023-05-28'));
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(13);
|
|
});
|
|
|
|
it('Adds 12 to plan.consecutive.trinkets when upgrading from basic_3mo to basic_12mo after initial cycle', async () => {
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_3mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
|
|
data.sub.key = 'basic_12mo';
|
|
data.updatedFrom.key = 'basic_3mo';
|
|
clock.restore();
|
|
clock = sinon.useFakeTimers(new Date('2023-09-03'));
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(13);
|
|
});
|
|
});
|
|
afterEach(async () => {
|
|
if (clock !== null) clock.restore();
|
|
});
|
|
});
|
|
});
|
|
|
|
context('Downgrades subscription', () => {
|
|
it('does not remove from plan.consecutive.gemCapExtra from basic_12mo to basic_3mo', async () => {
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
data.sub.key = 'basic_12mo';
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(26);
|
|
|
|
data.sub.key = 'basic_3mo';
|
|
data.updatedFrom = { key: 'basic_12mo' };
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(26);
|
|
});
|
|
|
|
it('does not remove from plan.consecutive.trinkets from basic_12mo to basic_3mo', async () => {
|
|
expect(user.purchased.plan.planId).to.not.exist;
|
|
|
|
data.sub.key = 'basic_12mo';
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(13);
|
|
|
|
data.sub.key = 'basic_3mo';
|
|
data.updatedFrom = { key: 'basic_12mo' };
|
|
await api.createSubscription(data);
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(13);
|
|
});
|
|
});
|
|
});
|
|
|
|
context('Mystery Items', () => {
|
|
let clock;
|
|
const mayMysteryItem = 'armor_mystery_201605';
|
|
|
|
beforeEach(() => {
|
|
const mayMysteryItemTimeframe = new Date(2016, 4, 31); // May 31st 2016
|
|
clock = sinon.useFakeTimers({
|
|
now: mayMysteryItemTimeframe,
|
|
toFake: ['Date'],
|
|
});
|
|
});
|
|
|
|
afterEach(() => {
|
|
if (clock) clock.restore();
|
|
});
|
|
|
|
it('awards mystery items when within the timeframe for a mystery item', async () => {
|
|
data = { paymentMethod: 'PaymentMethod', user, sub: { key: 'basic_3mo' } };
|
|
|
|
const oldNotificationsCount = user.notifications.length;
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.notifications.find(n => n.type === 'NEW_MYSTERY_ITEMS')).to.not.be.undefined;
|
|
expect(user.purchased.plan.mysteryItems).to.have.a.lengthOf(2);
|
|
expect(user.purchased.plan.mysteryItems).to.include('armor_mystery_201605');
|
|
expect(user.purchased.plan.mysteryItems).to.include('head_mystery_201605');
|
|
expect(user.notifications.length).to.equal(oldNotificationsCount + 1);
|
|
expect(user.notifications[0].type).to.equal('NEW_MYSTERY_ITEMS');
|
|
});
|
|
|
|
it('does not award mystery item when user already owns the item', async () => {
|
|
user.items.gear.owned[mayMysteryItem] = true;
|
|
|
|
data = { paymentMethod: 'PaymentMethod', user, sub: { key: 'basic_3mo' } };
|
|
|
|
await api.createSubscription(data);
|
|
|
|
expect(user.purchased.plan.mysteryItems).to.have.a.lengthOf(1);
|
|
expect(user.purchased.plan.mysteryItems).to.include('head_mystery_201605');
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('#cancelSubscription', () => {
|
|
beforeEach(() => {
|
|
data = { user };
|
|
});
|
|
|
|
context('Canceling a subscription for self', () => {
|
|
it('adds a month termination date by default', async () => {
|
|
await api.cancelSubscription(data);
|
|
|
|
const now = new Date();
|
|
const daysTillTermination = moment(user.purchased.plan.dateTerminated).diff(now, 'days');
|
|
|
|
expect(daysTillTermination).to.be.within(29, 30); // 1 month +/- 1 days
|
|
});
|
|
|
|
it('adds extraMonths to dateTerminated value', async () => {
|
|
user.purchased.plan.extraMonths = 2;
|
|
|
|
await api.cancelSubscription(data);
|
|
|
|
const now = new Date();
|
|
const daysTillTermination = moment(user.purchased.plan.dateTerminated).diff(now, 'days');
|
|
|
|
expect(daysTillTermination).to.be.within(89, 90); // 3 months +/- 1 days
|
|
});
|
|
|
|
it('handles extra month fractions', async () => {
|
|
user.purchased.plan.extraMonths = 0.3;
|
|
|
|
await api.cancelSubscription(data);
|
|
|
|
const now = new Date();
|
|
const daysTillTermination = moment(user.purchased.plan.dateTerminated).diff(now, 'days');
|
|
|
|
expect(daysTillTermination).to.be.within(38, 39); // should be about 1 month + 1/3 month
|
|
});
|
|
|
|
it('terminates at next billing date if it exists', async () => {
|
|
data.nextBill = moment().add({ days: 15 });
|
|
|
|
await api.cancelSubscription(data);
|
|
|
|
const now = new Date();
|
|
const daysTillTermination = moment(user.purchased.plan.dateTerminated).diff(now, 'days');
|
|
|
|
expect(daysTillTermination).to.be.within(13, 15);
|
|
});
|
|
|
|
it('terminates at next billing date even if dateUpdated is prior to now', async () => {
|
|
data.nextBill = moment().add({ days: 15 });
|
|
data.user.purchased.plan.dateUpdated = moment().subtract({ days: 10 });
|
|
|
|
await api.cancelSubscription(data);
|
|
|
|
const now = new Date();
|
|
const daysTillTermination = moment(user.purchased.plan.dateTerminated).diff(now, 'days');
|
|
|
|
expect(daysTillTermination).to.be.within(13, 15);
|
|
});
|
|
|
|
it('resets plan.extraMonths', async () => {
|
|
user.purchased.plan.extraMonths = 5;
|
|
|
|
await api.cancelSubscription(data);
|
|
|
|
expect(user.purchased.plan.extraMonths).to.eql(0);
|
|
});
|
|
|
|
it('does not reset gemCapExtra', async () => {
|
|
user.purchased.plan.consecutive.gemCapExtra = 12;
|
|
|
|
await api.cancelSubscription(data);
|
|
|
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(12);
|
|
});
|
|
|
|
it('initializes gemCapExtra', async () => {
|
|
await api.cancelSubscription(data);
|
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(0);
|
|
});
|
|
|
|
it('initializes hourglasses', async () => {
|
|
await api.cancelSubscription(data);
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(0);
|
|
});
|
|
|
|
it('does not reset owned hourglasses', async () => {
|
|
user.purchased.plan.consecutive.trinkets = 12;
|
|
|
|
await api.cancelSubscription(data);
|
|
|
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(12);
|
|
});
|
|
|
|
it('sends an email', async () => {
|
|
await api.cancelSubscription(data);
|
|
|
|
expect(sender.sendTxn).to.be.calledOnce;
|
|
expect(sender.sendTxn).to.be.calledWith(user, 'cancel-subscription');
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('#buyGems', () => {
|
|
beforeEach(() => {
|
|
data = {
|
|
user,
|
|
gemsBlock: common.content.gems['21gems'],
|
|
paymentMethod: 'payment',
|
|
headers: {
|
|
'x-client': 'habitica-web',
|
|
'user-agent': '',
|
|
},
|
|
};
|
|
});
|
|
|
|
context('Self Purchase', () => {
|
|
it('sends a donation email', async () => {
|
|
await api.buyGems(data);
|
|
|
|
expect(sender.sendTxn).to.be.calledOnce;
|
|
expect(sender.sendTxn).to.be.calledWith(data.user, 'donation');
|
|
});
|
|
});
|
|
|
|
context('No Active Promotion', () => {
|
|
beforeEach(() => {
|
|
sinon.stub(worldState, 'getCurrentEvent').returns(null);
|
|
sinon.stub(worldState, 'getCurrentEventList').returns([]);
|
|
});
|
|
|
|
afterEach(() => {
|
|
worldState.getCurrentEvent.restore();
|
|
worldState.getCurrentEventList.restore();
|
|
});
|
|
|
|
it('does not apply a discount', async () => {
|
|
const balanceBefore = user.balance;
|
|
|
|
await api.buyGems(data);
|
|
|
|
const balanceAfter = user.balance;
|
|
const balanceDiff = balanceAfter - balanceBefore;
|
|
|
|
expect(balanceDiff * 4).to.eql(21);
|
|
});
|
|
});
|
|
|
|
context('Active Promotion', () => {
|
|
beforeEach(() => {
|
|
sinon.stub(worldState, 'getCurrentEventList').returns([{
|
|
...common.content.events.fall2020,
|
|
event: 'fall2020',
|
|
}]);
|
|
});
|
|
|
|
afterEach(() => {
|
|
worldState.getCurrentEventList.restore();
|
|
});
|
|
|
|
it('applies a discount', async () => {
|
|
const balanceBefore = user.balance;
|
|
|
|
await api.buyGems(data);
|
|
|
|
const balanceAfter = user.balance;
|
|
const balanceDiff = balanceAfter - balanceBefore;
|
|
|
|
expect(balanceDiff * 4).to.eql(30);
|
|
});
|
|
});
|
|
|
|
context('Gift', () => {
|
|
let recipient;
|
|
|
|
beforeEach(() => {
|
|
recipient = new User();
|
|
recipient.profile.name = 'recipient';
|
|
|
|
data.gift = {
|
|
gems: {
|
|
amount: 4,
|
|
},
|
|
member: recipient,
|
|
};
|
|
});
|
|
|
|
it('calculates balance from gem amount if gift', async () => {
|
|
expect(recipient.balance).to.eql(0);
|
|
|
|
await api.buyGems(data);
|
|
|
|
expect(recipient.balance).to.eql(1);
|
|
});
|
|
|
|
it('sends a gifted-gems email', async () => {
|
|
await api.buyGems(data);
|
|
|
|
expect(sender.sendTxn).to.be.calledOnce;
|
|
expect(sender.sendTxn).to.be.calledWith(data.gift.member, 'gifted-gems');
|
|
});
|
|
|
|
it('sends a message from purchaser to recipient', async () => {
|
|
await api.buyGems(data);
|
|
const msg = '`Hello recipient, sender has sent you 4 gems!`';
|
|
|
|
expect(user.sendMessage).to.be
|
|
.calledWith(recipient, { receiverMsg: msg, senderMsg: msg, save: false });
|
|
});
|
|
|
|
it('sends a message from purchaser to recipient with custom message', async () => {
|
|
data.gift.message = 'giftmessage';
|
|
|
|
await api.buyGems(data);
|
|
|
|
const msg = `\`Hello recipient, sender has sent you 4 gems!\` ${data.gift.message}`;
|
|
expect(user.sendMessage).to.be
|
|
.calledWith(recipient, { receiverMsg: msg, senderMsg: msg, save: false });
|
|
});
|
|
|
|
it('sends a push notification if user did not gift to self', async () => {
|
|
await api.buyGems(data);
|
|
expect(notifications.sendNotification).to.be.calledOnce;
|
|
});
|
|
|
|
it('sends gem donation message in each participant\'s language', async () => {
|
|
// TODO using english for both users because other languages are not loaded
|
|
// for api.buyGems
|
|
await recipient.updateOne({
|
|
'preferences.language': 'en',
|
|
});
|
|
await user.updateOne({
|
|
'preferences.language': 'en',
|
|
});
|
|
await api.buyGems(data);
|
|
|
|
const [recipientsMessageContent, sendersMessageContent] = ['en', 'en'].map(lang => {
|
|
const messageContent = t('giftedGemsFull', {
|
|
username: recipient.profile.name,
|
|
sender: user.profile.name,
|
|
gemAmount: data.gift.gems.amount,
|
|
}, lang);
|
|
|
|
return `\`${messageContent}\``;
|
|
});
|
|
|
|
expect(user.sendMessage).to.be.calledWith(
|
|
recipient,
|
|
{ receiverMsg: recipientsMessageContent, senderMsg: sendersMessageContent, save: false },
|
|
);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('addSubToGroupUser', () => {
|
|
it('adds a group subscription to a new user', async () => {
|
|
expect(group.purchased.plan.planId).to.not.exist;
|
|
data.groupId = group._id;
|
|
|
|
await api.addSubToGroupUser(user, group);
|
|
|
|
const updatedUser = await User.findById(user._id).exec();
|
|
|
|
expect(updatedUser.purchased.plan.planId).to.eql('group_plan_auto');
|
|
expect(updatedUser.purchased.plan.customerId).to.eql('group-plan');
|
|
expect(updatedUser.purchased.plan.dateUpdated).to.exist;
|
|
expect(updatedUser.purchased.plan.gemsBought).to.eql(0);
|
|
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;
|
|
});
|
|
|
|
it('awards the Royal Purple Jackalope pet', async () => {
|
|
await api.addSubToGroupUser(user, group);
|
|
|
|
const updatedUser = await User.findById(user._id).exec();
|
|
|
|
expect(updatedUser.items.pets['Jackalope-RoyalPurple']).to.eql(5);
|
|
});
|
|
|
|
it('saves previously unused Mystery Items and Hourglasses for an expired subscription', async () => {
|
|
const planExpirationDate = new Date();
|
|
planExpirationDate.setDate(planExpirationDate.getDate() - 2);
|
|
const mysteryItem = 'item';
|
|
const mysteryItems = [mysteryItem];
|
|
const consecutive = {
|
|
trinkets: 3,
|
|
};
|
|
|
|
// set expired plan with unused items
|
|
plan.mysteryItems = mysteryItems;
|
|
plan.consecutive = consecutive;
|
|
plan.dateCreated = planExpirationDate;
|
|
plan.dateTerminated = planExpirationDate;
|
|
plan.customerId = null;
|
|
|
|
user.purchased.plan = plan;
|
|
|
|
await user.save();
|
|
await api.addSubToGroupUser(user, group);
|
|
|
|
const updatedUser = await User.findById(user._id).exec();
|
|
|
|
expect(updatedUser.purchased.plan.mysteryItems[0]).to.eql(mysteryItem);
|
|
expect(updatedUser.purchased.plan.consecutive.trinkets).to.equal(consecutive.trinkets);
|
|
});
|
|
});
|
|
});
|