Skip to content

Commit

Permalink
Fixed browser tests running in docker compose (#21974)
Browse files Browse the repository at this point in the history
ref
https://linear.app/ghost/issue/ENG-1968/get-browser-tests-working-in-docker

- When running browser tests in docker compose, the `stripe listen`
command was not outputting the API key from the environment, because
it's expecting that you've already run `stripe login` to authenticate
with stripe. It only outputs the API key in the command line in CI.
- This isn't convenient/practical to do in docker, and it's much easier
to supply the API key as an environment variable, so this changes the
logic to use the API key from the command line when running in docker
_or_ CI.
  • Loading branch information
cmraible authored Jan 8, 2025
1 parent c1f9740 commit 0c56c9b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ services:
volumes:
- .:/home/ghost
tty: true
env_file:
- .env
depends_on:
- mysql
- redis
Expand Down
10 changes: 8 additions & 2 deletions ghost/core/test/e2e-browser/fixtures/ghost-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ const Stripe = require('stripe').Stripe;
const configUtils = require('../../utils/configUtils');

const startWebhookServer = (port) => {
const command = `stripe listen --forward-connect-to http://127.0.0.1:${port}/members/webhooks/stripe/ ${process.env.CI ? `--api-key ${process.env.STRIPE_SECRET_KEY}` : ''}`.trim();
const isCI = process.env.CI;
const isDocker = process.env.COMPOSE_PROFILES === 'full';
const stripeSecretKey = process.env.STRIPE_SECRET_KEY;
const command = `stripe listen --forward-connect-to http://127.0.0.1:${port}/members/webhooks/stripe/ ${isDocker || isCI ? `--api-key ${stripeSecretKey}` : ''}`.trim();
const webhookServer = spawn(command.split(' ')[0], command.split(' ').slice(1));

// Adding event listeners here seems to prevent heisenbug where webhooks aren't received
Expand All @@ -22,7 +25,10 @@ const startWebhookServer = (port) => {
};

const getWebhookSecret = async () => {
const command = `stripe listen --print-secret ${process.env.CI ? `--api-key ${process.env.STRIPE_SECRET_KEY}` : ''}`.trim();
const isCI = process.env.CI;
const isDocker = process.env.COMPOSE_PROFILES === 'full';
const stripeSecretKey = process.env.STRIPE_SECRET_KEY;
const command = `stripe listen --print-secret ${isDocker || isCI ? `--api-key ${stripeSecretKey}` : ''}`.trim();
const webhookSecret = (await promisify(exec)(command)).stdout;
return webhookSecret.toString().trim();
};
Expand Down

0 comments on commit 0c56c9b

Please sign in to comment.