@@ -24,9 +24,17 @@ import { expect } from "chai";
2424
2525import * as functions from "../../../src/v1" ;
2626import * as https from "../../../src/v1/providers/https" ;
27- import { expectedResponseHeaders , MockRequest } from "../../fixtures/mockrequest" ;
27+ import * as debug from "../../../src/common/debug" ;
28+ import * as sinon from "sinon" ;
29+ import {
30+ expectedResponseHeaders ,
31+ generateUnsignedIdToken ,
32+ MockRequest ,
33+ mockRequest ,
34+ } from "../../fixtures/mockrequest" ;
2835import { runHandler } from "../../helper" ;
2936import { MINIMAL_V1_ENDPOINT } from "../../fixtures" ;
37+ import { CALLABLE_AUTH_HEADER , ORIGINAL_AUTH_HEADER } from "../../../src/common/providers/https" ;
3038
3139describe ( "CloudHttpsBuilder" , ( ) => {
3240 describe ( "#onRequest" , ( ) => {
@@ -66,6 +74,10 @@ describe("CloudHttpsBuilder", () => {
6674} ) ;
6775
6876describe ( "#onCall" , ( ) => {
77+ afterEach ( ( ) => {
78+ sinon . verifyAndRestore ( ) ;
79+ } ) ;
80+
6981 it ( "should return a trigger/endpoint with appropriate values" , ( ) => {
7082 const result = https . onCall ( ( ) => {
7183 return "response" ;
@@ -139,6 +151,45 @@ describe("#onCall", () => {
139151 expect ( response . status ) . to . equal ( 200 ) ;
140152 expect ( gotData ) . to . deep . equal ( { foo : "bar" } ) ;
141153 } ) ;
154+
155+ // Test for firebase-tools#5210
156+ it ( "should create context.auth for v1 emulated functions" , async ( ) => {
157+ sinon . stub ( debug , "isDebugFeatureEnabled" ) . withArgs ( "skipTokenVerification" ) . returns ( true ) ;
158+
159+ let gotData : Record < string , any > ;
160+ let gotContext : Record < string , any > ;
161+ const reqData = { hello : "world" } ;
162+ const authContext = {
163+ uid : "SomeUID" ,
164+ token : {
165+ aud : "123456" ,
166+ sub : "SomeUID" ,
167+ uid : "SomeUID" ,
168+ } ,
169+ } ;
170+ const originalAuth = "Bearer " + generateUnsignedIdToken ( "123456" ) ;
171+ const func = https . onCall ( ( data , context ) => {
172+ gotData = data ;
173+ gotContext = context ;
174+ } ) ;
175+ const mockReq = mockRequest (
176+ reqData ,
177+ "application/json" ,
178+ { } ,
179+ {
180+ [ CALLABLE_AUTH_HEADER ] : encodeURIComponent ( JSON . stringify ( authContext ) ) ,
181+ [ ORIGINAL_AUTH_HEADER ] : originalAuth ,
182+ }
183+ ) ;
184+
185+ const response = await runHandler ( func , mockReq as any ) ;
186+
187+ expect ( response . status ) . to . equal ( 200 ) ;
188+ expect ( gotData ) . to . deep . eq ( reqData ) ;
189+ expect ( gotContext . rawRequest ) . to . deep . eq ( mockReq ) ;
190+ expect ( gotContext . rawRequest . headers [ "authorization" ] ) . to . eq ( originalAuth ) ;
191+ expect ( gotContext . auth ) . to . deep . eq ( authContext ) ;
192+ } ) ;
142193} ) ;
143194
144195describe ( "callable CORS" , ( ) => {
0 commit comments