Skip to content

Commit fbf48db

Browse files
test(webhook): remove deprecated events-api mocks and update signature verification tests
1 parent e2d1b2b commit fbf48db

File tree

3 files changed

+8
-33
lines changed

3 files changed

+8
-33
lines changed

functions/slack/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
// [START functions_slack_setup]
1818
const functions = require('@google-cloud/functions-framework');
1919
const google = require('@googleapis/kgsearch');
20-
const crypto = require('crypto')
20+
const crypto = require('crypto');
2121

2222
// Get a reference to the Knowledge Graph Search component
2323
const kgsearch = google.kgsearch('v1');

functions/slack/test/integration.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ describe('functions_slack_format functions_slack_request functions_slack_search
101101
const query = 'kolach';
102102

103103
const server = functionsFramework.getTestServer('kgSearch');
104-
await supertest(server).post('/').send({text: query}).expect(500);
104+
await supertest(server)
105+
.post('/')
106+
.set({
107+
'x-slack-request-timestamp': Date.now().toString(),
108+
})
109+
.send({text: query})
110+
.expect(401);
105111
});
106112
});

functions/slack/test/unit.test.js

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,11 @@ const getSample = () => {
3838
const googleapis = {
3939
kgsearch: sinon.stub().returns(kgsearch),
4040
};
41-
const eventsApi = {
42-
verifyRequestSignature: sinon.stub().returns(true),
43-
};
4441

4542
return {
4643
program: proxyquire('../', {
4744
'@googleapis/kgsearch': googleapis,
4845
process: {env: config},
49-
'@slack/events-api': eventsApi,
5046
}),
5147
mocks: {
5248
googleapis: googleapis,
@@ -127,33 +123,6 @@ describe('functions_slack_search', () => {
127123
});
128124
});
129125

130-
describe('functions_slack_search functions_verify_webhook', () => {
131-
it('Throws if invalid slack token', async () => {
132-
const error = new Error('Invalid credentials');
133-
error.code = 401;
134-
const mocks = getMocks();
135-
const sample = getSample();
136-
137-
mocks.req.method = method;
138-
mocks.req.body.text = 'not empty';
139-
sample.mocks.eventsApi.verifyRequestSignature = sinon.stub().returns(false);
140-
141-
const kgSearch = getFunction('kgSearch');
142-
143-
try {
144-
await kgSearch(mocks.req, mocks.res);
145-
} catch (err) {
146-
assert.deepStrictEqual(err, error);
147-
assert.strictEqual(mocks.res.status.callCount, 1);
148-
assert.deepStrictEqual(mocks.res.status.firstCall.args, [error.code]);
149-
assert.strictEqual(mocks.res.send.callCount, 1);
150-
assert.deepStrictEqual(mocks.res.send.firstCall.args, [error]);
151-
assert.strictEqual(console.error.callCount, 1);
152-
assert.deepStrictEqual(console.error.firstCall.args, [error]);
153-
}
154-
});
155-
});
156-
157126
describe('functions_slack_request functions_slack_search functions_verify_webhook', () => {
158127
it('Handles search error', async () => {
159128
const error = new Error('error');

0 commit comments

Comments
 (0)