diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 34f93c02..7c3c1405 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,28 +20,22 @@ 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: Accessing the tunnel url + run: echo ${{ steps.expose-tunnel.outputs.tunnel-url }} >> $PROXY_URL - name: NPM install run: npm install - name: Install firebase CLI @@ -57,6 +51,3 @@ jobs: run: 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..0e20c6fd 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,30 @@ 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; + + /** Check for client proxy url */ + const envOptions = + process.env.PROXY_URL || (await fs.readFile(pathToenvFile, 'utf8')); + let { PROXY_URL } = parse(envOptions); + + console.log('process.env.PROXY_URL', 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 +62,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); }); });