51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import { describe, expect, test, vi } from "vitest";
|
|
|
|
import { createLogtoAuthClient } from "../src/background/auth/client";
|
|
|
|
vi.mock("@logto/chrome-extension", () => {
|
|
const signIn = vi.fn(async () => undefined);
|
|
const signOut = vi.fn(async () => undefined);
|
|
const MockLogtoClient = vi.fn(function MockLogtoClient() {
|
|
return {
|
|
getAccessToken: vi.fn(async () => "token"),
|
|
getIdTokenClaims: vi.fn(async () => null),
|
|
isAuthenticated: vi.fn(async () => false),
|
|
signIn,
|
|
signOut
|
|
};
|
|
});
|
|
|
|
return {
|
|
default: MockLogtoClient
|
|
};
|
|
});
|
|
|
|
describe("background-auth-client", () => {
|
|
test("uses chrome identity redirect URLs for sign in and sign out", async () => {
|
|
const getRedirectURL = vi.fn((path?: string) =>
|
|
path ? `https://extension.chromiumapp.org${path}` : "https://extension.chromiumapp.org/"
|
|
);
|
|
|
|
(
|
|
globalThis as typeof globalThis & {
|
|
chrome?: {
|
|
identity?: {
|
|
getRedirectURL?: (path?: string) => string;
|
|
};
|
|
};
|
|
}
|
|
).chrome = {
|
|
identity: {
|
|
getRedirectURL
|
|
}
|
|
};
|
|
|
|
const authClient = createLogtoAuthClient();
|
|
await authClient.signIn();
|
|
await authClient.signOut();
|
|
|
|
expect(getRedirectURL).toHaveBeenNthCalledWith(1, "/callback");
|
|
expect(getRedirectURL).toHaveBeenNthCalledWith(2);
|
|
});
|
|
});
|