Skip to content

Add test case for #1345 #1347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion spec/fixtures/mockrequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export function mockRequest(
authorization?: string;
instanceIdToken?: string;
appCheckToken?: string;
} = {}
} = {},
reqHeaders?: Record<string, string>,
) {
const body: any = {};
if (typeof data !== 'undefined') {
Expand All @@ -41,6 +42,7 @@ export function mockRequest(
'firebase-instance-id-token': context.instanceIdToken,
'x-firebase-appcheck': context.appCheckToken,
origin: 'example.com',
...reqHeaders,
};

return new MockRequest(body, headers);
Expand Down
53 changes: 52 additions & 1 deletion spec/v1/providers/https.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ import { expect } from "chai";

import * as functions from "../../../src/v1";
import * as https from "../../../src/v1/providers/https";
import { expectedResponseHeaders, MockRequest } from "../../fixtures/mockrequest";
import * as debug from "../../../src/common/debug";
import * as sinon from "sinon";
import {
expectedResponseHeaders,
generateUnsignedIdToken,
MockRequest,
mockRequest,
} from "../../fixtures/mockrequest";
import { runHandler } from "../../helper";
import { MINIMAL_V1_ENDPOINT } from "../../fixtures";
import { CALLABLE_AUTH_HEADER, ORIGINAL_AUTH_HEADER } from "../../../src/common/providers/https";

describe("CloudHttpsBuilder", () => {
describe("#onRequest", () => {
Expand Down Expand Up @@ -66,6 +74,10 @@ describe("CloudHttpsBuilder", () => {
});

describe("#onCall", () => {
afterEach(() => {
sinon.verifyAndRestore();
});

it("should return a trigger/endpoint with appropriate values", () => {
const result = https.onCall(() => {
return "response";
Expand Down Expand Up @@ -139,6 +151,45 @@ describe("#onCall", () => {
expect(response.status).to.equal(200);
expect(gotData).to.deep.equal({ foo: "bar" });
});

// Test for firebase-tools#5210
it("should create context.auth for v1 emulated functions", async () => {
sinon.stub(debug, "isDebugFeatureEnabled").withArgs("skipTokenVerification").returns(true);

let gotData: Record<string, any>;
let gotContext: Record<string, any>;
const reqData = { hello: "world" };
const authContext = {
uid: "SomeUID",
token: {
aud: "123456",
sub: "SomeUID",
uid: "SomeUID",
},
};
const originalAuth = "Bearer " + generateUnsignedIdToken("123456");
const func = https.onCall((data, context) => {
gotData = data;
gotContext = context;
});
const mockReq = mockRequest(
reqData,
"application/json",
{},
{
[CALLABLE_AUTH_HEADER]: encodeURIComponent(JSON.stringify(authContext)),
[ORIGINAL_AUTH_HEADER]: originalAuth,
}
);

const response = await runHandler(func, mockReq as any);

expect(response.status).to.equal(200);
expect(gotData).to.deep.eq(reqData);
expect(gotContext.rawRequest).to.deep.eq(mockReq);
expect(gotContext.rawRequest.headers["authorization"]).to.eq(originalAuth);
expect(gotContext.auth).to.deep.eq(authContext);
});
});

describe("callable CORS", () => {
Expand Down
6 changes: 4 additions & 2 deletions src/common/providers/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ import { TaskContext } from "./tasks";

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

const CALLABLE_AUTH_HEADER = "x-callable-context-auth";
const ORIGINAL_AUTH_HEADER = "x-original-auth";
/** @internal */
export const CALLABLE_AUTH_HEADER = "x-callable-context-auth";
/** @internal */
export const ORIGINAL_AUTH_HEADER = "x-original-auth";

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