From 1f58a2c255c62f1924d8cf47ae10b88ef1096017 Mon Sep 17 00:00:00 2001 From: Beatriz Rizental Date: Wed, 25 Jan 2023 15:40:21 +0100 Subject: [PATCH] Address review comments --- tests/functional/testAddons.js | 102 +++++++++++++++------------------ 1 file changed, 46 insertions(+), 56 deletions(-) diff --git a/tests/functional/testAddons.js b/tests/functional/testAddons.js index 7485a80716..c9df9f4e13 100644 --- a/tests/functional/testAddons.js +++ b/tests/functional/testAddons.js @@ -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'); @@ -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); + }) }); }); });