A little email test refactoring.

This commit is contained in:
Sam Rose 2025-02-27 09:39:56 +00:00
parent 478d28285b
commit 8df8494b16
No known key found for this signature in database
3 changed files with 37 additions and 29 deletions

View File

@ -3,8 +3,7 @@ import { TestConfiguration } from "../../../../tests"
import {
captureEmail,
deleteAllEmail,
getAttachment,
getUnusedPort,
getAttachments,
Mailserver,
startMailserver,
stopMailserver,
@ -17,8 +16,7 @@ describe("/api/global/email", () => {
beforeAll(async () => {
await config.beforeAll()
const port = await getUnusedPort()
mailserver = await startMailserver(config, { smtp: port })
mailserver = await startMailserver(config)
})
afterAll(async () => {
@ -45,9 +43,13 @@ describe("/api/global/email", () => {
},
])("can send $purpose emails", async ({ purpose, expectedText }) => {
const email = await captureEmail(mailserver, async () => {
const res = await config.api.emails.sendEmail(purpose)
expect(res.body.message).toBeDefined()
expect(res.status).toBe(200)
const res = await config.api.emails.sendEmail({
email: "foo@example.com",
subject: "Test",
userId: config.user!._id,
purpose,
})
expect(res.message).toBeDefined()
})
expect(email.html).toContain(expectedText)
@ -74,12 +76,14 @@ describe("/api/global/email", () => {
}
const email = await captureEmail(mailserver, async () => {
const res = await config.api.emails.sendEmail(
EmailTemplatePurpose.WELCOME,
[attachmentObject]
)
expect(res.body.message).toBeDefined()
expect(res.status).toBe(200)
const res = await config.api.emails.sendEmail({
email: "foo@example.com",
subject: "Test",
userId: config.user!._id,
purpose: EmailTemplatePurpose.WELCOME,
attachments: [attachmentObject],
})
expect(res.message).toBeDefined()
})
expect(email.html).toContain(
@ -87,11 +91,7 @@ describe("/api/global/email", () => {
)
expect(email.html).not.toContain("Invalid binding")
const attachment = await getAttachment(
mailserver,
email,
email.attachments[0]
)
expect(attachment).toEqual("test data")
const attachments = await getAttachments(mailserver, email)
expect(attachments).toEqual(["test data"])
})
})

View File

@ -1,19 +1,19 @@
import { EmailAttachment } from "@budibase/types"
import {
EmailAttachment,
SendEmailRequest,
SendEmailResponse,
} from "@budibase/types"
import { TestAPI } from "./base"
export class EmailAPI extends TestAPI {
sendEmail = (purpose: string, attachments?: EmailAttachment[]) => {
return this.request
sendEmail = async (req: SendEmailRequest): Promise<SendEmailResponse> => {
const res = await this.request
.post(`/api/global/email/send`)
.send({
email: "test@example.com",
attachments,
purpose,
tenantId: this.config.getTenantId(),
userId: this.config.user!._id!,
})
.send(req)
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
return res.body as SendEmailResponse
}
}

View File

@ -146,3 +146,11 @@ export function getAttachment(
)
})
}
export function getAttachments(mailserver: Mailserver, email: Email) {
return Promise.all(
email.attachments.map(attachment =>
getAttachment(mailserver, email, attachment)
)
)
}