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');
|
2022-12-08 15:43:07 +03:00
|
|
|
const {setupGhost, setupStripe, setupMailgun} = 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');
|
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
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
|
|
|
|
await startGhost({
|
|
|
|
frontend: true,
|
|
|
|
server: true,
|
2022-12-08 13:13:51 +03:00
|
|
|
backend: true,
|
|
|
|
mockMailgun: true
|
2022-12-06 13:30:07 +03:00
|
|
|
});
|
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
|
|
|
}
|
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;
|