Skip to content

How to pass auth in CallableRequest? #260

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

Open
risalfajar opened this issue May 9, 2025 · 2 comments · May be fixed by #266
Open

How to pass auth in CallableRequest? #260

risalfajar opened this issue May 9, 2025 · 2 comments · May be fixed by #266
Assignees
Labels
triaged Triaged type: enhancement New feature or request

Comments

@risalfajar
Copy link

I'm trying to test authorization in my function, but auth.token object needed lots of properties which I don't need

import { beforeEach, describe, expect, it } from "vitest"
import { updateService as fn } from "./update"
import { WrappedV2CallableFunction, wrapV2 } from "firebase-functions-test/lib/v2"
import { defaultService } from "@features/service/data/service"
import { CallableRequest } from "firebase-functions/lib/common/providers/https"

describe("update service", () => {
	let updateService: WrappedV2CallableFunction<any>

	beforeEach(() => {
		updateService = wrapV2(fn)
	})

	it("should reject unauthorized users", async () => {
		const request: CallableRequest = {
			acceptsStreaming: false,
			rawRequest      : {} as any,
			data            : defaultService(),
			auth            : {
				uid  : "test",
				token: {
					// TOO much properties
				}
			}
		}
		await expect(updateService(request)).rejects.toThrowError("Login")
	})
})

I also don't need acceptsStreaming and rawRequest, but that has been talked about in #257

@CorieW
Copy link
Member

CorieW commented May 12, 2025

Hi @risalfajar,

Thanks for reporting this issue! We’ve received it and are reviewing it. We’ll provide updates as soon as possible.

@CorieW CorieW added type: enhancement New feature or request triaged Triaged labels May 12, 2025
@CorieW
Copy link
Member

CorieW commented May 13, 2025

You could do something like this:

index.ts

import * as functions from "firebase-functions";
import { info } from "firebase-functions/logger";

export const callable = functions.https.onCall(async (request) => {
  return request.auth?.token.email
});

index.test.ts

import { beforeEach, describe, expect, it } from "vitest"
import { WrappedV2CallableFunction, wrapV2 } from "firebase-functions-test/lib/v2"
import { callable } from "."

describe("auth testing", () => {
  let callableFunc: WrappedV2CallableFunction<any>
  
  beforeEach(() => {
    callableFunc = wrapV2(callable)
  })

  it("should return the email from the request", async () => {
    const email = '[email protected]';

    const request: any = {
      auth: {
        uid: "test",
	token: {
	  email: email,
	}
      }
    }

    expect(await callableFunc(request)).equals(email)
  })
})

However, this isn't ideal, since it may be preferable to not lose typing. For that, I'll make a PR that should make it easier to test callable functions.

@CorieW CorieW linked a pull request May 13, 2025 that will close this issue
@CorieW CorieW self-assigned this May 14, 2025
@CorieW CorieW moved this from In Review to In Progress in [Cloud] Extensions + Functions May 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triaged Triaged type: enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants