Some checks failed
App Prod-Parity E2E Dispatch / dispatch (push) Failing after 10s
CD Storybook UI / build (push) Failing after 3m25s
CD Storybook UI / dispatch (push) Has been skipped
CD deploy main / website-changed-files (push) Successful in 18m8s
CI Agent Skills / changed-files-check (push) Successful in 18m9s
CI Create App E2E minimal / changed-files-check (push) Successful in 24s
CI Create App / changed-files-check (push) Successful in 26s
CI Docs / changed-files-check (push) Successful in 26s
CI Emails / changed-files-check (push) Successful in 28s
CI Example App Hello World / changed-files-check (push) Successful in 23s
CI Front / changed-files-check (push) Has been skipped
CI Example App Postcard / changed-files-check (push) Successful in 27s
CI UI / changed-files-check (push) Has been skipped
CI Utils Test / changed-files-check (push) Successful in 45s
CI Twenty Apps / discover (push) Successful in 1m3s
CD deploy main / deploy-main (push) Failing after 3s
CI Agent Skills / agent-skills-validate (push) Has been skipped
CI Create App / create-app-test (lint) (push) Has been skipped
CI Emails / emails-test (push) Has been skipped
CI Front / front-task (lingui:compile) (push) Has been skipped
CI Front / front-task (lint) (push) Has been skipped
CI Example App Postcard / example-app-postcard (push) Has been skipped
CI UI / ui-task (lint) (push) Has been skipped
CI UI / ui-task (pack-size) (push) Has been skipped
CI UI / ui-task (size) (push) Has been skipped
CI UI / ui-task (test) (push) Has been skipped
CI UI / ui-task (typecheck) (push) Has been skipped
CI Utils Test / utils-test (push) Has been skipped
CI Twenty Apps / ${{ matrix.app.name }} (push) Has been skipped
CI Emails / ci-emails-status-check (push) Successful in 2s
CI Example App Hello World / ci-example-app-hello-world-status-check (push) Successful in 2s
CI Example App Postcard / ci-example-app-postcard-status-check (push) Successful in 3s
CI UI / ui-sb-test (push) Has been skipped
CI Utils Test / ci-utils-test-status-check (push) Successful in 2s
CI Twenty Apps / ci-twenty-apps-status-check (push) Successful in 2s
CI UI / ci-ui-status-check (push) Successful in 2s
CI Desktop / desktop (push) Failing after 1m8s
Push docs to Crowdin / Push documentation to Crowdin (push) Failing after 55s
Push translations to Crowdin / Extract and upload translations (push) Failing after 5s
Push website translations to Crowdin / Extract and upload website translations (push) Failing after 5s
CI Create App / create-app-test (test) (push) Has been skipped
CI Create App / create-app-test (typecheck) (push) Has been skipped
CI Docs / docs-lint (push) Has been skipped
CI Example App Hello World / example-app-hello-world (push) Has been skipped
CI Front / front-task (lingui:extract) (push) Has been skipped
CI Front / front-task (test) (push) Has been skipped
CI Front / front-task (typecheck) (push) Has been skipped
CI UI / ui-sb-build (push) Failing after 1m32s
CI Twenty Apps / ${{ matrix.app.name }} (${{ matrix.server-source }}) (push) Has been skipped
CI Agent Skills / agent-skills-publish (push) Has been skipped
CI Create App / ci-create-app-status-check (push) Successful in 2s
CI Agent Skills / agent-skills-remote-source (push) Has been skipped
CI Create App E2E minimal / create-app-e2e-minimal (push) Has been cancelled
CI E2E Main / e2e-test (push) Has been cancelled
CI Front / front-sb-build (push) Has been cancelled
CI Front / front-build (push) Has been cancelled
CI Create App E2E minimal / ci-create-app-e2e-minimal-status-check (push) Has been cancelled
CI E2E Main / ci-e2e-main-status-check (push) Has been cancelled
CI E2E Main / notify-main-ci-failure (push) Has been cancelled
CI E2E Main / qa-scout-comment (push) Has been cancelled
CI Front / front-sb-test (push) Has been cancelled
CI Front / front-sb-screenshots (push) Has been cancelled
CI Front / ci-front-status-check (push) Has been cancelled
DPA Sub-Processor Sync / sync-subprocessors (push) Failing after 3s
AI Catalog Sync / sync-catalog (push) Failing after 2s
284 lines
8.8 KiB
TypeScript
284 lines
8.8 KiB
TypeScript
import { type Event, type MailFolder } from '@microsoft/microsoft-graph-types';
|
|
import { isNonEmptyString } from '@sniptt/guards';
|
|
import { http, HttpResponse } from 'msw';
|
|
import { isDefined } from 'twenty-shared/utils';
|
|
|
|
import { setupHttpMock } from 'test/integration/utils/http-mock.util';
|
|
import {
|
|
MICROSOFT_TOKEN_URL,
|
|
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;
|
|
createdMessages: Array<Record<string, unknown>>;
|
|
patchedMessages: Array<Record<string, unknown>>;
|
|
createdAttachments: Array<Record<string, unknown>>;
|
|
sentMessageIds: string[];
|
|
createdCalendarEvents: Event[];
|
|
serveCalendarEvents: (
|
|
events: Event[],
|
|
options?: { deltaToken?: string; removedEventIds?: string[] },
|
|
) => void;
|
|
moveMessageToFolder: (messageId: string, targetFolderId: string) => void;
|
|
deleteMessage: (messageId: string) => void;
|
|
failMessageFetch: (messageId: string) => void;
|
|
failSubscriptionRenewal: () => void;
|
|
failMessageDelta: (failure: MicrosoftGraphFailure) => void;
|
|
failCalendarDelta: (failure: MicrosoftGraphFailure) => void;
|
|
declineTokenRefresh: () => void;
|
|
};
|
|
|
|
export type MicrosoftGraphFailure = {
|
|
status: number;
|
|
code: string;
|
|
message: string;
|
|
};
|
|
|
|
const microsoftGraphErrorResponse = ({
|
|
status,
|
|
code,
|
|
message,
|
|
}: MicrosoftGraphFailure) =>
|
|
HttpResponse.json({ error: { code, message } }, { status });
|
|
|
|
export const setupMicrosoftMock = ({
|
|
handle,
|
|
aliases = [],
|
|
folders = DEFAULT_FOLDERS,
|
|
messages = [],
|
|
}: {
|
|
handle: string;
|
|
aliases?: string[];
|
|
folders?: MailFolder[];
|
|
messages?: Array<Record<string, unknown>>;
|
|
}): MicrosoftMock => {
|
|
const folderStore = createMockEntityStore(
|
|
folders,
|
|
(folder) => folder.id ?? '',
|
|
);
|
|
|
|
const subscriptionStore = createMicrosoftSubscriptionStore();
|
|
const createdMessages: Array<Record<string, unknown>> = [];
|
|
const patchedMessages: Array<Record<string, unknown>> = [];
|
|
const createdAttachments: Array<Record<string, unknown>> = [];
|
|
const sentMessageIds: string[] = [];
|
|
const createdCalendarEvents: Event[] = [];
|
|
const removedMessageIdsByFolderId: Record<string, string[]> = {};
|
|
|
|
const recordFolderRemoval = (messageId: string) => {
|
|
const message = messages.find((candidate) => candidate.id === messageId);
|
|
|
|
if (!isDefined(message)) {
|
|
throw new Error(`No mocked Microsoft message with id ${messageId}`);
|
|
}
|
|
|
|
const folderId = message.parentFolderId;
|
|
|
|
if (!isNonEmptyString(folderId)) {
|
|
throw new Error(
|
|
`Mocked Microsoft message ${messageId} has no parentFolderId`,
|
|
);
|
|
}
|
|
|
|
removedMessageIdsByFolderId[folderId] = [
|
|
...(removedMessageIdsByFolderId[folderId] ?? []),
|
|
messageId,
|
|
];
|
|
|
|
return message;
|
|
};
|
|
|
|
const httpMock = setupHttpMock(
|
|
...microsoftAuthHandlers(handle, aliases),
|
|
...microsoftMailboxHandlers(
|
|
folderStore,
|
|
messages,
|
|
removedMessageIdsByFolderId,
|
|
),
|
|
...microsoftWebhookSubscriptionHandlers(subscriptionStore),
|
|
http.post('*/me/messages', async ({ request }) => {
|
|
const message = (await request.json()) as Record<string, unknown>;
|
|
const id = `microsoft-message-${createdMessages.length + 1}`;
|
|
|
|
createdMessages.push(message);
|
|
|
|
return HttpResponse.json({
|
|
id,
|
|
internetMessageId: `<${id}@example.com>`,
|
|
conversationId: `microsoft-conversation-${createdMessages.length}`,
|
|
});
|
|
}),
|
|
http.post(
|
|
'*/me/messages/:messageId/attachments',
|
|
async ({ request, params }) => {
|
|
const attachment = (await request.json()) as Record<string, unknown>;
|
|
|
|
createdAttachments.push({
|
|
messageId: params.messageId as string,
|
|
...attachment,
|
|
});
|
|
|
|
return HttpResponse.json({
|
|
id: `microsoft-attachment-${createdAttachments.length}`,
|
|
...attachment,
|
|
});
|
|
},
|
|
),
|
|
http.post('*/me/messages/:messageId/send', ({ params }) => {
|
|
sentMessageIds.push(params.messageId as string);
|
|
|
|
return new HttpResponse(null, { status: 202 });
|
|
}),
|
|
http.delete(
|
|
'*/me/messages/:messageId',
|
|
() => new HttpResponse(null, { status: 204 }),
|
|
),
|
|
http.get('*/me/messages', () =>
|
|
HttpResponse.json({
|
|
value: [
|
|
{
|
|
id: 'microsoft-parent-message',
|
|
internetMessageId: '<microsoft-parent@example.com>',
|
|
},
|
|
],
|
|
}),
|
|
),
|
|
http.post('*/me/messages/:messageId/createReply', () =>
|
|
HttpResponse.json({
|
|
id: 'microsoft-reply-message',
|
|
internetMessageId: '<microsoft-reply-message@example.com>',
|
|
conversationId: 'microsoft-parent-conversation',
|
|
}),
|
|
),
|
|
http.patch('*/me/messages/:messageId', async ({ request }) => {
|
|
const message = (await request.json()) as Record<string, unknown>;
|
|
|
|
patchedMessages.push(message);
|
|
|
|
return HttpResponse.json({
|
|
internetMessageId: '<microsoft-reply-message@example.com>',
|
|
conversationId: 'microsoft-parent-conversation',
|
|
});
|
|
}),
|
|
http.post('*/me/calendar/events', async ({ request }) => {
|
|
const event = (await request.json()) as Event;
|
|
const id = `microsoft-created-calendar-event-${createdCalendarEvents.length + 1}`;
|
|
const createdEvent: Event = {
|
|
...event,
|
|
id,
|
|
iCalUId: `${id}@microsoft.com`,
|
|
isCancelled: false,
|
|
createdDateTime: '2026-08-13T00:00:00.000Z',
|
|
lastModifiedDateTime: '2026-08-13T00:00:00.000Z',
|
|
};
|
|
|
|
createdCalendarEvents.push(createdEvent);
|
|
|
|
return HttpResponse.json(createdEvent);
|
|
}),
|
|
);
|
|
|
|
return {
|
|
folders: folderStore,
|
|
subscriptions: subscriptionStore,
|
|
createdMessages,
|
|
patchedMessages,
|
|
createdAttachments,
|
|
sentMessageIds,
|
|
createdCalendarEvents,
|
|
serveCalendarEvents: (
|
|
events,
|
|
{ deltaToken = 'mock-calendar-delta-token', removedEventIds = [] } = {},
|
|
) =>
|
|
httpMock.use(
|
|
...microsoftCalendarEventsHandlers(events, deltaToken, removedEventIds),
|
|
),
|
|
moveMessageToFolder: (messageId, targetFolderId) => {
|
|
const message = recordFolderRemoval(messageId);
|
|
|
|
message.parentFolderId = targetFolderId;
|
|
},
|
|
deleteMessage: (messageId) => {
|
|
const message = recordFolderRemoval(messageId);
|
|
|
|
messages.splice(messages.indexOf(message), 1);
|
|
},
|
|
failMessageFetch: (messageId) =>
|
|
httpMock.use(
|
|
...microsoftMailboxHandlers(
|
|
folderStore,
|
|
messages,
|
|
removedMessageIdsByFolderId,
|
|
{ unfetchableMessageIds: [messageId] },
|
|
),
|
|
),
|
|
failSubscriptionRenewal: () =>
|
|
httpMock.use(
|
|
...microsoftWebhookSubscriptionHandlers(subscriptionStore, {
|
|
renewalFails: true,
|
|
}),
|
|
),
|
|
// The driver issues the per-folder delta requests inside a /$batch POST, so
|
|
// the failure has to come back as the status on each sub-response rather
|
|
// than on the delta route itself.
|
|
failMessageDelta: (failure) =>
|
|
httpMock.use(
|
|
http.post('*/$batch', async ({ request }) => {
|
|
const { requests } = (await request.json()) as {
|
|
requests: { id: string }[];
|
|
};
|
|
|
|
return HttpResponse.json({
|
|
responses: requests.map(({ id }) => ({
|
|
id,
|
|
status: failure.status,
|
|
body: {
|
|
error: { code: failure.code, message: failure.message },
|
|
},
|
|
})),
|
|
});
|
|
}),
|
|
http.get('*/messages/delta', () =>
|
|
microsoftGraphErrorResponse(failure),
|
|
),
|
|
),
|
|
failCalendarDelta: (failure) =>
|
|
httpMock.use(
|
|
http.get('*/me/calendar/events/delta', () =>
|
|
microsoftGraphErrorResponse(failure),
|
|
),
|
|
),
|
|
declineTokenRefresh: () =>
|
|
httpMock.use(
|
|
http.post(MICROSOFT_TOKEN_URL, () =>
|
|
HttpResponse.json(
|
|
{
|
|
error: 'invalid_grant',
|
|
error_description:
|
|
'AADSTS50173: The provided grant has expired due to it being revoked',
|
|
},
|
|
{ status: 400 },
|
|
),
|
|
),
|
|
),
|
|
};
|
|
};
|