Skip to content
Open
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
40 changes: 35 additions & 5 deletions apps/web/utils/outlook/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { Client } from "@microsoft/microsoft-graph-client";
import { Client, MiddlewareFactory } from "@microsoft/microsoft-graph-client";
import { saveTokens } from "@/utils/auth/save-tokens";
import { createTestLogger } from "@/__tests__/helpers";
import {
Expand All @@ -16,6 +16,10 @@ import {
vi.mock("@microsoft/microsoft-graph-client", () => ({
Client: {
init: vi.fn(),
initWithMiddleware: vi.fn(),
},
MiddlewareFactory: {
getDefaultMiddlewareChain: vi.fn(),
},
}));

Expand Down Expand Up @@ -58,26 +62,52 @@ vi.mock("@/env", () => ({
describe("outlook client emulator configuration", () => {
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(MiddlewareFactory.getDefaultMiddlewareChain).mockReturnValue([
{
execute: vi.fn(),
setNext: vi.fn(),
},
{
execute: vi.fn(),
setNext: vi.fn(),
},
]);
});

it("passes emulator-aware Graph options into the client", () => {
it("authenticates requests to the HTTP emulator", async () => {
createOutlookClient("emulator-token", createTestLogger());

expect(getMicrosoftGraphClientOptions).toHaveBeenCalledWith(
"emulator-token",
);
expect(Client.init).toHaveBeenCalledWith({
authProvider: expect.any(Function),
expect(Client.initWithMiddleware).toHaveBeenCalledWith({
baseUrl: "http://localhost:4003/",
customHosts: new Set(["localhost"]),
defaultVersion: "v1.0",
fetchOptions: {
headers: {
Authorization: "Bearer emulator-token",
Prefer: 'IdType="ImmutableId"',
},
},
middleware: [expect.any(Object), expect.any(Object)],
});

const options = vi.mocked(Client.initWithMiddleware).mock.calls[0]?.[0];
const middleware = Array.isArray(options?.middleware)
? options.middleware
: [];
const request = {
request: "http://localhost:4003/v1.0/me",
options: { headers: {} },
};

middleware[0]?.setNext?.(middleware[1]!);
await middleware[0]?.execute(request);

expect(new Headers(request.options.headers).get("Authorization")).toBe(
"Bearer emulator-token",
);
expect(middleware[1]?.execute).toHaveBeenCalledWith(request);
});

it("uses the emulator authorize URL for linking", () => {
Expand Down
60 changes: 53 additions & 7 deletions apps/web/utils/outlook/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Client } from "@microsoft/microsoft-graph-client";
import { Client, MiddlewareFactory } from "@microsoft/microsoft-graph-client";
import type { User } from "@microsoft/microsoft-graph-types";
import { saveTokens } from "@/utils/auth/save-tokens";
import { cleanupInvalidTokens } from "@/utils/auth/cleanup-invalid-tokens";
Expand Down Expand Up @@ -27,6 +27,29 @@ export class OutlookClient {
this.accessToken = accessToken;
this.logger = logger;
const graphClientOptions = getMicrosoftGraphClientOptions(accessToken);
const fetchOptions = {
headers: {
Prefer: 'IdType="ImmutableId"',
},
};
const emulatorAuthorization =
graphClientOptions.fetchOptions?.headers?.Authorization;

if (emulatorAuthorization) {
const middleware = MiddlewareFactory.getDefaultMiddlewareChain({
getAccessToken: async () => this.accessToken,
});
middleware[0] = createEmulatorAuthenticationMiddleware(
emulatorAuthorization,
);
this.client = Client.initWithMiddleware({
...graphClientOptions,
fetchOptions,
middleware,
});
return;
}

this.client = Client.init({
authProvider: (done) => {
done(null, this.accessToken);
Expand All @@ -35,12 +58,7 @@ export class OutlookClient {
...graphClientOptions,
// Use immutable IDs to ensure message IDs remain stable
// https://learn.microsoft.com/en-us/graph/outlook-immutable-id
fetchOptions: {
headers: {
...(graphClientOptions.fetchOptions?.headers ?? {}),
Prefer: 'IdType="ImmutableId"',
},
},
fetchOptions,
});
}

Expand Down Expand Up @@ -256,5 +274,33 @@ export function getLinkingOAuth2Url() {
return `${getMicrosoftOauthAuthorizeUrl()}?${params.toString()}`;
}

type GraphMiddleware = ReturnType<
typeof MiddlewareFactory.getDefaultMiddlewareChain
>[number];

function createEmulatorAuthenticationMiddleware(
authorization: string,
): GraphMiddleware {
let nextMiddleware: GraphMiddleware | undefined;

return {
async execute(context) {
context.options ??= {};
const headers = new Headers(context.options.headers);
headers.set("Authorization", authorization);
context.options.headers = headers;

if (!nextMiddleware) {
throw new Error("Outlook emulator middleware is not configured");
}

await nextMiddleware.execute(context);
},
setNext(middleware) {
nextMiddleware = middleware;
},
};
}

// Helper types for common Microsoft Graph operations
export type { Client as GraphClient };
Loading