-
Notifications
You must be signed in to change notification settings - Fork 2
Implement real token verification tests for HTTPS callables (matching Node.js mockrequest.ts) #91
Description
Context
Currently, the firebase_functions_dart SDK relies entirely on skipTokenVerification: true (or implicitly dropping the Admin SDK instance) in both unit and E2E tests for HTTPS callables. This means we are not testing the actual dart_firebase_admin token verification path (verifyIdToken / verifyToken).
The firebase-functions-js SDK solves this issue natively in its unit tests without relying on complex E2E environment token minting.
How the JS SDK handles it
In spec/fixtures/mockrequest.ts, the JS SDK sets up an in-memory integration test for the Admin SDK's verification logic:
- Mock Keys: They use a hardcoded test private/public keypair in their test fixtures (
key.json,jwk.json). - Local Token Minting: They mint real signed JWTs locally using the
jsonwebtokenlibrary and their fake private key. - HTTP Interception: They use
nockto intercept the outgoing HTTP request the Admin SDK makes to fetch Google's public keys (e.g.,https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com). - Verification:
nockreturns the fake public key, and the Admin SDK successfully performs the cryptographic validation.
Proposal for Dart
We should replicate this testing strategy in Dart to ensure full coverage of the token validation boundary:
- Token Generation: Use
package:dart_jsonwebtokento generate and sign properly formed JWTs locally using a test private key. - HTTP Interception: Intercept the backend HTTP requests
dart_firebase_adminattempts to make to fetch the keys. This could be achieved by:- Injecting a mocked
http.Client(e.g.,MockClientfrompackage:http/testing.dart) if the Admin SDK permits dependency injection for its internal HTTP requests. - Alternatively, using
HttpOverrides.globalin the test suite to catch globaldart:ioHTTP traffic to the Google certificate endpoints and returning a mocked payload.
- Injecting a mocked
Implementing this will give us high-confidence SDK integration tests over the authentication headers without introducing flake or demanding complex credentials in E2E.