From 23284d8352caf693a4f8418fc19f20224dd23ddc Mon Sep 17 00:00:00 2001 From: Angel Caamal Date: Tue, 26 May 2026 21:34:22 +0000 Subject: [PATCH 1/2] fix: mock sendgrid SDK to prevent 401 errors in CI pipeline --- cloud-tasks/tutorial-gcf/function/test/index.test.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/cloud-tasks/tutorial-gcf/function/test/index.test.js b/cloud-tasks/tutorial-gcf/function/test/index.test.js index 35d6c4b1c9..9488eeed5e 100644 --- a/cloud-tasks/tutorial-gcf/function/test/index.test.js +++ b/cloud-tasks/tutorial-gcf/function/test/index.test.js @@ -23,16 +23,17 @@ describe('tasks/function', () => { let key; const getSample = function () { - const requestPromise = sinon - .stub() - .returns(new Promise(resolve => resolve('test'))); + const sendGridStub = { + setApiKey: sinon.stub(), + send: sinon.stub().resolves([{statusCode: 200}]), + }; return { program: proxyquire('../', { - 'request-promise': requestPromise, + '@sendgrid/mail': sendGridStub, }), mocks: { - requestPromise: requestPromise, + sendGridStub: sendGridStub, }, }; }; From 40d999ab294f2460c7dede68e3d7652e4cb3f5c6 Mon Sep 17 00:00:00 2001 From: Angel Caamal Date: Tue, 26 May 2026 21:51:06 +0000 Subject: [PATCH 2/2] fix: mock sendgrid package and add payload assertions to function tests --- .../tutorial-gcf/function/test/index.test.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/cloud-tasks/tutorial-gcf/function/test/index.test.js b/cloud-tasks/tutorial-gcf/function/test/index.test.js index 9488eeed5e..dd1a8f542c 100644 --- a/cloud-tasks/tutorial-gcf/function/test/index.test.js +++ b/cloud-tasks/tutorial-gcf/function/test/index.test.js @@ -17,7 +17,6 @@ const proxyquire = require('proxyquire').noCallThru(); const sinon = require('sinon'); const assert = require('assert'); -const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); describe('tasks/function', () => { let key; @@ -55,14 +54,7 @@ describe('tasks/function', () => { }; before(async () => { - const secrets = new SecretManagerServiceClient(); - const projectId = await secrets.getProjectId(); - const secretName = 'sendgrid-api-key'; - const secretVersion = 1; - const [version] = await secrets.accessSecretVersion({ - name: secrets.secretVersionPath(projectId, secretName, secretVersion), - }); - key = version.payload.data.toString(); + key = 'SG.dummy_key_for_testing'; process.env.SENDGRID_API_KEY = key; }); @@ -168,5 +160,11 @@ describe('tasks/function', () => { assert.strictEqual(mocks.res.status.callCount, 1); assert.deepStrictEqual(mocks.res.status.firstCall.args, [200]); assert.strictEqual(mocks.res.send.callCount, 1); + sinon.assert.calledOnceWithExactly(sample.mocks.sendGridStub.send, { + to: 'to@gmail.com', + from: 'postcard@example.com', + subject: 'A Postcard Just for You!', + html: sinon.match.string, + }); }); });