|
| 1 | +import { createMockContext } from '../../../tests/helpers/mock-context'; |
| 2 | + |
| 3 | +const loadHandler = () => { |
| 4 | + const mod = require('../handler'); |
| 5 | + return mod.default ?? mod; |
| 6 | +}; |
| 7 | + |
| 8 | +describe('simple-email handler', () => { |
| 9 | + beforeEach(() => { |
| 10 | + jest.resetModules(); |
| 11 | + process.env.SIMPLE_EMAIL_DRY_RUN = 'false'; |
| 12 | + process.env.EMAIL_SEND_USE_SMTP = 'false'; |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + delete process.env.SIMPLE_EMAIL_DRY_RUN; |
| 17 | + delete process.env.EMAIL_SEND_USE_SMTP; |
| 18 | + delete process.env.MAILGUN_FROM; |
| 19 | + delete process.env.SMTP_FROM; |
| 20 | + }); |
| 21 | + |
| 22 | + describe('validation', () => { |
| 23 | + it('throws on missing "to"', async () => { |
| 24 | + const handler = loadHandler(); |
| 25 | + await expect( |
| 26 | + handler({ subject: 'test', html: '<p>hi</p>' }, createMockContext()) |
| 27 | + ).rejects.toThrow("Missing required field 'to'"); |
| 28 | + }); |
| 29 | + |
| 30 | + it('throws on missing "subject"', async () => { |
| 31 | + const handler = loadHandler(); |
| 32 | + await expect( |
| 33 | + handler({ to: 'a@b.com', html: '<p>hi</p>' }, createMockContext()) |
| 34 | + ).rejects.toThrow("Missing required field 'subject'"); |
| 35 | + }); |
| 36 | + |
| 37 | + it('throws when neither html nor text provided', async () => { |
| 38 | + const handler = loadHandler(); |
| 39 | + await expect( |
| 40 | + handler({ to: 'a@b.com', subject: 'hi' }, createMockContext()) |
| 41 | + ).rejects.toThrow("Either 'html' or 'text' must be provided"); |
| 42 | + }); |
| 43 | + |
| 44 | + it('accepts text-only email (no html)', async () => { |
| 45 | + const handler = loadHandler(); |
| 46 | + const result = await handler( |
| 47 | + { to: 'a@b.com', subject: 'Hi', text: 'hello' }, |
| 48 | + createMockContext() |
| 49 | + ); |
| 50 | + expect(result).toEqual({ complete: true }); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('sending', () => { |
| 55 | + it('returns { complete: true } on valid payload', async () => { |
| 56 | + const handler = loadHandler(); |
| 57 | + const result = await handler( |
| 58 | + { to: 'a@b.com', subject: 'Hi', html: '<p>hi</p>' }, |
| 59 | + createMockContext() |
| 60 | + ); |
| 61 | + expect(result).toEqual({ complete: true }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('calls postmaster.send by default (not SMTP)', async () => { |
| 65 | + const handler = loadHandler(); |
| 66 | + await handler( |
| 67 | + { to: 'a@b.com', subject: 'Hi', html: '<p>hi</p>' }, |
| 68 | + createMockContext() |
| 69 | + ); |
| 70 | + const postmaster = require('@constructive-io/postmaster'); |
| 71 | + expect(postmaster.send).toHaveBeenCalledWith( |
| 72 | + expect.objectContaining({ to: 'a@b.com', subject: 'Hi' }) |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it('uses SMTP when EMAIL_SEND_USE_SMTP=true', async () => { |
| 77 | + process.env.EMAIL_SEND_USE_SMTP = 'true'; |
| 78 | + const handler = loadHandler(); |
| 79 | + await handler( |
| 80 | + { to: 'a@b.com', subject: 'Hi', html: '<p>hi</p>' }, |
| 81 | + createMockContext() |
| 82 | + ); |
| 83 | + const smtp = require('simple-smtp-server'); |
| 84 | + expect(smtp.send).toHaveBeenCalled(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('uses MAILGUN_FROM as fallback when from not in payload', async () => { |
| 88 | + process.env.MAILGUN_FROM = 'noreply@example.com'; |
| 89 | + const handler = loadHandler(); |
| 90 | + await handler( |
| 91 | + { to: 'a@b.com', subject: 'Hi', html: '<p>hi</p>' }, |
| 92 | + createMockContext() |
| 93 | + ); |
| 94 | + const postmaster = require('@constructive-io/postmaster'); |
| 95 | + expect(postmaster.send).toHaveBeenCalledWith( |
| 96 | + expect.objectContaining({ from: 'noreply@example.com' }) |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + it('prefers payload "from" over env fallback', async () => { |
| 101 | + process.env.MAILGUN_FROM = 'env@example.com'; |
| 102 | + const handler = loadHandler(); |
| 103 | + await handler( |
| 104 | + { |
| 105 | + to: 'a@b.com', |
| 106 | + subject: 'Hi', |
| 107 | + html: '<p>hi</p>', |
| 108 | + from: 'payload@example.com' |
| 109 | + }, |
| 110 | + createMockContext() |
| 111 | + ); |
| 112 | + const postmaster = require('@constructive-io/postmaster'); |
| 113 | + expect(postmaster.send).toHaveBeenCalledWith( |
| 114 | + expect.objectContaining({ from: 'payload@example.com' }) |
| 115 | + ); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + describe('dry-run mode', () => { |
| 120 | + beforeEach(() => { |
| 121 | + process.env.SIMPLE_EMAIL_DRY_RUN = 'true'; |
| 122 | + }); |
| 123 | + |
| 124 | + it('returns { complete: true } without sending', async () => { |
| 125 | + const handler = loadHandler(); |
| 126 | + const result = await handler( |
| 127 | + { to: 'a@b.com', subject: 'Hi', html: '<p>hi</p>' }, |
| 128 | + createMockContext() |
| 129 | + ); |
| 130 | + expect(result).toEqual({ complete: true }); |
| 131 | + const postmaster = require('@constructive-io/postmaster'); |
| 132 | + const smtp = require('simple-smtp-server'); |
| 133 | + expect(postmaster.send).not.toHaveBeenCalled(); |
| 134 | + expect(smtp.send).not.toHaveBeenCalled(); |
| 135 | + }); |
| 136 | + }); |
| 137 | +}); |
0 commit comments