Skip to content

Commit af55194

Browse files
authored
test(cloud-tasks/tutorial-gcf/function): mock sendgrid package to prevent 401 errors in CI pipeline (#4325)
* fix: mock sendgrid SDK to prevent 401 errors in CI pipeline * fix: mock sendgrid package and add payload assertions to function tests
1 parent 450fac6 commit af55194

1 file changed

Lines changed: 13 additions & 14 deletions

File tree

cloud-tasks/tutorial-gcf/function/test/index.test.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@
1717
const proxyquire = require('proxyquire').noCallThru();
1818
const sinon = require('sinon');
1919
const assert = require('assert');
20-
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
2120

2221
describe('tasks/function', () => {
2322
let key;
2423

2524
const getSample = function () {
26-
const requestPromise = sinon
27-
.stub()
28-
.returns(new Promise(resolve => resolve('test')));
25+
const sendGridStub = {
26+
setApiKey: sinon.stub(),
27+
send: sinon.stub().resolves([{statusCode: 200}]),
28+
};
2929

3030
return {
3131
program: proxyquire('../', {
32-
'request-promise': requestPromise,
32+
'@sendgrid/mail': sendGridStub,
3333
}),
3434
mocks: {
35-
requestPromise: requestPromise,
35+
sendGridStub: sendGridStub,
3636
},
3737
};
3838
};
@@ -54,14 +54,7 @@ describe('tasks/function', () => {
5454
};
5555

5656
before(async () => {
57-
const secrets = new SecretManagerServiceClient();
58-
const projectId = await secrets.getProjectId();
59-
const secretName = 'sendgrid-api-key';
60-
const secretVersion = 1;
61-
const [version] = await secrets.accessSecretVersion({
62-
name: secrets.secretVersionPath(projectId, secretName, secretVersion),
63-
});
64-
key = version.payload.data.toString();
57+
key = 'SG.dummy_key_for_testing';
6558
process.env.SENDGRID_API_KEY = key;
6659
});
6760

@@ -167,5 +160,11 @@ describe('tasks/function', () => {
167160
assert.strictEqual(mocks.res.status.callCount, 1);
168161
assert.deepStrictEqual(mocks.res.status.firstCall.args, [200]);
169162
assert.strictEqual(mocks.res.send.callCount, 1);
163+
sinon.assert.calledOnceWithExactly(sample.mocks.sendGridStub.send, {
164+
to: 'to@gmail.com',
165+
from: 'postcard@example.com',
166+
subject: 'A Postcard Just for You!',
167+
html: sinon.match.string,
168+
});
170169
});
171170
});

0 commit comments

Comments
 (0)