mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 17:47:03 +00:00
message: migrate more delivery logic into manager.go (#414)
* message: migrate more delivery logic into manager.go Signed-off-by: James Hillyerd <james@hillyerd.com> * manager: tidy up a few things Signed-off-by: James Hillyerd <james@hillyerd.com> --------- Signed-off-by: James Hillyerd <james@hillyerd.com>
This commit is contained in:
@@ -3,6 +3,7 @@ package message_test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/inbucket/inbucket/v3/pkg/config"
|
||||
"github.com/inbucket/inbucket/v3/pkg/extension"
|
||||
"github.com/inbucket/inbucket/v3/pkg/message"
|
||||
"github.com/inbucket/inbucket/v3/pkg/policy"
|
||||
@@ -11,23 +12,38 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestManagerEmitsMessageStoredEvent(t *testing.T) {
|
||||
extHost := extension.NewHost()
|
||||
sm := &message.StoreManager{
|
||||
AddrPolicy: &policy.Addressing{},
|
||||
Store: test.NewStore(),
|
||||
ExtHost: extHost,
|
||||
func TestDeliverStoresMessages(t *testing.T) {
|
||||
sm, _ := testStoreManager()
|
||||
|
||||
// Attempt to deliver a message to two mailboxes.
|
||||
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\nSubject: tsub\n\ntest email"),
|
||||
); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assertMessageCount(t, sm, "u1@example.com", 1)
|
||||
assertMessageCount(t, sm, "u2@example.com", 1)
|
||||
}
|
||||
|
||||
func TestDeliverEmitsAfterMessageStoredEvent(t *testing.T) {
|
||||
sm, extHost := testStoreManager()
|
||||
|
||||
listener := extHost.Events.AfterMessageStored.AsyncTestListener("manager", 1)
|
||||
|
||||
// Attempt to deliver a message to generate event.
|
||||
origin, _ := sm.AddrPolicy.ParseOrigin("from@example.com")
|
||||
if _, err := sm.Deliver(
|
||||
&policy.Recipient{},
|
||||
recip, _ := sm.AddrPolicy.NewRecipient("to@example.com")
|
||||
if err := sm.Deliver(
|
||||
origin,
|
||||
[]*policy.Recipient{},
|
||||
"prefix",
|
||||
[]*policy.Recipient{recip},
|
||||
"Received: xyz\n",
|
||||
[]byte("From: from@example.com\n\ntest email"),
|
||||
); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -36,4 +52,36 @@ func TestManagerEmitsMessageStoredEvent(t *testing.T) {
|
||||
got, err := listener()
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, got, "No event received, or it was nil")
|
||||
assertMessageCount(t, sm, "to@example.com", 1)
|
||||
}
|
||||
|
||||
func testStoreManager() (*message.StoreManager, *extension.Host) {
|
||||
extHost := extension.NewHost()
|
||||
|
||||
sm := &message.StoreManager{
|
||||
AddrPolicy: &policy.Addressing{
|
||||
Config: &config.Root{
|
||||
MailboxNaming: config.FullNaming,
|
||||
SMTP: config.SMTP{
|
||||
DefaultStore: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
Store: test.NewStore(),
|
||||
ExtHost: extHost,
|
||||
}
|
||||
|
||||
return sm, extHost
|
||||
}
|
||||
|
||||
func assertMessageCount(t *testing.T, sm *message.StoreManager, mailbox string, count int) {
|
||||
t.Helper()
|
||||
|
||||
metas, err := sm.GetMetadata(mailbox)
|
||||
assert.NoError(t, err, "StoreManager GetMetadata failed")
|
||||
|
||||
got := len(metas)
|
||||
if got != count {
|
||||
t.Errorf("Mailbox %q got %v messages, wanted %v", mailbox, got, count)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user