Skip to content

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
mainfrom
fix/cloud-tasks-tests
Open

test(cloud-tasks/tutorial-gcf/function): mock sendgrid package to prevent 401 errors in CI pipeline#4325
angelcaamal wants to merge 2 commits into
mainfrom
fix/cloud-tasks-tests

Conversation

@angelcaamal
Copy link
Copy Markdown
Contributor

@angelcaamal angelcaamal commented May 26, 2026

Description

This PR fixes a CI pipeline failure in the cloud-tasks/tutorial-gcf/function test suite.

The test send succeeds was previously failing with a 401 Unauthorized error (API key does not start with "SG.") because the @sendgrid/mail package 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

  • Mocked the @sendgrid/mail package using proxyquire and sinon.stub inside getSample() to isolate the tests from external network calls.
  • Removed the live Google Cloud Secret Manager API call from the before hook, replacing it with a local dummy key (SG.dummy_key_for_testing) to make the test suite 100% hermetic and offline-capable.
  • Added a payload assertion using sinon.assert.calledOnceWithExactly in the send succeeds test case to verify that the correct recipient, sender, subject, and template structure are passed to SendGrid.
  • Resolved the 401 validation error by bypassing the live SDK initialization during test runs, ensuring the test suite resolves locally with a mocked 200 status 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

  • I have followed guidelines from CONTRIBUTING.MD and Samples Style Guide
  • Tests pass: npm test (see Testing)
  • Lint pass: npm run lint (see Style)
  • Required CI tests pass (see CI testing)
  • These samples need a new API enabled in testing projects to pass (let us know which ones)
  • These samples need a new/updated env vars in testing projects set to pass (let us know which ones)
  • This pull request is from a branch created directly off of GoogleCloudPlatform/nodejs-docs-samples. Not a fork.
  • This sample adds a new sample directory, and I updated the CODEOWNERS file with the codeowners for this sample
  • This sample adds a new sample directory, and I created GitHub Actions workflow for this sample
  • This sample adds a new Product API, and I updated the Blunderbuss issue/PR auto-assigner with the codeowners for this sample
  • Please merge this PR for me once it is approved

Note: Any check with (dev), (experimental), or (legacy) can be ignored and should not block your PR from merging (see CI testing).

@angelcaamal angelcaamal added api: cloudtasks Issues related to the Cloud Tasks API. samples Issues that are directly related to samples. labels May 26, 2026
@product-auto-label product-auto-label Bot added api: tasks Issues related to the Tasks API API. asset: pattern DEE Asset tagging - Pattern. labels May 26, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cloud-tasks/tutorial-gcf/function/test/index.test.js
Comment thread cloud-tasks/tutorial-gcf/function/test/index.test.js
@angelcaamal angelcaamal marked this pull request as ready for review May 26, 2026 21:53
@angelcaamal angelcaamal requested review from a team as code owners May 26, 2026 21:53
Copy link
Copy Markdown
Contributor

@amcolin amcolin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look solid to me 👍 . Great refactor! Moving away from fetching live secrets via Secret Manager.

@product-auto-label product-auto-label Bot removed the api: tasks Issues related to the Tasks API API. label May 27, 2026
Copy link
Copy Markdown

@Kef131 Kef131 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: cloudtasks Issues related to the Cloud Tasks API. asset: pattern DEE Asset tagging - Pattern. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants