testing for sending files via smtp

This commit is contained in:
Peter Clement 2024-03-25 14:43:06 +00:00
parent 40d575ee58
commit 99f2b3e7fd
6 changed files with 39 additions and 24 deletions

View File

@ -50,6 +50,7 @@ describe("test the outgoing webhook action", () => {
cc: "cc",
bcc: "bcc",
addInvite: true,
attachments: "attachment1,attachment2",
...invite,
}
let resp = generateResponse(inputs.to, inputs.from)
@ -69,6 +70,7 @@ describe("test the outgoing webhook action", () => {
bcc: "bcc",
invite,
automation: true,
attachments: ["attachment1", "attachment2"],
})
})
})

View File

@ -1,15 +0,0 @@
const mockS3 = {
headBucket: jest.fn().mockReturnThis(),
deleteObject: jest.fn().mockReturnThis(),
deleteObjects: jest.fn().mockReturnThis(),
createBucket: jest.fn().mockReturnThis(),
listObjects: jest.fn().mockReturnThis(),
promise: jest.fn().mockReturnThis(),
catch: jest.fn(),
}
const AWS = {
S3: jest.fn(() => mockS3),
}
export default AWS

View File

@ -1,7 +1,11 @@
jest.unmock("node-fetch")
import { TestConfiguration } from "../../../../tests"
import { EmailTemplatePurpose } from "../../../../constants"
import { objectStoreTestProviders, mocks } from "@budibase/backend-core/tests"
import { objectStore } from "@budibase/backend-core"
import env from "../../../../environment"
import tk from "timekeeper"
jest.unmock("aws-sdk")
const nodemailer = require("nodemailer")
const fetch = require("node-fetch")
@ -12,14 +16,16 @@ describe("/api/global/email", () => {
const config = new TestConfiguration()
beforeAll(async () => {
await objectStoreTestProviders.minio.start()
await config.beforeAll()
})
afterAll(async () => {
await objectStoreTestProviders.minio.stop()
await config.afterAll()
})
async function sendRealEmail(purpose: string) {
async function sendRealEmail(purpose: string, attachments?: string[]) {
let response, text
try {
const timeout = () =>
@ -35,8 +41,14 @@ describe("/api/global/email", () => {
)
await Promise.race([config.saveEtherealSmtpConfig(), timeout()])
await Promise.race([config.saveSettingsConfig(), timeout()])
const res = await config.api.emails.sendEmail(purpose).timeout(20000)
let res
if (attachments) {
res = await config.api.emails
.sendEmail(purpose, attachments)
.timeout(20000)
} else {
res = await config.api.emails.sendEmail(purpose).timeout(20000)
}
// ethereal hiccup, can't test right now
if (res.status >= 300) {
return
@ -81,4 +93,19 @@ describe("/api/global/email", () => {
it("should be able to send a password recovery email", async () => {
await sendRealEmail(EmailTemplatePurpose.PASSWORD_RECOVERY)
})
it("should be able to send an email with attachments", async () => {
tk.reset()
let bucket = "test-bucket"
let filename = "test.txt"
await objectStore.upload({
bucket,
filename,
body: Buffer.from("test data"),
})
tk.freeze(mocks.date.MOCK_DATE)
let presignedUrl = await objectStore.getPresignedUrl(bucket, filename, 600)
await sendRealEmail(EmailTemplatePurpose.WELCOME, [presignedUrl])
})
})

View File

@ -6,11 +6,12 @@ export class EmailAPI extends TestAPI {
super(config)
}
sendEmail = (purpose: string) => {
sendEmail = (purpose: string, attachments?: string[]) => {
return this.request
.post(`/api/global/email/send`)
.send({
email: "test@example.com",
attachments,
purpose,
tenantId: this.config.getTenantId(),
userId: this.config.user?._id!,

View File

@ -4,8 +4,8 @@ process.env.JWT_SECRET = "test-jwtsecret"
process.env.LOG_LEVEL = process.env.LOG_LEVEL || "error"
process.env.MULTI_TENANCY = "1"
process.env.MINIO_URL = "http://localhost"
process.env.MINIO_ACCESS_KEY = "test"
process.env.MINIO_SECRET_KEY = "test"
process.env.MINIO_ACCESS_KEY = "budibase"
process.env.MINIO_SECRET_KEY = "budibase"
process.env.PLATFORM_URL = "http://localhost:10000"
process.env.INTERNAL_API_KEY = "tet"
process.env.DISABLE_ACCOUNT_PORTAL = "0"

View File

@ -62,8 +62,8 @@ export function smtpEthereal(): SMTPConfig {
from: "testfrom@example.com",
secure: false,
auth: {
user: "wyatt.zulauf29@ethereal.email",
pass: "tEwDtHBWWxusVWAPfa",
user: "mortimer.leuschke@ethereal.email",
pass: "5hSjsPbzRv7gEUsfzx",
},
connectionTimeout: 1000, // must be less than the jest default of 5000
},