Files
twenty/packages/twenty-server/test/integration/microsoft/mocks/setup-microsoft-mock.util.ts
T
neo773 8e5bdcc781 webhook subscriptions error handling (#23707)
A `subscriptionRemoved` lifecycle notification was routed to
`renewSubscription`, which PATCHes a subscription Microsoft has already
deleted and always 404s
([TWENTY-SERVER-J1N](https://twenty-v7.sentry.io/issues/7604034376/),
884 events). Every sampled webhook event on that issue was
`subscriptionRemoved`.

Each lifecycle event now gets its own path: `subscriptionRemoved`
recreates and resyncs the gap, `reauthorizationRequired` renews in
place, `missed` resyncs, unrecognised events are logged and ignored.
Provider errors are parsed into driver exception codes following the
message-import drivers.

Max retry for the renewal cron is deliberately left out and will follow
separately.

<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/twentyhq/twenty/pull/23707?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->

---------

Co-authored-by: neo773 <huzef@twenty.com>
2026-08-04 10:23:52 +00:00

67 lines
2.1 KiB
TypeScript

import { type Event, type MailFolder } from '@microsoft/microsoft-graph-types';
import { setupHttpMock } from 'test/integration/utils/http-mock.util';
import { microsoftAuthHandlers } from 'test/integration/microsoft/mocks/microsoft-auth-handlers.util';
import { microsoftCalendarEventsHandlers } from 'test/integration/microsoft/mocks/microsoft-calendar-events-handlers.util';
import { microsoftMailboxHandlers } from 'test/integration/microsoft/mocks/microsoft-mailbox-handlers.util';
import {
createMicrosoftSubscriptionStore,
microsoftWebhookSubscriptionHandlers,
type MicrosoftSubscriptionStore,
} from 'test/integration/microsoft/mocks/microsoft-webhook-subscription-handlers.util';
import {
createMockEntityStore,
type MockEntityStore,
} from 'test/integration/utils/mock-entity-store.util';
const DEFAULT_FOLDERS: MailFolder[] = [
{ id: 'inbox', displayName: 'Inbox' },
{ id: 'sentitems', displayName: 'Sent Items' },
];
export type MicrosoftMock = {
folders: MockEntityStore<MailFolder>;
subscriptions: MicrosoftSubscriptionStore;
serveCalendarEvents: (
events: Event[],
options?: { deltaToken?: string },
) => void;
failSubscriptionRenewal: () => void;
};
export const setupMicrosoftMock = ({
handle,
folders = DEFAULT_FOLDERS,
}: {
handle: string;
folders?: MailFolder[];
}): MicrosoftMock => {
const folderStore = createMockEntityStore(
folders,
(folder) => folder.id ?? '',
);
const subscriptionStore = createMicrosoftSubscriptionStore();
const httpMock = setupHttpMock(
...microsoftAuthHandlers(handle),
...microsoftMailboxHandlers(folderStore),
...microsoftWebhookSubscriptionHandlers(subscriptionStore),
);
return {
folders: folderStore,
subscriptions: subscriptionStore,
serveCalendarEvents: (
events,
{ deltaToken = 'mock-calendar-delta-token' } = {},
) => httpMock.use(...microsoftCalendarEventsHandlers(events, deltaToken)),
failSubscriptionRenewal: () =>
httpMock.use(
...microsoftWebhookSubscriptionHandlers(subscriptionStore, {
renewalFails: true,
}),
),
};
};