urish/firebase-server

How do I provide a credential to firebase-admin when using firebase-server?

mbifulco opened this issue · 5 comments

I'm writing test cases for a node app against firebase-server that uses firebase-admin for calls against the live database.

When I try to initialize the admin package like this, I get an error:

import admin from 'firebase-admin';
const server = new FirebaseServer(5001, 'localhost.url', MockDatabase); // eslint-disable-line no-unused-vars
const config = {
  databaseURL: 'ws://localhost.url:5000',
  credential: admin.credential.applicationDefault(),
};
admin.initializeApp(config);

the error:

  console.error node_modules/firebase-admin/lib/firebase-app.js:167
    Error: Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "getaddrinfo ENOTFOUND metadata.google.internal metadata.google.internal:80".
        at /src/node_modules/firebase-admin/lib/firebase-app.js:74:23
        at run (/src/node_modules/core-js/modules/es6.promise.js:87:22)
        at /src/node_modules/core-js/modules/es6.promise.js:100:28
        at flush (/src/node_modules/core-js/modules/_microtask.js:18:9)
        at _combinedTickCallback (internal/process/next_tick.js:67:7)
        at process._tickCallback (internal/process/next_tick.js:98:9)

So it seems that admin.credential.applicationDefault() won't work here. What syntax should I be using?

You may be able to make it stop complaining by providing an empty "stub" for the credential service:

  const config = {
    databaseURL: 'ws://localhost.url:5000',
    credential: {
      getAccessToken: () => ({
        expires_in: 0,
        access_token: '',
      }),
    },
  };

But I haven't figured out how to prevent it from attempting a network connection to Google's servers regardless. It's slowing down my unit tests.

UPDATE: It turns out this will indeed prevent any network connectivity to Google's OAuth servers. The occasional test slowdown I noticed does not seem related to network activity (verified with tcpdump). Current guess is that it's just pausing for GC occasionally.

@nfarina I'm having a little trouble with your solution. It does indeed prevent complianing but my attempts to write data are failing with Error: An internal error has occurred. Raw server response: "{"error":{"errors":[{"domain":"global","reason":"authError","message":"Invalid Credentials","locationType":"header","location":"Authorization"}],"code":401,"message":"Invalid Credentials"}}"

I've tried with and without rules. Any ideas why this might be?

Hm, not sure. I am personally not using firebase-server anymore - I only needed a simulated firebase database for testing, and the official JS client works for that purpose as long as you "put it offline" first. Although I haven't upgraded my node modules in a while, it's possible something broke with a recent update. I'm on v4.8.1.

Hi @nfarina, I'm looking back into this as I'm trying to test my firebase rules from my browser based javascript unit tests. Could you share how you are putting the official JS client "offline" when running your tests?