Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
brizental committed Jan 25, 2023
1 parent 9be40bc commit ba75483
Showing 1 changed file with 46 additions and 56 deletions.
102 changes: 46 additions & 56 deletions tests/functional/testAddons.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,50 @@ describe('Addons', function() {
// });

describe('test message_subscription_expiring addon condition', async () => {
async function checkForSubscriptionExpiringMessage(ctx, subscriptionExpirationCases, shouldBeAvailable) {
for (const expiresOn of subscriptionExpirationCases) {
const mockDetails = { ...SubscriptionDetails };
// We are faking a Stripe subscription, so this value is expected to be in seconds.
mockDetails.subscription.current_period_end = Math.floor(expiresOn / 1000);
ctx.guardianSubscriptionDetailsCallback = () => {
ctx.guardianOverrideEndpoints.GETs['/api/v1/vpn/subscriptionDetails'].status = 200;
ctx.guardianOverrideEndpoints
.GETs['/api/v1/vpn/subscriptionDetails']
.body = mockDetails;
};

// Restart the VPN to load the new sub details.
await vpn.quit();
await startAndConnect();

// 1 to 7 days out from expiring.
//
// In these cases the message is shown.
const subscriptionCloseToExpirationCases = Array.from(
{ length: 7 },
(_, i) => [Date.now() + 1000 * 60 * 60 * 24 * (i + 1), true]
);

// In these cases the message is NOT shown.
const subscriptionFarFromExpirationCases = [
// Seven days out + a minute from expiring.
[Date.now() + 1000 * 60 * 60 * 24 * 7 + 1000 * 60, false],
// Eight days from expiring.
[Date.now() + 1000 * 60 * 60 * 24 * 8, false],
// One month from expiring.
[Date.now() + 1000 * 60 * 60 * 24 * 30, false],
];

// In these cases the message is NOT shown.
const subscriptionExpiredCases = [
// Literally, has just expired.
[Date.now(), false],
// Has been expired for a day.
[Date.now() - 1000 * 60 * 60 * 24, false],
// Has been expired for 30 days.
[Date.now() - 1000 * 60 * 60 * 24 * 30, false],
];

[
...subscriptionCloseToExpirationCases,
...subscriptionFarFromExpirationCases,
...subscriptionExpiredCases
].forEach(([expiresOn, isMessageShown]) => {
const mockDetails = { ...SubscriptionDetails };
// We are faking a Stripe subscription, so this value is expected to be in seconds.
mockDetails.subscription.current_period_end = Math.floor(expiresOn / 1000);
ctx.guardianSubscriptionDetailsCallback = () => {
ctx.guardianOverrideEndpoints.GETs['/api/v1/vpn/subscriptionDetails'].status = 200;
ctx.guardianOverrideEndpoints
.GETs['/api/v1/vpn/subscriptionDetails']
.body = mockDetails;
};

it(`is message shown when expiration happens ${Date.now() - expiresOn} from now`, async () => {
// Load all production addons.
// These are loaded all together, so we don't know the exact number of addons.
await vpn.resetAddons('prod');
Expand All @@ -156,48 +184,10 @@ describe('Addons', function() {

await vpn.waitForCondition(async () => {
const loadedMessages = await vpn.messages();
console.log(loadedMessages)
const isSubscriptionExpiringMessageAvailable = loadedMessages.includes("message_subscription_expiring");

return shouldBeAvailable ? isSubscriptionExpiringMessageAvailable : !isSubscriptionExpiringMessageAvailable;
return isMessageShown ? isSubscriptionExpiringMessageAvailable : !isSubscriptionExpiringMessageAvailable;
});
}
}

it('message is enabled when subscription is about to expire', async () => {
// 1 to 7 days out from expiring.
const subscriptionExpirationCases = Array.from(
{ length: 7 },
(_, i) => Date.now() + 1000 * 60 * 60 * 24 * (i + 1)
);

await checkForSubscriptionExpiringMessage(this.ctx, subscriptionExpirationCases, true);
});

it('message is not enabled when subscription is not about to expire', async () => {
const subscriptionExpirationCases = [
// Seven days out + a minute from expiring.
Date.now() + 1000 * 60 * 60 * 24 * 7 + 1000 * 60,
// Eight days from expiring.
Date.now() + 1000 * 60 * 60 * 24 * 8,
// One month from expiring.
Date.now() + 1000 * 60 * 60 * 24 * 30,
]

await checkForSubscriptionExpiringMessage(this.ctx, subscriptionExpirationCases, false);
});

it('message is not enabled when subscription is already expired', async () => {
const subscriptionExpirationCases = [
// Literally, has just expired.
Date.now(),
// Has been expired for a day.
Date.now() - 1000 * 60 * 60 * 24,
// Has been expired for 30 days.
Date.now() - 1000 * 60 * 60 * 24 * 30,
]

await checkForSubscriptionExpiringMessage(this.ctx, subscriptionExpirationCases, false);
})
});
});
});

0 comments on commit ba75483

Please sign in to comment.