Skip to content

Commit

Permalink
chore: testing
Browse files Browse the repository at this point in the history
  • Loading branch information
dackers86 committed Sep 12, 2023
1 parent 1c2345c commit 0d3e8f8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 48 deletions.
31 changes: 11 additions & 20 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
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
Expand All @@ -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
59 changes: 32 additions & 27 deletions firestore-stripe-payments/functions/__tests__/helpers/setupProxy.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
const ngrok = require('ngrok');
const fs = require('fs');
const fs = require('fs').promises;
const { parse, stringify } = require('envfile');

import { clearWebhooks, setupWebhooks, clearAllWebhooks } from './webhooks';
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 () => {
Expand All @@ -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),
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ describe('subscription webhook events', () => {

expect(prices).toBeDefined();
expect(prices.length).toBe(1);
});
}, 20000);
});
});

0 comments on commit 0d3e8f8

Please sign in to comment.