-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(email-plugin): Extend attachment support
Fixes #882. You can now specify attachments as strings, Buffers or Streams in addition to by file path.
- Loading branch information
1 parent
221051f
commit 70a55fd
Showing
4 changed files
with
186 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,10 @@ import { | |
VendureEvent, | ||
} from '@vendure/core'; | ||
import { TestingLogger } from '@vendure/testing'; | ||
import { createReadStream, readFileSync } from 'fs'; | ||
import { readFile } from 'fs-extra'; | ||
import path from 'path'; | ||
import { Readable } from 'stream'; | ||
|
||
import { orderConfirmationHandler } from './default-email-handlers'; | ||
import { EmailEventHandler } from './event-handler'; | ||
|
@@ -504,6 +507,132 @@ describe('EmailPlugin', () => { | |
|
||
expect(onSend.mock.calls[0][0].attachments).toEqual([{ path: TEST_IMAGE_PATH }]); | ||
}); | ||
|
||
it('attachment content as a string buffer', async () => { | ||
const handler = new EmailEventListener('test') | ||
.on(MockEvent) | ||
.setFrom('"test from" <[email protected]>') | ||
.setRecipient(() => '[email protected]') | ||
.setSubject('Hello {{ subjectVar }}') | ||
.setAttachments(() => [ | ||
{ | ||
content: Buffer.from('hello'), | ||
}, | ||
]); | ||
|
||
await initPluginWithHandlers([handler]); | ||
eventBus.publish(new MockEvent(ctx, true)); | ||
await pause(); | ||
|
||
const attachment = onSend.mock.calls[0][0].attachments[0].content; | ||
expect(Buffer.compare(attachment, Buffer.from('hello'))).toBe(0); // 0 = buffers are equal | ||
}); | ||
|
||
it('attachment content as an image buffer', async () => { | ||
const imageFileBuffer = readFileSync(TEST_IMAGE_PATH); | ||
const handler = new EmailEventListener('test') | ||
.on(MockEvent) | ||
.setFrom('"test from" <[email protected]>') | ||
.setRecipient(() => '[email protected]') | ||
.setSubject('Hello {{ subjectVar }}') | ||
.setAttachments(() => [ | ||
{ | ||
content: imageFileBuffer, | ||
}, | ||
]); | ||
|
||
await initPluginWithHandlers([handler]); | ||
eventBus.publish(new MockEvent(ctx, true)); | ||
await pause(); | ||
|
||
const attachment = onSend.mock.calls[0][0].attachments[0].content; | ||
expect(Buffer.compare(attachment, imageFileBuffer)).toBe(0); // 0 = buffers are equal | ||
}); | ||
|
||
it('attachment content as a string', async () => { | ||
const handler = new EmailEventListener('test') | ||
.on(MockEvent) | ||
.setFrom('"test from" <[email protected]>') | ||
.setRecipient(() => '[email protected]') | ||
.setSubject('Hello {{ subjectVar }}') | ||
.setAttachments(() => [ | ||
{ | ||
content: 'hello', | ||
}, | ||
]); | ||
|
||
await initPluginWithHandlers([handler]); | ||
eventBus.publish(new MockEvent(ctx, true)); | ||
await pause(); | ||
|
||
const attachment = onSend.mock.calls[0][0].attachments[0].content; | ||
expect(attachment).toBe('hello'); | ||
}); | ||
|
||
it('attachment content as a string stream', async () => { | ||
const handler = new EmailEventListener('test') | ||
.on(MockEvent) | ||
.setFrom('"test from" <[email protected]>') | ||
.setRecipient(() => '[email protected]') | ||
.setSubject('Hello {{ subjectVar }}') | ||
.setAttachments(() => [ | ||
{ | ||
content: Readable.from(['hello']), | ||
}, | ||
]); | ||
|
||
await initPluginWithHandlers([handler]); | ||
eventBus.publish(new MockEvent(ctx, true)); | ||
await pause(); | ||
|
||
const attachment = onSend.mock.calls[0][0].attachments[0].content; | ||
expect(Buffer.compare(attachment, Buffer.from('hello'))).toBe(0); // 0 = buffers are equal | ||
}); | ||
|
||
it('attachment content as an image stream', async () => { | ||
const imageFileBuffer = readFileSync(TEST_IMAGE_PATH); | ||
const imageFileStream = createReadStream(TEST_IMAGE_PATH); | ||
const handler = new EmailEventListener('test') | ||
.on(MockEvent) | ||
.setFrom('"test from" <[email protected]>') | ||
.setRecipient(() => '[email protected]') | ||
.setSubject('Hello {{ subjectVar }}') | ||
.setAttachments(() => [ | ||
{ | ||
content: imageFileStream, | ||
}, | ||
]); | ||
|
||
await initPluginWithHandlers([handler]); | ||
eventBus.publish(new MockEvent(ctx, true)); | ||
await pause(); | ||
|
||
const attachment = onSend.mock.calls[0][0].attachments[0].content; | ||
expect(Buffer.compare(attachment, imageFileBuffer)).toBe(0); // 0 = buffers are equal | ||
}); | ||
|
||
it('raises a warning for large content attachments', async () => { | ||
testingLogger.warnSpy.mockClear(); | ||
const largeBuffer = Buffer.from(Array.from({ length: 65535, 0: 0 })); | ||
const handler = new EmailEventListener('test') | ||
.on(MockEvent) | ||
.setFrom('"test from" <[email protected]>') | ||
.setRecipient(() => '[email protected]') | ||
.setSubject('Hello {{ subjectVar }}') | ||
.setAttachments(() => [ | ||
{ | ||
content: largeBuffer, | ||
}, | ||
]); | ||
|
||
await initPluginWithHandlers([handler]); | ||
eventBus.publish(new MockEvent(ctx, true)); | ||
await pause(); | ||
|
||
expect(testingLogger.warnSpy.mock.calls[0][0]).toContain( | ||
`Email has a large 'content' attachment (64k). Consider using the 'path' instead for improved performance.`, | ||
); | ||
}); | ||
}); | ||
|
||
describe('orderConfirmationHandler', () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters