@@ -24,9 +24,17 @@ import { expect } from "chai";
24
24
25
25
import * as functions from "../../../src/v1" ;
26
26
import * 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" ;
28
35
import { runHandler } from "../../helper" ;
29
36
import { MINIMAL_V1_ENDPOINT } from "../../fixtures" ;
37
+ import { CALLABLE_AUTH_HEADER , ORIGINAL_AUTH_HEADER } from "../../../src/common/providers/https" ;
30
38
31
39
describe ( "CloudHttpsBuilder" , ( ) => {
32
40
describe ( "#onRequest" , ( ) => {
@@ -66,6 +74,10 @@ describe("CloudHttpsBuilder", () => {
66
74
} ) ;
67
75
68
76
describe ( "#onCall" , ( ) => {
77
+ afterEach ( ( ) => {
78
+ sinon . verifyAndRestore ( ) ;
79
+ } ) ;
80
+
69
81
it ( "should return a trigger/endpoint with appropriate values" , ( ) => {
70
82
const result = https . onCall ( ( ) => {
71
83
return "response" ;
@@ -139,6 +151,45 @@ describe("#onCall", () => {
139
151
expect ( response . status ) . to . equal ( 200 ) ;
140
152
expect ( gotData ) . to . deep . equal ( { foo : "bar" } ) ;
141
153
} ) ;
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
+ } ) ;
142
193
} ) ;
143
194
144
195
describe ( "callable CORS" , ( ) => {
0 commit comments