From b0927f431d81c9cd2efb4a1a726f45ee44cb7f24 Mon Sep 17 00:00:00 2001 From: Darren Ackers Date: Mon, 11 Sep 2023 08:41:57 +0100 Subject: [PATCH] chore: testing --- .github/workflows/test.yml | 33 ++++------ .../functions/__tests__/helpers/setupProxy.ts | 63 +++++++++++-------- .../tests/webhookevents/subscriptions.test.ts | 2 +- .../functions/package.json | 2 +- .../functions/src/index.ts | 1 + 5 files changed, 51 insertions(+), 50 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 34f93c02..42759752 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,28 +20,20 @@ jobs: max-parallel: 1 name: node.js_${{ matrix.node }}_test steps: - - name: Check CLOUDFLARE_TUNNEL_ID - id: check_tunnel - run: | - if [[ -n "${{ secrets.CLOUDFLARE_TUNNEL_ID }}" ]]; then - echo "::save-state name=useCfTunnel::true" - else - echo "::save-state name=useCfTunnel::false" - fi - - name: Setup cloudflared - if: steps.check_tunnel.outputs.useCfTunnel == 'true' - uses: AnimMouse/setup-cloudflared@v1 - with: - cloudflare_tunnel_credential: - ${{ secrets.CLOUDFLARE_TUNNEL_CREDENTIAL }} - cloudflare_tunnel_configuration: - ${{ secrets.CLOUDFLARE_TUNNEL_CONFIGURATION }} - cloudflare_tunnel_id: ${{ secrets.CLOUDFLARE_TUNNEL_ID }} - uses: actions/checkout@v3 - name: Setup node uses: actions/setup-node@v3 with: node-version: ${{ matrix.node }} + - uses: codetalkio/expose-tunnel@v1.5.0 + id: expose-tunnel + with: + service: bore.pub + port: 5001 + - name: Accessing the tunnel url + run: | + echo "Tunnel has been started at '${{ steps.expose-tunnel.outputs.tunnel-url }}'" + - name: NPM install run: npm install - name: Install firebase CLI @@ -54,9 +46,8 @@ jobs: - name: mask env paramaters run: echo "::add-mask::$STRIPE_WEBHOOK_SECRET" - name: Run tests with coverage - run: npm run test + run: | + export PROXY_URL=${{ steps.expose-tunnel.outputs.tunnel-url }}/demo-project/us-central1/ext-firestore-stripe-payments-handleWebhookEvents + npm run test env: STRIPE_API_KEY: ${{ secrets.STRIPE_API_KEY }} - - name: Shutdown and view logs of cloudflared - if: always() && steps.check_tunnel.outputs.useCfTunnel == 'true' - uses: AnimMouse/setup-cloudflared/shutdown@v1 diff --git a/firestore-stripe-payments/functions/__tests__/helpers/setupProxy.ts b/firestore-stripe-payments/functions/__tests__/helpers/setupProxy.ts index e408f1b9..3123fc23 100644 --- a/firestore-stripe-payments/functions/__tests__/helpers/setupProxy.ts +++ b/firestore-stripe-payments/functions/__tests__/helpers/setupProxy.ts @@ -1,5 +1,5 @@ const ngrok = require('ngrok'); -const fs = require('fs'); +const fs = require('fs').promises; const { parse, stringify } = require('envfile'); import { clearWebhooks, setupWebhooks, clearAllWebhooks } from './webhooks'; @@ -7,27 +7,17 @@ import { pathTosecretsFile, pathToenvFile } from './setupEnvironment'; import { setupEnvironment } from './setupEnvironment'; async function setEnv(key: string, value, isSecret?: boolean) { - return new Promise((resolve, reject) => { - /** Load Stripe key into env */ - setupEnvironment(); - - const path = isSecret ? pathTosecretsFile : pathToenvFile; - - fs.readFile(path, 'utf8', function (err, data) { - if (err) { - return reject(err); - } - var result = parse(data); - result[key] = value; - - fs.writeFile(path, stringify(result), (err) => { - if (err) { - return reject(err); - } - return resolve('Completed'); - }); - }); - }); + /** Load Stripe key into env */ + setupEnvironment(); + + const path = isSecret ? pathTosecretsFile : pathToenvFile; + + const data = await fs.readFile(path, 'utf8'); + + var result = parse(data); + result[key] = value; + + await fs.writeFile(path, stringify(result)); } export const setupProxy = async () => { @@ -37,17 +27,34 @@ export const setupProxy = async () => { } /** Load Stripe key before initialisation */ - fs.readFile(pathTosecretsFile, 'utf8', (err, data) => { - const { STRIPE_API_KEY } = parse(data); - process.env.STRIPE_API_KEY = STRIPE_API_KEY; - }); + const secretsEnv = await fs.readFile(pathTosecretsFile, 'utf8'); + const { STRIPE_API_KEY } = parse(secretsEnv); + process.env.STRIPE_API_KEY = STRIPE_API_KEY; + + console.log('process.env.PROXY_URL', process.env.PROXY_URL); + + /** Check for client proxy url */ + let PROXY_URL = process.env.PROXY_URL; + + if (!PROXY_URL) { + const envOptions = await fs.readFile(pathToenvFile, 'utf8'); + const opts = parse(envOptions); + PROXY_URL = opts.PROXY_URL; + } + + if (!PROXY_URL) { + console.info('No proxy tunnel provided, using Ngrok to create a tunnel'); + PROXY_URL = await ngrok.connect(5001); + } - const PROXY_URL = await ngrok.connect(5001); const webhook = await setupWebhooks( `${PROXY_URL}/demo-project/us-central1/ext-firestore-stripe-payments-handleWebhookEvents` ); + console.log('here zero one >>>'); + await Promise.all([ + await setEnv('STRIPE_API_KEY', STRIPE_API_KEY, true), await setEnv('STRIPE_WEBHOOK_SECRET', webhook.secret, true), await setEnv('WEBHOOK_URL', webhook.url), await setEnv('WEBHOOK_ID', webhook.id), @@ -59,6 +66,8 @@ export const setupProxy = async () => { await setEnv('DELETE_STRIPE_CUSTOMERS', 'Auto delete'), ]); + console.log('here two >>>>>'); + /** Load additional key into env */ setupEnvironment(); diff --git a/firestore-stripe-payments/functions/__tests__/tests/webhookevents/subscriptions.test.ts b/firestore-stripe-payments/functions/__tests__/tests/webhookevents/subscriptions.test.ts index 1b4dff86..7fa88869 100644 --- a/firestore-stripe-payments/functions/__tests__/tests/webhookevents/subscriptions.test.ts +++ b/firestore-stripe-payments/functions/__tests__/tests/webhookevents/subscriptions.test.ts @@ -65,6 +65,6 @@ describe('subscription webhook events', () => { expect(prices).toBeDefined(); expect(prices.length).toBe(1); - }); + }, 20000); }); }); diff --git a/firestore-stripe-payments/functions/package.json b/firestore-stripe-payments/functions/package.json index 510a9a18..cdcb2184 100644 --- a/firestore-stripe-payments/functions/package.json +++ b/firestore-stripe-payments/functions/package.json @@ -11,7 +11,7 @@ "build:watch": "tsc --watch", "compile": "tsc", "generate-readme": "firebase ext:info .. --markdown > ../README.md", - "test": "ts-node ./__tests__/run-script.ts", + "test": "PROXY_URL=$PROXY_URL ts-node ./__tests__/run-script.ts", "test:watch": "ts-node ./__tests__/run-script-watch.ts", "start:emulator": "cd ../_emulator && firebase emulators:start -P demo-project", "exec:emulator": "cd ../_emulator && firebase emulators:exec \"../runTests.sh\" -P demo-project", diff --git a/firestore-stripe-payments/functions/src/index.ts b/firestore-stripe-payments/functions/src/index.ts index ed5e451b..13ea8da0 100644 --- a/firestore-stripe-payments/functions/src/index.ts +++ b/firestore-stripe-payments/functions/src/index.ts @@ -796,6 +796,7 @@ export const handleWebhookEvents = functions.handler.https.onRequest( case 'customer.subscription.created': case 'customer.subscription.updated': case 'customer.subscription.deleted': + console.log('Test one >>>>'); const subscription = event.data.object as Stripe.Subscription; await manageSubscriptionStatusChange( subscription.id,