1
0
mirror of https://github.com/axllent/mailpit.git synced 2025-08-13 20:04:49 +02:00

Testing: Add tenantIDs to tests

This commit is contained in:
Ralph Slooten
2024-10-18 17:55:46 +13:00
parent 882adeebe3
commit 405babda7b
5 changed files with 379 additions and 306 deletions

View File

@@ -233,14 +233,9 @@ func VerifyConfig() error {
return err
}
TenantID = tools.Normalize(TenantID)
TenantID = DBTenantID(TenantID)
if TenantID != "" {
logger.Log().Infof("[db] using tenant \"%s\"", TenantID)
re := regexp.MustCompile(`[^a-zA-Z0-9\_]`)
TenantID = re.ReplaceAllString(TenantID, "_")
if !strings.HasSuffix(TenantID, "_") {
TenantID = TenantID + "_"
}
}
re := regexp.MustCompile(`.*:\d+$`)
@@ -632,3 +627,17 @@ func isValidURL(s string) bool {
return strings.HasPrefix(u.Scheme, "http")
}
// DBTenantID converts a tenant ID to a DB-friendly value if set
func DBTenantID(s string) string {
s = tools.Normalize(s)
if s != "" {
re := regexp.MustCompile(`[^a-zA-Z0-9\_]`)
s = re.ReplaceAllString(s, "_")
if !strings.HasSuffix(s, "_") {
s = s + "_"
}
}
return s
}

View File

@@ -3,10 +3,12 @@ package storage
import (
"testing"
"time"
"github.com/axllent/mailpit/config"
)
func TestTextEmailInserts(t *testing.T) {
setup()
setup("")
defer Close()
t.Log("Testing text email storage")
@@ -38,10 +40,16 @@ func TestTextEmailInserts(t *testing.T) {
}
func TestMimeEmailInserts(t *testing.T) {
setup()
defer Close()
for _, tenantID := range []string{"", "MyServer 3", "host.example.com"} {
tenantID = config.DBTenantID(tenantID)
setup(tenantID)
if tenantID == "" {
t.Log("Testing mime email storage")
} else {
t.Logf("Testing mime email storage (tenant %s)", tenantID)
}
start := time.Now()
@@ -65,13 +73,22 @@ func TestMimeEmailInserts(t *testing.T) {
assertEqual(t, CountTotal(), float64(0), "incorrect number of mime emails deleted")
t.Logf("Deleted %d mime emails in %s", testRuns, time.Since(delStart))
Close()
}
}
func TestRetrieveMimeEmail(t *testing.T) {
setup()
defer Close()
for _, tenantID := range []string{"", "MyServer 3", "host.example.com"} {
tenantID = config.DBTenantID(tenantID)
setup(tenantID)
if tenantID == "" {
t.Log("Testing mime email retrieval")
} else {
t.Logf("Testing mime email retrieval (tenant %s)", tenantID)
}
id, err := Store(&testMimeEmail)
if err != nil {
@@ -109,13 +126,22 @@ func TestRetrieveMimeEmail(t *testing.T) {
t.Fail()
}
assertEqual(t, float64(len(inlineData.Content)), msg.Inline[0].Size, "inline attachment size does not match")
Close()
}
}
func TestMessageSummary(t *testing.T) {
setup()
defer Close()
for _, tenantID := range []string{"", "MyServer 3", "host.example.com"} {
tenantID = config.DBTenantID(tenantID)
setup(tenantID)
if tenantID == "" {
t.Log("Testing message summary")
} else {
t.Logf("Testing message summary (tenant %s)", tenantID)
}
if _, err := Store(&testMimeEmail); err != nil {
t.Log("error ", err)
@@ -141,10 +167,13 @@ func TestMessageSummary(t *testing.T) {
assertEqual(t, msg.Snippet, "Message with inline image and attachment:", "\"Snippet\" does does not match")
assertEqual(t, msg.Attachments, 1, "Expected 1 attachment")
assertEqual(t, msg.MessageID, "33af2ac1-c33d-9738-35e3-a6daf90bbd89@gmail.com", "\"MessageID\" does not match")
Close()
}
}
func BenchmarkImportText(b *testing.B) {
setup()
setup("")
defer Close()
for i := 0; i < b.N; i++ {
@@ -156,7 +185,7 @@ func BenchmarkImportText(b *testing.B) {
}
func BenchmarkImportMime(b *testing.B) {
setup()
setup("")
defer Close()
for i := 0; i < b.N; i++ {

View File

@@ -6,14 +6,22 @@ import (
"math/rand"
"testing"
"github.com/axllent/mailpit/config"
"github.com/jhillyerd/enmime"
)
func TestSearch(t *testing.T) {
setup()
defer Close()
for _, tenantID := range []string{"", "MyServer 3", "host.example.com"} {
tenantID = config.DBTenantID(tenantID)
setup(tenantID)
if tenantID == "" {
t.Log("Testing search")
} else {
t.Logf("Testing search (tenant %s)", tenantID)
}
for i := 0; i < testRuns; i++ {
msg := enmime.Builder().
From(fmt.Sprintf("From %d", i), fmt.Sprintf("from-%d@example.com", i)).
@@ -91,13 +99,23 @@ func TestSearch(t *testing.T) {
t.Fail()
}
assertEqual(t, len(summaries), testRuns, "search results expected")
Close()
}
}
func TestSearchDelete100(t *testing.T) {
setup()
defer Close()
for _, tenantID := range []string{"", "MyServer 3", "host.example.com"} {
tenantID = config.DBTenantID(tenantID)
setup(tenantID)
if tenantID == "" {
t.Log("Testing search delete of 100 messages")
} else {
t.Logf("Testing search delete of 100 messages (tenant %s)", tenantID)
}
for i := 0; i < 100; i++ {
if _, err := Store(&testTextEmail); err != nil {
t.Log("error ", err)
@@ -129,10 +147,13 @@ func TestSearchDelete100(t *testing.T) {
}
assertEqual(t, total, 0, "0 search results expected")
Close()
}
}
func TestSearchDelete1100(t *testing.T) {
setup()
setup("")
defer Close()
t.Log("Testing search delete of 1100 messages")

View File

@@ -4,13 +4,22 @@ import (
"fmt"
"strings"
"testing"
"github.com/axllent/mailpit/config"
)
func TestTags(t *testing.T) {
setup()
defer Close()
for _, tenantID := range []string{"", "MyServer 3", "host.example.com"} {
tenantID = config.DBTenantID(tenantID)
setup(tenantID)
if tenantID == "" {
t.Log("Testing tags")
} else {
t.Logf("Testing tags (tenant %s)", tenantID)
}
ids := []string{}
@@ -127,4 +136,8 @@ func TestTags(t *testing.T) {
t.Log("error ", err)
t.Fail()
}
Close()
}
}

View File

@@ -16,10 +16,11 @@ var (
testRuns = 100
)
func setup() {
func setup(tenantID string) {
logger.NoLogging = true
config.MaxMessages = 0
config.Database = os.Getenv("MP_DATABASE")
config.TenantID = config.DBTenantID(tenantID)
if err := InitDB(); err != nil {
panic(err)