2022-11-12 04:43:41 +13:00
|
|
|
import { TestConfiguration } from "../../../../tests"
|
2022-08-26 06:41:47 +12:00
|
|
|
import { EmailTemplatePurpose } from "../../../../constants"
|
2021-04-27 01:14:51 +12:00
|
|
|
const nodemailer = require("nodemailer")
|
|
|
|
const fetch = require("node-fetch")
|
|
|
|
|
2022-03-31 02:41:39 +13:00
|
|
|
// for the real email tests give them a long time to try complete/fail
|
2022-07-14 02:39:58 +12:00
|
|
|
jest.setTimeout(30000)
|
2022-03-31 02:41:39 +13:00
|
|
|
|
2021-08-05 20:59:08 +12:00
|
|
|
describe("/api/global/email", () => {
|
2022-08-26 06:41:47 +12:00
|
|
|
const config = new TestConfiguration()
|
2021-04-27 01:14:51 +12:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2022-04-07 03:57:56 +12:00
|
|
|
await config.beforeAll()
|
2021-04-27 01:14:51 +12:00
|
|
|
})
|
|
|
|
|
2022-04-07 03:57:56 +12:00
|
|
|
afterAll(async () => {
|
|
|
|
await config.afterAll()
|
|
|
|
})
|
2021-04-27 01:14:51 +12:00
|
|
|
|
2022-08-26 06:41:47 +12:00
|
|
|
async function sendRealEmail(purpose: string) {
|
2021-10-08 04:11:33 +13:00
|
|
|
let response, text
|
|
|
|
try {
|
2022-08-26 06:41:47 +12:00
|
|
|
const timeout = () =>
|
|
|
|
new Promise((resolve, reject) =>
|
|
|
|
setTimeout(
|
|
|
|
() =>
|
|
|
|
reject({
|
|
|
|
status: 301,
|
|
|
|
errno: "ETIME",
|
|
|
|
}),
|
|
|
|
20000
|
|
|
|
)
|
|
|
|
)
|
2022-07-14 02:39:58 +12:00
|
|
|
await Promise.race([config.saveEtherealSmtpConfig(), timeout()])
|
|
|
|
await Promise.race([config.saveSettingsConfig(), timeout()])
|
2022-08-26 06:41:47 +12:00
|
|
|
|
2022-11-12 04:43:41 +13:00
|
|
|
const res = await config.api.emails.sendEmail(purpose).timeout(20000)
|
2022-02-16 00:30:45 +13:00
|
|
|
// ethereal hiccup, can't test right now
|
|
|
|
if (res.status >= 300) {
|
|
|
|
return
|
|
|
|
}
|
2021-10-08 04:14:00 +13:00
|
|
|
expect(res.body.message).toBeDefined()
|
|
|
|
const testUrl = nodemailer.getTestMessageUrl(res.body)
|
|
|
|
console.log(`${purpose} URL: ${testUrl}`)
|
|
|
|
expect(testUrl).toBeDefined()
|
2021-10-08 04:11:33 +13:00
|
|
|
response = await fetch(testUrl)
|
|
|
|
text = await response.text()
|
2022-08-26 06:41:47 +12:00
|
|
|
} catch (err: any) {
|
2021-10-08 04:11:33 +13:00
|
|
|
// ethereal hiccup, can't test right now
|
2022-03-31 02:41:39 +13:00
|
|
|
if (parseInt(err.status) >= 300 || (err && err.errno === "ETIME")) {
|
2021-10-08 04:11:33 +13:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
2021-04-27 01:14:51 +12:00
|
|
|
let toCheckFor
|
|
|
|
switch (purpose) {
|
|
|
|
case EmailTemplatePurpose.WELCOME:
|
|
|
|
toCheckFor = `Thanks for getting started with Budibase's Budibase platform.`
|
|
|
|
break
|
|
|
|
case EmailTemplatePurpose.INVITATION:
|
|
|
|
toCheckFor = `Use the button below to set up your account and get started:`
|
|
|
|
break
|
|
|
|
case EmailTemplatePurpose.PASSWORD_RECOVERY:
|
|
|
|
toCheckFor = `You recently requested to reset your password for your Budibase account in your Budibase platform`
|
|
|
|
break
|
|
|
|
}
|
|
|
|
expect(text).toContain(toCheckFor)
|
|
|
|
}
|
|
|
|
|
|
|
|
it("should be able to send a welcome email", async () => {
|
|
|
|
await sendRealEmail(EmailTemplatePurpose.WELCOME)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should be able to send a invitation email", async () => {
|
|
|
|
await sendRealEmail(EmailTemplatePurpose.INVITATION)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should be able to send a password recovery email", async () => {
|
2021-05-06 04:49:34 +12:00
|
|
|
await sendRealEmail(EmailTemplatePurpose.PASSWORD_RECOVERY)
|
2021-04-27 01:14:51 +12:00
|
|
|
})
|
2022-08-26 06:41:47 +12:00
|
|
|
})
|