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
432 lines
13 KiB
TypeScript
432 lines
13 KiB
TypeScript
import { EmailOperation } from 'twenty-shared/types';
|
|
import { EmailComposerService } from 'src/engine/core-modules/tool/tools/email-tool/email-composer.service';
|
|
|
|
import { getAppProviderByClassName } from 'test/integration/utils/get-app-provider-by-class-name.util';
|
|
|
|
const WORKSPACE_ID = '20202020-1c25-4d02-bf25-6aeccf7ea419';
|
|
|
|
const PHIL_USER_WORKSPACE_ID = '20202020-7169-42cf-bc47-1cfef15264b1';
|
|
const PHIL_CONNECTED_ACCOUNT_ID = '20202020-cafc-4323-908d-e5b42ad69fdf';
|
|
|
|
const JONY_CONNECTED_ACCOUNT_ID = '20202020-0cc8-4d60-a3a4-803245698908';
|
|
|
|
const UNKNOWN_USER_WORKSPACE_ID = '20202020-0000-4000-8000-00000000dead';
|
|
const UNKNOWN_CONNECTED_ACCOUNT_ID = '20202020-0000-4000-8000-00000000beef';
|
|
|
|
const baseParams = {
|
|
recipients: { to: 'customer@example.com' },
|
|
subject: 'Subject',
|
|
body: '<p>body</p>',
|
|
files: [],
|
|
};
|
|
|
|
const getFirstWorkspaceConnectedAccountId = async (): Promise<string> => {
|
|
const [{ id }] = await global.testDataSource.query(
|
|
`SELECT id FROM core."connectedAccount"
|
|
WHERE "workspaceId" = $1 AND "archivedAt" IS NULL
|
|
AND provider NOT IN ('app', 'oidc', 'saml')
|
|
ORDER BY "createdAt" ASC, id ASC
|
|
LIMIT 1`,
|
|
[WORKSPACE_ID],
|
|
);
|
|
|
|
return id;
|
|
};
|
|
|
|
const readConnectedAccountState = async (
|
|
connectedAccountId: string,
|
|
): Promise<{ visibility: string; archivedAt: string | null }> => {
|
|
const [state] = await global.testDataSource.query(
|
|
`SELECT visibility, "archivedAt" FROM core."connectedAccount" WHERE id = $1`,
|
|
[connectedAccountId],
|
|
);
|
|
|
|
return state;
|
|
};
|
|
|
|
const setVisibility = async (
|
|
connectedAccountId: string,
|
|
visibility: string,
|
|
) => {
|
|
await global.testDataSource.query(
|
|
`UPDATE core."connectedAccount" SET visibility = $1 WHERE id = $2`,
|
|
[visibility, connectedAccountId],
|
|
);
|
|
};
|
|
|
|
const getWorkspaceSharedConnectedAccountIds = async (): Promise<string[]> => {
|
|
const rows = await global.testDataSource.query(
|
|
`SELECT id FROM core."connectedAccount"
|
|
WHERE "workspaceId" = $1 AND visibility = 'workspace'`,
|
|
[WORKSPACE_ID],
|
|
);
|
|
|
|
return rows.map((row: { id: string }) => row.id);
|
|
};
|
|
|
|
const insertConnectedAccount = async ({
|
|
id,
|
|
provider,
|
|
userWorkspaceId,
|
|
visibility = 'user',
|
|
}: {
|
|
id: string;
|
|
provider: 'app' | 'oidc' | 'saml' | 'email_group';
|
|
userWorkspaceId: string;
|
|
visibility?: 'user' | 'workspace';
|
|
}) => {
|
|
await global.testDataSource.query(
|
|
`INSERT INTO core."connectedAccount"
|
|
(id, handle, provider, "userWorkspaceId", "workspaceId", visibility, "createdAt", "updatedAt")
|
|
VALUES ($1, $2, $3, $4, $5, $6, '1970-01-01T00:00:00Z', now())`,
|
|
[
|
|
id,
|
|
`${provider}@example.com`,
|
|
provider,
|
|
userWorkspaceId,
|
|
WORKSPACE_ID,
|
|
visibility,
|
|
],
|
|
);
|
|
};
|
|
|
|
const insertNonMailboxConnectedAccount = async (params: {
|
|
id: string;
|
|
provider: 'app' | 'oidc' | 'saml';
|
|
userWorkspaceId: string;
|
|
}) => insertConnectedAccount(params);
|
|
|
|
const deleteConnectedAccount = async (connectedAccountId: string) => {
|
|
await global.testDataSource.query(
|
|
`DELETE FROM core."connectedAccount" WHERE id = $1`,
|
|
[connectedAccountId],
|
|
);
|
|
};
|
|
|
|
const setArchivedAt = async (
|
|
connectedAccountId: string,
|
|
archivedAt: string | null,
|
|
) => {
|
|
await global.testDataSource.query(
|
|
`UPDATE core."connectedAccount" SET "archivedAt" = $1 WHERE id = $2`,
|
|
[archivedAt, connectedAccountId],
|
|
);
|
|
};
|
|
|
|
describe('EmailComposerService connected account resolution (integration)', () => {
|
|
let service: EmailComposerService;
|
|
|
|
beforeAll(() => {
|
|
service = getAppProviderByClassName<EmailComposerService>(
|
|
'EmailComposerService',
|
|
);
|
|
});
|
|
|
|
describe('when the caller names a connected account', () => {
|
|
it('uses that account, whoever owns it', async () => {
|
|
const result = await service.composeEmail({
|
|
parameters: {
|
|
...baseParams,
|
|
connectedAccountId: JONY_CONNECTED_ACCOUNT_ID,
|
|
},
|
|
context: {
|
|
workspaceId: WORKSPACE_ID,
|
|
userWorkspaceId: PHIL_USER_WORKSPACE_ID,
|
|
},
|
|
operation: EmailOperation.SEND,
|
|
});
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.success && result.data.connectedAccount.id).toBe(
|
|
JONY_CONNECTED_ACCOUNT_ID,
|
|
);
|
|
});
|
|
|
|
it('uses that account when there is no caller (workflow run)', async () => {
|
|
const result = await service.composeEmail({
|
|
parameters: {
|
|
...baseParams,
|
|
connectedAccountId: JONY_CONNECTED_ACCOUNT_ID,
|
|
},
|
|
context: { workspaceId: WORKSPACE_ID },
|
|
operation: EmailOperation.SEND,
|
|
});
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.success && result.data.connectedAccount.id).toBe(
|
|
JONY_CONNECTED_ACCOUNT_ID,
|
|
);
|
|
});
|
|
|
|
it('throws when the id is not a valid UUID', async () => {
|
|
await expect(
|
|
service.composeEmail({
|
|
parameters: { ...baseParams, connectedAccountId: 'not-a-uuid' },
|
|
context: {
|
|
workspaceId: WORKSPACE_ID,
|
|
userWorkspaceId: PHIL_USER_WORKSPACE_ID,
|
|
},
|
|
operation: EmailOperation.SEND,
|
|
}),
|
|
).rejects.toThrow('Connected account id is not a valid UUID');
|
|
});
|
|
|
|
it('throws when no connected account matches the id', async () => {
|
|
await expect(
|
|
service.composeEmail({
|
|
parameters: {
|
|
...baseParams,
|
|
connectedAccountId: UNKNOWN_CONNECTED_ACCOUNT_ID,
|
|
},
|
|
context: {
|
|
workspaceId: WORKSPACE_ID,
|
|
userWorkspaceId: PHIL_USER_WORKSPACE_ID,
|
|
},
|
|
operation: EmailOperation.SEND,
|
|
}),
|
|
).rejects.toThrow('No connected account found for id');
|
|
});
|
|
|
|
it('refuses an application connection that cannot carry mail', async () => {
|
|
const appConnectedAccountId = '20202020-0000-4000-8000-0000000000a1';
|
|
|
|
await insertNonMailboxConnectedAccount({
|
|
id: appConnectedAccountId,
|
|
provider: 'app',
|
|
userWorkspaceId: PHIL_USER_WORKSPACE_ID,
|
|
});
|
|
|
|
try {
|
|
await expect(
|
|
service.composeEmail({
|
|
parameters: {
|
|
...baseParams,
|
|
connectedAccountId: appConnectedAccountId,
|
|
},
|
|
context: {
|
|
workspaceId: WORKSPACE_ID,
|
|
userWorkspaceId: PHIL_USER_WORKSPACE_ID,
|
|
},
|
|
operation: EmailOperation.SEND,
|
|
}),
|
|
).rejects.toThrow('cannot send email');
|
|
} finally {
|
|
await deleteConnectedAccount(appConnectedAccountId);
|
|
}
|
|
});
|
|
|
|
it('refuses an archived account named explicitly', async () => {
|
|
const { archivedAt } = await readConnectedAccountState(
|
|
JONY_CONNECTED_ACCOUNT_ID,
|
|
);
|
|
|
|
await setArchivedAt(JONY_CONNECTED_ACCOUNT_ID, new Date().toISOString());
|
|
|
|
try {
|
|
await expect(
|
|
service.composeEmail({
|
|
parameters: {
|
|
...baseParams,
|
|
connectedAccountId: JONY_CONNECTED_ACCOUNT_ID,
|
|
},
|
|
context: { workspaceId: WORKSPACE_ID },
|
|
operation: EmailOperation.SEND,
|
|
}),
|
|
).rejects.toThrow('No connected account found for id');
|
|
} finally {
|
|
await setArchivedAt(JONY_CONNECTED_ACCOUNT_ID, archivedAt);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('when drafting rather than sending', () => {
|
|
it('composes a draft from the caller own mailbox', async () => {
|
|
const result = await service.composeEmail({
|
|
parameters: baseParams,
|
|
context: {
|
|
workspaceId: WORKSPACE_ID,
|
|
userWorkspaceId: PHIL_USER_WORKSPACE_ID,
|
|
},
|
|
operation: EmailOperation.DRAFT,
|
|
});
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.success && result.data.connectedAccount.id).toBe(
|
|
PHIL_CONNECTED_ACCOUNT_ID,
|
|
);
|
|
});
|
|
|
|
it('refuses an email group, which has no drafts folder', async () => {
|
|
const emailGroupConnectedAccountId =
|
|
'20202020-0000-4000-8000-0000000000a4';
|
|
|
|
await insertConnectedAccount({
|
|
id: emailGroupConnectedAccountId,
|
|
provider: 'email_group',
|
|
userWorkspaceId: PHIL_USER_WORKSPACE_ID,
|
|
visibility: 'workspace',
|
|
});
|
|
|
|
try {
|
|
await expect(
|
|
service.composeEmail({
|
|
parameters: {
|
|
...baseParams,
|
|
connectedAccountId: emailGroupConnectedAccountId,
|
|
},
|
|
context: { workspaceId: WORKSPACE_ID },
|
|
operation: EmailOperation.DRAFT,
|
|
}),
|
|
).rejects.toThrow('cannot draft email');
|
|
} finally {
|
|
await deleteConnectedAccount(emailGroupConnectedAccountId);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('when the caller names none', () => {
|
|
it('composes from the caller own account rather than the first of the workspace', async () => {
|
|
const result = await service.composeEmail({
|
|
parameters: baseParams,
|
|
context: {
|
|
workspaceId: WORKSPACE_ID,
|
|
userWorkspaceId: PHIL_USER_WORKSPACE_ID,
|
|
},
|
|
operation: EmailOperation.SEND,
|
|
});
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.success && result.data.connectedAccount.id).toBe(
|
|
PHIL_CONNECTED_ACCOUNT_ID,
|
|
);
|
|
});
|
|
|
|
it('skips an older application connection and still picks the mailbox', async () => {
|
|
const appConnectedAccountId = '20202020-0000-4000-8000-0000000000a2';
|
|
|
|
await insertNonMailboxConnectedAccount({
|
|
id: appConnectedAccountId,
|
|
provider: 'app',
|
|
userWorkspaceId: PHIL_USER_WORKSPACE_ID,
|
|
});
|
|
|
|
try {
|
|
const result = await service.composeEmail({
|
|
parameters: baseParams,
|
|
context: {
|
|
workspaceId: WORKSPACE_ID,
|
|
userWorkspaceId: PHIL_USER_WORKSPACE_ID,
|
|
},
|
|
operation: EmailOperation.SEND,
|
|
});
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.success && result.data.connectedAccount.id).toBe(
|
|
PHIL_CONNECTED_ACCOUNT_ID,
|
|
);
|
|
} finally {
|
|
await deleteConnectedAccount(appConnectedAccountId);
|
|
}
|
|
});
|
|
|
|
it('skips an older SSO connection when there is no caller (workflow run)', async () => {
|
|
const oidcConnectedAccountId = '20202020-0000-4000-8000-0000000000a3';
|
|
|
|
await insertNonMailboxConnectedAccount({
|
|
id: oidcConnectedAccountId,
|
|
provider: 'oidc',
|
|
userWorkspaceId: PHIL_USER_WORKSPACE_ID,
|
|
});
|
|
|
|
try {
|
|
const firstWorkspaceConnectedAccountId =
|
|
await getFirstWorkspaceConnectedAccountId();
|
|
|
|
const result = await service.composeEmail({
|
|
parameters: baseParams,
|
|
context: { workspaceId: WORKSPACE_ID },
|
|
operation: EmailOperation.SEND,
|
|
});
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.success && result.data.connectedAccount.id).toBe(
|
|
firstWorkspaceConnectedAccountId,
|
|
);
|
|
expect(result.success && result.data.connectedAccount.id).not.toBe(
|
|
oidcConnectedAccountId,
|
|
);
|
|
} finally {
|
|
await deleteConnectedAccount(oidcConnectedAccountId);
|
|
}
|
|
});
|
|
|
|
it('falls back to an account shared with the whole workspace', async () => {
|
|
const { visibility } = await readConnectedAccountState(
|
|
JONY_CONNECTED_ACCOUNT_ID,
|
|
);
|
|
|
|
await setVisibility(JONY_CONNECTED_ACCOUNT_ID, 'workspace');
|
|
|
|
try {
|
|
const result = await service.composeEmail({
|
|
parameters: baseParams,
|
|
context: {
|
|
workspaceId: WORKSPACE_ID,
|
|
userWorkspaceId: UNKNOWN_USER_WORKSPACE_ID,
|
|
},
|
|
operation: EmailOperation.SEND,
|
|
});
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.success && result.data.connectedAccount.id).toBe(
|
|
JONY_CONNECTED_ACCOUNT_ID,
|
|
);
|
|
} finally {
|
|
await setVisibility(JONY_CONNECTED_ACCOUNT_ID, visibility);
|
|
}
|
|
});
|
|
|
|
it('throws rather than composing from a colleague account', async () => {
|
|
const sharedConnectedAccountIds =
|
|
await getWorkspaceSharedConnectedAccountIds();
|
|
|
|
for (const connectedAccountId of sharedConnectedAccountIds) {
|
|
await setVisibility(connectedAccountId, 'user');
|
|
}
|
|
|
|
try {
|
|
await expect(
|
|
service.composeEmail({
|
|
parameters: baseParams,
|
|
context: {
|
|
workspaceId: WORKSPACE_ID,
|
|
userWorkspaceId: UNKNOWN_USER_WORKSPACE_ID,
|
|
},
|
|
operation: EmailOperation.SEND,
|
|
}),
|
|
).rejects.toThrow('available for user workspace');
|
|
} finally {
|
|
for (const connectedAccountId of sharedConnectedAccountIds) {
|
|
await setVisibility(connectedAccountId, 'workspace');
|
|
}
|
|
}
|
|
});
|
|
|
|
it('takes the first workspace account when there is no caller (workflow run)', async () => {
|
|
const firstWorkspaceConnectedAccountId =
|
|
await getFirstWorkspaceConnectedAccountId();
|
|
|
|
const result = await service.composeEmail({
|
|
parameters: baseParams,
|
|
context: { workspaceId: WORKSPACE_ID },
|
|
operation: EmailOperation.SEND,
|
|
});
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.success && result.data.connectedAccount.id).toBe(
|
|
firstWorkspaceConnectedAccountId,
|
|
);
|
|
});
|
|
});
|
|
});
|