firebase-functions-test icon indicating copy to clipboard operation
firebase-functions-test copied to clipboard

Using online mode and testing date logic with sinon.useFakeTimers() throws "Error: 16 UNAUTHENTICATED: Request had invalid authentication credentials."

Open dan-wu-open opened this issue 3 years ago • 1 comments
trafficstars

Version info

firebase-functions-test: 2.3.0

firebase-functions: 3.22.0

firebase-admin: 10.2.0

Steps to reproduce

  1. Initialize test app following https://firebase.google.com/docs/functions/unit-testing#online-mode
  2. Use sinon.useFakeTimers to change current date https://sinonjs.org/releases/latest/fake-timers/
  3. Any calls to firestore throws "Error: 16 UNAUTHENTICATED: Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential."

My hunch is that since useFakeTimers modifies the behavior of the Date object, it's causing the access token to be expired.

Is there any suggestions for a work around? Testing datetime logic seems pretty table stakes so I'd be surprised if no one else ran into this.

One idea was switching to offline mode, but that'd be a shame to have to do just for this kind of test.

dan-wu-open avatar Aug 31 '22 00:08 dan-wu-open

Hey @dan-wu-open

I know this is old, but do you happen to have a code snippet that produces the issue?

CorieW avatar Dec 31 '24 19:12 CorieW