From 7aa7447db9b65e3eedb8d2444886eb6655d0bf35 Mon Sep 17 00:00:00 2001 From: Sam Lord Date: Mon, 9 Oct 2023 21:22:01 +0100 Subject: [PATCH] Mocked GhostMailer in browser e2e tests refs: https://github.com/TryGhost/DevOps/issues/78 --- .../test/e2e-browser/fixtures/ghost-test.js | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/ghost/core/test/e2e-browser/fixtures/ghost-test.js b/ghost/core/test/e2e-browser/fixtures/ghost-test.js index 48ee1850e3..4d58778dbf 100644 --- a/ghost/core/test/e2e-browser/fixtures/ghost-test.js +++ b/ghost/core/test/e2e-browser/fixtures/ghost-test.js @@ -4,7 +4,7 @@ const base = require('@playwright/test'); const {promisify} = require('util'); const {spawn, exec} = require('child_process'); const {setupGhost, setupMailgun, enableLabs, setupStripe} = require('../utils/e2e-browser-utils'); -const {allowStripe} = require('../../utils/e2e-framework-mock-manager'); +const {allowStripe, mockMail} = require('../../utils/e2e-framework-mock-manager'); const MailgunClient = require('@tryghost/mailgun-client'); const sinon = require('sinon'); const ObjectID = require('bson-objectid').default; @@ -21,6 +21,10 @@ const getWebhookSecret = async () => { }; const generateStripeIntegrationToken = async () => { + if (!('STRIPE_PUBLISHABLE_KEY' in process.env) || !('STRIPE_SECRET_KEY' in process.env)) { + throw new Error('Missing STRIPE_PUBLISHABLE_KEY or STRIPE_SECRET_KEY environment variables'); + } + const publishableKey = process.env.STRIPE_PUBLISHABLE_KEY; const secretKey = process.env.STRIPE_SECRET_KEY; const accountId = process.env.STRIPE_ACCOUNT_ID ?? JSON.parse((await promisify(exec)('stripe get account')).stdout).id; @@ -33,20 +37,6 @@ const generateStripeIntegrationToken = async () => { })).toString('base64'); }; -const stubMailgun = () => { - // We need to stub the Mailgun client before starting Ghost - sinon.stub(MailgunClient.prototype, 'getInstance').returns({ - // @ts-ignore - messages: { - create: async function () { - return { - id: `mailgun-mock-id-${ObjectID().toHexString()}` - }; - } - } - }); -}; - // Global promises for webhook secret / Stripe integration token const webhookSecretPromise = getWebhookSecret(); const stripeIntegrationTokenPromise = generateStripeIntegrationToken(); @@ -105,7 +95,18 @@ module.exports = base.test.extend({ process.env.WEBHOOK_SECRET = await webhookSecretPromise; - stubMailgun(); + sandbox.stub(MailgunClient.prototype, 'getInstance').returns({ + // @ts-ignore + messages: { + create: async function () { + return { + id: `mailgun-mock-id-${ObjectID().toHexString()}` + }; + } + } + }); + + mockMail(); const {startGhost} = require('../../utils/e2e-framework'); const server = await startGhost({