2022-12-01 03:34:19 +03:00
|
|
|
const config = require('../../../core/shared/config');
|
|
|
|
const {promisify} = require('util');
|
|
|
|
const {spawn, exec} = require('child_process');
|
|
|
|
const {knex} = require('../../../core/server/data/db');
|
2023-03-22 14:16:36 +03:00
|
|
|
const {setupGhost, setupStripe, setupMailgun, enableLabs} = require('./e2e-browser-utils');
|
2022-12-01 03:34:19 +03:00
|
|
|
const {chromium} = require('@playwright/test');
|
2022-12-06 13:30:07 +03:00
|
|
|
const {startGhost} = require('../../utils/e2e-framework');
|
|
|
|
const {stopGhost} = require('../../utils/e2e-utils');
|
2023-01-20 20:58:54 +03:00
|
|
|
const MailgunClient = require('@tryghost/mailgun-client/lib/mailgun-client');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const ObjectID = require('bson-objectid').default;
|
2023-03-08 14:40:21 +03:00
|
|
|
const {allowStripe} = require('../../utils/e2e-framework-mock-manager');
|
2022-12-01 03:34:19 +03:00
|
|
|
|
|
|
|
const startWebhookServer = () => {
|
2022-12-07 06:18:21 +03:00
|
|
|
const command = `stripe listen --forward-to ${config.getSiteUrl()}members/webhooks/stripe/ ${process.env.CI ? `--api-key ${process.env.STRIPE_SECRET_KEY}` : ''}`.trim();
|
2022-12-01 03:34:19 +03:00
|
|
|
spawn(command.split(' ')[0], command.split(' ').slice(1));
|
|
|
|
};
|
|
|
|
|
2022-12-06 13:30:07 +03:00
|
|
|
const getWebhookSecret = async () => {
|
|
|
|
const command = `stripe listen --print-secret ${process.env.CI ? `--api-key ${process.env.STRIPE_SECRET_KEY}` : ''}`.trim();
|
|
|
|
const webhookSecret = (await promisify(exec)(command)).stdout;
|
|
|
|
return webhookSecret.toString().trim();
|
|
|
|
};
|
|
|
|
|
2022-12-01 15:05:20 +03:00
|
|
|
const generateStripeIntegrationToken = async () => {
|
2022-12-01 03:34:19 +03:00
|
|
|
const inquirer = require('inquirer');
|
|
|
|
const stripeDatabaseKeys = {
|
|
|
|
publishableKey: 'stripe_connect_publishable_key',
|
|
|
|
secretKey: 'stripe_connect_secret_key',
|
2022-12-01 04:30:24 +03:00
|
|
|
liveMode: 'stripe_connect_livemode'
|
2022-12-01 03:34:19 +03:00
|
|
|
};
|
2022-12-01 04:30:24 +03:00
|
|
|
const publishableKey = process.env.STRIPE_PUBLISHABLE_KEY ?? (await knex('settings').select('value').where('key', stripeDatabaseKeys.publishableKey).first())?.value
|
2022-12-01 03:34:19 +03:00
|
|
|
?? (await inquirer.prompt([{
|
|
|
|
message: 'Stripe publishable key (starts "pk_test_")',
|
|
|
|
type: 'password',
|
|
|
|
name: 'value'
|
|
|
|
}])).value;
|
2022-12-01 04:30:24 +03:00
|
|
|
const secretKey = process.env.STRIPE_SECRET_KEY ?? (await knex('settings').select('value').where('key', stripeDatabaseKeys.secretKey).first())?.value
|
2022-12-01 03:34:19 +03:00
|
|
|
?? (await inquirer.prompt([{
|
|
|
|
message: 'Stripe secret key (starts "sk_test_")',
|
|
|
|
type: 'password',
|
|
|
|
name: 'value'
|
|
|
|
}])).value;
|
|
|
|
|
2022-12-01 15:05:20 +03:00
|
|
|
const accountId = process.env.STRIPE_ACCOUNT_ID ?? JSON.parse((await promisify(exec)('stripe get account')).stdout).id;
|
|
|
|
|
|
|
|
return Buffer.from(JSON.stringify({
|
|
|
|
a: secretKey,
|
|
|
|
p: publishableKey,
|
|
|
|
l: false,
|
|
|
|
i: accountId
|
|
|
|
})).toString('base64');
|
2022-12-01 03:34:19 +03:00
|
|
|
};
|
|
|
|
|
2022-12-08 15:49:38 +03:00
|
|
|
const stubMailgun = () => {
|
2023-01-20 20:58:54 +03:00
|
|
|
// 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()}`
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-12-08 15:49:38 +03:00
|
|
|
};
|
|
|
|
|
2022-12-01 03:34:19 +03:00
|
|
|
/**
|
|
|
|
* Setup the environment
|
|
|
|
*/
|
|
|
|
const setup = async (playwrightConfig) => {
|
|
|
|
const usingRemoteServer = process.env.CI && process.env.TEST_URL;
|
|
|
|
|
2022-12-01 15:05:20 +03:00
|
|
|
let stripeConnectIntegrationToken;
|
2022-12-01 03:34:19 +03:00
|
|
|
if (!usingRemoteServer) {
|
|
|
|
startWebhookServer();
|
2022-12-01 15:05:20 +03:00
|
|
|
stripeConnectIntegrationToken = await generateStripeIntegrationToken();
|
2022-12-06 13:30:07 +03:00
|
|
|
|
|
|
|
process.env.WEBHOOK_SECRET = await getWebhookSecret();
|
|
|
|
|
2022-12-08 15:49:38 +03:00
|
|
|
// Stub out NodeMailer
|
|
|
|
stubMailgun();
|
|
|
|
|
2022-12-06 13:30:07 +03:00
|
|
|
await startGhost({
|
|
|
|
frontend: true,
|
|
|
|
server: true,
|
2022-12-08 15:49:38 +03:00
|
|
|
backend: true
|
2022-12-06 13:30:07 +03:00
|
|
|
});
|
2023-03-08 13:48:47 +03:00
|
|
|
|
|
|
|
// StartGhost automatically disables network, so we need to re-enable it for Stripe
|
2023-03-08 14:40:21 +03:00
|
|
|
allowStripe();
|
2022-12-01 03:34:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const {baseURL, storageState} = playwrightConfig.projects[0].use;
|
|
|
|
const browser = await chromium.launch();
|
|
|
|
const page = await browser.newPage({
|
|
|
|
baseURL
|
|
|
|
});
|
|
|
|
await setupGhost(page);
|
2022-12-01 15:05:20 +03:00
|
|
|
if (!usingRemoteServer) {
|
|
|
|
await setupStripe(page, stripeConnectIntegrationToken);
|
2022-12-08 15:43:07 +03:00
|
|
|
await setupMailgun(page);
|
2022-12-01 15:05:20 +03:00
|
|
|
}
|
2023-03-22 14:16:36 +03:00
|
|
|
await enableLabs(page);
|
2022-12-01 03:34:19 +03:00
|
|
|
await page.context().storageState({path: storageState});
|
|
|
|
await browser.close();
|
2022-12-06 13:30:07 +03:00
|
|
|
|
|
|
|
if (!usingRemoteServer) {
|
|
|
|
await stopGhost();
|
|
|
|
}
|
2022-12-01 03:34:19 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = setup;
|