0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00

Fixed failing migration requests when client runs in a test env

no issue

- When Ghost is running in a test environment, it is configured with an invalid Stripe key that looks like `sk_test***`. In this case the migrations try runnig creating request to Stripe, which fail. The failures pollute the output, which makes other valid errors lost.
- An example of such error log is following:
```
Invalid API Key provided: sk_test_******ripe

----------------------------------------

Error: Invalid API Key provided: sk_test_******ripe
    at res.toJSON.then.StripeAPIError.message (/home/naz/Workspace/Ghost/Ghost/node_modules/stripe/lib/StripeResource.js:214:23)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
```
- There doesn't seem to be a good reason to do migrations in the test environment. Skipping them as a special case to fix the output pollution problem seems like a right solution
This commit is contained in:
Naz 2022-04-04 13:31:53 +08:00 committed by naz
parent 0e09a57971
commit af4d7b4938
4 changed files with 66 additions and 1 deletions

View file

@ -22,7 +22,11 @@ module.exports = class StripeMigrations {
if (!this.api._configured) {
logging.info('Stripe not configured - skipping migrations');
return;
} else if (this.api.testEnv) {
logging.info('Stripe is in test mode - skipping migrations');
return;
}
await this.populateProductsAndPrices();
await this.populateStripePricesFromStripePlansSetting();
await this.populateMembersMonthlyPriceIdSettings();

View file

@ -24,6 +24,7 @@ const STRIPE_API_VERSION = '2020-08-27';
* @prop {string} checkoutSessionCancelUrl
* @prop {string} checkoutSetupSessionSuccessUrl
* @prop {string} checkoutSetupSessionCancelUrl
* @prop {boolean} param.testEnv - indicates if the module is run in test environment (note, NOT the test mode)
*/
module.exports = class StripeAPI {
@ -43,6 +44,10 @@ module.exports = class StripeAPI {
return this._configured;
}
get testEnv() {
return this._config.testEnv;
}
get mode() {
return this._testMode ? 'test' : 'live';
}

View file

@ -76,7 +76,8 @@ module.exports = class StripeService {
checkoutSessionSuccessUrl: config.checkoutSessionSuccessUrl,
checkoutSessionCancelUrl: config.checkoutSessionCancelUrl,
checkoutSetupSessionSuccessUrl: config.checkoutSetupSessionSuccessUrl,
checkoutSetupSessionCancelUrl: config.checkoutSetupSessionCancelUrl
checkoutSetupSessionCancelUrl: config.checkoutSetupSessionCancelUrl,
testEnv: config.testEnv
});
await this.webhookManager.configure({

View file

@ -4,6 +4,61 @@ const Migrations = require('../../../lib/Migrations');
describe('Migrations', function () {
describe('updateStripeProductNamesFromDefaultProduct', function () {
it('Does not run migration if api is not configured', async function () {
const api = {
getProduct: sinon.stub().resolves({
id: 'prod_123',
name: 'Bronze'
}),
_configured: false
};
const models = {
Product: {
transaction: sinon.spy()
}
};
const migrations = new Migrations({
models,
api
});
await migrations.execute();
assert(
models.Product.transaction.called === false,
'Stripe should not call any of the populateProductsAndPrices method parts if api is not configured'
);
});
it('Does not run migration if api is in test environment', async function () {
const api = {
getProduct: sinon.stub().resolves({
id: 'prod_123',
name: 'Bronze'
}),
_configured: true,
testEnv: true
};
const models = {
Product: {
transaction: sinon.spy()
}
};
const migrations = new Migrations({
models,
api
});
await migrations.execute();
assert(
models.Product.transaction.called === false,
'Stripe should not call any of the populateProductsAndPrices method parts if api is not configured'
);
});
it('Does not update Stripe product if name is not "Default Product"', async function () {
const api = {
getProduct: sinon.stub().resolves({