Skip to content

Commit

Permalink
Add tests for subscription expiring message
Browse files Browse the repository at this point in the history
  • Loading branch information
brizental committed Jan 23, 2023
1 parent a89a787 commit 94fde9f
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/apps/vpn/ui/screens/messaging/ViewMessagesInbox.qml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ VPNViewBase {
VPNSwipeDelegate {
id: swipeDelegate

objectName: typeof(addon) !== "undefined" ? addon.id : ""

property real deleteLabelWidth: 0.0

//avoids qml warnings when addon messages get disabled via condition
Expand Down
13 changes: 13 additions & 0 deletions tests/functional/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,19 @@ module.exports = {
});
},

async checkForFailedQuery(query, tries = 3, interval = 300) {
let attempts = 0;
while(attempts < tries) {
try {
await this.query(query);
throw new Error(`Query '${query}' was expected to fail!`);
} catch(e) {
attempts++;
await this.wait(interval);
}
};
},

async clickOnQuery(id) {
assert(await this.query(id), 'Clicking on an non-existing element?!?');
const json = await this._writeCommand(`click ${encodeURIComponent(id)}`);
Expand Down
1 change: 1 addition & 0 deletions tests/functional/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ const global = {
};

module.exports = {
QmlQueryComposer,
screenHome,
screenInitialize,
screenInitializeMobileOnBoarding,
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/servers/guardian_endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const SubscriptionDetails = {
},
};

exports.SubscriptionDetails = SubscriptionDetails;

const VALIDATORS = {
guardianLoginVerify: {
type: 'object',
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/setupVpn.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ async function startAndConnect() {
await vpn.connect(vpnWS, {hostname: '127.0.0.1'});
}

exports.startAndConnect = startAndConnect;

exports.mochaHooks = {
async beforeAll() {
// Check VPN app exists. If not, bail.
Expand Down
78 changes: 78 additions & 0 deletions tests/functional/testAddons.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
const assert = require('assert');
const queries = require('./queries.js');
const vpn = require('./helper.js');
const { SubscriptionDetails } = require('./servers/guardian_endpoints.js')
const { QmlQueryComposer } = require('./queries.js')
const { startAndConnect } = require('./setupVpn.js')

describe('Addons', function() {
this.ctx.authenticationNeeded = true;
Expand Down Expand Up @@ -127,4 +130,79 @@ describe('Addons', function() {
await vpn.getVPNProperty('VPNCurrentServer', 'exitCountryCode') ===
exitCountryCode);
});

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 = 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();

// Load all production addons.
// These are loaded all together, so we don't know the exact number of addons.
await vpn.resetAddons('prod');
await vpn.waitForCondition(async () => (
parseInt(await vpn.getVPNProperty('VPNAddonManager', 'count'), 10) > 0
));

// Enter messages screen.
await vpn.waitForQueryAndClick(queries.navBar.MESSAGES);
await vpn.waitForQuery(queries.screenMessaging.SCREEN.visible());

// Check that the subscription expiry message is there or not.
const query = new QmlQueryComposer("//message_subscription_expiring");
if (shouldBeAvailable) {
await vpn.waitForQuery(query);
} else {
await vpn.checkForFailedQuery(query);
}
}
}

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 94fde9f

Please sign in to comment.