Skip to content

Commit 82109da

Browse files
committed
test: promotions integ tests working in desktop and browser app
1 parent c2f732e commit 82109da

File tree

2 files changed

+17
-50
lines changed

2 files changed

+17
-50
lines changed

src/services/promotions.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
*/
1818

19-
/*global logger*/
19+
/*global logger, path*/
2020

2121
/**
2222
* Promotions Service
@@ -46,7 +46,7 @@ define(function (require, exports, module) {
4646

4747
// Constants
4848
const EVENT_PRO_UPGRADE_ON_INSTALL = "pro_upgrade_on_install";
49-
const PROMO_LOCAL_FILE = "entitlements_promo.json";
49+
const PROMO_LOCAL_FILE = path.join(Phoenix.app.getApplicationSupportDirectory(), "entitlements_promo.json");
5050
const TRIAL_POLL_MS = 10 * 1000; // 10 seconds after start, we assign a free trial if possible
5151
const FIRST_INSTALL_TRIAL_DAYS = 30;
5252
const SUBSEQUENT_TRIAL_DAYS = 7;
@@ -94,9 +94,8 @@ define(function (require, exports, module) {
9494
if (Phoenix.isNativeApp) {
9595
await KernalModeTrust.removeCredential(KernalModeTrust.CRED_KEY_PROMO);
9696
} else {
97-
const filePath = Phoenix.app.getApplicationSupportDirectory() + PROMO_LOCAL_FILE;
9897
await new Promise((resolve) => {
99-
window.fs.unlink(filePath, () => resolve()); // Always resolve, ignore errors
98+
window.fs.unlink(PROMO_LOCAL_FILE, () => resolve()); // Always resolve, ignore errors
10099
});
101100
}
102101
} catch (error) {
@@ -183,8 +182,7 @@ define(function (require, exports, module) {
183182
} else {
184183
// Browser app: use virtual filesystem. in future we need to always fetch from remote about trial
185184
// entitlements for browser app.
186-
const filePath = Phoenix.app.getApplicationSupportDirectory() + PROMO_LOCAL_FILE;
187-
const fileData = await _readFileAsync(filePath);
185+
const fileData = await _readFileAsync(PROMO_LOCAL_FILE);
188186

189187
if (!fileData) {
190188
return null; // No data exists - genuine first install
@@ -219,8 +217,7 @@ define(function (require, exports, module) {
219217
await KernalModeTrust.setCredential(KernalModeTrust.CRED_KEY_PROMO, JSON.stringify(trialData));
220218
} else {
221219
// Browser app: use virtual filesystem
222-
const filePath = Phoenix.app.getApplicationSupportDirectory() + PROMO_LOCAL_FILE;
223-
await _writeFileAsync(filePath, JSON.stringify(trialData));
220+
await _writeFileAsync(PROMO_LOCAL_FILE, JSON.stringify(trialData));
224221
}
225222
} catch (error) {
226223
console.error("Error setting trial data:", error);
@@ -475,6 +472,14 @@ define(function (require, exports, module) {
475472
console.log("Salt data cleanup completed (ignoring errors)");
476473
}
477474
},
475+
// Test-only functions for manipulating credentials directly (bypassing validation)
476+
_testSetRawCredential: async function(data) {
477+
if (Phoenix.isNativeApp) {
478+
await KernalModeTrust.setCredential(KernalModeTrust.CRED_KEY_PROMO, JSON.stringify(data));
479+
} else {
480+
await _writeFileAsync(PROMO_LOCAL_FILE, JSON.stringify(data));
481+
}
482+
},
478483
activateProTrial: activateProTrial,
479484
getProTrialDaysRemaining: getProTrialDaysRemaining,
480485
setDateNowFn: function _setDdateNowFn(fn) {

test/spec/promotions-integ-test.js

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -472,20 +472,7 @@ define(function (require, exports, module) {
472472
const tamperedTrial = { ...storedResult.data, signature: "fake_signature" };
473473

474474
// Manually store the tampered data (bypassing _setTrialData validation)
475-
if (testWindow.Phoenix.isNativeApp) {
476-
await testWindow.KernalModeTrust.setCredential(testWindow.KernalModeTrust.CRED_KEY_PROMO, JSON.stringify(tamperedTrial));
477-
} else {
478-
await new Promise((resolve, reject) => {
479-
const filePath = testWindow.Phoenix.app.getApplicationSupportDirectory() + "entitlements_promo.json";
480-
testWindow.fs.writeFile(filePath, JSON.stringify(tamperedTrial), 'utf8', (err) => {
481-
if (err) {
482-
reject(err);
483-
} else {
484-
resolve();
485-
}
486-
});
487-
});
488-
}
475+
await LoginService._testSetRawCredential(tamperedTrial);
489476

490477
// Verify: _getTrialData should detect corruption
491478
const corruptedResult = await LoginService._getTrialData();
@@ -571,20 +558,7 @@ define(function (require, exports, module) {
571558
};
572559

573560
// Manually store data without signature (bypassing _setTrialData)
574-
if (testWindow.Phoenix.isNativeApp) {
575-
await testWindow.KernalModeTrust.setCredential(testWindow.KernalModeTrust.CRED_KEY_PROMO, JSON.stringify(trialWithoutSignature));
576-
} else {
577-
await new Promise((resolve, reject) => {
578-
const filePath = testWindow.Phoenix.app.getApplicationSupportDirectory() + "entitlements_promo.json";
579-
testWindow.fs.writeFile(filePath, JSON.stringify(trialWithoutSignature), 'utf8', (err) => {
580-
if (err) {
581-
reject(err);
582-
} else {
583-
resolve();
584-
}
585-
});
586-
});
587-
}
561+
await LoginService._testSetRawCredential(trialWithoutSignature);
588562

589563
// Should detect corruption due to missing signature
590564
const result = await LoginService._getTrialData();
@@ -661,20 +635,8 @@ define(function (require, exports, module) {
661635
const storedResult = await LoginService._getTrialData();
662636
const tamperedTrial = { ...storedResult.data, signature: "fake_signature" };
663637

664-
if (testWindow.Phoenix.isNativeApp) {
665-
await testWindow.KernalModeTrust.setCredential(testWindow.KernalModeTrust.CRED_KEY_PROMO, JSON.stringify(tamperedTrial));
666-
} else {
667-
await new Promise((resolve, reject) => {
668-
const filePath = testWindow.Phoenix.app.getApplicationSupportDirectory() + "entitlements_promo.json";
669-
testWindow.fs.writeFile(filePath, JSON.stringify(tamperedTrial), 'utf8', (err) => {
670-
if (err) {
671-
reject(err);
672-
} else {
673-
resolve();
674-
}
675-
});
676-
});
677-
}
638+
// Manually store the tampered data (bypassing validation)
639+
await LoginService._testSetRawCredential(tamperedTrial);
678640

679641
// First activation should create expired marker
680642
await LoginService.activateProTrial();

0 commit comments

Comments
 (0)