Skip to content

Commit

Permalink
Test the JWT service errors
Browse files Browse the repository at this point in the history
  • Loading branch information
robintown committed Jan 17, 2025
1 parent 8bf6173 commit 9b532e6
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/livekit/openIDSFU.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright 2025 New Vector Ltd.
SPDX-License-Identifier: AGPL-3.0-only
Please see LICENSE in the repository root for full details.
*/

import { expect, type MockInstance, test, vi } from "vitest";
import { type IOpenIDToken } from "matrix-js-sdk/src/client";
import { type LivekitFocus } from "matrix-js-sdk/src/matrixrtc";

import { getSFUConfigWithOpenID, type OpenIDClientParts } from "./openIDSFU";
import {
AuthConnectionFailedError,
AuthConnectionRejectedError,
} from "../RichError";

async function withFetchSpy(
continuation: (fetchSpy: MockInstance<typeof fetch>) => Promise<void>,
): Promise<void> {
const fetchSpy = vi.spyOn(globalThis, "fetch");
try {
await continuation(fetchSpy);
} finally {
fetchSpy.mockRestore();
}
}

const mockClient: OpenIDClientParts = {
getOpenIdToken: async () => Promise.resolve({} as IOpenIDToken),
getDeviceId: () => "Device ID",
};
const mockFocus: LivekitFocus = {
type: "livekit",
livekit_alias: "LiveKit alias",
livekit_service_url: "LiveKit service URL",
};

test("getSFUConfigWithOpenID gets the JWT token", async () => {
await withFetchSpy(async (fetch) => {
fetch.mockResolvedValue({
ok: true,
json: async () =>
Promise.resolve({ jwt: "JWT token", url: "LiveKit URL" }),
} as Response);
expect(await getSFUConfigWithOpenID(mockClient, mockFocus)).toEqual({
jwt: "JWT token",
url: "LiveKit URL",
});
});
});

test("getSFUConfigWithOpenID throws if connection fails", async () => {
await withFetchSpy(async (fetch) => {
fetch.mockRejectedValue(new Error("Connection failed"));
await expect(async () =>
getSFUConfigWithOpenID(mockClient, mockFocus),
).rejects.toThrowError(expect.any(AuthConnectionFailedError));
});
});

test("getSFUConfigWithOpenID throws if server returns error", async () => {
await withFetchSpy(async (fetch) => {
fetch.mockResolvedValue({ ok: false, status: 404 } as Response);
await expect(async () =>
getSFUConfigWithOpenID(mockClient, mockFocus),
).rejects.toThrowError(expect.any(AuthConnectionRejectedError));
});
});

0 comments on commit 9b532e6

Please sign in to comment.