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 b0927f4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 50 deletions.
33 changes: 12 additions & 21 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[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: NPM install
run: npm install
- name: Install firebase CLI
Expand All @@ -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
63 changes: 36 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,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),
Expand All @@ -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();

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);
});
});
2 changes: 1 addition & 1 deletion firestore-stripe-payments/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions firestore-stripe-payments/functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b0927f4

Please sign in to comment.