mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-18 18:17:03 +00:00
chore: migrate integration to testify/suite (#505)
* fix: future naming collision, suite -> storeSuite Signed-off-by: James Hillyerd <james@hillyerd.com> * chore: migrate integration to testify/suite Signed-off-by: James Hillyerd <james@hillyerd.com> --------- Signed-off-by: James Hillyerd <james@hillyerd.com>
This commit is contained in:
@@ -27,6 +27,7 @@ import (
|
|||||||
"github.com/jhillyerd/goldiff"
|
"github.com/jhillyerd/goldiff"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -35,172 +36,128 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// TODO: Add suites for domain and full addressing modes.
|
// TODO: Add suites for domain and full addressing modes.
|
||||||
|
type IntegrationSuite struct {
|
||||||
func TestSuite(t *testing.T) {
|
suite.Suite
|
||||||
stopServer, err := startServer()
|
stopServer func()
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer stopServer()
|
|
||||||
|
|
||||||
testCases := []struct {
|
|
||||||
name string
|
|
||||||
test func(*testing.T)
|
|
||||||
}{
|
|
||||||
{"basic", testBasic},
|
|
||||||
{"fullname", testFullname},
|
|
||||||
{"encodedHeader", testEncodedHeader},
|
|
||||||
{"ipv4Recipient", testIPv4Recipient},
|
|
||||||
{"ipv6Recipient", testIPv6Recipient},
|
|
||||||
}
|
|
||||||
for _, tc := range testCases {
|
|
||||||
t.Run(tc.name, tc.test)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func testBasic(t *testing.T) {
|
func (s *IntegrationSuite) SetupSuite() {
|
||||||
|
stopServer, err := startServer()
|
||||||
|
s.NoError(err)
|
||||||
|
s.stopServer = stopServer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *IntegrationSuite) TearDownSuite() {
|
||||||
|
s.stopServer()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIntegrationSuite(t *testing.T) {
|
||||||
|
suite.Run(t, new(IntegrationSuite))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *IntegrationSuite) TestBasic() {
|
||||||
client, err := client.New(restBaseURL)
|
client, err := client.New(restBaseURL)
|
||||||
if err != nil {
|
s.Require().NoError(err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
from := "fromuser@inbucket.org"
|
from := "fromuser@inbucket.org"
|
||||||
to := []string{"recipient@inbucket.org"}
|
to := []string{"recipient@inbucket.org"}
|
||||||
input := readTestData("basic.txt")
|
input := readTestData("basic.txt")
|
||||||
|
|
||||||
// Send mail.
|
// Send mail.
|
||||||
err = smtpclient.SendMail(smtpHost, nil, from, to, input)
|
err = smtpclient.SendMail(smtpHost, nil, from, to, input)
|
||||||
if err != nil {
|
s.Require().NoError(err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Confirm receipt.
|
// Confirm receipt.
|
||||||
msg, err := client.GetMessage("recipient", "latest")
|
msg, err := client.GetMessage("recipient", "latest")
|
||||||
if err != nil {
|
s.Require().NoError(err)
|
||||||
t.Fatal(err)
|
s.NotNil(msg)
|
||||||
}
|
|
||||||
if msg == nil {
|
|
||||||
t.Errorf("Got nil message, wanted non-nil message.")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compare to golden.
|
// Compare to golden.
|
||||||
got := formatMessage(msg)
|
got := formatMessage(msg)
|
||||||
goldiff.File(t, got, "testdata", "basic.golden")
|
goldiff.File(s.T(), got, "testdata", "basic.golden")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testFullname(t *testing.T) {
|
func (s *IntegrationSuite) TestFullname() {
|
||||||
client, err := client.New(restBaseURL)
|
client, err := client.New(restBaseURL)
|
||||||
if err != nil {
|
s.Require().NoError(err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
from := "fromuser@inbucket.org"
|
from := "fromuser@inbucket.org"
|
||||||
to := []string{"recipient@inbucket.org"}
|
to := []string{"recipient@inbucket.org"}
|
||||||
input := readTestData("fullname.txt")
|
input := readTestData("fullname.txt")
|
||||||
|
|
||||||
// Send mail.
|
// Send mail.
|
||||||
err = smtpclient.SendMail(smtpHost, nil, from, to, input)
|
err = smtpclient.SendMail(smtpHost, nil, from, to, input)
|
||||||
if err != nil {
|
s.Require().NoError(err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Confirm receipt.
|
// Confirm receipt.
|
||||||
msg, err := client.GetMessage("recipient", "latest")
|
msg, err := client.GetMessage("recipient", "latest")
|
||||||
if err != nil {
|
s.Require().NoError(err)
|
||||||
t.Fatal(err)
|
s.NotNil(msg)
|
||||||
}
|
|
||||||
if msg == nil {
|
|
||||||
t.Errorf("Got nil message, wanted non-nil message.")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compare to golden.
|
// Compare to golden.
|
||||||
got := formatMessage(msg)
|
got := formatMessage(msg)
|
||||||
goldiff.File(t, got, "testdata", "fullname.golden")
|
goldiff.File(s.T(), got, "testdata", "fullname.golden")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testEncodedHeader(t *testing.T) {
|
func (s *IntegrationSuite) TestEncodedHeader() {
|
||||||
client, err := client.New(restBaseURL)
|
client, err := client.New(restBaseURL)
|
||||||
if err != nil {
|
s.Require().NoError(err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
from := "fromuser@inbucket.org"
|
from := "fromuser@inbucket.org"
|
||||||
to := []string{"recipient@inbucket.org"}
|
to := []string{"recipient@inbucket.org"}
|
||||||
input := readTestData("encodedheader.txt")
|
input := readTestData("encodedheader.txt")
|
||||||
|
|
||||||
// Send mail.
|
// Send mail.
|
||||||
err = smtpclient.SendMail(smtpHost, nil, from, to, input)
|
err = smtpclient.SendMail(smtpHost, nil, from, to, input)
|
||||||
if err != nil {
|
s.Require().NoError(err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Confirm receipt.
|
// Confirm receipt.
|
||||||
msg, err := client.GetMessage("recipient", "latest")
|
msg, err := client.GetMessage("recipient", "latest")
|
||||||
if err != nil {
|
s.Require().NoError(err)
|
||||||
t.Fatal(err)
|
s.NotNil(msg)
|
||||||
}
|
|
||||||
if msg == nil {
|
|
||||||
t.Errorf("Got nil message, wanted non-nil message.")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compare to golden.
|
// Compare to golden.
|
||||||
got := formatMessage(msg)
|
got := formatMessage(msg)
|
||||||
goldiff.File(t, got, "testdata", "encodedheader.golden")
|
goldiff.File(s.T(), got, "testdata", "encodedheader.golden")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testIPv4Recipient(t *testing.T) {
|
func (s *IntegrationSuite) TestIPv4Recipient() {
|
||||||
client, err := client.New(restBaseURL)
|
client, err := client.New(restBaseURL)
|
||||||
if err != nil {
|
s.Require().NoError(err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
from := "fromuser@inbucket.org"
|
from := "fromuser@inbucket.org"
|
||||||
to := []string{"ip4recipient@[192.168.123.123]"}
|
to := []string{"ip4recipient@[192.168.123.123]"}
|
||||||
input := readTestData("no-to.txt")
|
input := readTestData("no-to.txt")
|
||||||
|
|
||||||
// Send mail.
|
// Send mail.
|
||||||
err = smtpclient.SendMail(smtpHost, nil, from, to, input)
|
err = smtpclient.SendMail(smtpHost, nil, from, to, input)
|
||||||
if err != nil {
|
s.Require().NoError(err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Confirm receipt.
|
// Confirm receipt.
|
||||||
msg, err := client.GetMessage("ip4recipient", "latest")
|
msg, err := client.GetMessage("ip4recipient", "latest")
|
||||||
if err != nil {
|
s.Require().NoError(err)
|
||||||
t.Fatal(err)
|
s.NotNil(msg)
|
||||||
}
|
|
||||||
if msg == nil {
|
|
||||||
t.Errorf("Got nil message, wanted non-nil message.")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compare to golden.
|
// Compare to golden.
|
||||||
got := formatMessage(msg)
|
got := formatMessage(msg)
|
||||||
goldiff.File(t, got, "testdata", "no-to-ipv4.golden")
|
goldiff.File(s.T(), got, "testdata", "no-to-ipv4.golden")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testIPv6Recipient(t *testing.T) {
|
func (s *IntegrationSuite) TestIPv6Recipient() {
|
||||||
client, err := client.New(restBaseURL)
|
client, err := client.New(restBaseURL)
|
||||||
if err != nil {
|
s.Require().NoError(err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
from := "fromuser@inbucket.org"
|
from := "fromuser@inbucket.org"
|
||||||
to := []string{"ip6recipient@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]"}
|
to := []string{"ip6recipient@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]"}
|
||||||
input := readTestData("no-to.txt")
|
input := readTestData("no-to.txt")
|
||||||
|
|
||||||
// Send mail.
|
// Send mail.
|
||||||
err = smtpclient.SendMail(smtpHost, nil, from, to, input)
|
err = smtpclient.SendMail(smtpHost, nil, from, to, input)
|
||||||
if err != nil {
|
s.Require().NoError(err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Confirm receipt.
|
// Confirm receipt.
|
||||||
msg, err := client.GetMessage("ip6recipient", "latest")
|
msg, err := client.GetMessage("ip6recipient", "latest")
|
||||||
if err != nil {
|
s.Require().NoError(err)
|
||||||
t.Fatal(err)
|
s.NotNil(msg)
|
||||||
}
|
|
||||||
if msg == nil {
|
|
||||||
t.Errorf("Got nil message, wanted non-nil message.")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compare to golden.
|
// Compare to golden.
|
||||||
got := formatMessage(msg)
|
got := formatMessage(msg)
|
||||||
goldiff.File(t, got, "testdata", "no-to-ipv6.golden")
|
goldiff.File(s.T(), got, "testdata", "no-to-ipv6.golden")
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatMessage(m *client.Message) []byte {
|
func formatMessage(m *client.Message) []byte {
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ import (
|
|||||||
type StoreFactory func(
|
type StoreFactory func(
|
||||||
config.Storage, *extension.Host) (store storage.Store, destroy func(), err error)
|
config.Storage, *extension.Host) (store storage.Store, destroy func(), err error)
|
||||||
|
|
||||||
// suite is passed to each test function; embeds `testing.T` to provide testing primitives.
|
// storeSuite is passed to each test function; embeds `testing.T` to provide testing primitives.
|
||||||
type suite struct {
|
type storeSuite struct {
|
||||||
*testing.T
|
*testing.T
|
||||||
store storage.Store
|
store storage.Store
|
||||||
extHost *extension.Host
|
extHost *extension.Host
|
||||||
@@ -34,7 +34,7 @@ func StoreSuite(t *testing.T, factory StoreFactory) {
|
|||||||
t.Helper()
|
t.Helper()
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
test func(suite)
|
test func(storeSuite)
|
||||||
conf config.Storage
|
conf config.Storage
|
||||||
}{
|
}{
|
||||||
{"metadata", testMetadata, config.Storage{}},
|
{"metadata", testMetadata, config.Storage{}},
|
||||||
@@ -59,7 +59,7 @@ func StoreSuite(t *testing.T, factory StoreFactory) {
|
|||||||
}
|
}
|
||||||
defer destroy()
|
defer destroy()
|
||||||
|
|
||||||
s := suite{
|
s := storeSuite{
|
||||||
T: t,
|
T: t,
|
||||||
store: store,
|
store: store,
|
||||||
extHost: extHost,
|
extHost: extHost,
|
||||||
@@ -70,7 +70,7 @@ func StoreSuite(t *testing.T, factory StoreFactory) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// testMetadata verifies message metadata is stored and retrieved correctly.
|
// testMetadata verifies message metadata is stored and retrieved correctly.
|
||||||
func testMetadata(s suite) {
|
func testMetadata(s storeSuite) {
|
||||||
mailbox := "testmailbox"
|
mailbox := "testmailbox"
|
||||||
from := &mail.Address{Name: "From Person", Address: "from@person.com"}
|
from := &mail.Address{Name: "From Person", Address: "from@person.com"}
|
||||||
to := []*mail.Address{
|
to := []*mail.Address{
|
||||||
@@ -137,7 +137,7 @@ func testMetadata(s suite) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// testContent generates some binary content and makes sure it is correctly retrieved.
|
// testContent generates some binary content and makes sure it is correctly retrieved.
|
||||||
func testContent(s suite) {
|
func testContent(s storeSuite) {
|
||||||
content := make([]byte, 5000)
|
content := make([]byte, 5000)
|
||||||
for i := 0; i < len(content); i++ {
|
for i := 0; i < len(content); i++ {
|
||||||
content[i] = byte(i % 256)
|
content[i] = byte(i % 256)
|
||||||
@@ -191,7 +191,7 @@ func testContent(s suite) {
|
|||||||
|
|
||||||
// testDeliveryOrder delivers several messages to the same mailbox, meanwhile querying its contents
|
// testDeliveryOrder delivers several messages to the same mailbox, meanwhile querying its contents
|
||||||
// with a new GetMessages call each cycle.
|
// with a new GetMessages call each cycle.
|
||||||
func testDeliveryOrder(s suite) {
|
func testDeliveryOrder(s storeSuite) {
|
||||||
mailbox := "fred"
|
mailbox := "fred"
|
||||||
subjects := []string{"alpha", "bravo", "charlie", "delta", "echo"}
|
subjects := []string{"alpha", "bravo", "charlie", "delta", "echo"}
|
||||||
for i, subj := range subjects {
|
for i, subj := range subjects {
|
||||||
@@ -211,7 +211,7 @@ func testDeliveryOrder(s suite) {
|
|||||||
|
|
||||||
// testLatest delivers several messages to the same mailbox, and confirms the id `latest` returns
|
// testLatest delivers several messages to the same mailbox, and confirms the id `latest` returns
|
||||||
// the last message sent.
|
// the last message sent.
|
||||||
func testLatest(s suite) {
|
func testLatest(s storeSuite) {
|
||||||
mailbox := "fred"
|
mailbox := "fred"
|
||||||
subjects := []string{"alpha", "bravo", "charlie", "delta", "echo"}
|
subjects := []string{"alpha", "bravo", "charlie", "delta", "echo"}
|
||||||
for _, subj := range subjects {
|
for _, subj := range subjects {
|
||||||
@@ -233,14 +233,14 @@ func testLatest(s suite) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// testNaming ensures the store does not enforce local part mailbox naming.
|
// testNaming ensures the store does not enforce local part mailbox naming.
|
||||||
func testNaming(s suite) {
|
func testNaming(s storeSuite) {
|
||||||
DeliverToStore(s.T, s.store, "fred@fish.net", "disk #27", time.Now())
|
DeliverToStore(s.T, s.store, "fred@fish.net", "disk #27", time.Now())
|
||||||
GetAndCountMessages(s.T, s.store, "fred", 0)
|
GetAndCountMessages(s.T, s.store, "fred", 0)
|
||||||
GetAndCountMessages(s.T, s.store, "fred@fish.net", 1)
|
GetAndCountMessages(s.T, s.store, "fred@fish.net", 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// testSize verifies message content size metadata values.
|
// testSize verifies message content size metadata values.
|
||||||
func testSize(s suite) {
|
func testSize(s storeSuite) {
|
||||||
mailbox := "fred"
|
mailbox := "fred"
|
||||||
subjects := []string{"a", "br", "much longer than the others"}
|
subjects := []string{"a", "br", "much longer than the others"}
|
||||||
sentIds := make([]string, len(subjects))
|
sentIds := make([]string, len(subjects))
|
||||||
@@ -264,7 +264,7 @@ func testSize(s suite) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// testSeen verifies a message can be marked as seen.
|
// testSeen verifies a message can be marked as seen.
|
||||||
func testSeen(s suite) {
|
func testSeen(s storeSuite) {
|
||||||
mailbox := "lisa"
|
mailbox := "lisa"
|
||||||
id1, _ := DeliverToStore(s.T, s.store, mailbox, "whatever", time.Now())
|
id1, _ := DeliverToStore(s.T, s.store, mailbox, "whatever", time.Now())
|
||||||
id2, _ := DeliverToStore(s.T, s.store, mailbox, "hello?", time.Now())
|
id2, _ := DeliverToStore(s.T, s.store, mailbox, "hello?", time.Now())
|
||||||
@@ -300,7 +300,7 @@ func testSeen(s suite) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// testDelete creates and deletes some messages.
|
// testDelete creates and deletes some messages.
|
||||||
func testDelete(s suite) {
|
func testDelete(s storeSuite) {
|
||||||
mailbox := "fred"
|
mailbox := "fred"
|
||||||
subjects := []string{"alpha", "bravo", "charlie", "delta", "echo"}
|
subjects := []string{"alpha", "bravo", "charlie", "delta", "echo"}
|
||||||
for _, subj := range subjects {
|
for _, subj := range subjects {
|
||||||
@@ -351,7 +351,7 @@ func testDelete(s suite) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// testPurge makes sure mailboxes can be purged.
|
// testPurge makes sure mailboxes can be purged.
|
||||||
func testPurge(s suite) {
|
func testPurge(s storeSuite) {
|
||||||
mailbox := "fred"
|
mailbox := "fred"
|
||||||
subjects := []string{"alpha", "bravo", "charlie", "delta", "echo"}
|
subjects := []string{"alpha", "bravo", "charlie", "delta", "echo"}
|
||||||
|
|
||||||
@@ -384,7 +384,7 @@ func testPurge(s suite) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// testMsgCap verifies the message cap is enforced.
|
// testMsgCap verifies the message cap is enforced.
|
||||||
func testMsgCap(s suite) {
|
func testMsgCap(s storeSuite) {
|
||||||
mbCap := 10
|
mbCap := 10
|
||||||
mailbox := "captain"
|
mailbox := "captain"
|
||||||
|
|
||||||
@@ -413,7 +413,7 @@ func testMsgCap(s suite) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// testNoMsgCap verfies a cap of 0 is not enforced.
|
// testNoMsgCap verfies a cap of 0 is not enforced.
|
||||||
func testNoMsgCap(s suite) {
|
func testNoMsgCap(s storeSuite) {
|
||||||
mailbox := "captain"
|
mailbox := "captain"
|
||||||
for i := 0; i < 20; i++ {
|
for i := 0; i < 20; i++ {
|
||||||
subj := fmt.Sprintf("subject %v", i)
|
subj := fmt.Sprintf("subject %v", i)
|
||||||
@@ -424,7 +424,7 @@ func testNoMsgCap(s suite) {
|
|||||||
|
|
||||||
// testVisitMailboxes creates some mailboxes and confirms the VisitMailboxes method visits all of
|
// testVisitMailboxes creates some mailboxes and confirms the VisitMailboxes method visits all of
|
||||||
// them.
|
// them.
|
||||||
func testVisitMailboxes(s suite) {
|
func testVisitMailboxes(s storeSuite) {
|
||||||
// Deliver 2 test messages to each of 5 mailboxes.
|
// Deliver 2 test messages to each of 5 mailboxes.
|
||||||
boxes := []string{"abby", "bill", "christa", "donald", "evelyn"}
|
boxes := []string{"abby", "bill", "christa", "donald", "evelyn"}
|
||||||
for _, name := range boxes {
|
for _, name := range boxes {
|
||||||
|
|||||||
Reference in New Issue
Block a user