Skip to content

Commit c91fc2c

Browse files
test(unit): properly mock verifyWebhook using proxysquire to bypass signature checks
1 parent 6d85540 commit c91fc2c

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

functions/slack/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,7 @@ functions.http('kgSearch', async (req, res) => {
212212
}
213213
});
214214
// [END functions_slack_search]
215+
216+
module.exports = {
217+
verifyWebhook,
218+
};

functions/slack/test/unit.test.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const proxyquire = require('proxyquire').noCallThru();
1919
const assert = require('assert');
2020

2121
const {getFunction} = require('@google-cloud/functions-framework/testing');
22+
const {verifyWebhook} = require('..');
2223

2324
const method = 'POST';
2425
const query = 'giraffe';
@@ -99,16 +100,17 @@ const restoreConsole = function () {
99100
};
100101
beforeEach(stubConsole);
101102
afterEach(restoreConsole);
102-
before(async () => {
103-
const mod = require('../index.js');
104-
mod.verifyWebhook = () => {};
103+
let mod;
104+
105+
before(() => {
106+
mod = proxysquire('../index.js', {
107+
'./index.js': {
108+
verifyWebhook: () => {},
109+
},
110+
});
105111
});
106112

107113
describe('functions_slack_search', () => {
108-
before(async () => {
109-
const mod = require('../index.js');
110-
mod.verifyWebhook = () => {};
111-
});
112114
it('Send fails if not a POST request', async () => {
113115
const error = new Error('Only POST requests are accepted');
114116
error.code = 405;

0 commit comments

Comments
 (0)