From 6232981be7d0271a7a1ea3f216c2adbcb5da7a99 Mon Sep 17 00:00:00 2001 From: Fabien 'egg' O'Carroll Date: Mon, 20 Jul 2020 14:59:23 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=20Fixed=20importing=20Stripe=20?= =?UTF-8?q?Plans=20with=20amount=200=20(#12062)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes #12049 Stripe plans used to default to 0, and our new validation of plan amounts were causing issues when importing from an older version of Ghost, this updates the validation to be skipped when importing. - Added regression test for importing plans --- core/server/models/settings.js | 22 +++++++++++------- test/regression/importer/importer_spec.js | 27 +++++++++++++++++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/core/server/models/settings.js b/core/server/models/settings.js index f1afdda95d..7697056661 100644 --- a/core/server/models/settings.js +++ b/core/server/models/settings.js @@ -125,10 +125,10 @@ Settings = ghostBookshelf.Model.extend({ async onValidate(model, attr, options) { await ghostBookshelf.Model.prototype.onValidate.call(this, model, attr, options); - await Settings.validators.all(model); + await Settings.validators.all(model, options); if (typeof Settings.validators[model.get('key')] === 'function') { - await Settings.validators[model.get('key')](model); + await Settings.validators[model.get('key')](model, options); } }, @@ -353,14 +353,20 @@ Settings = ghostBookshelf.Model.extend({ throw new errors.ValidationError(validationErrors.join('\n')); } }, - async stripe_plans(model) { + async stripe_plans(model, options) { const plans = JSON.parse(model.get('value')); for (const plan of plans) { - // We check 100, not 1, because amounts are in fractional units - if (plan.amount < 100 && plan.name !== 'Complimentary') { - throw new errors.ValidationError({ - message: 'Plans cannot have an amount less than 1' - }); + // Stripe plans used to be allowed (and defaulted to!) 0 amount plans + // this causes issues to people importing from older versions of Ghost + // even if they don't use Members/Stripe + // issue: https://github.com/TryGhost/Ghost/issues/12049 + if (!options.importing) { + // We check 100, not 1, because amounts are in fractional units + if (plan.amount < 100 && plan.name !== 'Complimentary') { + throw new errors.ValidationError({ + message: 'Plans cannot have an amount less than 1' + }); + } } if (typeof plan.name !== 'string') { diff --git a/test/regression/importer/importer_spec.js b/test/regression/importer/importer_spec.js index e2b7e65e84..36e5f36a5a 100644 --- a/test/regression/importer/importer_spec.js +++ b/test/regression/importer/importer_spec.js @@ -1185,6 +1185,33 @@ describe('Integration: Importer', function () { posts[1].html.should.eql('

Post Content

\n'); }); }); + + it('Can import stripe plans with an "amount" of 0', async function () { + const exportData = exportedLatestBody().db[0]; + + exportData.data.settings.push({ + id: 'blahblah', + group: 'members', + type: 'array', + flags: null, + created_at: '2020-04-20 16:20:00', + updated_at: '2020-04-20 16:20:00', + key: 'stripe_plans', + value: JSON.stringify([{ + name: 'Monthly', + interval: 'month', + currency: 'usd', + amount: 0 + }, { + name: 'Yearly', + interval: 'year', + currency: 'usd', + amount: 0 + }]) + }); + + await dataImporter.doImport(exportData, importOptions); + }); }); describe('Existing database', function () {