A little email test refactoring.
This commit is contained in:
parent
478d28285b
commit
8df8494b16
|
@ -3,8 +3,7 @@ import { TestConfiguration } from "../../../../tests"
|
||||||
import {
|
import {
|
||||||
captureEmail,
|
captureEmail,
|
||||||
deleteAllEmail,
|
deleteAllEmail,
|
||||||
getAttachment,
|
getAttachments,
|
||||||
getUnusedPort,
|
|
||||||
Mailserver,
|
Mailserver,
|
||||||
startMailserver,
|
startMailserver,
|
||||||
stopMailserver,
|
stopMailserver,
|
||||||
|
@ -17,8 +16,7 @@ describe("/api/global/email", () => {
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
await config.beforeAll()
|
await config.beforeAll()
|
||||||
const port = await getUnusedPort()
|
mailserver = await startMailserver(config)
|
||||||
mailserver = await startMailserver(config, { smtp: port })
|
|
||||||
})
|
})
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
|
@ -45,9 +43,13 @@ describe("/api/global/email", () => {
|
||||||
},
|
},
|
||||||
])("can send $purpose emails", async ({ purpose, expectedText }) => {
|
])("can send $purpose emails", async ({ purpose, expectedText }) => {
|
||||||
const email = await captureEmail(mailserver, async () => {
|
const email = await captureEmail(mailserver, async () => {
|
||||||
const res = await config.api.emails.sendEmail(purpose)
|
const res = await config.api.emails.sendEmail({
|
||||||
expect(res.body.message).toBeDefined()
|
email: "foo@example.com",
|
||||||
expect(res.status).toBe(200)
|
subject: "Test",
|
||||||
|
userId: config.user!._id,
|
||||||
|
purpose,
|
||||||
|
})
|
||||||
|
expect(res.message).toBeDefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(email.html).toContain(expectedText)
|
expect(email.html).toContain(expectedText)
|
||||||
|
@ -74,12 +76,14 @@ describe("/api/global/email", () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const email = await captureEmail(mailserver, async () => {
|
const email = await captureEmail(mailserver, async () => {
|
||||||
const res = await config.api.emails.sendEmail(
|
const res = await config.api.emails.sendEmail({
|
||||||
EmailTemplatePurpose.WELCOME,
|
email: "foo@example.com",
|
||||||
[attachmentObject]
|
subject: "Test",
|
||||||
)
|
userId: config.user!._id,
|
||||||
expect(res.body.message).toBeDefined()
|
purpose: EmailTemplatePurpose.WELCOME,
|
||||||
expect(res.status).toBe(200)
|
attachments: [attachmentObject],
|
||||||
|
})
|
||||||
|
expect(res.message).toBeDefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(email.html).toContain(
|
expect(email.html).toContain(
|
||||||
|
@ -87,11 +91,7 @@ describe("/api/global/email", () => {
|
||||||
)
|
)
|
||||||
expect(email.html).not.toContain("Invalid binding")
|
expect(email.html).not.toContain("Invalid binding")
|
||||||
|
|
||||||
const attachment = await getAttachment(
|
const attachments = await getAttachments(mailserver, email)
|
||||||
mailserver,
|
expect(attachments).toEqual(["test data"])
|
||||||
email,
|
|
||||||
email.attachments[0]
|
|
||||||
)
|
|
||||||
expect(attachment).toEqual("test data")
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,19 +1,19 @@
|
||||||
import { EmailAttachment } from "@budibase/types"
|
import {
|
||||||
|
EmailAttachment,
|
||||||
|
SendEmailRequest,
|
||||||
|
SendEmailResponse,
|
||||||
|
} from "@budibase/types"
|
||||||
import { TestAPI } from "./base"
|
import { TestAPI } from "./base"
|
||||||
|
|
||||||
export class EmailAPI extends TestAPI {
|
export class EmailAPI extends TestAPI {
|
||||||
sendEmail = (purpose: string, attachments?: EmailAttachment[]) => {
|
sendEmail = async (req: SendEmailRequest): Promise<SendEmailResponse> => {
|
||||||
return this.request
|
const res = await this.request
|
||||||
.post(`/api/global/email/send`)
|
.post(`/api/global/email/send`)
|
||||||
.send({
|
.send(req)
|
||||||
email: "test@example.com",
|
|
||||||
attachments,
|
|
||||||
purpose,
|
|
||||||
tenantId: this.config.getTenantId(),
|
|
||||||
userId: this.config.user!._id!,
|
|
||||||
})
|
|
||||||
.set(this.config.defaultHeaders())
|
.set(this.config.defaultHeaders())
|
||||||
.expect("Content-Type", /json/)
|
.expect("Content-Type", /json/)
|
||||||
.expect(200)
|
.expect(200)
|
||||||
|
|
||||||
|
return res.body as SendEmailResponse
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue