fix tests and some cases

This commit is contained in:
Phillip Thelen
2022-12-02 17:12:17 +01:00
committed by Phillip Thelen
parent c0c6657536
commit 12479edb77
4 changed files with 100 additions and 19 deletions

View File

@@ -231,13 +231,16 @@ describe('cron', async () => {
}, },
}); });
// user1 has a 1-month recurring subscription starting today // user1 has a 1-month recurring subscription starting today
user1.purchased.plan.customerId = 'subscribedId'; beforeEach(async () => {
user1.purchased.plan.dateUpdated = moment().toDate(); user1.purchased.plan.customerId = 'subscribedId';
user1.purchased.plan.planId = 'basic'; user1.purchased.plan.dateUpdated = moment().toDate();
user1.purchased.plan.consecutive.count = 0; user1.purchased.plan.planId = 'basic';
user1.purchased.plan.consecutive.offset = 0; user1.purchased.plan.consecutive.count = 0;
user1.purchased.plan.consecutive.trinkets = 0; user1.purchased.plan.perkMonthCount = 0;
user1.purchased.plan.consecutive.gemCapExtra = 0; user1.purchased.plan.consecutive.offset = 0;
user1.purchased.plan.consecutive.trinkets = 0;
user1.purchased.plan.consecutive.gemCapExtra = 0;
});
it('does not increment consecutive benefits after the first month', async () => { it('does not increment consecutive benefits after the first month', async () => {
clock = sinon.useFakeTimers(moment().utcOffset(0).startOf('month').add(1, 'months') clock = sinon.useFakeTimers(moment().utcOffset(0).startOf('month').add(1, 'months')
@@ -271,6 +274,24 @@ describe('cron', async () => {
expect(user1.purchased.plan.consecutive.gemCapExtra).to.equal(0); expect(user1.purchased.plan.consecutive.gemCapExtra).to.equal(0);
}); });
it('increments consecutive benefits after the second month if they also received a 1 month gift subscription', async () => {
user1.purchased.plan.perkMonthCount = 1;
clock = sinon.useFakeTimers(moment().utcOffset(0).startOf('month').add(2, 'months')
.add(2, 'days')
.toDate());
// Add 1 month to simulate what happens a month after the subscription was created.
// Add 2 days so that we're sure we're not affected by any start-of-month effects
// e.g., from time zone oddness.
await cron({
user: user1, tasksByType, daysMissed, analytics,
});
expect(user1.purchased.plan.perkMonthCount).to.equal(0);
expect(user1.purchased.plan.consecutive.count).to.equal(2);
expect(user1.purchased.plan.consecutive.offset).to.equal(0);
expect(user1.purchased.plan.consecutive.trinkets).to.equal(1);
expect(user1.purchased.plan.consecutive.gemCapExtra).to.equal(5);
});
it('increments consecutive benefits after the third month', async () => { it('increments consecutive benefits after the third month', async () => {
clock = sinon.useFakeTimers(moment().utcOffset(0).startOf('month').add(3, 'months') clock = sinon.useFakeTimers(moment().utcOffset(0).startOf('month').add(3, 'months')
.add(2, 'days') .add(2, 'days')
@@ -315,6 +336,26 @@ describe('cron', async () => {
expect(user1.purchased.plan.consecutive.trinkets).to.equal(3); expect(user1.purchased.plan.consecutive.trinkets).to.equal(3);
expect(user1.purchased.plan.consecutive.gemCapExtra).to.equal(15); expect(user1.purchased.plan.consecutive.gemCapExtra).to.equal(15);
}); });
it('initialized plan.perkMonthCount if necessary', async () => {
user.purchased.plan.perkMonthCount = undefined;
clock = sinon.useFakeTimers(moment().utcOffset(0).startOf('month').add(1, 'months')
.add(2, 'days')
.toDate());
await cron({
user, tasksByType, daysMissed, analytics,
});
expect(user.purchased.plan.perkMonthCount).to.equal(1);
user.purchased.plan.perkMonthCount = undefined;
clock.restore();
clock = sinon.useFakeTimers(moment().utcOffset(0).startOf('month').add(2, 'months')
.add(2, 'days')
.toDate());
await cron({
user, tasksByType, daysMissed, analytics,
});
expect(user.purchased.plan.perkMonthCount).to.equal(2);
});
}); });
describe('for a 3-month recurring subscription', async () => { describe('for a 3-month recurring subscription', async () => {
@@ -330,13 +371,16 @@ describe('cron', async () => {
}, },
}); });
// user3 has a 3-month recurring subscription starting today // user3 has a 3-month recurring subscription starting today
user3.purchased.plan.customerId = 'subscribedId'; beforeEach(async () => {
user3.purchased.plan.dateUpdated = moment().toDate(); user3.purchased.plan.customerId = 'subscribedId';
user3.purchased.plan.planId = 'basic_3mo'; user3.purchased.plan.dateUpdated = moment().toDate();
user3.purchased.plan.consecutive.count = 0; user3.purchased.plan.planId = 'basic_3mo';
user3.purchased.plan.consecutive.offset = 3; user3.purchased.plan.perkMonthCount = 0;
user3.purchased.plan.consecutive.trinkets = 1; user3.purchased.plan.consecutive.count = 0;
user3.purchased.plan.consecutive.gemCapExtra = 5; user3.purchased.plan.consecutive.offset = 3;
user3.purchased.plan.consecutive.trinkets = 1;
user3.purchased.plan.consecutive.gemCapExtra = 5;
});
it('does not increment consecutive benefits in the first month of the first paid period that they already have benefits for', async () => { it('does not increment consecutive benefits in the first month of the first paid period that they already have benefits for', async () => {
clock = sinon.useFakeTimers(moment().utcOffset(0).startOf('month').add(1, 'months') clock = sinon.useFakeTimers(moment().utcOffset(0).startOf('month').add(1, 'months')
@@ -390,6 +434,17 @@ describe('cron', async () => {
expect(user3.purchased.plan.consecutive.gemCapExtra).to.equal(10); expect(user3.purchased.plan.consecutive.gemCapExtra).to.equal(10);
}); });
it('keeps existing plan.perkMonthCount intact when incrementing consecutive benefits', async () => {
user3.purchased.plan.perkMonthCount = 2
clock = sinon.useFakeTimers(moment().utcOffset(0).startOf('month').add(4, 'months')
.add(2, 'days')
.toDate());
await cron({
user: user3, tasksByType, daysMissed, analytics,
});
expect(user3.purchased.plan.perkMonthCount).to.equal(2);
});
it('does not increment consecutive benefits in the second month of the second period that they already have benefits for', async () => { it('does not increment consecutive benefits in the second month of the second period that they already have benefits for', async () => {
clock = sinon.useFakeTimers(moment().utcOffset(0).startOf('month').add(5, 'months') clock = sinon.useFakeTimers(moment().utcOffset(0).startOf('month').add(5, 'months')
.add(2, 'days') .add(2, 'days')
@@ -417,6 +472,7 @@ describe('cron', async () => {
}); });
it('increments consecutive benefits the month after the third paid period has started', async () => { it('increments consecutive benefits the month after the third paid period has started', async () => {
console.log(user3.purchased.plan);
clock = sinon.useFakeTimers(moment().utcOffset(0).startOf('month').add(7, 'months') clock = sinon.useFakeTimers(moment().utcOffset(0).startOf('month').add(7, 'months')
.add(2, 'days') .add(2, 'days')
.toDate()); .toDate());
@@ -456,13 +512,15 @@ describe('cron', async () => {
}, },
}); });
// user6 has a 6-month recurring subscription starting today // user6 has a 6-month recurring subscription starting today
user6.purchased.plan.customerId = 'subscribedId'; beforeEach(async () => {
user6.purchased.plan.customerId = 'subscribedId';
user6.purchased.plan.dateUpdated = moment().toDate(); user6.purchased.plan.dateUpdated = moment().toDate();
user6.purchased.plan.planId = 'google_6mo'; user6.purchased.plan.planId = 'google_6mo';
user6.purchased.plan.consecutive.count = 0; user6.purchased.plan.consecutive.count = 0;
user6.purchased.plan.consecutive.offset = 6; user6.purchased.plan.consecutive.offset = 6;
user6.purchased.plan.consecutive.trinkets = 2; user6.purchased.plan.consecutive.trinkets = 2;
user6.purchased.plan.consecutive.gemCapExtra = 10; user6.purchased.plan.consecutive.gemCapExtra = 10;
});
it('does not increment consecutive benefits in the first month of the first paid period that they already have benefits for', async () => { it('does not increment consecutive benefits in the first month of the first paid period that they already have benefits for', async () => {
clock = sinon.useFakeTimers(moment().utcOffset(0).startOf('month').add(1, 'months') clock = sinon.useFakeTimers(moment().utcOffset(0).startOf('month').add(1, 'months')

View File

@@ -13,7 +13,7 @@ import {
import * as worldState from '../../../../../website/server/libs/worldState'; import * as worldState from '../../../../../website/server/libs/worldState';
import { TransactionModel } from '../../../../../website/server/models/transaction'; import { TransactionModel } from '../../../../../website/server/models/transaction';
describe.only('payments/index', () => { describe('payments/index', () => {
let user; let user;
let group; let group;
let data; let data;
@@ -270,6 +270,8 @@ describe.only('payments/index', () => {
data.gift.subscription.months = 1; data.gift.subscription.months = 1;
expect(recipient.purchased.plan.perkMonthCount).to.eql(2); expect(recipient.purchased.plan.perkMonthCount).to.eql(2);
expect(recipient.purchased.plan.consecutive.trinkets).to.eql(0);
expect(recipient.purchased.plan.consecutive.gemCapExtra).to.eql(0);
await api.createSubscription(data); await api.createSubscription(data);
expect(recipient.purchased.plan.perkMonthCount).to.eql(0); expect(recipient.purchased.plan.perkMonthCount).to.eql(0);
@@ -277,6 +279,21 @@ describe.only('payments/index', () => {
expect(recipient.purchased.plan.consecutive.gemCapExtra).to.eql(5); expect(recipient.purchased.plan.consecutive.gemCapExtra).to.eql(5);
}); });
it('awards perks if plan.perkMonthCount goes over 3', async () => {
recipient.purchased.plan = plan;
recipient.purchased.plan.perkMonthCount = 2;
data.sub.key = 'basic_earned';
expect(recipient.purchased.plan.perkMonthCount).to.eql(2);
expect(recipient.purchased.plan.consecutive.trinkets).to.eql(0);
expect(recipient.purchased.plan.consecutive.gemCapExtra).to.eql(0);
await api.createSubscription(data);
expect(recipient.purchased.plan.perkMonthCount).to.eql(2);
expect(recipient.purchased.plan.consecutive.trinkets).to.eql(1);
expect(recipient.purchased.plan.consecutive.gemCapExtra).to.eql(5);
});
it('sets plan.customerId to "Gift" if it does not already exist', async () => { it('sets plan.customerId to "Gift" if it does not already exist', async () => {
expect(recipient.purchased.plan.customerId).to.not.exist; expect(recipient.purchased.plan.customerId).to.not.exist;
@@ -443,6 +460,7 @@ describe.only('payments/index', () => {
expect(user.purchased.plan.customerId).to.eql('customer-id'); expect(user.purchased.plan.customerId).to.eql('customer-id');
expect(user.purchased.plan.dateUpdated).to.exist; expect(user.purchased.plan.dateUpdated).to.exist;
expect(user.purchased.plan.gemsBought).to.eql(0); expect(user.purchased.plan.gemsBought).to.eql(0);
expect(user.purchased.plan.perkMonthCount).to.eql(0);
expect(user.purchased.plan.paymentMethod).to.eql('Payment Method'); expect(user.purchased.plan.paymentMethod).to.eql('Payment Method');
expect(user.purchased.plan.extraMonths).to.eql(0); expect(user.purchased.plan.extraMonths).to.eql(0);
expect(user.purchased.plan.dateTerminated).to.eql(null); expect(user.purchased.plan.dateTerminated).to.eql(null);

View File

@@ -258,7 +258,6 @@ async function createSubscription (data) {
if (!data.gift && !groupId) { if (!data.gift && !groupId) {
plan.consecutive.offset = months; plan.consecutive.offset = months;
} }
console.log("giving benes");
await plan.incrementPerkCounterAndReward(recipient._id, months); await plan.incrementPerkCounterAndReward(recipient._id, months);
} }

View File

@@ -53,11 +53,17 @@ schema.plugin(baseModel, {
schema.methods.incrementPerkCounterAndReward = async function incrementPerkCounterAndReward schema.methods.incrementPerkCounterAndReward = async function incrementPerkCounterAndReward
(userID, adding) { (userID, adding) {
// if perkMonthCount wasn't used before, initialize it. // if perkMonthCount wasn't used before, initialize it.
if (!this.perkMonthCount) this.perkMonthCount = this.consecutive.count % SUBSCRIPTION_BASIC_BLOCK_LENGTH; if (this.perkMonthCount == undefined && adding == 1) {
this.perkMonthCount += adding; this.perkMonthCount = (this.consecutive.count-1) % SUBSCRIPTION_BASIC_BLOCK_LENGTH;
console.log(`initializing perk count with ${this.perkMonthCount}`);
} else {
this.perkMonthCount += adding;
}
console.log(this.perkMonthCount);
const perks = Math.floor(this.perkMonthCount / 3); const perks = Math.floor(this.perkMonthCount / 3);
if (perks > 0) { if (perks > 0) {
console.log(`giving benefits for ${perks} from ${this.perkMonthCount}, ${adding}`);
this.consecutive.gemCapExtra += 5 * perks; // 5 extra Gems every 3 months this.consecutive.gemCapExtra += 5 * perks; // 5 extra Gems every 3 months
// cap it at 50 (hard 25 limit + extra 25) // cap it at 50 (hard 25 limit + extra 25)
if (this.consecutive.gemCapExtra > 25) this.consecutive.gemCapExtra = 25; if (this.consecutive.gemCapExtra > 25) this.consecutive.gemCapExtra = 25;