1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 09:37:02 +00:00

message: Prefer To header for BeforeMessageStored event (#446)

Signed-off-by: James Hillyerd <james@hillyerd.com>
This commit is contained in:
James Hillyerd
2023-11-22 16:58:14 -08:00
committed by GitHub
parent 3e94aacc20
commit d2121a52a9
2 changed files with 46 additions and 4 deletions

View File

@@ -76,10 +76,12 @@ func (s *StoreManager) Deliver(
// Process inbound message through extensions. // Process inbound message through extensions.
mailboxes := make([]string, len(recipients)) mailboxes := make([]string, len(recipients))
toAddrs := make([]mail.Address, len(recipients))
for i, recip := range recipients { for i, recip := range recipients {
mailboxes[i] = recip.Mailbox mailboxes[i] = recip.Mailbox
toAddrs[i] = recip.Address }
toAddrs := make([]mail.Address, len(toaddr))
for i, addr := range toaddr {
toAddrs[i] = *addr
} }
inbound := &event.InboundMessage{ inbound := &event.InboundMessage{

View File

@@ -106,7 +106,7 @@ func TestDeliverRespectsRecipientPolicy(t *testing.T) {
assertMessageCount(t, sm, "u2@example.com", 1) assertMessageCount(t, sm, "u2@example.com", 1)
} }
func TestDeliverEmitsBeforeMessageStoredEvent(t *testing.T) { func TestDeliverEmitsBeforeMessageStoredEventToHeader(t *testing.T) {
sm, extHost := testStoreManager() sm, extHost := testStoreManager()
// Register function to receive event. // Register function to receive event.
@@ -118,7 +118,47 @@ func TestDeliverEmitsBeforeMessageStoredEvent(t *testing.T) {
return nil return nil
}) })
// Deliver a message to trigger event. // Deliver a message to trigger event, To header differs from RCPT TO.
origin, _ := sm.AddrPolicy.ParseOrigin("from@example.com")
recip1, _ := sm.AddrPolicy.NewRecipient("u1@example.com")
recip2, _ := sm.AddrPolicy.NewRecipient("u2@example.com")
if err := sm.Deliver(
origin,
[]*policy.Recipient{recip1, recip2},
"Received: xyz\n",
[]byte(`From: from@example.com
To: u1@example.com, u3@external.com
Subject: tsub
test email`),
); err != nil {
t.Fatal(err)
}
require.NotNil(t, got, "BeforeMessageStored listener did not receive InboundMessage")
assert.Equal(t, []string{"u1@example.com", "u2@example.com"}, got.Mailboxes, "Mailboxes not equal")
assert.Equal(t, mail.Address{Name: "", Address: "from@example.com"}, got.From, "From not equal")
assert.Equal(t, []mail.Address{
{Name: "", Address: "u1@example.com"},
{Name: "", Address: "u3@external.com"},
}, got.To, "To not equal")
assert.Equal(t, "tsub", got.Subject, "Subject not equal")
assert.Equal(t, int64(84), got.Size, "Size not equal")
}
func TestDeliverEmitsBeforeMessageStoredEventRcptTo(t *testing.T) {
sm, extHost := testStoreManager()
// Register function to receive event.
var got *event.InboundMessage
extHost.Events.BeforeMessageStored.AddListener(
"test",
func(msg event.InboundMessage) *event.InboundMessage {
got = &msg
return nil
})
// Deliver a message to trigger event, lacks To header.
origin, _ := sm.AddrPolicy.ParseOrigin("from@example.com") origin, _ := sm.AddrPolicy.ParseOrigin("from@example.com")
recip1, _ := sm.AddrPolicy.NewRecipient("u1@example.com") recip1, _ := sm.AddrPolicy.NewRecipient("u1@example.com")
recip2, _ := sm.AddrPolicy.NewRecipient("u2@example.com") recip2, _ := sm.AddrPolicy.NewRecipient("u2@example.com")