Skip to content

Commit e7c4b49

Browse files
authored
adding test (#1347)
1 parent 233104c commit e7c4b49

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

spec/fixtures/mockrequest.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ export function mockRequest(
2828
authorization?: string;
2929
instanceIdToken?: string;
3030
appCheckToken?: string;
31-
} = {}
31+
} = {},
32+
reqHeaders?: Record<string, string>,
3233
) {
3334
const body: any = {};
3435
if (typeof data !== 'undefined') {
@@ -41,6 +42,7 @@ export function mockRequest(
4142
'firebase-instance-id-token': context.instanceIdToken,
4243
'x-firebase-appcheck': context.appCheckToken,
4344
origin: 'example.com',
45+
...reqHeaders,
4446
};
4547

4648
return new MockRequest(body, headers);

spec/v1/providers/https.spec.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@ import { expect } from "chai";
2424

2525
import * as functions from "../../../src/v1";
2626
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";
2835
import { runHandler } from "../../helper";
2936
import { MINIMAL_V1_ENDPOINT } from "../../fixtures";
37+
import { CALLABLE_AUTH_HEADER, ORIGINAL_AUTH_HEADER } from "../../../src/common/providers/https";
3038

3139
describe("CloudHttpsBuilder", () => {
3240
describe("#onRequest", () => {
@@ -66,6 +74,10 @@ describe("CloudHttpsBuilder", () => {
6674
});
6775

6876
describe("#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

144195
describe("callable CORS", () => {

src/common/providers/https.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ import { TaskContext } from "./tasks";
3636

3737
const JWT_REGEX = /^[a-zA-Z0-9\-_=]+?\.[a-zA-Z0-9\-_=]+?\.([a-zA-Z0-9\-_=]+)?$/;
3838

39-
const CALLABLE_AUTH_HEADER = "x-callable-context-auth";
40-
const ORIGINAL_AUTH_HEADER = "x-original-auth";
39+
/** @internal */
40+
export const CALLABLE_AUTH_HEADER = "x-callable-context-auth";
41+
/** @internal */
42+
export const ORIGINAL_AUTH_HEADER = "x-original-auth";
4143

4244
/** An express request with the wire format representation of the request body. */
4345
export interface Request extends express.Request {

0 commit comments

Comments
 (0)