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 7c9a83d commit cf56a4c
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/apps/vpn/inspector/inspectorhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,24 @@ static QList<InspectorCommand> s_commands{
return obj;
}},

InspectorCommand{"messages", "Returns a list of the loaded messages ids", 0,
[](InspectorHandler*, const QList<QByteArray>&) {
QJsonObject obj;

AddonManager* am = AddonManager::instance();
Q_ASSERT(am);

QJsonArray messages;
am->forEach([&](Addon* addon) {
if (addon->type() == "message") {
messages.append(addon->id());
}
});

obj["value"] = messages;
return obj;
}},

InspectorCommand{"translate", "Translate a string", 1,
[](InspectorHandler*, const QList<QByteArray>& arguments) {
QJsonObject obj;
Expand Down
8 changes: 8 additions & 0 deletions tests/functional/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,14 @@ module.exports = {
return json.value;
},

async messages() {
const json = await this._writeCommand('messages');
assert(
json.type === 'messages' && !('error' in json),
`Command failed: ${json.error}`);
return json.value;
},

async screenCapture() {
const json = await this._writeCommand('screen_capture');
assert(
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
74 changes: 74 additions & 0 deletions tests/functional/testAddons.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
const assert = require('assert');
const queries = require('./queries.js');
const vpn = require('./helper.js');
const { SubscriptionDetails } = require('./servers/guardian_endpoints.js')
const { startAndConnect } = require('./setupVpn.js')

describe('Addons', function() {
this.ctx.authenticationNeeded = true;
Expand Down Expand Up @@ -127,4 +129,76 @@ 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
));

const loadedMessages = await vpn.messages();
const isSubscriptionExpiringMessageAvailable = loadedMessages.includes("message_subscription_expiring");

if (shouldBeAvailable) {
assert(isSubscriptionExpiringMessageAvailable);
} else {
assert(!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 cf56a4c

Please sign in to comment.