Files
documenso/packages/app-tests/e2e/api/v1/document-sending.spec.ts
Lucas Smith 5fbed783fc feat: add controls for sending completion emails to document owners (#1534)
Adds a new `ownerDocumentCompleted` to the email settings that controls
whether a document will be sent to the owner upon completion.

This was previously the only email you couldn't disable and didn't
account for users integrating with just the API and Webhooks.

Also adds a flag to the public `sendDocument` endpoint which will adjust
this setting while sendint the document for users who aren't using
`emailSettings` on the `createDocument` endpoint.
2024-12-12 14:24:07 +11:00

138 lines
3.9 KiB
TypeScript

import { expect, test } from '@playwright/test';
import { WEBAPP_BASE_URL } from '@documenso/lib/constants/app';
import { createApiToken } from '@documenso/lib/server-only/public-api/create-api-token';
import { prisma } from '@documenso/prisma';
import { seedPendingDocumentWithFullFields } from '@documenso/prisma/seed/documents';
import { seedUser } from '@documenso/prisma/seed/users';
test.describe('Document API', () => {
test('sendDocument: should respect sendCompletionEmails setting', async ({ request }) => {
const user = await seedUser();
const { document } = await seedPendingDocumentWithFullFields({
owner: user,
recipients: ['signer@example.com'],
});
const { token } = await createApiToken({
userId: user.id,
tokenName: 'test',
expiresIn: null,
});
// Test with sendCompletionEmails: false
const response = await request.post(`${WEBAPP_BASE_URL}/api/v1/documents/${document.id}/send`, {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
data: {
sendCompletionEmails: false,
},
});
expect(response.ok()).toBeTruthy();
expect(response.status()).toBe(200);
// Verify email settings were updated
const updatedDocument = await prisma.document.findUnique({
where: { id: document.id },
include: { documentMeta: true },
});
expect(updatedDocument?.documentMeta?.emailSettings).toMatchObject({
documentCompleted: false,
ownerDocumentCompleted: false,
});
// Test with sendCompletionEmails: true
const response2 = await request.post(
`${WEBAPP_BASE_URL}/api/v1/documents/${document.id}/send`,
{
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
data: {
sendCompletionEmails: true,
},
},
);
expect(response2.ok()).toBeTruthy();
expect(response2.status()).toBe(200);
// Verify email settings were updated
const updatedDocument2 = await prisma.document.findUnique({
where: { id: document.id },
include: { documentMeta: true },
});
expect(updatedDocument2?.documentMeta?.emailSettings ?? {}).toMatchObject({
documentCompleted: true,
ownerDocumentCompleted: true,
});
});
test('sendDocument: should not modify email settings when sendCompletionEmails is not provided', async ({
request,
}) => {
const user = await seedUser();
const { document } = await seedPendingDocumentWithFullFields({
owner: user,
recipients: ['signer@example.com'],
});
// Set initial email settings
await prisma.documentMeta.upsert({
where: { documentId: document.id },
create: {
documentId: document.id,
emailSettings: {
documentCompleted: true,
ownerDocumentCompleted: false,
},
},
update: {
documentId: document.id,
emailSettings: {
documentCompleted: true,
ownerDocumentCompleted: false,
},
},
});
const { token } = await createApiToken({
userId: user.id,
tokenName: 'test',
expiresIn: null,
});
const response = await request.post(`${WEBAPP_BASE_URL}/api/v1/documents/${document.id}/send`, {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
data: {
sendEmail: true,
},
});
expect(response.ok()).toBeTruthy();
expect(response.status()).toBe(200);
// Verify email settings were not modified
const updatedDocument = await prisma.document.findUnique({
where: { id: document.id },
include: { documentMeta: true },
});
expect(updatedDocument?.documentMeta?.emailSettings ?? {}).toMatchObject({
documentCompleted: true,
ownerDocumentCompleted: false,
});
});
});