File size: 1,133 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package harness

import (
	"net/smtp"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestNewMailpit(t *testing.T) {
	mp := newMailpit(t)

	err := smtp.SendMail(mp.smtpAddr, nil, "example@example.com", []string{"foo@bar.com"}, []byte("Subject: Hello\nTo: foo@bar.com\n\nWorld!"))
	require.NoError(t, err, "expected to be able to send email")

	err = smtp.SendMail(mp.smtpAddr, nil, "example@example.com", []string{"bin@baz.com"}, []byte("Subject: There\nTo: bin@baz.com\n\nThen!"))
	require.NoError(t, err, "expected to be able to send email")

	found := assert.Eventually(t, func() bool {
		found := mp.ReadMessage("foo@bar.com", "World")
		return found
	}, 5*time.Second, 100*time.Millisecond, "expected to find email to foo@bar.com containing 'World'")
	if !found {
		msgs := mp.UnreadMessages()
		t.Fatalf("timeout waiting for email; Got:\n%v", msgs)
	}

	found = mp.ReadMessage("foo@bar.com", "World")
	assert.False(t, found, "expected message to have been marked as read")

	remaining := mp.UnreadMessages()
	assert.Len(t, remaining, 1, "expected one unread message")
}