test: testing workflow triggers (#12823)

* add workflows to bookingScenario

* activate sandbox mode for unit/integreation tests

* add sendgrid specific code to SendgridProvider

* Refactor WIP

* remove duplicate sendgridProvider file

* first implementation for testing workflows

* revert unintended changes

* comment out Workflow trigger tests

* move sendgrid check after test mode

* Update signup.tsx

* fix esLint

* test webhooks on all tests in fresh-booking.test.ts

* fix subjectPattern as title can be different

* add workflow tests to reschedule.test.ts

* code clean up

* code clean up

* fix sendgrid credentials missing message

* code clean up

---------

Co-authored-by: CarinaWolli <[email protected]>
This commit is contained in:
Carina WollendorferandCarinaWolli authored and GitHub committed 2023-12-21 18:57:06 +05:30
1 parent a03a1ba34e
commit 076868d243
17 files changed
+453 -158

No files matched your search

@@ -1,6 +1,4 @@
import client from "@sendgrid/client";
import type { MailData } from "@sendgrid/helpers/classes/mail";
import sgMail from "@sendgrid/mail";
import { createEvent } from "ics";
import type { ParticipationStatus } from "ics";
import type { DateArray } from "ics";
@@ -9,7 +7,6 @@ import { v4 as uuidv4 } from "uuid";
import dayjs from "@calcom/dayjs";
import { preprocessNameFieldDataWithVariant } from "@calcom/features/form-builder/utils";
import { SENDER_NAME } from "@calcom/lib/constants";
import logger from "@calcom/lib/logger";
import prisma from "@calcom/prisma";
import type { TimeUnit } from "@calcom/prisma/enums";
@@ -21,33 +18,13 @@ import {
} from "@calcom/prisma/enums";
import { bookingMetadataSchema } from "@calcom/prisma/zod-utils";
import { getBatchId, sendSendgridMail } from "./providers/sendgridProvider";
import type { AttendeeInBookingInfo, BookingInfo, timeUnitLowerCase } from "./smsReminderManager";
import type { VariablesType } from "./templates/customTemplate";
import customTemplate from "./templates/customTemplate";
import emailReminderTemplate from "./templates/emailReminderTemplate";
let sendgridAPIKey, senderEmail: string;
const log = logger.getSubLogger({ prefix: ["[emailReminderManager]"] });
if (process.env.SENDGRID_API_KEY) {
sendgridAPIKey = process.env.SENDGRID_API_KEY as string;
senderEmail = process.env.SENDGRID_EMAIL as string;
sgMail.setApiKey(sendgridAPIKey);
client.setApiKey(sendgridAPIKey);
}
async function getBatchId() {
if (!process.env.SENDGRID_API_KEY) {
console.info("No sendgrid API key provided, returning DUMMY_BATCH_ID");
return "DUMMY_BATCH_ID";
}
const batchIdResponse = await client.request({
url: "/v3/mail/batch",
method: "POST",
});
return batchIdResponse[1].batch_id as string;
}
function getiCalEventAsString(evt: BookingInfo, status?: ParticipationStatus) {
const uid = uuidv4();
@@ -149,12 +126,6 @@ export const scheduleEmailReminder = async (args: scheduleEmailReminderArgs) =>
scheduledDate = timeSpan.time && timeUnit ? dayjs(endTime).add(timeSpan.time, timeUnit) : null;
}
if (!process.env.SENDGRID_API_KEY || !process.env.SENDGRID_EMAIL) {
console.error("Sendgrid credentials are missing from the .env file");
}
const sandboxMode = process.env.NEXT_PUBLIC_IS_E2E ? true : false;
let attendeeEmailToBeUsedInMail: string | null = null;
let attendeeToBeUsedInMail: AttendeeInBookingInfo | null = null;
let name = "";
@@ -258,11 +229,6 @@ export const scheduleEmailReminder = async (args: scheduleEmailReminderArgs) =>
const batchId = await getBatchId();
function sendEmail(data: Partial<MailData>, triggerEvent?: WorkflowTriggerEvents) {
if (!process.env.SENDGRID_API_KEY) {
console.info("No sendgrid API key provided, skipping email");
return Promise.resolve();
}
const status: ParticipationStatus =
triggerEvent === WorkflowTriggerEvents.AFTER_EVENT
? "COMPLETED"
@@ -270,34 +236,28 @@ export const scheduleEmailReminder = async (args: scheduleEmailReminderArgs) =>
? "DECLINED"
: "ACCEPTED";
return sgMail.send({
to: data.to,
from: {
email: senderEmail,
name: sender || SENDER_NAME,
return sendSendgridMail(
{
to: data.to,
subject: emailContent.emailSubject,
html: emailContent.emailBody,
batchId,
replyTo: evt.organizer.email,
attachments: includeCalendarEvent
? [
{
content: Buffer.from(getiCalEventAsString(evt, status) || "").toString("base64"),
filename: "event.ics",
type: "text/calendar; method=REQUEST",
disposition: "attachment",
contentId: uuidv4(),
},
]
: undefined,
sendAt: data.sendAt,
},
subject: emailContent.emailSubject,
html: emailContent.emailBody,
batchId,
replyTo: evt.organizer.email,
mailSettings: {
sandboxMode: {
enable: sandboxMode,
},
},
attachments: includeCalendarEvent
? [
{
content: Buffer.from(getiCalEventAsString(evt, status) || "").toString("base64"),
filename: "event.ics",
type: "text/calendar; method=REQUEST",
disposition: "attachment",
contentId: uuidv4(),
},
]
: undefined,
sendAt: data.sendAt,
});
{ sender }
);
}
if (
@@ -0,0 +1,79 @@
import client from "@sendgrid/client";
import type { MailData } from "@sendgrid/helpers/classes/mail";
import sgMail from "@sendgrid/mail";
import { SENDER_NAME } from "@calcom/lib/constants";
import { setTestEmail } from "@calcom/lib/testEmails";
let sendgridAPIKey: string;
let senderEmail: string;
function assertSendgrid() {
if (process.env.SENDGRID_API_KEY && process.env.SENDGRID_EMAIL) {
sendgridAPIKey = process.env.SENDGRID_API_KEY as string;
senderEmail = process.env.SENDGRID_EMAIL as string;
sgMail.setApiKey(sendgridAPIKey);
client.setApiKey(sendgridAPIKey);
} else {
console.error("Sendgrid credentials are missing from the .env file");
}
}
export async function getBatchId() {
assertSendgrid();
if (!process.env.SENDGRID_API_KEY) {
console.info("No sendgrid API key provided, returning DUMMY_BATCH_ID");
return "DUMMY_BATCH_ID";
}
const batchIdResponse = await client.request({
url: "/v3/mail/batch",
method: "POST",
});
return batchIdResponse[1].batch_id as string;
}
export function sendSendgridMail(
mailData: Partial<MailData>,
addData: { sender?: string | null; includeCalendarEvent?: boolean }
) {
assertSendgrid();
const testMode = process.env.NEXT_PUBLIC_IS_E2E || process.env.INTEGRATION_TEST_MODE;
if (testMode) {
if (!mailData.sendAt) {
setTestEmail({
to: mailData.to?.toString() || "",
from: {
email: senderEmail,
name: addData.sender || SENDER_NAME,
},
subject: mailData.subject || "",
html: mailData.html || "",
});
}
console.log(
"Skipped Sending Email as process.env.NEXT_PUBLIC_IS_E2E or process.env.INTEGRATION_TEST_MODE is set. Emails are available in globalThis.testEmails"
);
return new Promise((r) => r("Skipped sendEmail for Unit Tests"));
}
if (!sendgridAPIKey) {
console.info("No sendgrid API key provided, skipping email");
return Promise.resolve();
}
return sgMail.send({
to: mailData.to,
from: {
email: senderEmail,
name: addData.sender || SENDER_NAME,
},
subject: mailData.subject,
html: mailData.html || "",
batchId: mailData.batchId,
replyTo: mailData.replyTo || senderEmail,
attachments: mailData.attachments,
sendAt: mailData.sendAt,
});
}
@@ -11,7 +11,7 @@ import type { CalEventResponses, RecurringEvent } from "@calcom/types/Calendar";
import { getSenderId } from "../alphanumericSenderIdSupport";
import type { ScheduleReminderArgs } from "./emailReminderManager";
import * as twilio from "./smsProviders/twilioProvider";
import * as twilio from "./providers/twilioProvider";
import type { VariablesType } from "./templates/customTemplate";
import customTemplate from "./templates/customTemplate";
import smsReminderTemplate from "./templates/smsReminderTemplate";
@@ -1,6 +1,6 @@
import prisma from "@calcom/prisma";
import * as twilio from "./smsProviders/twilioProvider";
import * as twilio from "./providers/twilioProvider";
export const sendVerificationCode = async (phoneNumber: string) => {
return twilio.sendVerificationCode(phoneNumber);
@@ -8,7 +8,7 @@ import {
WorkflowMethods,
} from "@calcom/prisma/enums";
import * as twilio from "./smsProviders/twilioProvider";
import * as twilio from "./providers/twilioProvider";
import type { ScheduleTextReminderArgs, timeUnitLowerCase } from "./smsReminderManager";
import { deleteScheduledSMSReminder } from "./smsReminderManager";
import {