Skip to content

Commit c6fd8fc

Browse files
authored
chore(*): add proxy tunnel for testing in ci
1 parent 9ab8170 commit c6fd8fc

File tree

16 files changed

+150
-94
lines changed

16 files changed

+150
-94
lines changed

.github/workflows/test.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ on:
1010

1111
jobs:
1212
nodejs:
13+
if:
14+
github.event_name == 'push' || (github.event_name == 'pull_request' &&
15+
github.event.pull_request.head.repo.full_name != github.repository)
1316
runs-on: ubuntu-latest
1417
concurrency:
1518
group: ${{ github.workflow }}
@@ -25,6 +28,15 @@ jobs:
2528
uses: actions/setup-node@v3
2629
with:
2730
node-version: ${{ matrix.node }}
31+
32+
- name: Install cloudflared
33+
run: |
34+
wget -q https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb
35+
sudo dpkg -i cloudflared-linux-amd64.deb
36+
sudo apt-get update
37+
sudo apt-get install -f
38+
shell: /usr/bin/bash -e {0}
39+
2840
- name: NPM install
2941
run: npm install
3042
- name: Install firebase CLI
@@ -40,3 +52,6 @@ jobs:
4052
run: npm run test
4153
env:
4254
STRIPE_API_KEY: ${{ secrets.STRIPE_API_KEY }}
55+
STRIPE_WEBHOOK_SECRET: ${{ secrets.STRIPE_WEBHOOK_SECRET }}
56+
WEBHOOK_URL: ${{ secrets.WEBHOOK_URL }}
57+
CLOUDFLARE_SECRET: ${{ secrets.CLOUDFLARE_SECRET }}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
LOCATION=us-central1
2+
PROJECT_ID=demo-project
3+
PRODUCTS_COLLECTION=products
4+
CUSTOMERS_COLLECTION=customers
5+
SYNC_USERS_ON_CREATE=Sync
6+
DELETE_STRIPE_CUSTOMERS=Auto delete
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
STRIPE_API_KEY=
1+
STRIPE_API_KEY=
2+
STRIPE_WEBHOOK_SECRET=

firestore-stripe-payments/_emulator/functions/index.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
* See a full list of supported triggers at https://firebase.google.com/docs/functions
88
*/
99

10-
const { onRequest } = require("firebase-functions/v2/https");
11-
const logger = require("firebase-functions/logger");
12-
1310
// Create and deploy your first functions
1411
// https://firebase.google.com/docs/functions/get-started
1512

firestore-stripe-payments/functions/__tests__/helpers/setupProxy.ts

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,58 @@
11
const ngrok = require('ngrok');
2-
const fs = require('fs');
2+
const fs = require('fs').promises;
33
const { parse, stringify } = require('envfile');
44

55
import { clearWebhooks, setupWebhooks, clearAllWebhooks } from './webhooks';
66
import { pathTosecretsFile, pathToenvFile } from './setupEnvironment';
77
import { setupEnvironment } from './setupEnvironment';
88

99
async function setEnv(key: string, value, isSecret?: boolean) {
10-
return new Promise((resolve, reject) => {
11-
/** Load Stripe key into env */
12-
setupEnvironment();
13-
14-
const path = isSecret ? pathTosecretsFile : pathToenvFile;
15-
16-
fs.readFile(path, 'utf8', function (err, data) {
17-
if (err) {
18-
return reject(err);
19-
}
20-
var result = parse(data);
21-
result[key] = value;
22-
23-
fs.writeFile(path, stringify(result), (err) => {
24-
if (err) {
25-
return reject(err);
26-
}
27-
return resolve('Completed');
28-
});
29-
});
30-
});
10+
/** Load Stripe key into env */
11+
setupEnvironment();
12+
13+
const path = isSecret ? pathTosecretsFile : pathToenvFile;
14+
15+
const data = await fs.readFile(path, 'utf8');
16+
17+
var result = parse(data);
18+
result[key] = value;
19+
20+
await fs.writeFile(path, stringify(result));
3121
}
3222

3323
export const setupProxy = async () => {
34-
/** Set Stripe secret if provided or running in CI */
24+
/** Set Stripe secret and webhooks if provided or running in CI */
3525
if (process.env.STRIPE_API_KEY) {
3626
await setEnv('STRIPE_API_KEY', process.env.STRIPE_API_KEY, true);
27+
await setEnv(
28+
'STRIPE_WEBHOOK_SECRET',
29+
process.env.STRIPE_WEBHOOK_SECRET,
30+
true
31+
);
32+
await setEnv('WEBHOOK_URL', process.env.STRIPE_API_KEY);
3733
}
3834

3935
/** Load Stripe key before initialisation */
40-
fs.readFile(pathTosecretsFile, 'utf8', (err, data) => {
41-
const { STRIPE_API_KEY } = parse(data);
42-
process.env.STRIPE_API_KEY = STRIPE_API_KEY;
43-
});
44-
45-
const PROXY_URL = await ngrok.connect(5001);
46-
const webhook = await setupWebhooks(
47-
`${PROXY_URL}/demo-project/us-central1/ext-firestore-stripe-payments-handleWebhookEvents`
36+
const secretsEnv = await fs.readFile(pathTosecretsFile, 'utf8');
37+
const paramsEnv = await fs.readFile(pathToenvFile, 'utf8');
38+
const { STRIPE_API_KEY, STRIPE_WEBHOOK_SECRET } = parse(secretsEnv);
39+
const { WEBHOOK_URL } = parse(paramsEnv);
40+
41+
/** Set configurable params */
42+
process.env.STRIPE_API_KEY = STRIPE_API_KEY;
43+
process.env.STRIPE_WEBHOOK_SECRET = STRIPE_WEBHOOK_SECRET;
44+
process.env.WEBHOOK_URL = WEBHOOK_URL;
45+
46+
console.log(
47+
'process.env.STRIPE_WEBHOOK_SECRET',
48+
process.env.STRIPE_WEBHOOK_SECRET
4849
);
4950

5051
await Promise.all([
51-
await setEnv('STRIPE_WEBHOOK_SECRET', webhook.secret, true),
52-
await setEnv('WEBHOOK_URL', webhook.url),
53-
await setEnv('WEBHOOK_ID', webhook.id),
52+
await setEnv('STRIPE_API_KEY', STRIPE_API_KEY, true),
53+
await setEnv('STRIPE_WEBHOOK_SECRET', STRIPE_WEBHOOK_SECRET, true),
54+
await setEnv('WEBHOOK_URL', WEBHOOK_URL),
55+
// await setEnv('WEBHOOK_ID', webhook.id),
5456
await setEnv('LOCATION', 'us-central1'),
5557
await setEnv('PROJECT_ID', 'demo-project'),
5658
await setEnv('PRODUCTS_COLLECTION', 'products'),
@@ -62,7 +64,9 @@ export const setupProxy = async () => {
6264
/** Load additional key into env */
6365
setupEnvironment();
6466

65-
return webhook.id;
67+
// return webhook.id;
68+
69+
return;
6670
};
6771

6872
export const cleanupProxy = async (webhookUrl) => {

firestore-stripe-payments/functions/__tests__/helpers/stripeApi/subscriptions.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,21 @@ export const createRandomSubscription = async (
2323
product: product.id,
2424
});
2525

26-
/** create payment method */
27-
const paymentMethod = await stripe.paymentMethods.create({
28-
type: 'card',
29-
card: {
30-
number: '4242424242424242',
31-
exp_month: 5,
32-
exp_year: new Date().getFullYear() + 1,
33-
cvc: '314',
34-
},
35-
});
26+
/** Attach the test PaymentMethod to the customer */
27+
const attachedPaymentMethod = await stripe.paymentMethods.attach(
28+
'pm_card_visa',
29+
{ customer: customer }
30+
);
3631

37-
/** attach payment method to customer */
38-
await stripe.paymentMethods.attach(paymentMethod.id, { customer });
32+
/** Update the customer's default PaymentMethod */
3933
await stripe.customers.update(customer, {
40-
invoice_settings: { default_payment_method: paymentMethod.id },
34+
invoice_settings: { default_payment_method: attachedPaymentMethod.id },
4135
});
4236

43-
/** Create a product */
37+
/** Create a subscription */
4438
const subscription: Subscription = await stripe.subscriptions.create({
45-
customer,
39+
customer: customer,
4640
items: [{ price: price.id }],
47-
payment_settings: {
48-
payment_method_types: ['card'],
49-
},
5041
});
5142

5243
return Promise.resolve(subscription);

firestore-stripe-payments/functions/__tests__/run-script-watch.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const concurrently = require('concurrently');
33
import { setupProxy, cleanupProxy } from './helpers/setupProxy';
44

55
(async () => {
6-
const proxyId = await setupProxy();
6+
// const proxyId = await setupProxy();
77

88
const { result } = await concurrently(
99
[
@@ -15,9 +15,9 @@ import { setupProxy, cleanupProxy } from './helpers/setupProxy';
1515
{}
1616
);
1717

18-
await result.then(async () => {
19-
await cleanupProxy(proxyId);
20-
console.log('Removed webhook ', proxyId);
21-
process.exit(0);
22-
});
18+
// await result.then(async () => {
19+
// await cleanupProxy(proxyId);
20+
// console.log('Removed webhook ', proxyId);
21+
// process.exit(0);
22+
// });
2323
})();

firestore-stripe-payments/functions/__tests__/run-script.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import {
2424
{}
2525
);
2626

27-
await result.then(async () => {
28-
await cleanupProxy(proxyId);
29-
console.log('Removed webhook ', proxyId);
30-
process.exit(0);
31-
});
27+
// await result.then(async () => {
28+
// await cleanupProxy(proxyId);
29+
// console.log('Removed webhook ', proxyId);
30+
// process.exit(0);
31+
// });
3232
})();

firestore-stripe-payments/functions/__tests__/tests/webhookevents/subscriptions.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ describe('subscription webhook events', () => {
6565

6666
expect(prices).toBeDefined();
6767
expect(prices.length).toBe(1);
68-
});
68+
}, 20000);
6969
});
7070
});

firestore-stripe-payments/functions/jest.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ module.exports = {
2121
],
2222
setupFiles: ['<rootDir>/__tests__/jest.setup.ts'],
2323
moduleNameMapper: {
24+
'firebase-admin/firestore':
25+
'<rootDir>/node_modules/firebase-admin/lib/firestore',
2426
'firebase-admin/eventarc':
2527
'<rootDir>/node_modules/firebase-admin/lib/eventarc/index.js',
2628
},

0 commit comments

Comments
 (0)