test(cloud-tasks/tutorial-gcf/function): mock sendgrid package to prevent 401 errors in CI pipeline#4325
Open
angelcaamal wants to merge 2 commits into
Open
test(cloud-tasks/tutorial-gcf/function): mock sendgrid package to prevent 401 errors in CI pipeline#4325angelcaamal wants to merge 2 commits into
angelcaamal wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the test suite for the Cloud Tasks tutorial function by replacing the request-promise mock with a mock for @sendgrid/mail. The reviewer suggested two valuable improvements: first, since the SendGrid API is now mocked, the tests can be made fully hermetic and offline-friendly by replacing the Secret Manager API call in the before hook with a dummy API key; second, the newly exposed sendGridStub should be utilized in assertions to verify that the email payload is constructed and sent correctly.
amcolin
approved these changes
May 26, 2026
Contributor
amcolin
left a comment
There was a problem hiding this comment.
Changes look solid to me 👍 . Great refactor! Moving away from fetching live secrets via Secret Manager.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a CI pipeline failure in the
cloud-tasks/tutorial-gcf/functiontest suite.The test
send succeedswas previously failing with a401 Unauthorizederror (API key does not start with "SG.") because the@sendgrid/mailpackage was making actual HTTP requests to the SendGrid API during the test execution. Since the CI environment lacks a valid production API key, the SDK rejected the request.Changes
@sendgrid/mailpackage usingproxyquireandsinon.stubinsidegetSample()to isolate the tests from external network calls.beforehook, replacing it with a local dummy key (SG.dummy_key_for_testing) to make the test suite 100% hermetic and offline-capable.sinon.assert.calledOnceWithExactlyin thesend succeedstest case to verify that the correct recipient, sender, subject, and template structure are passed to SendGrid.401validation error by bypassing the live SDK initialization during test runs, ensuring the test suite resolves locally with a mocked200status code.Fixes Internal: b/516872192
Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google.
Checklist
npm test(see Testing)npm run lint(see Style)GoogleCloudPlatform/nodejs-docs-samples. Not a fork.