question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Callable function in emulator returns 'unauthenticated' when passing in mock auth via firebase/testing lib

See original GitHub issue

Not sure if this is the right place for this report, or if perhaps I am using the emulators/sdk incorrectly. I would like to write some integration tests and run against the Firebase emulator suite. I found the docs on testing security rules helpful, and it seems like the intention of the @firebase/testing library is to allow testing of functions/database beyond just security rules.

That doc outlines how to mock authentication by passing and auth dictionary to initializeTestApp(..), but when I use the resulting app to call an httpsCallable function, the emulator returns an ‘unauthenticated’ error.

[REQUIRED] Environment info

firebase-tools: 7.0.2 Platform: macOS 10.14.5

[REQUIRED] Test case

Here is a minimal reproduction: https://github.com/ryanmeisters/firebase-callable-emulator-test

It contains a single helloCallable function that returns {hello: "world"}, and a single test that calls the function and verifies the response. The test passes when no auth is passed into initializeTestApp, but gets ‘unauthenticated’ error if auth is passed.

[REQUIRED] Steps to reproduce

  1. Clone the above repo and npm install
  2. in the root run ./emulate.sh (compiles typescript and runs emulators)
  3. cd functions && npm run test # test fails with 'unauthenticated'
  4. comment out this line and npm run test again. Test passes

[REQUIRED] Expected behavior

The callable function should be called and receive the mock auth dictionary

[REQUIRED] Actual behavior

Function returns and unauthenticated error.

Output of firebase emulators:start --debug and function call

➜  FirebaseTestingCallable git:(master) ./emulate.sh

> functions@ build /Users/ryan/Projects/FirebaseTestingCallable/functions
> tsc

[2019-07-06T17:00:46.379Z] ----------------------------------------------------------------------
[2019-07-06T17:00:46.382Z] Command:       /Users/ryan/.nvm/versions/node/v8.15.1/bin/node /Users/ryan/.nvm/versions/node/v8.15.1/bin/firebase emulators:start --debug
[2019-07-06T17:00:46.382Z] CLI Version:   7.0.2
[2019-07-06T17:00:46.382Z] Platform:      darwin
[2019-07-06T17:00:46.382Z] Node Version:  v8.15.1
[2019-07-06T17:00:46.383Z] Time:          Sat Jul 06 2019 10:00:46 GMT-0700 (PDT)
[2019-07-06T17:00:46.383Z] ----------------------------------------------------------------------

[2019-07-06T17:00:46.392Z] > command requires scopes: ["email","openid","https://www.googleapis.com/auth/cloudplatformprojects.readonly","https://www.googleapis.com/auth/firebase","https://www.googleapis.com/auth/cloud-platform"]
[2019-07-06T17:00:46.392Z] > authorizing via signed-in user
[2019-07-06T17:00:46.394Z] > refreshing access token with scopes: ["email","https://www.googleapis.com/auth/cloud-platform","https://www.googleapis.com/auth/cloudplatformprojects.readonly","https://www.googleapis.com/auth/firebase","openid"]
[2019-07-06T17:00:46.394Z] >>> HTTP REQUEST POST https://www.googleapis.com/oauth2/v3/token
 <request body omitted>
[2019-07-06T17:00:46.686Z] <<< HTTP RESPONSE 200 content-type=application/json; charset=utf-8, vary=X-Origin, Referer, Origin,Accept-Encoding, date=Sat, 06 Jul 2019 17:00:46 GMT, server=scaffolding on HTTPServer2, cache-control=private, x-xss-protection=0, x-frame-options=SAMEORIGIN, x-content-type-options=nosniff, alt-svc=quic=":443"; ma=2592000; v="46,43,39", accept-ranges=none, transfer-encoding=chunked
[2019-07-06T17:00:46.697Z] >>> HTTP REQUEST GET https://cloudresourcemanager.googleapis.com/v1/projects/testproject-dbefa

[2019-07-06T17:00:47.139Z] <<< HTTP RESPONSE 200 content-type=application/json; charset=UTF-8, vary=X-Origin, Referer, Origin,Accept-Encoding, date=Sat, 06 Jul 2019 17:00:47 GMT, server=ESF, cache-control=private, x-xss-protection=0, x-frame-options=SAMEORIGIN, x-content-type-options=nosniff, server-timing=gfet4t7; dur=252, alt-svc=quic=":443"; ma=2592000; v="46,43,39", accept-ranges=none, transfer-encoding=chunked
i  Starting emulators: ["functions","firestore"]
✔  functions: Using node@8 from host.
[2019-07-06T17:00:47.157Z] >>> HTTP REQUEST GET https://mobilesdk-pa.googleapis.com/v1/projects/1077985712794:getServerAppConfig

[2019-07-06T17:00:47.450Z] <<< HTTP RESPONSE 200 content-type=application/json; charset=UTF-8, vary=X-Origin, Referer, Origin,Accept-Encoding, date=Sat, 06 Jul 2019 17:00:47 GMT, server=ESF, cache-control=private, x-xss-protection=0, x-frame-options=SAMEORIGIN, x-content-type-options=nosniff, alt-svc=quic=":443"; ma=2592000; v="46,43,39", accept-ranges=none, transfer-encoding=chunked
✔  functions: Emulator started at http://localhost:5001
[2019-07-06T17:00:47.716Z] Starting emulator firestore with args {"host":"localhost","port":8080,"rules":"/Users/ryan/Projects/FirebaseTestingCallable/firestore.rules","functions_emulator":"localhost:5001"}
i  firestore: Logging to firestore-debug.log
✔  firestore: Emulator started at http://localhost:8080
[2019-07-06T17:00:48.406Z] API endpoint: http://localhost:8080
[2019-07-06T17:00:48.406Z]
If you are using a library that supports the FIRESTORE_EMULATOR_HOST environment variable, run:

   export FIRESTORE_EMULATOR_HOST=localhost:8080

Dev App Server is now running.


i  firestore: For testing set FIRESTORE_EMULATOR_HOST=localhost:8080
i  functions: Watching "/Users/ryan/Projects/FirebaseTestingCallable/functions" for Cloud Functions...
[2019-07-06T17:00:48.674Z] Functions runtime initialized.
[2019-07-06T17:00:48.675Z] Disabled runtime features: undefined
[2019-07-06T17:00:48.795Z] Found google-gax at /Users/ryan/Projects/FirebaseTestingCallable/functions/node_modules/google-gax/build/src/index.js
[2019-07-06T17:00:48.795Z] Outgoing network have been stubbed.
[2019-07-06T17:00:48.823Z] Checked functions.config()
[2019-07-06T17:00:48.826Z] firebase-admin has been stubbed.
[2019-07-06T17:00:48.827Z] config() parent accessed!
i  functions: HTTP trigger initialized at http://localhost:5001/testproject-dbefa/us-central1/helloCallable
[2019-07-06T17:01:11.648Z] File /Users/ryan/Projects/FirebaseTestingCallable/functions/lib/index.tests.js.map changed, reloading triggers
[2019-07-06T17:01:11.648Z] File /Users/ryan/Projects/FirebaseTestingCallable/functions/lib/index.tests.js changed, reloading triggers
[2019-07-06T17:01:11.649Z] File /Users/ryan/Projects/FirebaseTestingCallable/functions/lib/index.js.map changed, reloading triggers
[2019-07-06T17:01:11.649Z] File /Users/ryan/Projects/FirebaseTestingCallable/functions/lib/index.js changed, reloading triggers
[2019-07-06T17:01:12.794Z] Accepted request POST /testproject-dbefa/us-central1/helloCallable --> helloCallable
[2019-07-06T17:01:12.838Z] Functions runtime initialized.
[2019-07-06T17:01:12.839Z] Disabled runtime features: undefined
[2019-07-06T17:01:12.955Z] Found google-gax at /Users/ryan/Projects/FirebaseTestingCallable/functions/node_modules/google-gax/build/src/index.js
[2019-07-06T17:01:12.955Z] Outgoing network have been stubbed.
[2019-07-06T17:01:12.987Z] Checked functions.config()
[2019-07-06T17:01:12.987Z] Functions runtime initialized.
i  functions: Beginning execution of "helloCallable"
[2019-07-06T17:01:12.988Z] Disabled runtime features: undefined
[2019-07-06T17:01:12.989Z] firebase-admin has been stubbed.
[2019-07-06T17:01:12.989Z] config() parent accessed!
[2019-07-06T17:01:13.101Z] Found google-gax at /Users/ryan/Projects/FirebaseTestingCallable/functions/node_modules/google-gax/build/src/index.js
[2019-07-06T17:01:13.101Z] Outgoing network have been stubbed.
[2019-07-06T17:01:13.130Z] Checked functions.config()
[2019-07-06T17:01:13.131Z] firebase-admin has been stubbed.
[2019-07-06T17:01:13.132Z] config() parent accessed!
[2019-07-06T17:01:13.133Z] Trigger "helloCallable" has been found, beginning invocation!
[2019-07-06T17:01:13.133Z]
[2019-07-06T17:01:13.133Z] Running helloCallable in mode HTTPS
[2019-07-06T17:01:13.139Z] {"socketPath":"/var/folders/8j/lw39pjg97xbckxh0bk7rtw8r0000gn/T/firebase_emulator_invocation_74956.sock"}

Output of npm run test

➜  functions git:(master) ✗ npm run test

> functions@ test /Users/ryan/Projects/FirebaseTestingCallable/functions
> tsc && ava -v --serial


  ✖ we are able to call the callable function Rejected promise returned by test

  1 test failed

  we are able to call the callable function

  /Users/ryan/Projects/FirebaseTestingCallable/functions/node_modules/@firebase/functions/src/api/error.ts:66

  Rejected promise returned by test. Reason:

  Error (HttpsErrorImpl) {
    code: 'unauthenticated',
    details: undefined,
    message: 'Unauthenticated',
  }

  new HttpsErrorImpl (node_modules/@firebase/functions/src/api/error.ts:66:5)
  _errorForResponse (node_modules/@firebase/functions/src/api/error.ts:175:10)
  Service.<anonymous> (node_modules/@firebase/functions/src/api/service.ts:226:19)
  step (node_modules/tslib/tslib.js:133:27)
  Object.next (node_modules/tslib/tslib.js:114:57)
  fulfilled (node_modules/tslib/tslib.js:104:62)

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! functions@ test: `tsc && ava -v --serial`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the functions@ test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/ryan/.npm/_logs/2019-07-06T17_01_13_235Z-debug.log

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:18 (10 by maintainers)

github_iconTop GitHub Comments

5reactions
samtsterncommented, Jul 11, 2019

@ryanmeisters by the way you can check if (process.env.FUNCTIONS_EMULATOR) to know when your code is running in the emulator and when it’s not

3reactions
samtsterncommented, Mar 30, 2020

Version 7.16.2 has been released.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Build unit tests | Firebase Security Rules - Google
This method creates a RulesTestContext , which behaves like an authenticated Authentication user. Requests created via the returned context will have a mock...
Read more >
Unauthenticated Error In Firebase Firestore Https Callable ...
Callable function in emulator returns 'unauthenticated' when passing in mock auth via firebase/testing lib # ryanmeisters opened this issue on Jul 6, ...
Read more >
How to call a firebase functions from a unit test using ...
My other tests already interact with a database mocked with Firebase Emulators, why not functions too? I'm testing the whole end2end flow, for ......
Read more >
Unit testing security rules with the Firebase Emulator Suite
Unit testing is like flossing; we all know we should be doing it, but not all of us actually are. And there's no...
Read more >
firebase
I can make an authed call test by using test.wrap(fn)(data, {auth: uid: 'anyUid'}) , but I also need to test for anonymous authentication...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found